<template>
  <div class="screen-container">
    <!-- Cesium地图背景 -->
    <div id="cesiumContainer" class="cesium-background"></div>

    <!-- 新增：类型选择下拉框 -->
    <div class="type-selector">
      <label for="entityType">显示类型：</label>
      <select
        id="entityType"
        v-model="selectedEntityType"
        @change="onEntityTypeChange"
      >
        <option value="person">人员</option>
        <option value="vehicle">车辆</option>
      </select>
    </div>

    <!-- 顶部标题栏 -->
    <header class="top-title-bar" style="display: none">
      <h1 class="system-title">数据监测分析系统</h1>
      <div class="header-info">
        <div class="current-time">{{ currentTime }}</div>
        <div class="system-status">
          <span class="status-dot online"></span>
          <span>系统正常运行中</span>
        </div>
      </div>
    </header>

    <!-- 菜单栏 -->
    <!-- <nav class="menu-bar" style="display: none1">
      <ul class="menu-list">
        <li class="menu-item active">
          <span class="menu-icon home-icon"></span>
          <span>首页概览</span>
        </li>
        <li class="menu-item">
          <span class="menu-icon monitor-icon"></span>
          <span>实时监测</span>
        </li>
        <li class="menu-item">
          <span class="menu-icon alarm-icon"></span>
          <span>报警管理</span>
        </li>
        <li class="menu-item">
          <span class="menu-icon analysis-icon"></span>
          <span>数据分析</span>
        </li>
        <li class="menu-item">
          <span class="menu-icon system-icon"></span>
          <span>系统设置</span>
        </li>
      </ul>
    </nav> -->

    <!-- 主内容区 -->
    <main class="main-content" style="display: none1">
      <!-- 左侧数据模块 -->
      <div class="left-modules">
        <!-- 模块1：数据总览 -->
        <DataOverview @refresh-data="handleRefreshData" />

        <!-- 模块2：实时报警 -->
        <RealTimeAlarm />

        <!-- 模块3：环境监测 -->
        <EnvironmentalMonitoring />
      </div>

      <!-- 右侧数据模块 -->
      <div class="right-modules">
        <!-- 模块4：趋势分析 -->
        <TrendAnalysis @chart-ready="handleChartReady" />

        <!-- 模块5：设备状态 -->
        <DeviceStatus @view-more="handleViewMoreDevices" />

        <!-- 模块6：系统负载 -->
        <SystemLoad :current-time="currentTimeShort" />
      </div>
    </main>
  </div>
</template>

