<template>
  <div class="safety-platform-container">
    <header class="safety-platform-container__header">
      <div>
        <BreadcrumbBack />
        <span class="breadcrumb-title" style="margin-left: 8px">{{ '演练记录' }}</span>
      </div>
      <el-tabs v-model="activeName">
        <el-tab-pane
          v-for="item in EMERGENCY_DRILL_DETAIL_SUBPAGE"
          :key="item.value"
          :label="item.label"
          :name="item.value"
        />
      </el-tabs>
    </header>
    <main class="safety-platform-container__main">
      <component :is="dynamicComponent" :id="id" ref="dynamicComponentRef" />
    </main>
    <footer class="safety-platform-container__footer" v-if="activeName === EMERGENCY_DRILL_DETAIL_SUBPAGE[1].value">
      <!-- 按钮 -->
      <el-button type="primary" @click="downloadPDF">导出</el-button>
    </footer>
    <UploadLoading :form-loading="formLoading" v-if="formLoading" />
  </div>
</template>

<script setup lang="ts">
  import { ref, computed, defineAsyncComponent } from 'vue';
  import { useRoute, useRouter } from 'vue-router';
  import UploadLoading from '@/components/UploadLoading.vue';
  import { EMERGENCY_DRILL_DETAIL_SUBPAGE } from './constants';
  import { exportEmergencyDrillRecord } from '@/api/emergency-drill/emergency-drill';

  const formLoading = ref(false);
  const activeName = ref(EMERGENCY_DRILL_DETAIL_SUBPAGE[0].value);
  const router = useRouter();
  const route = useRoute();
  const id = route.query.id;

  const dynamicComponent = computed(() => {
    switch (activeName.value) {
      case 'activities':
        return defineAsyncComponent(() => import('./components/DrillPlanViewActivities.vue'));
      case 'records':
        return defineAsyncComponent(() => import('./components/DrillPlanViewRecords.vue'));
    }
  });

  const downloadPDF = async () => {
    try {
      await exportEmergencyDrillRecord(id);
    } catch (e) {
      console.log(e);
    }
  };
</script>

<style scoped lang="scss">
  @use '@/styles/page-details-layout.scss' as *;
  .safety-platform-container__header {
    padding-bottom: 0 !important;
  }
  :deep(.el-tabs__header) {
    margin: 0;
  }
  :deep(.el-tabs__item) {
    font-size: 14px !important;
  }
</style>
