<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="span" label="客户编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入客户编号" />
        </form-col>
        <form-col :span="span" label="客户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入客户名称" />
        </form-col>
        <form-col :span="span" label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.customer_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </form-col>
        <form-col :span="span" label="意向强度" prop="intentLevel">
          <el-select v-model="form.intentLevel" placeholder="请选择意向强度" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.customer_intent_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </form-col>
        <form-col :span="span" label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" maxlength="11" show-word-limit />
        </form-col>
        <form-col :span="span" label="微信号" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入微信号" maxlength="50" show-word-limit />
        </form-col>
        <form-col :span="span" label="来源" prop="source">
          <el-select v-model="form.source" placeholder="请选择来源" style="width: 100%;" allow-create filterable default-first-option>
            <el-option
              v-for="dict in dict.type.customer_source"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </form-col>
        <form-col :span="span" label="意向" prop="intents">
          <el-select v-model="form.intents" placeholder="请选择意向" style="width: 100%;" allow-create filterable default-first-option multiple>
            <el-option
              v-for="dict in dict.type.customer_intent"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </form-col>
        <form-col :span="span" label="跟进人" prop="followId">
          <user-select v-model="form.followId" />
        </form-col>
        <form-col :span="24" label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </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 { getCustomer, addCustomer, updateCustomer } from "@/api/bst/customer";
import FormCol from "@/components/FormCol/index.vue";
import { CustomerStatus, CustomerIntentLevel } from '@/utils/enums';
import { mapGetters } from 'vuex';
import UserSelect from '@/components/Business/User/UserSelect.vue';

export default {
  name: "CustomerEditDialog",
  components: { FormCol, UserSelect },
  dicts: ['customer_intent_level', 'customer_status', 'customer_source', 'customer_intent'],
  props: {
    show: {
      type: Boolean,
      required: true
    },
    id: {
      type: [String, Number],
      default: null
    }
  },
  data() {
    return {
      loading: false,
      submitLoading: false,
      span: 12,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "客户编号不能为空", trigger: "change" }
        ],
        name: [
          { required: true, message: "客户姓名不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        intentLevel: [
          { required: true, message: "意向强度不能为空", trigger: "change" }
        ],
        source: [
          { required: true, message: "来源不能为空", trigger: "change" }
        ],
        followId: [
          { 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;
      getCustomer(id).then(response => {
        this.form = response.data;
      }).finally(() => {
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        status: CustomerStatus.POTENTIAL,
        intentLevel: CustomerIntentLevel.MEDIUM,
        mobile: null,
        wechat: null,
        source: null,
        intents: [],
        followId: this.userId,
        remark: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.submitLoading = true;
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          } else {
            this.submitLoading = true;
            addCustomer(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>