<script>
// 引入所有组件
import DataOverview from "@/components/DashboardModules/DataOverview.vue";
import RealTimeAlarm from "@/components/DashboardModules/RealTimeAlarm.vue";
import EnvironmentalMonitoring from "@/components/DashboardModules/EnvironmentalMonitoring.vue";
import TrendAnalysis from "@/components/DashboardModules/TrendAnalysis.vue";
import DeviceStatus from "@/components/DashboardModules/DeviceStatus.vue";
import SystemLoad from "@/components/DashboardModules/SystemLoad.vue";
export default {
  // 注册组件
  components: {
    DataOverview,
    RealTimeAlarm,
    EnvironmentalMonitoring,
    TrendAnalysis,
    DeviceStatus,
    SystemLoad,
  },
  data() {
    return {
      currentTime: "",
      currentTimeShort: "",
      // 版本号
      version: "3.8.9",
      // Cesium查看器实例
      viewer: null,
      tileset: null,
      // 新增：实体类型选择
      selectedEntityType: "person", // 默认选择人员
      personnelList: [
        {
          longitude: 121.70710830038986,
          latitude: 36.853636402927986,
          height: 100,
          name: "张三",
          status: "online", // 在线
          avatar: "/static/images/avatars/zhangsan.png", // 头像
        },
      ],
      // 新增：车辆数据
      vehicleList: [],
      intervaler: null,
      bgEntities: {}, // 所有的key都对应一个实体对象。而人员的实体对应的key不是数字。
    };
  },
  mounted() {
    // 初始化Cesium地图
    this.initCesium();

    // 监听窗口大小变化
    window.addEventListener("resize", this.handleResize);
  },
  // 在beforeDestroy方法中添加清理代码
  beforeDestroy() {
    // 清理事件监听
    window.removeEventListener("resize", this.handleResize);

    // 清理地图资源
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
    // 清理点击事件处理器
    if (this.clickHandler) {
      this.clickHandler.destroy();
      this.clickHandler = null;
    }
    // 组件销毁前清理地图资源
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
    // 清理定时器
    if (this.intervaler) {
      clearInterval(this.intervaler);
    }
    if (this.trackingInterval) {
      clearInterval(this.trackingInterval);
    }

    // 清空实体引用
    this.bgEntities = {};
    this.trailEntities = {};
  },
  methods: {
    // 新增：实体类型选择变化处理
    onEntityTypeChange() {
      console.log("选择的实体类型:", this.selectedEntityType);
      // 清除现有的实体
      this.clearEntities();
      // 根据选择的类型重新创建实体
      if (this.selectedEntityType === "person") {
        this.createPersonModel();
      } else {
        this.createVehicleModel();
      }
    },

    // 新增：清除实体
    clearEntities() {
      if (this.viewer && this.bgEntities) {
        for (let key in this.bgEntities) {
          this.viewer.entities.remove(this.bgEntities[key]);
          delete this.bgEntities[key];
        }
      }
    },

    /**
     * 初始化Cesium地图
     */
    async initCesium() {
      Cesium.Ion.defaultAccessToken =
        "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI0ZDAxZGFhYy02MjJlLTRiNzktODNhZi00N2VjZGY0NTk4YmIiLCJpZCI6Mjc0NDAxLCJpYXQiOjE3NjMzNDYwNTR9.ZQW2DZ4KaMGbHuwrtIbyI6EdSSvgMJUHmmD74eZW7PQ";
      try {
        // 创建Cesium Viewer实例
        this.viewer = new Cesium.Viewer("cesiumContainer", {
          // 设置地图提供者，这里使用默认的Bing Maps
          imageryProvider: new Cesium.BingMapsImageryProvider({
            url: "https://dev.virtualearth.net",
            key: "AgcbDCAOb9zMfquaT4Z-MdHfx--9wUNrLRiiS7rIElFx8f-4lLulxZ0QnhqX5Lm6",
            mapStyle: Cesium.BingMapsStyle.AERIAL,
          }),
          // 配置是否显示各种控件
          animation: false, // 动画控件
          baseLayerPicker: true, // 底图选择器
          fullscreenButton: false, // 全屏按钮
          geocoder: false, // 地址搜索
          homeButton: false, // 主页按钮
          infoBox: false, // 信息框
          sceneModePicker: false, // 场景模式选择器
          selectionIndicator: false, // 选择指示器
          timeline: false, // 时间线
          navigationHelpButton: false, // 导航帮助按钮
          navigationInstructionsInitiallyVisible: false,
          // 设置初始视图位置（中国区域）
          center: Cesium.Cartesian3.fromDegrees(104.06, 30.67, 10000000),
        });

        this.viewer.scene.camera.setView({
          // 视角-环翠
          duration: 1,
          destination: {
            x: -2739843.563038797,
            y: 4357442.794747324,
            z: 3880768.3292693933,
          },
          orientation: {
            heading: 6.037000745578596,
            pitch: -1.2499586064720978,
            roll: 0.000005306352659495417,
          },
        });

        // 隐藏Cesium logo
        this.viewer._cesiumWidget._creditContainer.style.display = "none";

        const VUE_APP_GIS =
          "http://192.168.2.11:8080/Apps/assets/media/gaoquyingji";
        try {
          const tileset = await Cesium.Cesium3DTileset.fromUrl(
            VUE_APP_GIS + "/tiles/01_guanwei/tileset.json"
          );
          this.viewer.scene.primitives.add(tileset);

          console.log("倾斜摄影模型加载成功tileset", tileset);

          if (tileset) {
            this.tileset = tileset;
            this.locateToTileset();
            // 添加鼠标点击事件监听器，用于控制人员信息label显示
            this.addEntityClickHandler();
          }
        } catch (error) {
          console.error(`加载倾斜摄影模型失败: ${error}`);
        }

        console.log("Cesium地图初始化成功");
      } catch (error) {
        console.error("Cesium地图初始化失败:", error);
      }
    },

    /**
     * 获取倾斜摄影模型的经纬度并将摄像机视角转向模型上方
     */
    locateToTileset() {
      if (!this.tileset || !this.viewer) {
        console.error("模型或视图未准备就绪");
        return;
      }

      try {
        // 获取模型的边界球
        const boundingSphere = this.tileset.boundingSphere;

        if (!boundingSphere) {
          console.error("无法获取模型边界");
          return;
        }

        // 获取模型中心点的笛卡尔坐标
        const center = boundingSphere.center;

        // 将笛卡尔坐标转换为经纬度坐标
        const cartographic = Cesium.Cartographic.fromCartesian(center);
        const longitude = Cesium.Math.toDegrees(cartographic.longitude);
        const latitude = Cesium.Math.toDegrees(cartographic.latitude);
        const height = cartographic.height;

        console.log("倾斜摄影模型中心点经纬度:", {
          longitude: longitude,
          latitude: latitude,
          height: height,
        });

        // 计算合适的观察距离 - 基于模型半径的倍数
        const distance = boundingSphere.radius * 2.5; // 可以根据需要调整倍数

        // 设置相机位置在模型上方
        const cameraPosition = Cesium.Cartesian3.fromRadians(
          cartographic.longitude,
          cartographic.latitude,
          height + distance // 相机高度为模型最高点加上观察距离
        );

        // 计算相机看向模型中心的方向
        const heading = Cesium.Math.toRadians(0); // 方向角
        const pitch = Cesium.Math.toRadians(-90); // 俯仰角 - 负数表示向下看
        const roll = Cesium.Math.toRadians(0); // 翻滚角

        // 使用flyTo方法平滑过渡到目标位置
        this.viewer.camera.flyTo({
          destination: cameraPosition,
          orientation: {
            heading: heading,
            pitch: pitch,
            roll: roll,
          },
          duration: 2, // 过渡时间2秒
          complete: () => {
            console.log("相机已成功定位到模型上方");
            // this.createPersonModel(); // 定位后创建人员模型
            // 根据当前选择的类型创建相应的模型
            if (this.selectedEntityType === "person") {
              this.createPersonModel(); // 定位后创建人员模型
            } else {
              this.createVehicleModel(); // 定位后创建车辆模型
            }
          },
        });
      } catch (error) {
        console.error("定位到模型上方失败:", error);
      }
    },

    personCardList(fn) {
      // 将笛卡尔坐标转换为经纬度坐标
      const cartographic = Cesium.Cartographic.fromCartesian({
        x: -2686273.730145489,
        y: 4293961.185794622,
        z: 3863430.5618300107,
      });
      const baseLongitude = Cesium.Math.toDegrees(cartographic.longitude);
      const baseLatitude = Cesium.Math.toDegrees(cartographic.latitude);
      const baseHeight = cartographic.height + 1;

      console.log("人员经纬度基准点:", {
        longitude: baseLongitude,
        latitude: baseLatitude,
        height: baseHeight,
      });

      // 清除现有的人员列表
      this.personnelList = [];

      // 生成10个不同的经纬度坐标
      const maxOffset = 0.00005;

      for (let index = 0; index < 10; index++) {
        // 生成随机偏移量
        const lngOffset = (Math.random() - 0.5) * 2 * maxOffset;
        const latOffset = (Math.random() - 0.5) * 2 * maxOffset;

        this.personnelList.push({
          lng: baseLongitude + lngOffset,
          lat: baseLatitude + latOffset,
          height: baseHeight,
          perName: "张三" + index,
          status: "online", // 在线
          avatar: "/static/images/avatars/zhangsan.png", // 头像
        });
      }

      fn(this.personnelList);
    },

    // 新增：生成车辆数据
    generateVehicleData() {
      // 将笛卡尔坐标转换为经纬度坐标
      const cartographic = Cesium.Cartographic.fromCartesian({
        x: -2686273.730145489,
        y: 4293961.185794622,
        z: 3863430.5618300107,
      });
      const baseLongitude = Cesium.Math.toDegrees(cartographic.longitude);
      const baseLatitude = Cesium.Math.toDegrees(cartographic.latitude);
      const baseHeight = cartographic.height + 1;

      // 清除现有的车辆列表
      this.vehicleList = [];

      // 生成5辆不同的车辆数据
      const maxOffset = 0.00008; // 车辆分布范围稍大一些

      for (let index = 0; index < 5; index++) {
        // 生成随机偏移量
        const lngOffset = (Math.random() - 0.5) * 2 * maxOffset;
        const latOffset = (Math.random() - 0.5) * 2 * maxOffset;

        this.vehicleList.push({
          lng: baseLongitude + lngOffset,
          lat: baseLatitude + latOffset,
          height: baseHeight,
          vehicleName: "车辆" + (index + 1),
          status: Math.random() > 0.2 ? "online" : "offline", // 80%的概率在线
          vehicleType:
            index % 3 === 0 ? "卡车" : index % 3 === 1 ? "挖掘机" : "装载机",
        });
      }
    },

    // 修改：更新点击事件处理器，支持车辆显示
    addEntityClickHandler() {
      // 创建鼠标事件处理器
      this.clickHandler = new Cesium.ScreenSpaceEventHandler(
        this.viewer.canvas
      );

      // 监听鼠标左键点击事件
      this.clickHandler.setInputAction((click) => {
        // 检测点击位置的实体
        const pickedObject = this.viewer.scene.pick(click.position);

        // 修复条件判断：检查是否有info属性
        if (
          Cesium.defined(pickedObject) &&
          pickedObject.id &&
          pickedObject.id.info
        ) {
          // 获取点击的实体
          const entity = pickedObject.id;
          const entityInfo = entity.info;

          // 如果已有详细信息label，则移除它
          if (entity.detailLabel) {
            this.viewer.entities.remove(entity.detailLabel);
            delete entity.detailLabel;
            return;
          }

          console.log("点击实体了", entityInfo);

          // 根据实体类型显示不同的信息
          let labelText = "";
          if (entity.type === "vehicle") {
            // 车辆信息
            labelText = `${entityInfo.vehicleName}\n类型: ${
              entityInfo.vehicleType
            }\n状态: ${
              entityInfo.status === "online" ? "在线" : "离线"
            }\n高度: ${entityInfo.height.toFixed(2)}m`;
          } else {
            // 人员信息
            labelText = `${entityInfo.perName}\n状态: ${
              entityInfo.status === "online" ? "在线" : "离线"
            }\n高度: ${entityInfo.height.toFixed(2)}m`;
          }

          // 创建详细信息label
          const detailLabel = this.viewer.entities.add({
            position: entity.position.getValue(),
            label: {
              text: labelText,
              font: "14px 微软雅黑",
              backgroundColor: Cesium.Color.fromCssColorString("#173349"),
              showBackground: true,
              fillColor: Cesium.Color.WHITE,
              pixelOffset: new Cesium.Cartesian2(0, -60),
              eyeOffset: new Cesium.Cartesian3(0, 0, -10),
              horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
              verticalOrigin: Cesium.VerticalOrigin.TOP,
              scaleByDistance: new Cesium.NearFarScalar(1000, 1, 500000, 0.5),
              disableDepthTestDistance: Number.POSITIVE_INFINITY, // 确保始终显示在最前面
            },
          });

          // 将详细信息label关联到实体，便于后续管理
          entity.detailLabel = detailLabel;
        }
      }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
    },

    // 处理窗口大小变化
    handleResize() {
      // 重新调整Cesium视图大小
      if (this.viewer) {
        this.viewer.resize();
      }

      // 重新绘制图表
      const chartContainer = document.getElementById("trendChart");
      if (chartContainer && chartContainer.firstChild) {
        chartContainer.removeChild(chartContainer.firstChild);
        this.drawTrendChart();
      }
    },
    // 1. 提取通用实体创建函数
    createEntity(item, type) {
      const isPerson = type === "person";
      const idField = isPerson ? "perName" : "vehicleName";
      const labelColor = isPerson ? Cesium.Color.YELLOW : Cesium.Color.CYAN;
      const iconPath = isPerson
        ? "/poi-marker-default.png"
        : "/poi-marker-vehicle.png";

      let lng = Number(item.lng);
      let lat = Number(item.lat);
      let height = Number(item.height);
      let position = Cesium.Cartesian3.fromDegrees(lng, lat, height);

      console.log(item[idField], position);

      // 创建实体
      let entity = this.viewer.entities.add({
        position: position,
        label: {
          text: item[idField],
          font: "16px",
          backgroundColor: Cesium.Color.fromCssColorString("#173349"),
          showBackground: true,
          fillColor: labelColor,
          depthTestAgainstTerrain: false,
          pixelOffset: new Cesium.Cartesian2(0, -35),
        },
        billboard: {
          image: iconPath,
          scale: 0.5,
          heightReference: Cesium.HeightReference.CLAMP_TO_3D_TILE,
        },
        description: `<div><h4>${item[idField]}${item.status}</h4></div>`,
        fixedFrame: Cesium.Transforms.eastNorthUpToFixedFrame(position),
      });

      entity.info = item;
      entity.type = type;
      this.bgEntities[item[idField]] = entity;

      return entity;
    },

    // 2. 提取实体更新函数
    updateEntity(entity, item) {
      let position = Cesium.Cartesian3.fromDegrees(
        item.lng,
        item.lat,
        item.height
      );
      entity.position.setValue(position);
      entity.fixedFrame = Cesium.Transforms.eastNorthUpToFixedFrame(position);
    },

    // 3. 提取批量创建实体的函数
    createEntities(entityList, type) {
      const isPerson = type === "person";
      const idField = isPerson ? "perName" : "vehicleName";

      for (let item of entityList) {
        this.createEntity(item, type);
      }
    },

    // 4. 提取批量更新实体的函数（增量更新模式）
    updateEntities(entityList, type) {
      const isPerson = type === "person";
      const idField = isPerson ? "perName" : "vehicleName";

      for (let item of entityList) {
        let entity = this.bgEntities[item[idField]];
        if (entity && entity.type === type) {
          // 更新现有实体
          this.updateEntity(entity, item);
        } else {
          // 创建新实体
          this.createEntity(item, type);
        }
      }
    },

    // 5. 提取定时器逻辑为可重用函数
    setupEntityUpdateInterval(type, dataGenerator, interval = 10000) {
      // 清除现有定时器
      if (this.intervaler) {
        clearInterval(this.intervaler);
        this.intervaler = null;
      }

      // 设置新的定时器
      this.intervaler = setInterval(() => {
        console.log(`开始获取实时${type === "person" ? "人员" : "车辆"}数据`);

        // 生成或获取最新数据
        if (typeof dataGenerator === "function") {
          dataGenerator();
        }

        const entityList =
          type === "person" ? this.personnelList : this.vehicleList;

        // 更新实体（这里可以选择清除重建或增量更新）
        // 方案1：清除所有实体后重建
        this.clearEntities();
        this.createEntities(entityList, type);

        // 方案2：增量更新（推荐，性能更好）
        // this.updateEntities(entityList, type);
      }, interval);
    },

    // 修改后的createPersonModel方法
    createPersonModel() {
      // 清除现有定时器
      if (this.intervaler) {
        clearInterval(this.intervaler);
        this.intervaler = null;
      }

      // 清除现有实体
      this.clearEntities();

      // 立即生成并显示人员实体
      console.log("立即创建人员实体");
      this.personCardList((list) => {
        console.log("人员数据", this.personnelList);
        this.createEntities(this.personnelList, "person");
      });

      // 设置定时刷新
      this.setupEntityUpdateInterval("person", () => {
        this.personCardList(() => {
          console.log("人员数据", this.personnelList);
        });
      });
    },

    // 修改后的createVehicleModel方法
    createVehicleModel() {
      // 清除现有定时器
      if (this.intervaler) {
        clearInterval(this.intervaler);
        this.intervaler = null;
      }

      // 清除现有实体
      this.clearEntities();

      // 立即生成并显示车辆实体
      console.log("立即创建车辆实体");
      this.generateVehicleData();
      this.createEntities(this.vehicleList, "vehicle");

      // 设置定时刷新
      this.setupEntityUpdateInterval(
        "vehicle",
        this.generateVehicleData.bind(this)
      );
    },
  },
};
</script>

<style lang="scss" scope>
// 基础样式 - 1rem = 100px
html {
  font-size: 100px;
}

// 屏幕容器
.screen-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background-color: #000;
  color: #fff;
  font-family: "Microsoft YaHei", Arial, sans-serif;
  overflow: hidden;
}

