<template>
  <el-dialog
    v-model="visible"
    :title="type === 'add' ? '新增' : '修改'"
    :before-close="fnClose"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="上级部门">
        <el-tag>{{ parentName }}</el-tag>
      </el-form-item>
      <el-form-item label="部门名称" prop="NAME">
        <el-input v-model="form.NAME" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="部门级别" prop="LEVEL">
        <el-select v-model="form.LEVEL">
          <el-option
            v-for="item in departmentLevel"
            :key="item.BIANMA"
            :label="item.NAME"
            :value="item.BIANMA"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门排序" prop="DEP_ORDER">
        <el-input v-model.number="form.DEP_ORDER" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="负责人" prop="HEADMAN">
        <el-input v-model="form.HEADMAN" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="电话" prop="TEL">
        <el-input v-model="form.TEL" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="部门职能" prop="FUNCTIONS">
        <el-input
          :autosize="{
            minRows: 3,
          }"
          v-model="form.FUNCTIONS"
          type="textarea"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="是否监管部门" prop="ISSUPERVISE">
        <el-radio-group
          v-model="form.ISSUPERVISE"
          @change="fnChangeRegulatoryAuthorities"
        >
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        v-if="form.ISSUPERVISE === '1'"
        label="监管部门"
        prop="checkedIds"
      >
        <layout-department
          v-model="form.checkedIds"
          multiple
          show-checkbox
          root-disabled="Y"
        />
      </el-form-item>
      <el-form-item label="备注" prop="BZ">
        <el-input
          :autosize="{
            minRows: 3,
          }"
          v-model="form.BZ"
          type="textarea"
          placeholder="请输入"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="fnSubmit">确认</el-button>
      <el-button @click="fnClose">关闭</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { useVModels } from "@vueuse/core";
import { ref } from "vue";
import { debounce } from "throttle-debounce";
import useFormValidate from "@/assets/js/useFormValidate.js";
import { layoutFnGetDepartmentLevel } from "@/assets/js/data_dictionary.js";
import LayoutDepartment from "@/components/department/index.vue";
import { useUserStore } from "@/pinia/user.js";
import {
  setDepartmentAdd,
  setDepartmentEdit,
} from "@/request/enterprise_management.js";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false,
  },
  form: {
    type: Object,
    required: true,
    default: () => ({}),
  },
  type: {
    type: String,
    required: true,
    default: "",
  },
  parentName: {
    type: String,
    required: true,
    default: "",
  },
  parentId: {
    type: String,
    required: true,
    default: "",
  },
});
const emits = defineEmits(["update:visible", "update:form", "get-data"]);
const { visible, form } = useVModels(props, emits);
const rules = {
  NAME: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
  LEVEL: [{ required: true, message: "部门级别不能为空", trigger: "change" }],
  DEP_ORDER: [
    { required: true, message: "部门序号不能为空", trigger: "blur" },
    { type: "number", message: "部门序号必须为数字", trigger: "blur" },
  ],
  ISSUPERVISE: [
    { required: true, message: "是否监管部门不能为空", trigger: "blur" },
  ],
  checkedIds: [
    { required: true, message: "是否监管部门不能为空", trigger: "change" },
  ],
};
const userStore = useUserStore();
const formRef = ref(null);
const departmentLevel = await layoutFnGetDepartmentLevel();
const fnChangeRegulatoryAuthorities = (event) => {
  if (event === "1" && form.value.checkedIds.length === 0) {
    if (props.type === "add")
      form.value.checkedIds = [userStore.getUserInfo.DEPARTMENT_ID];
    else form.value.checkedIds = [form.value.DEPARTMENT_ID];
  }
};
const fnClose = () => {
  formRef.value.resetFields();
  visible.value = false;
};
const fnSubmit = debounce(
  1000,
  async () => {
    await useFormValidate(formRef);
    props.type === "add"
      ? await setDepartmentAdd({
          ...form.value,
          PARENT_ID: props.parentId,
          checkedIds: form.value.checkedIds.join(","),
        })
      : await setDepartmentEdit({
          ...form.value,
          checkedIds: form.value.checkedIds.join(","),
        });
    fnClose();
    emits("get-data");
  },
  { atBegin: true }
);
</script>

<style scoped lang="scss"></style>
