<template>
  <div class="disaster-warning-records-container">
    <div class="container-title">
      <span class="line"></span>
      <span class="title">灾害预警记录</span>
      <span
        >共 <span class="record-num">{{ disasterWarningCount }}</span> 条
      </span>
    </div>
    <div class="records-container">
      <div class="time-tabs">
        <span
          v-for="item in timeTabs"
          :key="item.value"
          :class="activeTab === item.value ? 'active-tab' : 'normal-tab'"
          @click="activeTab = item.value"
          >{{ item.label }}</span
        >
      </div>
      <div class="records-info" v-loading="loading">
        <div v-if="disasterWarningCount === 0" class="empty-style">
          <img class="empty-img" src="@/assets/images/empty@1X.png" alt="" />
          <span>暂无数据</span>
        </div>
        <div v-if="activeTab === DisasterTimeSpan.ONE_QUARTER">
          <QuarterRecords :quarter-records="disasterWarningRecords" :weather-disaster-dic="weatherDisasterDic" />
        </div>
        <div v-else>
          <RecordItem
            v-for="(record, index) in warningRecordsList"
            :key="index"
            :img-url="weatherDisasterDic.find((item) => item.itemCode === record.disasterType)?.imageUrl"
            :disasterType="weatherDisasterDic.find((item) => item.itemCode === record.disasterType)?.itemValue"
            :warnTime="record.warnTime"
            :source="record.source"
            :content="record.content"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, ref, watch } from 'vue';
  import RecordItem from './disaster-warning-records/RecordItem.vue';
  import QuarterRecords from './disaster-warning-records/QuarterRecords.vue';
  import { DisasterTimeSpan, disasterTimeSpanTextMap } from './disaster-warning-records/constant';
  import { SysDictDataDetail, queryDictTypeDetail } from '@/api/dict';
  import { WarnRecord, DisasterWarningRecordsRes, getDisasterWarningRecords } from '@/api/disaster-overview';

  const loading = ref(true);
  const activeTab = ref<DisasterTimeSpan>(DisasterTimeSpan.ONE_WEEK);
  const weatherDisasterDic = ref<SysDictDataDetail[]>([]);
  const disasterWarningCount = ref(0); // 季度求和，其他直接赋值
  const disasterWarningRecords = ref<DisasterWarningRecordsRes[]>([]);
  const warningRecordsList = ref<WarnRecord[]>([]);

  const timeTabs = [
    {
      id: DisasterTimeSpan.ONE_WEEK,
      label: disasterTimeSpanTextMap[DisasterTimeSpan.ONE_WEEK],
      value: DisasterTimeSpan.ONE_WEEK,
    },
    {
      id: DisasterTimeSpan.ONE_MONTH,
      label: disasterTimeSpanTextMap[DisasterTimeSpan.ONE_MONTH],
      value: DisasterTimeSpan.ONE_MONTH,
    },
    {
      id: DisasterTimeSpan.ONE_QUARTER,
      label: disasterTimeSpanTextMap[DisasterTimeSpan.ONE_QUARTER],
      value: DisasterTimeSpan.ONE_QUARTER,
    },
    {
      id: DisasterTimeSpan.ONE_YEAR,
      label: disasterTimeSpanTextMap[DisasterTimeSpan.ONE_YEAR],
      value: DisasterTimeSpan.ONE_YEAR,
    },
  ];

  watch(
    () => activeTab.value,
    async () => {
      loading.value = true;
      const res = await getDisasterWarningRecords(timeTabs.find((item) => item.value === activeTab.value)?.id || 1);
      disasterWarningRecords.value = res;
      if (activeTab.value === DisasterTimeSpan.ONE_QUARTER) {
        disasterWarningCount.value = disasterWarningRecords.value.reduce((acc, cur) => acc + cur.warnCount, 0);
      } else {
        disasterWarningCount.value = disasterWarningRecords.value[0].warnCount;
        warningRecordsList.value = disasterWarningRecords.value[0].warnRecord;
      }
      loading.value = false;
    },
    { immediate: true },
  );

  onMounted(async () => {
    await queryDictTypeDetail('weather_warning').then((res) => {
      weatherDisasterDic.value = res.sysDictDataList;
    });
  });
</script>

<style lang="scss" scoped>
  .disaster-warning-records-container {
    .container-title {
      height: 24px;
      display: flex;
      align-items: center;
      margin: 10px 0;
      font-weight: 500;
      font-size: 16px;
      color: #000000;

      .line {
        width: 3px;
        height: 16px;
        background: #1777ff;
        margin-right: 10px;
      }

      .title {
        margin-right: 12px;
      }

      .record-num {
        color: #1777ff;
      }
    }

    .records-container {
      height: calc(100% - 44px);
      padding-bottom: 10px;

      .time-tabs {
        font-size: 14px;
        cursor: pointer;
        display: flex;
        justify-content: space-evenly;
        margin-bottom: 20px;

        .normal-tab {
          color: rgba(0, 0, 0, 0.65);
        }

        .normal-tab:hover {
          color: #1777ff;
        }

        .active-tab {
          color: #1777ff;
          font-weight: 600;
          position: relative;
        }

        .active-tab::after {
          content: '';
          display: block;
          width: 16px;
          height: 2px;
          background-color: #1777ff;
          border-radius: 2px;
          margin-top: 2px;
          position: absolute;
          left: 50%;
          transform: translateX(-50%);
        }
      }

      .records-info {
        height: calc(100% - 40px);
        overflow: auto;
        padding: 0 16px;

        .empty-style {
          width: 100%;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          color: rgba(0, 0, 0, 0.5);

          .empty-img {
            width: 100%;
            object-fit: contain;
          }
        }
      }
    }
  }
</style>
