<template>
  <div class="bottom-panel">
    <div class="nav-menu">
      <div
        v-for="(item, index) in menuItems"
        :key="index"
        class="nav-item"
        :class="{ active: activeIndex === index }"
        @click="activeIndex = index"
      >
        <div class="nav-icon-box">
          <!-- <i :class="item.icon"></i> -->
          <img :src="activeIndex === index ? item.icon1 : item.icon" alt="" />
        </div>
        <div class="nav-label">{{ item.label }}</div>
      </div>
    </div>
    <!-- 气象预警监测表格 -->
    <div class="weather-warning-wrapper">
      <div class="weather-warning-panel">
        <img
          class="clear-icon"
          src="../../assets/RiskWarning_img/清除icon@2x.png"
          alt=""
        />
        <div class="panel-header">
          <div class="header-title">气象预警监测</div>
          <div class="filter-tags">
            <label class="tag">
              <input type="checkbox" v-model="filters.red" />
              <span class="">红色预警</span>
            </label>
            <label class="tag">
              <input type="checkbox" v-model="filters.blue" />
              <span class="">蓝色预警</span>
            </label>
            <label class="tag">
              <input type="checkbox" v-model="filters.orange" />
              <span class="">橙色预警</span>
            </label>
            <label class="tag">
              <input type="checkbox" v-model="filters.yellow" />
              <span class="">黄色预警</span>
            </label>
          </div>
        </div>
        <div class="table-container">
          <el-table
            :data="filteredData"
            height="100%"
            style="width: 100%; background: transparent"
            :header-cell-style="headerCellStyle"
            :cell-style="cellStyle"
            :row-class-name="rowClassName"
          >
            <el-table-column prop="time" label="预警时间" min-width="vw(140)" />
            <el-table-column
              prop="type"
              label="类型"
              min-width="vw(80)"
              align="center"
            />
            <el-table-column
              label="预警等级"
              min-width="vw(100)"
              align="center"
            >
              <template #default="{ row }">
                <div class="warning-level">
                  <img
                    :src="
                      row.levelClass === 'red'
                        ? redIcon
                        : row.levelClass === 'blue'
                          ? blueIcon
                          : row.levelClass === 'orange'
                            ? orangeIcon
                            : yellowIcon
                    "
                    alt=""
                  />
                  <span class="ml_10">{{ row.level }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="district"
              label="预警区县"
              min-width="vw(80)"
            />
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, watch, nextTick } from "vue";
import { ElTable } from "element-plus";
import orangeIcon from "../../assets/RiskWarning_img/橙色@2x.png";
import yellowIcon from "../../assets/RiskWarning_img/黄色@2x.png";
import redIcon from "../../assets/RiskWarning_img//红色@2x.png";
import blueIcon from "../../assets/RiskWarning_img/蓝色@2x.png";

import warningIconIcon from "../../assets/RiskWarning_img/风险预警icon@2x.png";
import tunnelIconIcon from "../../assets/RiskWarning_img/隧道icon@2x.png";
import slopeIconIcon from "../../assets/RiskWarning_img/边坡icon@2x.png";
import bridgeIconIcon from "../../assets/RiskWarning_img/桥梁icon@2x.png";
import roadIconIcon from "../../assets/RiskWarning_img/线路路段icon@2x.png";
import teamIconIcon from "../../assets/RiskWarning_img/队伍icon@2x.png";

import warningIconIcon1 from "../../assets/RiskWarning_img/风险预警icon1@2x.png";
import tunnelIconIcon1 from "../../assets/RiskWarning_img/隧道icon1@2x.png";
import slopeIconIcon1 from "../../assets/RiskWarning_img/边坡icon1@2x.png";
import bridgeIconIcon1 from "../../assets/RiskWarning_img/桥梁icon1@2x.png";
import roadIconIcon1 from "../../assets/RiskWarning_img/线路路段icon1@2x.png";
import teamIconIcon1 from "../../assets/RiskWarning_img/队伍icon1@2x.png";