// Cesium地图背景
.cesium-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

// 顶部标题栏
.top-title-bar {
  position: relative;
  height: 0.6rem;
  background: linear-gradient(to right, #003366, #0066cc);
  border-bottom: 2px solid #00aaff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0.4rem;
  z-index: 100;
  box-shadow: 0 2px 10px rgba(0, 170, 255, 0.3);

  .system-title {
    font-size: 0.22rem;
    font-weight: bold;
    color: #ffffff;
    text-shadow: 0 0 10px rgba(0, 170, 255, 0.5);
    margin: 0;
  }

  .header-info {
    display: flex;
    align-items: center;
    gap: 0.3rem;
  }

  .current-time {
    font-size: 0.14rem;
    color: #00ffcc;
    font-weight: 500;
  }

  .system-status {
    display: flex;
    align-items: center;
    gap: 0.08rem;
    font-size: 0.14rem;
  }

  .status-dot {
    width: 0.08rem;
    height: 0.08rem;
    border-radius: 50%;

    &.online {
      background-color: #00ff00;
      box-shadow: 0 0 0.05rem #00ff00;
    }

    &.offline {
      background-color: #ff3333;
      box-shadow: 0 0 0.05rem #ff3333;
    }
  }
}

// 菜单栏
.menu-bar {
  position: relative;
  height: 0.4rem;
  background: rgba(0, 30, 60, 0.9);
  border-bottom: 1px solid #005080;
  z-index: 99;

  .menu-list {
    display: flex;
    align-items: center;
    height: 100%;
    margin: 0;
    padding: 0 0.2rem;
    list-style: none;
  }

  .menu-item {
    display: flex;
    align-items: center;
    gap: 0.08rem;
    padding: 0 0.2rem;
    height: 0.3rem;
    margin-right: 0.2rem;
    border-radius: 0.04rem;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 0.14rem;

    &:hover {
      background: rgba(0, 170, 255, 0.2);
    }

    &.active {
      background: linear-gradient(to right, #005080, #0078b0);
      box-shadow: 0 0 0.1rem rgba(0, 170, 255, 0.5);
    }
  }

  .menu-icon {
    width: 0.16rem;
    height: 0.16rem;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
  }

  .home-icon {
    background-image: url("/static/images/home-icon.png");
  }

  .monitor-icon {
    background-image: url("/static/images/monitor-icon.png");
  }

  .alarm-icon {
    background-image: url("/static/images/alarm-icon.png");
  }

  .analysis-icon {
    background-image: url("/static/images/analysis-icon.png");
  }

  .system-icon {
    background-image: url("/static/images/system-icon.png");
  }
}

// 主内容区
.main-content {
  position: relative;
  display: flex;
  justify-content: space-between;
  padding: 0.2rem;
  height: calc(100vh - 1rem);
  z-index: 2;
}

// 左右模块容器
.left-modules,
.right-modules {
  width: 3.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  position: absolute;
  z-index: 5;
  top: 1rem;
}
.left-modules {
  left: 0;
}

.right-modules {
  right: 0;
}

// 数据模块通用样式
.data-module {
  background: rgba(0, 20, 40, 0.85);
  border: 1px solid #005080;
  border-radius: 0.04rem;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(0.05rem);
  box-shadow: 0 0 0.15rem rgba(0, 100, 200, 0.3);

  &:nth-child(1) {
    height: 2.2rem;
  }

  &:nth-child(2) {
    height: 2.5rem;
  }

  &:nth-child(3) {
    height: 2rem;
  }
}

// 模块头部
.module-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.12rem 0.16rem;
  background: linear-gradient(to right, #003060, #005090);
  border-bottom: 1px solid #0070b0;

  h3 {
    margin: 0;
    font-size: 0.16rem;
    font-weight: 500;
    color: #00ffcc;
  }

  .refresh-icon,
  .view-more {
    font-size: 0.12rem;
    color: #99ccff;
    cursor: pointer;
  }

  .alarm-count {
    background-color: #ff3333;
    color: #fff;
    padding: 0.02rem 0.08rem;
    border-radius: 0.1rem;
    font-size: 0.12rem;
    font-weight: bold;
  }

  .time-range,
  .chart-type,
  .system-time {
    font-size: 0.12rem;
    color: #66ccff;
  }
}

// 模块内容区
.module-body {
  flex: 1;
  padding: 0.16rem;
  overflow: hidden;
}

// 数据总览样式
.data-stats {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.16rem;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0.8rem;
}

.stat-label {
  font-size: 0.12rem;
  color: #99ccff;
  margin-bottom: 0.05rem;
}

.stat-value {
  font-size: 0.2rem;
  font-weight: bold;
  color: #ffffff;

  &.online {
    color: #00ff00;
  }
}

.progress-bars {
  margin-top: 0.1rem;
}

.progress-item {
  display: flex;
  align-items: center;
  gap: 0.1rem;
  margin-bottom: 0.08rem;
}

.progress-label {
  font-size: 0.12rem;
  color: #ffffff;
  width: 0.8rem;
}

.progress-container {
  flex: 1;
  height: 0.06rem;
  background: rgba(0, 40, 80, 0.6);
  border: 1px solid #005080;
  border-radius: 0.03rem;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(to right, #00aaff, #00ffcc);
  box-shadow: 0 0 0.05rem rgba(0, 255, 204, 0.7);
}

.progress-value {
  font-size: 0.12rem;
  color: #00ffcc;
  font-weight: bold;
  width: 0.4rem;
  text-align: right;
}

// 报警列表样式
.alarm-list {
  height: 100%;
  overflow-y: auto;

  &::-webkit-scrollbar {
    width: 0.04rem;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 40, 80, 0.5);
  }

  &::-webkit-scrollbar-thumb {
    background: #0070b0;
    border-radius: 0.02rem;
  }
}

.alarm-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.1rem;
  margin-bottom: 0.08rem;
  border-radius: 0.04rem;
  border-left: 3px solid;
  background: rgba(0, 30, 60, 0.7);
  font-size: 0.12rem;

  &.level-1 {
    border-color: #ff3333;
    background: rgba(255, 51, 51, 0.1);
  }

  &.level-2 {
    border-color: #ff9900;
    background: rgba(255, 153, 0, 0.1);
  }

  &.level-3 {
    border-color: #ffff00;
    background: rgba(255, 255, 0, 0.1);
  }
}

.alarm-level {
  font-weight: bold;
  width: 0.35rem;

  .alarm-item.level-1 & {
    color: #ff3333;
  }

  .alarm-item.level-2 & {
    color: #ff9900;
  }

  .alarm-item.level-3 & {
    color: #ffff00;
  }
}

.alarm-content {
  flex: 1;
  color: #ffffff;
}

.alarm-point {
  color: #99ccff;
  width: 0.5rem;
}

.alarm-time {
  color: #66ccff;
  font-size: 0.11rem;
}

// 环境数据样式
.env-data-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.15rem;
  height: 100%;
}

