<template>
  <el-dialog
      v-model="props.visible"
      :title="props.deptId?'编辑':'新增'"
      width="600"
      :close-on-click-modal="false"
      :before-close="dialogClose"
  >
    <el-form
        ref="formRef"
        :model="formData"
        label-width="80px"
    >
    <el-form-item
          label="部门名称"
          prop="name"
          :rules="{ required: true, message: '请输入部门名称', trigger: 'blur' }"
      >
        <el-input
            v-model="formData.name"
            placeholder="请输入部门名称"
            clearable
        />
      </el-form-item>
      <el-form-item
          label="父级部门"
          prop="parentId"
          :rules="{ required: true, message: '请选择父级部门', trigger: 'change' }"
      >
        <el-tree-select
            class="flex-1"
            v-model="formData.parentId"
            :data="deptOptions"
            clearable
            node-key="id"
            :props="{
              label: 'name',
            }"
            :default-expand-all="true"
            placeholder="请选择父级菜单"
            check-strictly
        />
      </el-form-item>
      <el-form-item label="部门类型" prop="type">
          <el-select v-model="formData.type" class="flex-1" clearable placeholder="请选择部门类型">
            <el-option v-for="(item, index) in optionData.deptTypeList" :key="index" :label="item.name" :value="item.id" />
          </el-select>
      </el-form-item>
      <el-form-item label="部门排序" prop="sort"  class="flex-1">
        <div>
          <el-input-number v-model="formData.sort" :max="9999"/>
          <div class="form-tips">数值越小越排前</div>
        </div>
      </el-form-item>
      <el-form-item label="部门备注" prop="note">
          <el-input
            class="flex-1"
            v-model="formData.note"
            type="textarea"
            placeholder="请输入部门名称"
            clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button :loading="subLoading" type="primary" @click="submit">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import type {FormInstance} from "element-plus";
import { deptAdd,deptUpdate,getDeptList,getDeptDetail } from '@/api/system/dept';
import {onMounted, reactive, ref, shallowRef} from "vue";
import {message,buildTree} from "@/utils/auth";
import {useLockFn} from "@/utils/useLockFn";
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  },
  deptId: {
    type: Number,
    required: true,
    default: 0
  },
  pid: {
    type: Number,
    default: 0
  }
});
const optionData = reactive({
  deptTypeList: [{
    id:1,
    name:'公司'
  },
  {
    id:2,
    name:'子公司'
  },
  {
    id:3,
    name:'部门'
  },
  {
    id:4,
    name:'小组'
  }],
});
const emit = defineEmits(["success","update:visible"]);
const formRef = shallowRef<FormInstance>();
const formData = reactive({
  id: "",
  //父级id
  parentId: 0,
  //类型
  type: 1,
  //名称
  name: "",
  //排序
  sort: 0,
  note:''
});

const dialogClose = () => {
  emit("update:visible", false);
};

const deptOptions = ref<any[]>([]);

const getMenu = async () => {
  const data: any = await getDeptList();
  const menu: any = [{id: 0, name: "顶级", children: []}];
  const lists = buildTree(data)
  menu[0].children.push(...lists)
  deptOptions.value = menu
};

const handleSubmit = async () => {
    await formRef.value?.validate();
    props.deptId ? await deptUpdate(formData) : await deptAdd(formData);
    message("操作成功");
    emit("update:visible", false);
    emit("success");
};

const { isLock:subLoading,lockFn: submit } = useLockFn(handleSubmit);


const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key];
    }
  }
};

const getDetail = async () => {
  const data = await getDeptDetail(
    props.deptId
  );
  setFormData(data);
};




onMounted(() => {
  getMenu()
  if (props.deptId) {
    getDetail()
  }else{
    formData.parentId=props.pid
  }
});
</script>