const activeIndex = ref(0);

const menuItems = [
  {
    label: "项目",
    icon: "icon-warning",
    iconClass: "warning",
    icon: warningIconIcon,
    icon1: warningIconIcon1,
  },
  {
    label: "隧道",
    icon: "icon-tunnel",
    iconClass: "tunnel",
    icon: tunnelIconIcon,
    icon1: tunnelIconIcon1,
  },
  {
    label: "边坡",
    icon: "icon-slope",
    iconClass: "slope",
    icon: slopeIconIcon,
    icon1: slopeIconIcon1,
  },
  {
    label: "桥梁",
    icon: "icon-bridge",
    iconClass: "bridge",
    icon: bridgeIconIcon,
    icon1: bridgeIconIcon1,
  },
  {
    label: "路段",
    icon: "icon-road",
    iconClass: "road",
    icon: roadIconIcon,
    icon1: roadIconIcon1,
  },
  {
    label: "队伍",
    icon: "icon-team",
    iconClass: "team",
    icon: teamIconIcon,
    icon1: teamIconIcon1,
  },
];

// 筛选状态
const filters = ref({
  red: false,
  blue: false,
  orange: false,
  yellow: false,
});

// 预警数据
const warningData = ref([
  {
    time: "2025-11-18 09:24:81",
    type: "暴雨",
    level: "蓝色预警",
    levelClass: "blue",
    district: "合川区",
  },
  {
    time: "2025-11-12 09:24:81",
    type: "冰雪",
    level: "红色预警",
    levelClass: "red",
    district: "万州区",
  },
  {
    time: "2025-11-12 09:24:81",
    type: "大雾",
    level: "橙色预警",
    levelClass: "orange",
    district: "涪陵区",
  },
  {
    time: "2025-11-12 09:24:81",
    type: "大风",
    level: "黄色预警",
    levelClass: "yellow",
    district: "城口县",
  },
]);

// 过滤后的数据
const filteredData = computed(() => {
  const hasFilter =
    filters.value.red ||
    filters.value.blue ||
    filters.value.orange ||
    filters.value.yellow;
  if (!hasFilter) return warningData.value;

  return warningData.value.filter((item) => {
    if (filters.value.red && item.levelClass === "red") return true;
    if (filters.value.blue && item.levelClass === "blue") return true;
    if (filters.value.orange && item.levelClass === "orange") return true;
    if (filters.value.yellow && item.levelClass === "yellow") return true;
    return false;
  });
});

// el-table 样式
const headerCellStyle = () => ({
  background: "#17466F",
  color: "rgba(255, 255, 255, 0.6)",
  fontWeight: "normal",
  borderBottom: "1px solid rgba(64, 169, 255, 0.2)",
  padding: "5px 20px",
});

const cellStyle = () => ({
  background: "#142E49",
  color: "rgba(255, 255, 255, 0.9)",
  borderBottom: "1px solid rgba(64, 169, 255, 0.1)",
  padding: "5px 5px",
});

const rowClassName = ({ rowIndex }) => {
  return rowIndex % 2 === 0 ? "even-row" : "odd-row";
};

// 表格自动滚动相关
const tableRef = ref(null);
const isScrolling = ref(true);
const scrollTimer = ref(null);
const scrollSpeed = 50; // 滚动间隔（毫秒）
const scrollStep = 1; // 每次滚动像素

// 检查是否有筛选条件
const hasFilter = computed(() => {
  return (
    filters.value.red ||
    filters.value.blue ||
    filters.value.orange ||
    filters.value.yellow
  );
});

