<template>
  <div class="drill-activity-container">
    <div class="drill-container__title">
      <div class="drill-container--line"></div>
      <span>演练活动</span>
    </div>
    <div class="drill-container__content">
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="drill-container__content--item">
            <span class="label">演练规模：</span>
            <span class="value">{{ getDrillScope(drillPlanItemDetail.drillScope) }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="drill-container__content--item">
            <span class="label">演练内容：</span>
            <span class="value">{{ drillPlanItemDetail.drillContent }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="drill-container__content--item">
            <span class="label">计划完成时间：</span>
            <span class="value">{{ drillPlanItemDetail.dueCompleteTime }}</span>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="drill-container__content--item">
            <span class="label">责任部门：</span>
            <span class="value">
              <template
                v-for="(dept, index) in safatyJsonParse(drillPlanItemDetail.responsibleDeptNameList)"
                :key="index"
              >
                {{ dept }}
                <span v-if="index !== safatyJsonParse(drillPlanItemDetail.responsibleDeptNameList).length - 1">、</span>
              </template>
            </span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="drill-container__content--item" v-if="drillPlanItemDetail.coordinateDeptNameList">
            <span class="label">配合部门：</span>
            <span class="value">
              <template
                v-for="(dept, index) in safatyJsonParse(drillPlanItemDetail.coordinateDeptNameList)"
                :key="index"
              >
                {{ dept }}
                <span v-if="index !== safatyJsonParse(drillPlanItemDetail.coordinateDeptNameList).length - 1">、</span>
              </template>
            </span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="drill-container__content--item" v-if="drillPlanItemDetail.emergencyPlanAppendix">
            <span class="label">关联应急预案：</span>
            <span
              class="value font-primary link"
              @click="handlePreviewFile(drillPlanItemDetail.emergencyPlanAppendix)"
              >{{ JSON.parse(drillPlanItemDetail.emergencyPlanAppendix)[0].fileName }}</span
            >
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col>
          <div class="drill-container__content--item">
            <span class="label">审批流程：</span>
            <span class="value">{{ getApprovalName(drillPlanItemDetail.approvalTemplateId) }}</span>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue';
  import { useEmergencyDrillHook } from '../hook';
  import { DrillPlanItemDetail } from '../types';
  import { ApprovalInstanceType } from '@/views/system/approval/types';
  import { getAllApproval } from '@/api/approval/approval';
  import PreviewOnline from '@/views/disaster/components/PreviewOnline.vue';
  import { FILE_TYPE_ICON } from '@/components/UploadFiles/constants';

  const approvalList = ref<ApprovalInstanceType[]>([]);
  const previewOnlineRef = ref<InstanceType<typeof PreviewOnline>>();
  const { getDrillScopeDict, getDrillScope } = useEmergencyDrillHook();
  const safatyJsonParse = (str: string) => {
    return str.slice(1, -1).split(',');
  };
  const getApprovalList = async () => {
    approvalList.value = await getAllApproval();
  };
  const getApprovalName = (id: number) => {
    return approvalList.value.find((item) => item.id === id)?.templateName;
  };
  const handlePreviewFile = (str: string) => {
    const obj = JSON.parse(str);
    const file = Array.isArray(obj) ? obj[0] : obj;
    const url = file.fileUrl;
    const type = file.fileType as keyof typeof FILE_TYPE_ICON;
    if (!url) return;
    previewOnlineRef.value?.open(url, type);
  };
  defineProps<{
    drillPlanItemDetail: DrillPlanItemDetail;
  }>();
  onMounted(() => {
    getDrillScopeDict();
    getApprovalList();
  });
</script>

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