<template>
  <div class="safety-platform-container">
    <header class="safety-platform-container__header">
      <BreadcrumbBack />
      <span class="breadcrumb-title">{{ headerTitle }}</span>
    </header>
    <main class="safety-platform-container__main">
      <component :is="dynamicComponent" :id="id" ref="dynamicComponentRef" />
    </main>
    <footer class="safety-platform-container__footer" v-if="operate">
      <el-button @click="router.back()">取消</el-button>
      <el-button type="primary" @click="submit">提交</el-button>
    </footer>
    <UploadLoading :form-loading="formLoading" v-if="formLoading" />
  </div>
</template>

<script lang="ts" setup>
  import { useRoute, useRouter } from 'vue-router';
  import { ref, computed, defineAsyncComponent } from 'vue';
  import { ElMessage } from 'element-plus';
  import UploadLoading from '@/components/UploadLoading.vue';
  import type { WarningInfoRuleForm } from './src/type';
  import { createWarningInfoItem, editWarningInfoItem } from '@/api/disaster-warning';

  const formLoading = ref(false);
  const router = useRouter();
  const route = useRoute();
  const operate = route.query.operate;
  const id = route.query.id;
  const headerTitle = computed(() => {
    const fixedTitle = '灾害预警信息';
    if (operate === 'create') {
      return `创建${fixedTitle}`;
    } else if (operate === 'edit') {
      return `编辑${fixedTitle}`;
    }
    return '未知操作';
  });
  const dynamicComponent = computed(() => {
    if (operate === 'create') {
      return defineAsyncComponent(() => import('./src/components/CreateWarningInfoItem.vue'));
    } else if (operate === 'edit') {
      return defineAsyncComponent(() => import('./src/components/EditWarningInfoItem.vue'));
    } else {
      return '';
    }
  });
  const dynamicComponentRef = ref();
  const createWarningInfoItemFunc = async (formData: WarningInfoRuleForm) => {
    const createParam = {
      disasterType: formData.disasterType,
      disasterLevel: formData.disasterLevel,
      warnTime: formData.warnTime,
      source: formData.source,
      content: formData.content,
      isPush: formData.isPush,
      userGroupList: formData.isPush ? formData.userGroupList : [],
    };
    await createWarningInfoItem(createParam);
  };
  const editWarningInfoItemFunc = async (formData: WarningInfoRuleForm) => {
    const editParam = {
      id: Number(id),
      disasterType: formData.disasterType,
      disasterLevel: formData.disasterLevel,
      warnTime: formData.warnTime,
      source: formData.source,
      content: formData.content,
      isPush: formData.isPush,
      userGroupList: formData.isPush ? formData.userGroupList : [],
    };
    await editWarningInfoItem(editParam);
  };
  const submit = async () => {
    if (!dynamicComponentRef.value) return;
    const res = await dynamicComponentRef.value.handleValidate();
    if (!res) return;
    const formData = dynamicComponentRef.value.getFormData();
    let message;
    try {
      formLoading.value = true;
      if (operate === 'create') {
        await createWarningInfoItemFunc(formData);
        message = '创建成功';
      } else if (operate === 'edit') {
        await editWarningInfoItemFunc(formData);
        message = '编辑成功';
      }
      ElMessage.success(message);
      router.back();
    } finally {
      formLoading.value = false;
    }
  };
</script>

<style lang="scss" scoped>
  @use '@/styles/page-details-layout.scss' as *;
  @use './src/common.scss' as *;
</style>
