<script setup lang="ts">
import { ref, onMounted, computed } from "vue";
import EventStatus from "./EventStatus.vue";
import EventTask from "./EventTask.vue";
import RealTimeImage from "./RealTimeImage.vue";
import { weatherForecast, weatherHourly, todayStatusCount, todayHourly, getDictData } from "@/api/modules/index";
import { useTodayTime, } from "@/utils/packge";

const { todayTime, getTodayTime } = useTodayTime();
// 模拟数据，实际项目中可能需要从API获取
const pendingCount = ref<string>('');
const processingCount = ref<string>('');
const serviceArea = ref<any[]>([]);
const Hub = ref<any[]>([]);
const Intercommunication = ref<any[]>([]);
// 服务区数据
const selectedServiceArea = ref("全部");
const serviceAreas = ref([]);
// 服务区选择变更处理
const handleServiceAreaChange = (area: string) => {
  selectedServiceArea.value = area;
};

// 区域类型选择状态（服务区、互通、枢纽）
const areaTypes = ref([
  { id: "service", name: "服务区", selected: true },
  { id: "interchange", name: "互通", selected: false },
  { id: "hub", name: "枢纽", selected: false },
]);

// 切换区域类型选中状态
const toggleAreaType = (id: string) => {
   areaTypes.value.forEach((type) => {
    type.selected = type.id === id;
  });
};
onMounted(()=>{
    todayStatusCount({todayTime: todayTime.value}).then((res: any) => {
      if(res.code === 200){
        res.data.forEach((item: any) => {
          if(item.status == '0'){ //待处理
            pendingCount.value = item.count || 0
          } else if(item.status == '2'){ //处置中
            processingCount.value = item.count || 0
          }
        });
      }
    })
    getDictData("hb_service_area").then((res: any) => { //服务区
      if (res.code === 200) {
        serviceArea.value = res.data || [];
      }
    });
    getDictData("hb_hub").then((res: any) => {  //枢纽
      if (res.code === 200) {
        Hub.value = res.data || [];
      }
    });
    getDictData("hb_interchange").then((res: any) => { //互通
      if (res.code === 200) {
        Intercommunication.value = res.data || [];
      }
    });
})
const currentAreaList = computed(() => {
  const selectedType = areaTypes.value.find((type) => type.selected);
  if (selectedType?.id === "service") {
    return serviceArea.value;
  } else if (selectedType?.id === "interchange") {
    return Intercommunication.value;
  } else if (selectedType?.id === "hub") {
    return Hub.value;
  }
  return [];
});
const isDropdownOpen = ref(false);
</script>