// 开始自动滚动
const startAutoScroll = () => {
  if (scrollTimer.value) {
    clearInterval(scrollTimer.value);
  }
  if (!isScrolling.value || hasFilter.value) return;

  const tableBody = document.querySelector(
    ".weather-warning-panel .el-table__body-wrapper .el-scrollbar__wrap",
  );
  if (!tableBody) return;

  scrollTimer.value = setInterval(() => {
    if (tableBody) {
      tableBody.scrollTop += scrollStep;
      // 滚动到底部后回到顶部（使用 >= 判断，并留出一点余量确保准确触发）
      const maxScroll = tableBody.scrollHeight - tableBody.clientHeight;
      if (tableBody.scrollTop >= maxScroll - 2) {
        tableBody.scrollTop = 0;
      }
    }
  }, scrollSpeed);
};

// 停止自动滚动
const stopAutoScroll = () => {
  if (scrollTimer.value) {
    clearInterval(scrollTimer.value);
    scrollTimer.value = null;
  }
};

// 重置滚动到顶部
const resetScrollToTop = () => {
  const tableBody = document.querySelector(
    ".weather-warning-panel .el-table__body-wrapper .el-scrollbar__wrap",
  );
  if (tableBody) {
    tableBody.scrollTop = 0;
  }
};

// 鼠标移入停止滚动
const handleMouseEnter = () => {
  stopAutoScroll();
};

// 鼠标移出开始滚动
const handleMouseLeave = () => {
  if (!hasFilter.value) {
    isScrolling.value = true;
    startAutoScroll();
  }
};

// 监听筛选条件变化
watch(hasFilter, (newVal) => {
  if (newVal) {
    // 有筛选条件时停止滚动并回到顶部
    stopAutoScroll();
    resetScrollToTop();
  } else {
    // 取消筛选后开始滚动
    isScrolling.value = true;
    nextTick(() => {
      startAutoScroll();
    });
  }
});

onMounted(() => {
  nextTick(() => {
    startAutoScroll();
    // 添加鼠标事件监听
    const tableContainer = document.querySelector(
      ".weather-warning-panel .table-container",
    );
    if (tableContainer) {
      tableContainer.addEventListener("mouseenter", handleMouseEnter);
      tableContainer.addEventListener("mouseleave", handleMouseLeave);
    }
  });
});

onUnmounted(() => {
  stopAutoScroll();
  const tableContainer = document.querySelector(
    ".weather-warning-panel .table-container",
  );
  if (tableContainer) {
    tableContainer.removeEventListener("mouseenter", handleMouseEnter);
    tableContainer.removeEventListener("mouseleave", handleMouseLeave);
  }
});
</script>

<style lang="scss" scoped>
// 视频屏幕自适应 - 基于视口宽度动态调整
// 基准宽度 1920px，使用 vw 单位实现自适应
@function vw($px) {
  @return calc($px / 1920 * 100vw);
}

.bottom-panel {
  position: relative;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  display: flex;

  @media (max-width: 1366px) {
    padding: vw(8);
  }

  @media (max-width: 1024px) {
    padding: vw(6);
  }
}
.nav-menu {
  width: vw(50);
  min-width: vw(40);
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: end;
  align-items: center;
  gap: vw(5);
  z-index: 2;
  position: absolute;
  bottom: 0;
  left: 0;

  .nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: pointer;
    transition: all 0.3s ease;

    &:hover,
    &.active {
      .nav-icon-box {
        background: rgba(64, 169, 255, 0.3);
        border-color: rgba(64, 169, 255, 0.6);
      }

      .nav-label {
        color: #40a9ff;
      }
    }

    .nav-icon-box {
      width: vw(50);
      height: vw(50);
      min-width: vw(36);
      min-height: vw(36);
      margin-bottom: vw(5);
      background: rgba(64, 169, 255, 0.1);
      border: 1px solid rgba(64, 169, 255, 0.3);
      border-radius: vw(8);
      display: flex;
      align-items: center;
      justify-content: center;
      transition: all 0.3s ease;
      img {
        width: vw(50);
        height: vw(50);
        min-width: vw(32);
        min-height: vw(32);
      }

      i {
        font-size: vw(24);
        color: #40a9ff;
      }

      // 使用 emoji 作为图标占位
      &.warning::before {
        content: "📍";
        font-size: vw(24);
      }

      &.tunnel::before {
        content: "🚇";
        font-size: vw(24);
      }

      &.slope::before {
        content: "⛰️";
        font-size: vw(24);
      }

      &.bridge::before {
        content: "🌉";
        font-size: vw(24);
      }

      &.road::before {
        content: "🛣️";
        font-size: vw(24);
      }
    }

    .nav-label {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: vw(14);
      color: #fff;
      transition: all 0.3s ease;
    }
  }
}

