<template>
  <div id="CustomersDepartment" class="app-container">
    <el-divider content-position="left">科室信息</el-divider>
    <el-super-form
      v-model="params"
      :dict="dict"
      :rules="rules"
      :size="size"
      :columns="FormColumns"
      ref="superForm"
      label-width="auto"
      label-position="right"
      style="padding: 20px"
      :disabled="disable"
    >
      <template slot="responsiblePersonName" slot-scope="scope">
        <component
          v-bind="scope.attr"
          v-model="scope.row[scope.item.key]"
          :size="$attrs.size"
          :source.sync="scope.row"
          :queryParams="
            () => ({
              parame: {
                customersName: params.customersName,
                operator: operator,
              },
            })
          "
          :disabled="disable"
        >
        </component
      ></template>
    </el-super-form>
    <div slot="footer" style="text-align: right">
      <el-button
        type="primary"
        :size="size"
        @click="submitForm('superForm')"
        :disabled="disable"
        >确 定</el-button
      >
      <el-button :size="size" @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { dicts } from "../dicts";
import useColumns from "./columns";
import {
  listCustomersDepartment,
  getCustomersDepartment,
  delCustomersDepartment,
  addCustomersDepartment,
  updateCustomersDepartment,
} from "@/api/business/spd/cm/customersDepartment";
export default {
  name: "CustomersDepartment",
  props: ["pageStu", "row", "disable", "customersId", "customersName"],
  dicts: [...dicts],
  model: {
    prop: "isAdd2",
    event: "jugislist",
  },
  components: {
    ElPopoverSelectV2: () => import("@/components/popover-select-v2"),
    ElSuperForm: () => import("@/components/super-form/index.vue"),
  },
  data() {
    const { FormColumns } = useColumns();
    const params = this.$init.params(FormColumns);
    const rules = this.$init.rules(FormColumns);
    return {
      rules,
      params: {
        ...params,
        id: null,
        responsiblePerson: null,
        marketingArea: null,
        marketingAreaName: null,
        state: null,
        customersId: null,
        tenantId: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      },
      FormColumns,
      size: "mini",
      // 遮罩层
      loading: true,
      // 客户科室表格数据
      customersDepartmentList: [],
      // 当前操作人
      operator: null,
    };
  },
  created() {
    this.initialization();
  },
  methods: {
    // 初始化
    initialization() {
      // 客户数据赋值
      this.params.customersId = this.customersId;
      this.params.customersName = this.customersName;
      if (this.pageStu == "check" || this.pageStu == "update") {
        this.getDetails(this.row);
      }
      // 获取当前登录用户的编码
      this.operator = this.$store.state.user.name;
    },
    // 取消按钮
    cancel() {
      this.$emit("jugislist", false);
    },
    /** 提交按钮 */
    submitForm(prop) {
      this.$refs[prop].validate(async (valid) => {
        if (valid) {
          try {
            let { params } = this;
            let { code, msg } =
              params.id != null
                ? await updateCustomersDepartment(params)
                : await addCustomersDepartment(params);

            if (code == 200) {
              this.$notify.success(msg);
              this.cancel();
              this.$parent.handleCheck(params.customersId);
            }
          } catch (error) {}
        }
      });
    },
    /** 获取详情 */
    getDetails(row) {
      getCustomersDepartment(row.id).then((res) => {
        if (res.code == 200) {
          this.params = res.data;
        }
      });
    },
  },
};
</script>