.env-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 30, 60, 0.7);
  border-radius: 0.04rem;
  border: 1px solid #005080;
  padding: 0.1rem;
}

.env-icon {
  width: 0.3rem;
  height: 0.3rem;
  margin-bottom: 0.08rem;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.env-icon.temp {
  background-image: url("/static/images/temp-icon.png");
}

.env-icon.humidity {
  background-image: url("/static/images/humidity-icon.png");
}

.env-icon.pressure {
  background-image: url("/static/images/pressure-icon.png");
}

.env-icon.wind {
  background-image: url("/static/images/wind-icon.png");
}

.env-label {
  font-size: 0.12rem;
  color: #99ccff;
  margin-bottom: 0.05rem;
}

.env-value {
  font-size: 0.16rem;
  font-weight: bold;
  color: #00ffcc;
}

// 图表容器
.chart-container {
  width: 100%;
  height: 100%;
  min-height: 1.5rem;
}

// 设备状态列表
.device-status-list {
  height: 100%;
  overflow-y: auto;

  &::-webkit-scrollbar {
    width: 0.04rem;
  }

  &::-webkit-scrollbar-track {
    background: rgba(0, 40, 80, 0.5);
  }

  &::-webkit-scrollbar-thumb {
    background: #0070b0;
    border-radius: 0.02rem;
  }
}

.device-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.1rem 0;
  border-bottom: 1px solid rgba(0, 100, 200, 0.2);
  font-size: 0.12rem;

  &:last-child {
    border-bottom: none;
  }
}