// 气象预警监测包装器
.weather-warning-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  width: 92%;
  height: 50%;
  position: absolute;
  z-index: 2;
  right: 0px;
  bottom: 0px;
}

// 气象预警监测面板样式
.weather-warning-panel {
  // border-radius: vw(8);
  display: flex;
  flex-direction: column;
  height: 100%;

  .clear-icon {
    position: absolute;
    top: vw(-50);
    right: 0px;
    width: vw(40) !important;
    height: vw(40) !important;
    min-width: vw(28);
    min-height: vw(28);
  }

  .panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-image: url("../../assets/RiskWarning_img/二级标题栏bg1@2x.png");
    background-size: 100% 100%;
    background-position: left top;
    padding: vw(15);

    .header-title {
      font-size: vw(18);
      font-weight: 500;
      color: #fff;
      position: relative;
    }

    .filter-tags {
      display: flex;
      gap: vw(15);

      .tag {
        display: flex;
        align-items: center;
        gap: vw(5);
        cursor: pointer;

        input {
          width: vw(14);
          height: vw(14);
          min-width: vw(12);
          min-height: vw(12);
          accent-color: #40a9ff;
        }

        span {
          font-size: vw(14);
          color: rgba(255, 255, 255, 0.8);

          &.tag-red {
            color: #ff4d4f;
          }

          &.tag-blue {
            color: #40a9ff;
          }

          &.tag-orange {
            color: #ff7a45;
          }

          &.tag-yellow {
            color: #ffc53d;
          }
        }
      }
    }
  }

  .table-container {
    background: rgb(20, 46, 74, 0.95);
    flex: 1;
    overflow: hidden;

    :deep(.el-table__body-wrapper) {
      scrollbar-width: none;
      -ms-overflow-style: none;

      &::-webkit-scrollbar {
        display: none;
      }
    }
  }

  // el-table 样式
  :deep(.el-table) {
    background: transparent;

    &::before {
      display: none;
    }

    .el-table__inner-wrapper {
      background: transparent;
    }
    .el-table__inner-wrapper:before {
      width: 0%;
    }

    .el-table__header-wrapper {
      background: transparent;

      th.el-table__cell {
        background: transparent;
        font-size: vw(14);
        color: rgba(255, 255, 255, 0.6);
        padding: vw(10) vw(8);
      }
    }

    .el-table__body-wrapper {
      background: transparent;

      td.el-table__cell {
        background: transparent;
        font-size: vw(14);
        color: rgba(255, 255, 255, 0.9);
        padding: vw(5) vw(5);
      }

      &::-webkit-scrollbar {
        display: none !important;
      }
    }

    tr {
      background: transparent;

      &:hover {
        background: rgba(64, 169, 255, 0.05) !important;
      }
    }
  }

  .warning-level {
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: vw(15);
      height: vw(15);
      min-width: vw(10);
      min-height: vw(10);
    }

    // .level-icon {
    //   width: 0;
    //   height: 0;
    //   border-left: 6px solid transparent;
    //   border-right: 6px solid transparent;
    //   border-bottom: 10px solid currentColor;
    // }

    &.red {
      color: #ff4d4f;
      background: rgba(255, 77, 79, 0.1);
    }

    &.blue {
      color: #40a9ff;
      background: rgba(64, 169, 255, 0.1);
    }

    &.orange {
      color: #ff7a45;
      background: rgba(255, 122, 69, 0.1);
    }

    &.yellow {
      color: #ffc53d;
      background: rgba(255, 197, 61, 0.1);
    }
  }
}
</style>
