<template>
  <div class="task-item-container">
    <BasicForm ref="basicFormRef" :formData="ruleFormData" :formRules="formRules" :formConfig="ruleFormConfig">
      <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 } from 'vue';
  import BasicForm from '@/components/BasicForm.vue';
  import GroupSelect from '@/components/PersonGroup/components/GroupSelect.vue';
  import { useFormConfigHook } from '@/hooks/useFormConfigHook';
  import { useGroupInfoHook } from '@/components/PersonGroup/hook/groupInfo';
  import type { TaskManagementRuleForm } from '../type';
  import { viewTaskManagementDetail } from '@/api/disaster-precaution';
  import { TASK_MANAGEMENT_FROM_CONFIG_EDIT, 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 props = defineProps<{
    id: number;
  }>();

  const { getUserGroupList, groupOptions } = useGroupInfoHook();

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

  const getTaskManagementItemInfo = async () => {
    const res = await viewTaskManagementDetail(props.id);
    for (const key in res) {
      if (key in ruleFormData) {
        ruleFormData[key] = res[key as keyof typeof res];
        ruleFormData.userGroupList = JSON.parse(res.userGroupList as unknown as string);
      }
    }
    ruleFormData.deptName = res.deptName;
    cloneRuleFormData();
  };

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

  const { ruleFormConfig, ruleFormData, formRules, cloneRuleFormData, beforeRouteLeave } =
    useFormConfigHook<TaskManagementRuleForm>(
      TASK_MANAGEMENT_FROM_CONFIG_EDIT,
      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;
  };
  defineExpose({
    handleValidate,
    getFormData,
  });

  onMounted(() => {
    getUserGroupList();
    getTaskManagementItemInfo();
    beforeRouteLeave();
  });
</script>

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