<template>
  <div class="disaster-warning-container">
    <BasicForm ref="basicFormRef" :formData="ruleFormData" :formRules="formRules" :formConfig="ruleFormConfig">
      <template #disasterType>
        <el-select v-model="ruleFormData.disasterType" placeholder="请选择灾害类型" filterable>
          <el-option
            v-for="item in disasterTypeDice"
            :key="item.itemCode"
            :label="item.itemValue"
            :value="item.itemCode"
          />
        </el-select>
      </template>
      <template #disasterLevel>
        <el-select v-model="ruleFormData.disasterLevel" placeholder="请选择灾害等级">
          <el-option
            v-for="item in disasterLevelDice"
            :key="item.itemCode"
            :label="item.itemValue"
            :value="item.itemCode"
          />
        </el-select>
      </template>
      <template #attachmentListRes>
        <UploadFiles
          label="上传并导入文件"
          ref="uploadFilesRef"
          :fileList="ruleFormData.attachmentListRes"
          @uploadSuccess="handleUploadSuccess"
        />
      </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>
        <SelectGroup
          v-if="ruleFormData.isPush"
          ref="selectGroupRef"
          :userGroupList="ruleFormData.userGroupList || []"
          @userGroupListChange="handleUserGroupListChange"
        />
      </template>
    </BasicForm>
  </div>
</template>

<script setup lang="ts">
  import { onMounted, ref } from 'vue';
  import BasicForm from '@/components/BasicForm.vue';
  import UploadFiles from '@/components/UploadFiles/UploadFiles.vue';
  import SelectGroup from '@/components/PersonGroup/SelectGroup.vue';
  import { useFormConfigHook } from '@/hooks/useFormConfigHook';
  import { useDisasterWarningHook } from '../hook';
  import type { DefenseNoticeRuleForm } from '../type';
  import type { FileItem } from '@/components/UploadFiles/types.ts';
  import { getDefenseNoticeDetail } from '@/api/disaster-warning';
  import { DEFENSE_NOTICE_FROM_CONFIG, DEFENSE_NOTICE_FROM_DATA, DEFENSE_NOTICE_FROM_RULES } from '../config';
  import { IS_PUSH } from '@/views/disaster/constant';

  const props = defineProps<{
    id: number;
  }>();

  const basicFormRef = ref<InstanceType<typeof BasicForm>>();
  const selectGroupRef = ref<InstanceType<typeof SelectGroup>>();
  const { disasterTypeDice, disasterLevelDice, getDisasterTypeDict, getDisasterLevelDict } = useDisasterWarningHook();

  const { ruleFormConfig, ruleFormData, formRules, cloneRuleFormData, beforeRouteLeave } =
    useFormConfigHook<DefenseNoticeRuleForm>(
      DEFENSE_NOTICE_FROM_CONFIG,
      DEFENSE_NOTICE_FROM_DATA,
      DEFENSE_NOTICE_FROM_RULES,
    );

  const handleUserGroupListChange = (userGroupList: number[]) => {
    ruleFormData.userGroupList = userGroupList;
  };

  const handleUploadSuccess = (fileList: FileItem[]) => {
    ruleFormData.attachmentListRes = fileList;
  };

  const getDefenseNoticeDetailData = async () => {
    const res = await getDefenseNoticeDetail(props.id);
    for (const key in res) {
      ruleFormData[key] = res[key as keyof typeof res];
      ruleFormData.userGroupList = JSON.parse(res.userGroupList as unknown as string);
    }
    cloneRuleFormData();
  };
  const handleValidate = async () => {
    if (!basicFormRef.value) return;
    const parentValidateResult = await basicFormRef.value.validateForm();
    let childValidateResult = true;
    if (selectGroupRef.value) {
      childValidateResult = (await selectGroupRef.value.validateForm()) as boolean;
    }
    return parentValidateResult && childValidateResult;
  };
  const getFormData = () => {
    if (!ruleFormData.isPush) {
      ruleFormData.userGroupList = [];
    }
    cloneRuleFormData();
    return ruleFormData;
  };
  defineExpose({
    handleValidate,
    getFormData,
  });
  onMounted(() => {
    getDefenseNoticeDetailData();
    getDisasterTypeDict();
    getDisasterLevelDict();
    beforeRouteLeave();
  });
</script>

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