<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" @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 === IS_PUSH.PUSH"
          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 SelectGroup from '@/components/PersonGroup/SelectGroup.vue';
  import UploadFiles from '@/components/UploadFiles/UploadFiles.vue';
  import { useFormConfigHook } from '@/hooks/useFormConfigHook';
  import { useDisasterWarningHook } from '../hook';
  import { useUserInfoHook } from '@/views/disaster/hooks';
  import type { DefenseNoticeRuleForm } from '../type';
  import type { FileItem } from '@/components/UploadFiles/types.ts';
  import { DEFENSE_NOTICE_FROM_CONFIG, DEFENSE_NOTICE_FROM_DATA, DEFENSE_NOTICE_FROM_RULES } from '../config';
  import { IS_PUSH } from '@/views/disaster/constant';

  const { realname } = useUserInfoHook();
  const { disasterTypeDice, disasterLevelDice, getDisasterTypeDict, getDisasterLevelDict } = useDisasterWarningHook();

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

  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 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(() => {
    getDisasterTypeDict();
    getDisasterLevelDict();
    ruleFormData.realname = realname;
    cloneRuleFormData();
    beforeRouteLeave();
  });
</script>

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