<template>
  <div id="CustomersDepartment" class="app-container">
    <el-form ref="form" :model="form" :rules="rules" 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="name">
            <el-select v-model.trim="form.name" placeholder="请输入科室名称" :disabled="disable" clearable filterable>
              <el-option v-for="dict in dict.type.mk_bo_section" :key="dict.value" :label="dict.label"
                :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="客户名称" prop="customersName">
            <el-input v-model="form.customersName" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="负责人姓名" prop="responsiblePersonName">
            <el-popover-select-v2 v-model="form.responsiblePersonName" title="负责人" valueKey="name"
              referName="MKCMCONTACTS_PARAM" :dataMapping="{responsiblePerson: 'id', responsiblePersonName: 'name', phone: 'cell'}"
              :source.sync="form" :queryParams="referQuery" placeholder="请选择负责人" :disabled="disable" />
          </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="phone">
            <el-input v-model="form.phone" placeholder="请输入联系人手机" :disabled="disable" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="科室位置" prop="address">
            <el-input v-model="form.address" placeholder="请输入科室位置" :disabled="disable" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="常规治疗" prop="conventionalTherapy">
            <el-input v-model="form.conventionalTherapy" placeholder="请输入常规治疗" :disabled="disable" clearable />
          </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="characteristic">
            <el-input v-model="form.characteristic" placeholder="请输入科室特色" :disabled="disable" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业务员名称" prop="createByName">
            <el-input v-model="form.createByName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注" prop="memo">
            <el-input v-model="form.memo" placeholder="请输入备注" :disabled="disable" clearable />
          </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 {
    listCustomersDepartment,
    getCustomersDepartment,
    delCustomersDepartment,
    addCustomersDepartment,
    updateCustomersDepartment
  } from "@/api/business/spd/cm/customersDepartment";
  // 参照弹出框
  import ElPopoverSelectV2 from "@/components/popover-select-v2";

  export default {
    name: "CustomersDepartment",
    props: ['pageStu', 'row', 'disable', 'customersId', 'customersName'],
    dicts: ['mk_bo_section'],
    model: {
      prop: 'isAdd2',
      event: 'jugislist'
    },
    components: {
      ElPopoverSelectV2,
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户科室表格数据
        customersDepartmentList: [],
        // 当前操作人
        operator: null,
        // 表单参数
        form: {
          id: null,
          name: null,
          customersName: this.customersName,
          responsiblePerson: null,
          responsiblePersonName: null,
          phone: null,
          address: null,
          conventionalTherapy: null,
          characteristic: null,
          marketingArea: null,
          marketingAreaName: null,
          memo: null,
          state: null,
          customersId: null,
          tenantId: null,
          revision: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          delFlag: null
        },
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: '科室名称不能为空',
            trigger: 'blur'
          }],
          phone: [{
            required: true,
            message: "手机号不能为空",
            trigger: "blur"
          }, {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }],
        },
      };
    },
    created() {
      // 客户主键赋值
      this.form.customersId = this.customersId;
      if (this.pageStu == 'check' || this.pageStu == 'update') {
        this.getDetails(this.row);
      }
      // 获取当前登录用户的编码
      this.operator = this.$store.state.user.name;
    },
    methods: {
      // 取消按钮
      cancel() {
        this.$emit('jugislist', false);
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateCustomersDepartment(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.cancel();
                this.$parent.handleCheck(this.form.customersId);
              });
            } else {
              addCustomersDepartment(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.cancel();
                this.$parent.handleCheck(this.form.customersId);
              });
            }
          }
        });
      },
      /** 获取详情 */
      getDetails(row) {
        getCustomersDepartment(row.id).then(res => {
          if (res.code == 200) {
            this.form = res.data;
          }
        })
      },
      // 参照查询条件
      referQuery() {
        const parameter = {
          parame: {
            customersName: this.form.customersName,
            operator: this.operator
          }
        };
        return parameter;
      }
    }
  };

</script>