.device-name {
  color: #ffffff;
  width: 0.8rem;
}

.device-status {
  padding: 0.02rem 0.08rem;
  border-radius: 0.03rem;
  font-size: 0.11rem;
  font-weight: bold;

  &.normal {
    background: rgba(0, 255, 0, 0.2);
    color: #00ff00;
  }

  &.warning {
    background: rgba(255, 153, 0, 0.2);
    color: #ff9900;
  }

  &.offline {
    background: rgba(255, 51, 51, 0.2);
    color: #ff3333;
  }
}

.device-value {
  color: #00ffcc;
  font-weight: bold;
  width: 0.6rem;
  text-align: right;
}

.device-time {
  color: #66ccff;
  font-size: 0.11rem;
}

// 系统资源监控
.system-resources {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}

.resource-item {
  display: flex;
  align-items: center;
  gap: 0.1rem;
}

.resource-label {
  font-size: 0.12rem;
  color: #ffffff;
  width: 0.9rem;
}

.resource-bar-container {
  flex: 1;
  height: 0.08rem;
  background: rgba(0, 40, 80, 0.6);
  border: 1px solid #005080;
  border-radius: 0.04rem;
  overflow: hidden;
}

.resource-bar {
  height: 100%;
  box-shadow: 0 0 0.05rem;

  &.cpu-bar {
    background: linear-gradient(to right, #00aaff, #00ffcc);
    box-shadow: 0 0 0.05rem rgba(0, 255, 204, 0.7);
  }

  &.mem-bar {
    background: linear-gradient(to right, #ff9900, #ffcc00);
    box-shadow: 0 0 0.05rem rgba(255, 204, 0, 0.7);
  }

  &.disk-bar {
    background: linear-gradient(to right, #00cc66, #00ff99);
    box-shadow: 0 0 0.05rem rgba(0, 255, 153, 0.7);
  }
}

.resource-value {
  font-size: 0.12rem;
  font-weight: bold;
  width: 0.4rem;
  text-align: right;

  &.low {
    color: #00ff00;
  }

  &.medium {
    color: #ffcc00;
  }

  &.high {
    color: #ff3333;
  }
}

// 响应式设计 - 基于1920*1080分辨率的适配
@media (max-width: 1600px) {
  html {
    font-size: 83.3px; // 1600/1920 * 100
  }
}

@media (max-width: 1440px) {
  html {
    font-size: 75px; // 1440/1920 * 100
  }

  .left-modules,
  .right-modules {
    width: 3rem;
  }
}

@media (max-width: 1280px) {
  html {
    font-size: 66.7px; // 1280/1920 * 100
  }

  .left-modules,
  .right-modules {
    width: 2.6rem;
  }
}

// 修复滚动条样式
*::-webkit-scrollbar {
  width: 0.04rem;
  height: 0.04rem;
}

*::-webkit-scrollbar-track {
  background: rgba(0, 40, 80, 0.3);
  border-radius: 0.02rem;
}

*::-webkit-scrollbar-thumb {
  background: #0070b0;
  border-radius: 0.02rem;
}

*::-webkit-scrollbar-thumb:hover {
  background: #0090d0;
}

// 新增：类型选择器样式
.type-selector {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 100;
  background: rgba(23, 51, 73, 0.9);
  padding: 10px 15px;
  border-radius: 4px;
  border: 1px solid #005080;
  color: white;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;

  select {
    background: rgba(255, 255, 255, 0.1);
    color: white;
    border: 1px solid #0078b0;
    border-radius: 4px;
    padding: 5px 10px;
    outline: none;

    option {
      background: #173349;
      color: white;
    }

    &:focus {
      border-color: #00aaff;
      box-shadow: 0 0 5px rgba(0, 170, 255, 0.5);
    }
  }
}
</style>