<template>
  <div class="task-item-container">
    <BasicForm ref="basicFormRef" :formData="ruleFormData" :formRules="formRules" :formConfig="ruleFormConfig">
      <template #deptName>
        <el-select
          v-model="ruleFormData.deptIdList"
          multiple
          collapse-tags
          collapse-tags-tooltip
          :max-collapse-tags="3"
          placeholder="请选择被检查（自查）所/部/中心"
          filterable
          :filter-method="handleFilterMethod"
        >
          <template #header v-if="isFilterData">
            <el-checkbox v-model="checkAll" :indeterminate="indeterminate" @change="handleCheckAllChange">
              全部单位
            </el-checkbox>
          </template>
          <el-option
            v-for="item in disasterLevelDepts"
            :key="item.itemCode"
            :label="item.itemValue"
            :value="String(item.itemCode)"
          />
        </el-select>
      </template>
      <template #inspectType>
        <div class="task-type-container">
          <el-select v-model="ruleFormData.inspectType" placeholder="请选择检查类型" @change="handleTaskTypeChange">
            <el-option v-for="item in INSPECT_TYPE_OPTIONS" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-select v-model="ruleFormData.templateId" placeholder="请选择关联模板" disabled>
            <el-option v-for="item in TASK_TEMPLATE_LIST" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </div>
      </template>
      <template #userGroupList>
        <GroupSelect v-model="ruleFormData.userGroupList" :groupOptions="groupOptions" />
      </template>
      <template #isPush>
        <el-radio-group v-model="ruleFormData.isPush">
          <el-radio :value="IS_PUSH.PUSH">是</el-radio>
          <el-radio :value="IS_PUSH.NOT_PUSH">否</el-radio>
        </el-radio-group>
      </template>
    </BasicForm>
  </div>
</template>

<script setup lang="ts">
  import { onMounted, ref, watch } from 'vue';
  import BasicForm from '@/components/BasicForm.vue';
  import GroupSelect from '@/components/PersonGroup/components/GroupSelect.vue';
  import { useFormConfigHook } from '@/hooks/useFormConfigHook';
  import { useUserInfoHook, useDeptInfoHook } from '@/views/disaster/hooks';
  import { useGroupInfoHook } from '@/components/PersonGroup/hook/groupInfo';
  import type { TaskManagementRuleForm } from '../type';
  import { TASK_MANAGEMENT_FROM_CONFIG_CREATE, TASK_MANAGEMENT_FROM_DATA, TASK_MANAGEMENT_FROM_RULES } from '../config';
  import { INSPECT_TYPE_OPTIONS } from '../constants/task-execution';
  import { TASK_TEMPLATE_LIST } from '../constants/template-detail';
  import { IS_PUSH } from '@/views/disaster/constant';

  const { realname } = useUserInfoHook();
  const { getDisasterLevelDepts, originalDisasterLevelDepts, disasterLevelDepts, isFilterData, handleFilterMethod } =
    useDeptInfoHook();
  const { getUserGroupList, groupOptions } = useGroupInfoHook();

  const basicFormRef = ref<InstanceType<typeof BasicForm>>();

  const handleTaskTypeChange = (taskType: number | null) => {
    ruleFormData.templateId = taskType;
  };

  const { ruleFormConfig, ruleFormData, formRules, cloneRuleFormData, beforeRouteLeave } =
    useFormConfigHook<TaskManagementRuleForm>(
      TASK_MANAGEMENT_FROM_CONFIG_CREATE,
      TASK_MANAGEMENT_FROM_DATA,
      TASK_MANAGEMENT_FROM_RULES,
    );

  const handleValidate = async () => {
    if (!basicFormRef.value) return;
    const parentValidateResult = await basicFormRef.value.validateForm();
    return parentValidateResult;
  };
  const getFormData = () => {
    cloneRuleFormData();
    return ruleFormData;
  };
  const checkAll = ref(false);
  const indeterminate = ref(false);
  const handleCheckAllChange = () => {
    if (checkAll.value) {
      ruleFormData.deptIdList = originalDisasterLevelDepts.value.map((item) => String(item.itemCode));
    } else {
      ruleFormData.deptIdList = [];
    }
  };
  defineExpose({
    handleValidate,
    getFormData,
  });

  onMounted(() => {
    getDisasterLevelDepts();
    getUserGroupList();
    ruleFormData.realname = realname;
    cloneRuleFormData();
    beforeRouteLeave();
  });
  watch(
    () => ruleFormData.deptIdList,
    (newVal) => {
      checkAll.value = newVal.length === originalDisasterLevelDepts.value.length;
      indeterminate.value = newVal.length > 0 && newVal.length < originalDisasterLevelDepts.value.length;
    },
  );
</script>

<style scoped lang="scss">
  @use '../style/task-item.scss' as *;
</style>
