<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 { TaskManagementRuleForm } from './src/type';
  import { createTaskManagementItem, editTaskManagementItem } from '@/api/disaster-precaution';

  const router = useRouter();
  const route = useRoute();
  const operate = route.query.operate;
  const id = route.query.id;
  const formLoading = ref(false);
  const headerTitle = computed(() => {
    const title = '灾害检查任务';
    if (operate === 'create') {
      return `创建${title}`;
    } else if (operate === 'edit') {
      return `编辑${title}`;
    }
    return `查看${title}`;
  });
  const dynamicComponent = computed(() => {
    if (operate === 'create') {
      return defineAsyncComponent(() => import('./src/components/CreateTaskItem.vue'));
    } else if (operate === 'edit') {
      return defineAsyncComponent(() => import('./src/components/EditTaskItem.vue'));
    }
    return defineAsyncComponent(() => import('./src/components/ViewTaskItem.vue'));
  });
  const dynamicComponentRef = ref();
  const createTaskManagementItemFunc = async (formData: TaskManagementRuleForm) => {
    const createParam = {
      name: formData.name,
      deptIdList: formData.deptIdList,
      inspectType: formData.inspectType,
      templateId: formData.templateId,
      dueCompleteTime: formData.dueCompleteTime,
      inspectRequirement: formData.inspectRequirement,
      isPush: formData.isPush,
      userGroupList: formData.userGroupList,
    };
    await createTaskManagementItem(createParam);
  };
  const editTaskManagementItemFunc = async (formData: TaskManagementRuleForm) => {
    const editParam = {
      id,
      name: formData.name,
      inspectType: formData.inspectType,
      templateId: formData.templateId,
      dueCompleteTime: formData.dueCompleteTime,
      inspectRequirement: formData.inspectRequirement,
      isPush: formData.isPush,
      userGroupList: formData.userGroupList,
    };
    await editTaskManagementItem(editParam);
  };
  const submit = async () => {
    if (!dynamicComponentRef.value) return;
    const res = await dynamicComponentRef.value.handleValidate();
    if (res) {
      const formData = dynamicComponentRef.value.getFormData();
      let message;
      try {
        formLoading.value = true;
        if (operate === 'create') {
          await createTaskManagementItemFunc(formData);
          message = '创建成功';
        } else if (operate === 'edit') {
          await editTaskManagementItemFunc(formData);
          message = '编辑成功';
        }
        ElMessage.success(message);
        router.back();
      } finally {
        formLoading.value = false;
      }
    } else {
      console.log('不提交');
    }
  };
</script>

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