<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">
      <customer-follow-form :form="form" :disabled="disabledKeys" :span="span"/>
    </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 { mapGetters } from 'vuex';
import { parseTime } from '@/utils/ruoyi.js';
import CustomerFollowForm from '@/views/bst/customerFollow/components/CustomerFollowForm.vue';
export default {
  name: "CustomerFollowEditDialog",
  components: { CustomerFollowForm },
  props: {
    show: {
      type: Boolean,
      required: true
    },
    id: {
      type: [String, Number],
      default: null
    },
    initData: {
      type: Object,
      default: () => ({})
    },
  },
  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" }
        ],
        followTime: [
          { required: true, message: "跟进时间不能为空", trigger: "change" }
        ]
      },
      disabledKeys: [],
    };
  },
  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: null,
        nextFollowTime: null,
        followTime: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
        // vo
        customerName: null,
        ...this.initData
      };
      this.disabledKeys = [];
      Object.keys(this.initData).forEach(key => {
        if (this.initData[key] != null) {
          this.disabledKeys.push(key);
        }
      });
      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>