<template>
  <div>
    <BasicForm ref="basicFormRef" :formData="ruleFormData" :formRules="formRules" :formConfig="ruleFormConfig">
      <template #drillScope>
        <el-select v-model="ruleFormData.drillScope" placeholder="请选择演练规模" filterable>
          <el-option
            v-for="item in drillScopeDice"
            :key="item.itemCode"
            :label="item.itemValue"
            :value="item.itemCode"
          />
        </el-select>
      </template>
      <template #responsibleDeptIdList>
        <el-cascader
          v-model="ruleFormData.responsibleDeptIdList"
          :options="deptTree"
          :props="cascaderProp"
          clearable
          collapse-tags
          :show-all-levels="false"
          :max-collapse-tags="3"
          placeholder="请选择责任部门"
        >
        </el-cascader>
      </template>
      <template #coordinateDeptIdList>
        <el-cascader
          v-model="ruleFormData.coordinateDeptIdList"
          :options="deptTree"
          :props="cascaderProp"
          clearable
          collapse-tags
          :show-all-levels="false"
          :max-collapse-tags="3"
          popper-class="cascader-popper--custom"
          placeholder="请选择配合部门"
        >
        </el-cascader>
      </template>
      <template #emergencyPlanId>
        <el-select
          v-model="ruleFormData.emergencyPlanId"
          placeholder="请选择应急预案"
          popper-class="el-scrollbar--custom"
          filterable
        >
          <el-option v-for="item in allPlans" :key="item.id" :label="item.planName" :value="item.id" />
        </el-select>
      </template>
      <template #approvalTemplateId>
        <el-select
          v-model="ruleFormData.approvalTemplateId"
          placeholder="请选择审批流程"
          popper-class="el-scrollbar--custom"
          filterable
        >
          <el-option v-for="item in allApprovals" :key="item.id" :label="item.templateName" :value="item.id" />
        </el-select>
      </template>
    </BasicForm>
  </div>
</template>

<script setup lang="ts">
  import BasicForm from '@/components/BasicForm.vue';
  import { useFormConfigHook } from '@/hooks/useFormConfigHook';
  import { DRILL_CREATE_FORM_CONFIG, DRILL_CREATE_FORM_DATA, DRILL_CREATE_FORM_RULES } from '../configs/plan/form';
  import { CreateEmergencyDrillRuleForm } from '../types';
  import { onMounted, ref } from 'vue';
  import { DeptTree } from '@/types/dept/type';
  import { getAllDepartments } from '@/api/auth/dept';
  import { getAllApproval } from '@/api/approval/approval';
  import { queryAllEmergencyPlan } from '@/api/emergency-drill/emergency-drill';
  import { ApprovalInstanceType } from '@/views/system/approval/types';
  import { useEmergencyDrillHook } from '../hook';

  // 表单配置初始化
  const basicFormRef = ref<InstanceType<typeof BasicForm>>();
  const { ruleFormConfig, ruleFormData, formRules, cloneRuleFormData, beforeRouteLeave } =
    useFormConfigHook<CreateEmergencyDrillRuleForm>(
      DRILL_CREATE_FORM_CONFIG,
      DRILL_CREATE_FORM_DATA,
      DRILL_CREATE_FORM_RULES,
    );
  const cascaderProp = {
    multiple: true,
    expandTrigger: 'hover',
    checkStrictly: true,
    emitPath: false,
    value: 'id',
    label: 'deptName',
  };

  // 获取级联部门数据
  const deptTree = ref<DeptTree[]>();
  const loadDeptTreeData = async () => {
    const result = await getAllDepartments();
    deptTree.value = result[0].children;
  };

  // 获取所有预案
  const allPlans = ref<{ id: number; planName: string; status: number }[]>();
  const loadAllPlans = async () => {
    const result = await queryAllEmergencyPlan();
    allPlans.value = result.filter((item) => item.status === 3);
  };

  // 获取所有审批流程
  const allApprovals = ref<ApprovalInstanceType[]>();
  const loadApprovalData = async () => {
    const result = await getAllApproval();
    allApprovals.value = result;
  };

  // 获取演练规模字典
  const { drillScopeDice, getDrillScopeDict } = useEmergencyDrillHook();

  onMounted(() => {
    cloneRuleFormData();
    beforeRouteLeave();
    getDrillScopeDict();
    loadDeptTreeData();
    loadAllPlans();
    loadApprovalData();
  });

  // 表单校验
  const formValidate = async () => {
    if (!basicFormRef.value) return;
    const parentValidateResult = await basicFormRef.value.validateForm();
    return parentValidateResult;
  };

  // 表单数据获取
  const getFormData = () => {
    cloneRuleFormData();
    return ruleFormData;
  };

  defineExpose({
    formValidate,
    getFormData,
  });
</script>

<style scoped></style>
