<template>
  <div id="CustomersResponsiblePerson" class="app-container">
    <el-form :model="form" :rules="rules" ref="form" label-width="auto">
      <el-divider content-position="left">负责人信息</el-divider>
      <el-row type="flex" class="row-bg" justify="space-around">
        <el-col :span="6">
          <el-form-item label="人员姓名" prop="personName">
            <el-popover-select-v2 v-model="form.personName" title="负责人" valueKey="name" referName="CONTACTS_PARAM"
              :dataMapping="{person: 'id', personName: 'name'}" :source.sync="form" placeholder="请选择负责人"
              :disabled="disable" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户名称" prop="customersName">
            <el-input v-model="form.customersName" placeholder="请输入客户名称" clearable :disabled="disable" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="关系类型" prop="relationshipType">
            <el-select clearable v-model="form.relationshipType" :disabled="disable">
              <el-option v-for="dict in dict.type.mk_cm_relationship_type" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row type="flex" class="row-bg" justify="space-around">
        <el-col :span="6">
          <el-form-item label="产线" prop="productionLine">
            <el-select clearable v-model="form.productionLine" :disabled="disable">
              <el-option v-for="dict in dict.type.mk_cm_production_line" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="开始日期" prop="startDate">
            <el-date-picker v-model="form.startDate" align="right" type="date" placeholder="请选择开始日期"
              :disabled="disable" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="结束日期" prop="endDate">
            <el-date-picker v-model="form.endDate" align="right" type="date" placeholder="请选择结束日期"
              :disabled="disable" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" style="margin-left: 88%; margin-top: 1%">
      <el-button type="primary" @click="submitForm" :disabled="disable">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
  import {
    listCustomersResponsiblePerson,
    getCustomersResponsiblePerson,
    delCustomersResponsiblePerson,
    addCustomersResponsiblePerson,
    updateCustomersResponsiblePerson
  } from "@/api/business/spd/cm/customersResponsiblePerson";
  // 参照弹出框
  import ElPopoverSelectV2 from "@/components/popover-select-v2";

  export default {
    name: "CustomersResponsiblePerson",
    props: ['pageStu', 'row', 'disable', 'customersId'],
    model: {
      prop: 'isAdd1',
      event: 'jugislist'
    },
    dicts: ['mk_cm_relationship_type', 'mk_cm_production_line'],
    components: {
      ElPopoverSelectV2,
    },
    data() {
      return {
        // 不能直接改变props传来的值
        sonPageStu: this.pageStu,
        sonDisable: this.disable,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户负责人表格数据
        customersResponsiblePersonList: [],
        // 表单参数
        form: {
          id: null,
          person: null,
          personName: null,
          customersName: null,
          relationshipType: null,
          productionLine: null,
          startDate: null,
          endDate: null,
          customersId: null,
          tenantId: null,
          revision: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          delFlag: null
        },
        // 表单校验
        rules: {
          personName: [{
            required: true,
            message: '人员名称不能为空',
            trigger: 'blur'
          }],
          relationshipType: [{
            required: true,
            message: '关系类型不能为空',
            trigger: 'blur'
          }],
          startDate: [{
            required: true,
            message: '开始日期不能为空',
            trigger: 'blur'
          }],
        },
      };
    },
    created() {
      // 客户主键赋值
      this.form.customersId = this.customersId;
      if (this.pageStu == 'check' || this.pageStu == 'update') {
        this.getDetails(this.row);
      }
    },
    methods: {
      // 取消按钮
      cancel() {
        this.$emit('jugislist', false);
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateCustomersResponsiblePerson(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.cancel();
              });
            } else {
              addCustomersResponsiblePerson(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.cancel();
              });
            }
          }
        });
      },
      /** 获取详情 */
      getDetails(row) {
        getCustomersResponsiblePerson(row.id).then(res => {
          if (res.code == 200) {
            this.form = res.data;
          }
        })
      },
    }
  }

</script>
