<template>
  <Dialog type="xl" :title="title" class="custom-dialog" customShow hide-footer @close="handleClose">
    <div class="common-header">
<!--      <div v-if="detailData.del_flag !== '2'" class="common-btn-primary2" @click="handleUpdate">-->
<!--        <i class="edit-icon" />-->
<!--        编辑-->
<!--      </div>-->
      <div v-if="detailData.event_status === '0'" class="common-btn-primary2" style="margin-left: 20px" @click="handleStartEvent">
        <i class="command-icon" />
        开始指挥
      </div>
      <div v-if="detailData.event_status === '2'" class="common-btn-danger" style="margin-left: 20px" @click="handleCloseEvent">
        <i class="close-icon" />
        关闭事件
      </div>
      <div v-if="detailData.del_flag === '0'" class="common-btn-danger" style="margin-left: 20px" @click="handleShowDeleteDialog">删除事件</div>
    </div>
    <div class="scroll-box">
      <div class="common-title-box">基础信息</div>
      <div class="form">
        <div class="form-item">
          <div class="text1">事件编号</div>
          <div class="text2">{{ detailData.event_id }}</div>
        </div>
        <div class="form-item">
          <div class="text1">事件类型</div>
          <div class="text2">
            <dict-tag :options="mm_event_type" :value="detailData.event_type" />
          </div>
        </div>
        <div class="form-item">
          <div class="text1">事件等级</div>
          <div class="text2">
            <dict-tag :options="mm_event_level" :value="detailData.event_level" />
            <!--          <el-icon style="margin-left: 20px; cursor: pointer" @click="handleEventLevelOpen"><Fold /></el-icon>-->
          </div>
        </div>
        <div class="form-item">
          <div class="text1">事件状态</div>
          <div class="text2">
            <dict-tag :options="mm_event_state" :value="detailData.event_status" />
          </div>
        </div>
        <div class="form-item">
          <div class="text1">事发地点</div>
          <div class="text2">{{ detailData.address }}</div>
        </div>
        <div class="form-item">
          <div class="text1">事发时间</div>
          <div class="text2">{{ detailData.event_time }}</div>
        </div>
        <div class="form-item">
          <div class="text1">上报时间</div>
          <div class="text2">{{ detailData.report_time }}</div>
        </div>
        <div class="form-item">
          <div class="text1">伤亡情况</div>
          <div class="text2">
            <div v-if="detailData.casualties == '0'" class="flex">
              <span>未上报</span>
              <!--            <span class="link" style="margin-left: 20px" @click="uploadeCasualties">去上报</span>-->
            </div>
            <div v-if="detailData.casualties == '1'" class="flex">
              <span>已上报</span>
            </div>
          </div>
        </div>
        <div class="form-item">
          <div class="text1">登记人</div>
          <div class="text2">{{ detailData.reported_by }}</div>
        </div>
        <div class="form-item">
          <div class="text1">登记时间</div>
          <div class="text2">{{ detailData.report_time }}</div>
        </div>
        <div class="form-item">
          <div class="text1">联系方式</div>
          <div class="text2">{{ detailData.contact }}</div>
        </div>
        <div class="form-item">
          <div class="text1">事件来源</div>
          <div class="text2">{{ detailData.event_source }}</div>
        </div>
      </div>
      <div class="common-title-box">事件概要</div>
      <div v-if="!!detailData.event_description" class="text-box">{{ detailData.event_description }}</div>
      <div class="common-title-box">事件跟踪</div>
      <div style="padding: 0px 50px 15px 16px">
        <Step :step-data="eventTrackState" style="width: 100%" />
      </div>
      <div class="common-title-box">指挥记录</div>
      <div class="info-content">
        <div class="list">
          <div class="list-item">
            <i class="img" />
            <div class="item-title">大屏指挥记录</div>
          </div>
          <div class="list-item">
            <i class="img" />
            <div class="item-title">中屏指挥记录</div>
          </div>
        </div>
      </div>
      <div class="common-title-box">匹配预案</div>
      <div v-if="!!planFiles && planFiles.length > 0" class="common-info-content">
        <div class="list2">
          <div v-for="(item, index) in planFiles" :key="index" class="list-item" style="margin-bottom: 10px">
            <div class="link" @click="previewSummaryFile2(item)">{{ item.name }}</div>
            <div style="margin-left: 40px" @click="previewSummaryFile2(item)">查看</div>
            <div style="margin-left: 20px; display: flex; align-items: center" @click="downloadSummaryFile2(item)">
              <span>下载</span>
              <el-icon class="icon" style="margin-left: 0px"><Download /></el-icon>
            </div>
          </div>
        </div>
      </div>
