<template>
  <el-drawer v-model="isDrawer" :size="width" :title="title" @close="handleReset">
    <el-form
      :model="formParams"
      :rules="rules"
      ref="formRef"
      label-placement="left"
      :label-width="80"
    >
      <el-form-item label="部门名称" prop="deptName">
        <!-- <el-input placeholder="请输入部门名称" v-model="formParams.deptName" /> -->
        <el-input
          placeholder="上海飞机制造有限公司"
          disabled
          v-if="formParams.deptId != null && formParams.deptId === props.deptList![0].deptId"
        />
        <el-select
          v-model="formParams.deptName"
          placeholder="请选择部门"
          @click="updateDeptInfoList"
          v-else
        >
          <el-option
            v-for="item in deptNameList"
            :label="item.deptName"
            :value="item.deptName"
            :disabled="!item.isEnable"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="上级部门" prop="parent">
        <el-input placeholder="上海飞机制造有限公司" disabled />
        <!-- <el-tree-select
          :data="getTreeList"
          clearable
          check-strictly
          v-model="formParams.parent"
          placeholder="请选择上级部门"
        /> -->
      </el-form-item>

      <el-form-item label="排序" prop="orderNum">
        <el-input-number placeholder="请输入排序" v-model="formParams.orderNum" />
      </el-form-item>
      <el-form-item label="状态" prop="isEnable">
        <el-switch v-model="formParams.isEnable">
          <template #checked> 启用 </template>
          <template #unchecked> 禁用 </template>
        </el-switch>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-space>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="primary" :loading="subLoading" @click="formSubmit">提交</el-button>
      </el-space>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import { ElMessage } from 'element-plus';
  import type { formParamsType } from './types';

  import { addDept, editDept, deptInfo } from '@/api/auth/dept';

  import { replaceParams } from '@/utils/helper/treeHelper';
  import { getDeptInfoList } from '@/api/deptInfo';

  type DeptNameListType = {
    isEnable: boolean;
    deptName: string;
  };
  const deptNameList = ref<DeptNameListType[]>([]);
  const updateDeptInfoList = () => {
    getDeptInfoList().then((res) => {
      deptNameList.value = res;
    });
  };

  const rules = {
    deptName: {
      required: true,
      message: '部门不能为空',
      trigger: 'blur',
    },
  };

  const emit = defineEmits(['change']);

  const props = defineProps({
    title: {
      type: String,
      default: '添加部门',
    },
    width: {
      type: Number,
      default: 450,
    },
    deptList: {
      type: Array<formParamsType>,
    },
  });

  const defaultValueRef = () => ({
    deptId: null,
    parent: null,
    isEnable: true,
    deptName: '',
    orderNum: undefined,
  });

  const message = ElMessage;
  const formRef: any = ref(null);
  const isDrawer = ref(false);
  const subLoading = ref(false);

  const getTreeList = computed(() => {
    return [
      {
        label: '顶级部门',
        value: 0,
        children: replaceParams(props.deptList || [], 'deptName', 'deptId'),
      },
    ];
  });

  const formParams = ref<formParamsType>(defaultValueRef());

  function openDrawer(deptId?) {
    isDrawer.value = true;
    if (deptId) {
      formParams.value.deptId = deptId;
      getInfo();
      return;
    } else {
      formParams.value.parent = props.deptList![0].deptId;
    }
  }

  function closeDrawer() {
    isDrawer.value = false;
  }

  function formSubmit() {
    formRef.value.validate((valid) => {
      if (!valid) {
        return message.error('请填写完整信息');
      }
      const msg = formParams.value.deptId ? '编辑成功' : '添加成功';
      if (formParams.value.deptId) {
        editDept(formParams.value).then((_) => {
          message.success(msg);
          emit('change');
          handleReset();
          closeDrawer();
        });
      } else {
        addDept(formParams.value).then((_) => {
          message.success(msg);
          emit('change');
          handleReset();
          closeDrawer();
        });
      }
    });
  }

  function handleReset() {
    formRef.value.resetFields();
    formParams.value = Object.assign(formParams.value, defaultValueRef());
  }

  function getInfo() {
    deptInfo({ id: formParams.value.deptId }).then((res) => {
      const params = {
        deptId: res.deptId,
        parent: res.parent,
        isEnable: res.isEnable,
        deptName: res.deptName,
        orderNum: res.orderNum,
      };
      formParams.value = Object.assign(formParams.value, params);
      isDrawer.value = true;
    });
  }

  defineExpose({
    openDrawer,
    closeDrawer,
  });
</script>