<template>
  <div class="traffic-flow-grid">
    <div class="top">
      <div class="traffic-flow-item">
        <div style="margin-left: 396px" class="item-title">今日事件处理</div>
        <div style="margin-left: 396px" class="item-content">
          <EventStatus
            :pending-count="pendingCount"
            :processing-count="processingCount"
          />
        </div>
      </div>
      <div class="traffic-flow-item">
        <div class="item-title">事件处置任务</div>
        <div class="item-content">
          <EventTask />
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="item-title">
        <div class="title-text">实时图像</div>

        <div class="area-type-selector">
          <div
            v-for="type in areaTypes"
            :key="type.id"
            class="area-type-item"
            :class="{ active: type.selected }"
            @click="toggleAreaType(type.id)"
          >
            {{ type.name }}
          </div>
        </div>

        <div class="service-area-selector">
          <div class="selected-area" @click="isDropdownOpen = !isDropdownOpen">
            {{ selectedServiceArea }} <span class="arrow-down">▼</span>
          </div>
          <div class="dropdown-menu" v-show="isDropdownOpen">
            <div
              v-for="(item, index) in currentAreaList"
              :key="index"
              class="dropdown-item"
              :class="{ active: selectedServiceArea === item.dictLabel }"
              @click="
                handleServiceAreaChange(item.dictLabel);
                isDropdownOpen = false;
              "
            >
              {{ item.dictLabel }}
            </div>
          </div>
        </div>
      </div>
      <div class="item-content">
        <RealTimeImage :service-area="selectedServiceArea" />
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.traffic-flow-grid {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  margin-right: 20px;
  // justify-content: space-between;
  // align-items: flex-start;
  .top {
    width: 100%;
    height: 652px;
    display: flex;
    .traffic-flow-item {
      flex: 1;
      .item-title {
        width: 1080px;
        height: 104px;
        background-image: url("../../assets/img/leftoright/title.png");
        margin-left: 48px;
        margin-top: 48px;
        font-family: Alimama ShuHeiTi, Alimama ShuHeiTi;
        font-weight: 700;
        font-size: 52px;
        color: #ffffff;
        line-height: 104px;
        padding-left: 56px;
        letter-spacing: 2px;
        text-shadow: 0px 4px 0px #095a8c;
        text-align: left;
        font-style: normal;
        text-transform: none;
      }
      .item-content {
        width: 1080px;
        height: 468px;
        margin-left: 48px;
        margin-top: 32px;
        background-image: url("../../assets/img/leftoright/rightcontent.png");
      }
    }
  }
  .bottom {
    flex: 1;
    width: 100%;
    height: 100%;
    margin-top: 48px;
    .item-title {
      width: 2208px;
      height: 104px;
      margin-left: 396px;
      background-image: url("../../assets/img/leftoright/bigtitle.png");
      font-family: Alimama ShuHeiTi, Alimama ShuHeiTi;
      font-weight: 700;
      font-size: 52px;
      color: #ffffff;
      line-height: 104px;
      padding-left: 56px;
      letter-spacing: 2px;
      text-shadow: 0px 4px 0px #095a8c;
      text-align: left;
      font-style: normal;
      text-transform: none;
      position: relative;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      padding-right: 56px;

      .title-text {
        font-family: Alimama ShuHeiTi, Alimama ShuHeiTi;
        font-weight: 700;
        font-size: 52px;
        color: #ffffff;
        letter-spacing: 2px;
        text-shadow: 0px 4px 0px #095a8c;
        text-align: left;
        font-style: normal;
        text-transform: none;
      }

      .area-type-selector {
        position: absolute;
        right: 320px;
        top: 50%;
        transform: translateY(-50%);
        display: flex;
        gap: 20px;
        font-size: 32px;
        font-weight: normal;
        color: #ffffff;
        text-shadow: none;
        z-index: 99;

        .area-type-item {
          width: 150px;
          height: 64px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          transition: all 0.2s;
          color: #bad5ff;
          position: relative;
          border: 1px solid #bad5ff;
          border-radius: 4px;
          background-color: #304b75;
          box-shadow: inset 0 0 10px rgba(0, 79, 127, 0.3),
            0 0 5px rgba(0, 198, 255, 0.2);
          text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
          overflow: hidden;

          &::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(
              135deg,
              rgba(0, 198, 255, 0.1) 0%,
              rgba(0, 118, 170, 0.05) 40%,
              transparent 100%
            );
            z-index: -1;
            border-radius: 3px;
          }

          &.active {
            color: #acc0df;
            border: 1px solid rgba(0, 198, 255, 0.7);
            box-shadow: inset 0 0 15px rgba(0, 79, 127, 0.6),
              0 0 10px rgba(0, 198, 255, 0.5);
            background-color: rgba(0, 67, 123, 0.7);
            text-shadow: 0 0 8px rgba(0, 198, 255, 0.5);

            &::before {
              background: linear-gradient(
                135deg,
                rgba(0, 198, 255, 0.3) 0%,
                rgba(0, 118, 170, 0.15) 50%,
                rgba(0, 79, 127, 0.05) 100%
              );
            }

            &::after {
              content: "";
              position: absolute;
              top: 0;
              left: 0;
              width: 100%;
              height: 2px;
              background: linear-gradient(
                90deg,
                transparent 0%,
                rgba(0, 198, 255, 0.8) 50%,
                transparent 100%
              );
            }
          }

          &:hover:not(.active) {
            border-color: rgba(0, 198, 255, 0.5);
            box-shadow: inset 0 0 12px rgba(0, 79, 127, 0.4),
              0 0 8px rgba(0, 198, 255, 0.3);
            background-color: rgba(32, 64, 113, 0.6);
            color: #d8e8ff;
          }
        }
      }

      .service-area-selector {
        position: absolute;
        right: 56px;
        top: 50%;
        transform: translateY(-50%);
        font-size: 32px;
        font-weight: normal;
        color: #ffffff;
        display: inline-block;
        text-shadow: none;
        z-index: 99;

        .selected-area {
          display: flex;
          align-items: center;
          justify-content: space-between;
          background: #304b75;
          border: 1px solid #718cb6;
          border-radius: 4px;
          padding: 0 20px;
          height: 64px;
          cursor: pointer;
          width: 240px;
          color: #ffffff;
          position: relative;

          .arrow-down {
            font-size: 18px;
            color: #ffffff;
            margin-left: 4px;
          }
        }

        .dropdown-menu {
          position: absolute;
          top: 66px;
          right: 0;
          background: #003366;
          border: 1px solid rgba(0, 198, 255, 0.5);
          border-radius: 0 0 4px 4px;
          width: 240px;
          z-index: 100;
          max-height: 200px;
          overflow-y: auto;
          padding-top: 8px;
          transform-origin: top right;

          /* 自定义滚动条样式 */
          &::-webkit-scrollbar {
            width: 6px;
          }

          &::-webkit-scrollbar-track {
            background: rgba(0, 27, 61, 0.3);
            border-radius: 3px;
          }

          &::-webkit-scrollbar-thumb {
            background: rgba(0, 198, 255, 0.5);
            border-radius: 3px;
          }

          &::-webkit-scrollbar-thumb:hover {
            background: rgba(0, 198, 255, 0.7);
          }

          &::before {
            content: "";
            position: absolute;
            top: 0;
            left: 10px;
            right: 10px;
            height: 1px;
            border-top: 1px dashed rgba(0, 198, 255, 0.5);
          }

          .dropdown-item {
            cursor: pointer;
            transition: background 0.2s;
            text-align: center;

            &:hover,
            &.active {
              background: #304b75;
            }

            &.active {
              color: #00c6ff;
            }
          }
        }
      }
    }
    .item-content {
      width: 2208px;
      height: 1428px;
      margin-left: 396px;
      margin-top: 32px;
      background-image: url("../../assets/img/leftoright/rbcontent.png");
    }
  }
}
</style>