<!--      <div v-if="!!detailData.plan_name" class="info-content">-->
<!--        <div class="list">-->
<!--          <div class="list-item">-->
<!--            <i class="img" />-->
<!--            <div class="item-title">{{ detailData.plan_name }}</div>-->
<!--          </div>-->
<!--        </div>-->
<!--      </div>-->
      <div class="common-title-box">事件总结报告</div>
      <div v-if="!!summaryFiles && summaryFiles.length > 0" class="info-content">
        <div class="list2">
          <div v-for="(item, index) in summaryFiles" :key="index" class="list-item" style="margin-bottom: 10px">
            <div class="link" @click="previewSummaryFile(item)">{{ item.file_name }}</div>
            <div style="margin-left: 40px" @click="previewSummaryFile(item)">查看</div>
            <div style="margin-left: 20px; display: flex; align-items: center" @click="downloadSummaryFile(item)">
              <span>下载</span>
              <el-icon class="icon" style="margin-left: 0px"><Download /></el-icon>
            </div>
          </div>
        </div>
      </div>
<!--      <div v-if="!!summaryFiles && summaryFiles.length > 0" class="info-content">-->
<!--        <div class="list2">-->
<!--          <div v-for="(item, index) in summaryFiles" :key="index" class="list-item" @click="downloadSummaryFile(item.url)">-->
<!--            <div class="link">{{ item.file_name }}</div>-->
<!--            <el-icon class="icon"><Download /></el-icon>-->
<!--          </div>-->
<!--        </div>-->
<!--      </div>-->
    </div>
    <Dialog v-model="showDeleteDialog" class="tip" type="xs" title="提示">确认删除事件吗</Dialog>
  </Dialog>
  <Dialog v-if="dialogTableVisible" v-model="dialogTableVisible" title="文件预览" height="1800px">
    <div style="font-size: 18px;font-weight: bold;margin-bottom: 10px;">{{ detailInfo.file_name }}</div>
    <pdf-viewer :url="baseUrl + '/file/download/' + detailInfo.url" />
  </Dialog>
</template>

<script lang="ts" setup name="EventDetail">
import { getEventDetail } from '@/api/duty/eventing';
import { download2 } from '@/utils/request';
import PdfViewer from '@/views/knowledge/HiddenStandards/PdfViewer.vue';

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: () => {
      return false;
    }
  },
  eventId: String,
  title: String
});
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { mm_event_type, mm_event_level, mm_event_state, region } = toRefs<any>(
  proxy?.useDict('mm_event_type', 'mm_event_level', 'mm_event_state', 'region')
);
const emits = defineEmits(['update:modelValue']);

