<template>
  <div>
    <div class="container-title">
      <span class="line"></span>
      <span class="title">应急物资</span>
    </div>
    <div class="supplies-container">
      <div class="supply-tabs">
        <span
          v-for="item in emergencySupplyDice"
          :key="item.itemCode"
          :class="activeTab === item.itemCode ? 'active-tab' : 'normal-tab'"
          @click="activeTab = item.itemCode"
        >
          {{ item.itemValue }}
        </span>
      </div>
      <div class="supply-list" v-loading="loading">
        <el-table :data="emergencySuppliesList" style="width: 100%; height: 100%">
          <el-table-column prop="supplyName" label="物资名称" width="105" show-overflow-tooltip />
          <el-table-column prop="location" label="位置" show-overflow-tooltip>
            <template #default="scope">
              {{ getLocation(scope.row.location) }}
            </template>
          </el-table-column>
          <el-table-column prop="currentQuantity" label="数量" width="80" show-overflow-tooltip />
          <el-table-column prop="keeperName" label="保管人" show-overflow-tooltip />
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { onMounted, ref, watch } from 'vue';
  import { useEmergencySuppliesHook } from '@/views/emergency/emergency-supplies/src/hook';
  import { QueryEmergencySuppliesInfoListRes, getEmergencySuppliesInfoList } from '@/api/command-center';

  const { emergencySupplyDice, getEmergencySupplyDict, getLocationDict, getLocation } = useEmergencySuppliesHook();

  const activeTab = ref('');
  const loading = ref(true);
  const emergencySuppliesList = ref<QueryEmergencySuppliesInfoListRes[]>([]);

  const getEmergencySuppliesList = async () => {
    emergencySuppliesList.value = await getEmergencySuppliesInfoList({
      supplyType: activeTab.value,
    });
    loading.value = false;
  };

  watch(
    () => activeTab.value,
    async () => {
      loading.value = true;
      await getEmergencySuppliesList();
      loading.value = false;
    },
  );

  onMounted(async () => {
    await getEmergencySupplyDict();
    await getLocationDict('all');
    activeTab.value = emergencySupplyDice.value[0]?.itemCode || '';
  });
</script>

<style scoped lang="scss">
  .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;
    }
  }

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

    .supply-tabs {
      font-size: 14px;
      cursor: pointer;
      display: flex;
      justify-content: space-evenly;
      margin: 15px 10px 25px 10px;

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

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

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

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

    .supply-list {
      height: calc(100% - 65px);
      overflow: auto;
    }
  }
</style>
