<template>
  <el-dialog 
    :title="title" 
    :visible.sync="dialogVisible" 
    width="800px" 
    append-to-body 
    :close-on-click-modal="false"
    @close="handleClose"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-row>
        <form-col :span="24" label="图片" prop="picture">
          <image-upload v-model="form.picture" />
        </form-col>
        <form-col :span="span" label="客户" prop="customerId">
          <customer-input v-model="form.customerId" :text.sync="form.customerName" />
        </form-col>
        <form-col :span="span" label="跟进方式" prop="type">
          <el-select v-model="form.type" placeholder="请选择跟进方式" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.customer_follow_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </form-col>
        <form-col :span="24" label="跟进内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入跟进内容" maxlength="1000" show-word-limit :autosize="{ minRows: 4 }"/>
        </form-col>
        <form-col :span="span" label="跟进人" prop="userId">
          <user-select v-model="form.userId" disabled readonly/>
        </form-col>
        <form-col :span="span" label="下次跟进" prop="nextFollowTime">
          <el-date-picker clearable
            style="width: 100%;"
            v-model="form.nextFollowTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="09:00:00"
            placeholder="请选择下次跟进时间">
          </el-date-picker>
        </form-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
import { getCustomerFollow, addCustomerFollow, updateCustomerFollow } from "@/api/bst/customerFollow";
import FormCol from "@/components/FormCol/index.vue";
import UserSelect from '@/components/Business/User/UserSelect.vue';
import CustomerInput from '@/components/Business/Customer/CustomerInput.vue';
import { mapGetters } from 'vuex';
export default {
  name: "CustomerFollowEditDialog",
  components: { FormCol, UserSelect, CustomerInput },
  dicts: ['customer_follow_type'],
  props: {
    show: {
      type: Boolean,
      required: true
    },
    id: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      loading: false,
      submitLoading: false,
      span: 12,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        customerId: [
          { required: true, message: "客户不能为空", trigger: "change" }
        ],
        type: [
          { required: true, message: "跟进方式不能为空", trigger: "change" }
        ],
        content: [
          { required: true, message: "跟进内容不能为空", trigger: "change" }
        ],
        userId: [
          { required: true, message: "跟进人不能为空", trigger: "change" }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(['userId']),
    title() {
      return this.id ? '修改跟进记录' : '新增跟进记录';
    },
    dialogVisible: {
      get() {
        return this.show;
      },
      set(value) {
        this.$emit('update:show', value);
      }
    }
  },
  methods: {
    /** 获取详细信息 */
    getInfo(id) {
      this.loading = true;
      getCustomerFollow(id).then(response => {
        this.form = response.data;
      }).finally(() => {
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        followId: null,
        customerId: null,
        type: null,
        content: null,
        picture: null,
        userId: this.userId,
        nextFollowTime: null,
        customerName: null,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.followId != null) {
            this.submitLoading = true;
            updateCustomerFollow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          } else {
            this.submitLoading = true;
            addCustomerFollow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          }
        }
      });
    },
    // 取消按钮
    handleClose() {
      this.$emit('update:show', false);
    },
    // 打开事件
    handleOpen() {
      if (this.id) {
        this.getInfo(this.id);
      } else {
        this.reset();
      }
    }
  }
};
</script>

<style lang="scss" scoped>

</style>