const detailData = ref({
  event_id: '',
  event_title: '',
  event_type: '',
  event_level: '',
  event_status: '',
  event_time: '',
  address: '',
  report_time: '',
  event_source: '',
  reported_by: '',
  event_description: '',
  contact: '',
  plan_name: '',
  casualties: '',
  deaths: '',
  injuries: '',
  missing: '',
  del_flag: ''
});
// 事件跟踪数据
let eventTrackState = reactive({
  active: 0,
  items: [
    {
      title: '事件登记',
      description: ''
    },
    {
      title: '进入指挥',
      description: ''
    },
    {
      title: '结束指挥',
      description: ''
    },
    {
      title: '关闭事件',
      description: ''
    }
  ]
});
// 事件等级数据
let eventLevelState = reactive({
  show: false,
  data: []
});
// 下载总结报告
let summaryFiles = ref([]);
let planFiles = ref([]);
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const downloadSummaryFile = (file: any) => {
  // window.open(url);
  download2(baseUrl + '/file/download/' + file.url, file.file_name);
};
const detailInfo = ref({});
const detailInfo2 = ref({});
const dialogTableVisible = ref(false);
const dialogTableVisible2 = ref(false);
const previewSummaryFile = (file) => {
  detailInfo.value = file;
  if (file) {
    dialogTableVisible.value = true;
  }
};
const previewSummaryFile2 = (file) => {
  detailInfo2.value = file;
  if (file) {
    dialogTableVisible2.value = true;
  }
};
const downloadSummaryFile2 = (file: any) => {
  download2(baseUrl + '/file/download/' + file.url, file.name);
};

const handleClose = () => {
  emits('update:modelValue', false);
};
const handleUpdate = () => {
  console.log('handleUpdate');
  // eventEditDialogState.title = '修改事件';
  // eventEditDialogState.show = true;
};

const showDeleteDialog = ref(false);
// 删除事件
const handleShowDeleteDialog = () => {
  showDeleteDialog.value = true;
};
const handleDeleteEvent = () => {
  deleteEvent({ eventId: props.eventId }).then((res) => {
    proxy?.$modal.msgSuccess(res.msg);
    fetchEventDetail();
  });
};

const initData = () => {
  getEventDetail({ event_id: props.eventId }).then((res) => {
    detailData.value = res.data;
    eventTrackState = res.data.event_status_tracks;
    eventLevelState.data = res.data.event_level_tracks;
    summaryFiles.value = res.data.summary_file;
    planFiles.value = res.data.plan_files;
  });
};

onMounted(() => {
  initData();
});
</script>

<style lang="scss" scoped>
.custom-dialog {
  :deep(.dialog-header) {
    pointer-events: none;
    .dialog-close {
      pointer-events: auto;
    }
  }
  :deep(.dialog-content) {
    margin-top: -20px;
  }
  .tip {
    :deep(.dialog-content) {
      margin-top: 0;
    }
  }
  .common-header {
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
  .form {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    padding: 0 15px 10px;
    .form-item {
      width: calc(25% - 15px);
      margin-left: 20px;
      &:nth-child(4n + 1) {
        margin-left: 0;
      }
      .text1 {
        font-size: 14px;
        color: #8da1be;
        line-height: 30px;
      }
      .text2 {
        font-size: 14px;
        line-height: 30px;
        color: #ffffff;
        display: flex;
        align-items: center;
      }
    }
  }
  .common-title-box {
    margin-bottom: 10px;
  }
  .list {
    display: flex;
    .list-item {
      display: flex;
      flex-direction: column;
      align-content: center;
      justify-content: center;
      margin-right: 20px;
      cursor: pointer;
      position: relative;
      .img {
        width: 251px;
        height: 150px;
        background: url('@/assets/images/cardBg.png') no-repeat;
        background-size: 100% 100%;
        cursor: pointer;
      }
      .item-title {
        position: absolute;
        left: 50%;
        bottom: 16px;
        transform: translateX(-50%);
        color: #2c81ff;
      }
      &:last-child {
        margin-right: 0;
      }
    }
    .list-item2 {
      margin-right: 40px;
      cursor: pointer;
      &:last-child {
        margin-right: 0;
      }
    }
  }
  .info-content {
    padding: 20px 35px 30px;
  }
  .list2 {
    .list-item {
      display: flex;
      align-items: center;
      color: #2c81ff;
      cursor: pointer;
      .icon {
        margin-left: 10px;
        cursor: pointer;
      }
    }
  }
  .scroll-box {
    height: 600px;
    overflow-y: auto;
  }
}
</style>
