<template>
  <div class="device-management-system">
    <!-- 查询条件区域 -->
    <div class="search-form">
      <el-form :model="queryParams" ref="searchForm" :inline="true">
        <el-form-item label="设备类型">
          <el-select
              v-model="queryParams.device_type"
              placeholder="请选择设备类型"
              clearable
              style="width: 240px;"
              @change="handleDeviceTypeChange"
              size="large"
          >
            <el-option
                v-for="(label, value) in deviceTypes"
                :key="value"
                :label="`${label}(${value})`"
                :value="value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              :icon="Search"
              @click="handleSearch"
              :loading="isLoading"
              size="large"
          >
            搜索
          </el-button>
          <el-button
              :icon="Refresh"
              @click="resetSearch"
              size="large"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格区域 -->
    <div class="table-container">
      <!-- 表格头部 -->
      <div class="table-header">
        <div class="header-left">
          <el-icon class="header-icon"><Monitor /></el-icon>
          <span class="header-title">设备列表</span>
        </div>
        <div class="header-actions">
          <el-tooltip content="刷新数据" placement="top">
            <el-button
                size="default"
                :icon="Refresh"
                @click="getList"
                :loading="isLoading"
                circle
            />
          </el-tooltip>
          <el-tooltip :content="isCardView ? '切换到表格视图' : '切换到卡片视图'" placement="top">
            <el-button
                type="primary"
                size="default"
                :icon="View"
                @click="toggleTableView"
                circle
            />
          </el-tooltip>
        </div>
      </div>

      <!-- 默认空状态 -->
      <div v-if="!queryParams.device_type" class="empty-state">
        <div class="empty-content">
          <div class="empty-icon">
            <el-icon size="120"><Monitor /></el-icon>
          </div>
          <h3 class="empty-title">欢迎使用设备管理系统</h3>
          <p class="empty-description">请选择设备类型开始查询设备信息</p>
        </div>
      </div>

      <!-- 表格视图 -->
      <div v-else-if="!isCardView" class="table-view">
        <el-table
            :data="deviceList"
            v-loading="isLoading"
            :height="tableHeight"
            style="width: 100%"
            @row-click="handleRowClick"
            empty-text="暂无设备数据"
            border
            :row-class-name="tableRowClassName"
            highlight-current-row
        >
          <el-table-column label="设备名称" width="200" fixed="left">
            <template #default="scope">
              <div class="device-info">
                <div class="device-name">
                  <el-link
                      type="primary"
                      @click.stop="handleRowClick(scope.row)"
                  >
                    {{ scope.row.deviceName }}
                  </el-link>
                </div>
                <div class="device-type">{{ deviceTypes[queryParams.device_type] }}</div>
              </div>
            </template>
          </el-table-column>

          <!-- 动态列 -->
          <el-table-column
              v-for="column in dynamicColumns"
              :key="column.prop"
              :label="column.label"
              :prop="column.prop"
              :width="column.width"
              align="center"
              show-overflow-tooltip
          >
            <template #default="scope">
              <div class="cell-content">
                <div
                    :class="['status-indicator', getCellClass(column.prop, scope.row[column.prop])]"
                    v-if="column.type === 'status'"
                >
                  <el-tag
                      :type="scope.row[column.prop] === '是' ? 'success' : 'info'"
                      size="small"
                  >
                    <el-icon v-if="scope.row[column.prop] === '是'"><CircleCheck /></el-icon>
                    <el-icon v-else><CircleClose /></el-icon>
                    {{ scope.row[column.prop] }}
                  </el-tag>
                </div>
                <div
                    v-else-if="column.type === 'number'"
                    :class="['number-display', getNumberClass(column.prop, scope.row[column.prop])]"
                >
                  <span class="number-value">{{ formatNumber(scope.row[column.prop]) }}</span>
                  <span class="unit" v-if="column.unit">{{ column.unit }}</span>
                </div>
                <span v-else class="text-value">{{ scope.row[column.prop] }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="200" fixed="right" align="center">
            <template #default="scope">
              <el-button
                  type="primary"
                  size="small"
                  @click.stop="handleView(scope.row)"
                  icon="View"
              >
                详情
              </el-button>
              <el-button
                  type="success"
                  size="small"
                  @click.stop="handleViewConfiguration(scope.row)"
                  icon="Setting"
              >
                组态信息
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 卡片视图 -->
      <div v-else class="card-view">
        <div class="cards-grid">
          <div
              v-for="device in deviceList"
              :key="device.deviceName"
              class="device-card-wrapper"
          >
            <el-card class="device-card" @click="handleRowClick(device)">
              <template #header>
                <div class="device-card-header">
                  <div class="device-title">
                    <div class="device-avatar-large">
                      <el-icon class="device-type-icon"><Monitor /></el-icon>
                    </div>
                    <div class="device-info">
                      <h4 class="device-name">{{ device.deviceName }}</h4>
                      <p class="device-type">{{ deviceTypes[queryParams.device_type] }}</p>
                    </div>
                  </div>
                  <el-tag
                      :type="getDeviceStatusType(device)"
                      size="large"
                      effect="dark"
                  >
                    {{ getDeviceStatus(device) }}
                  </el-tag>
                </div>
              </template>

              <div class="device-card-content">
                <div class="parameters-grid">
                  <div
                      v-for="(value, key) in getCardDisplayData(device)"
                      :key="key"
                      class="parameter-item"
                  >
                    <div class="parameter-label">{{ key }}</div>
                    <div class="parameter-value" :class="getCellClass(key, value)">
                      {{ formatCardValue(key, value) }}
                    </div>
                  </div>
                </div>
              </div>

              <template #footer>
                <div class="device-card-actions">
                  <el-button
                      type="primary"
                      size="default"
                      @click.stop="handleView(device)"
                  >
                    <el-icon><View /></el-icon>
                    查看详情
                  </el-button>
                  <el-button
                      type="success"
                      size="default"
                      @click.stop="handleViewConfiguration(device)"
                  >
                    <el-icon><Setting /></el-icon>
                    组态信息
                  </el-button>
                </div>
              </template>
            </el-card>
          </div>
        </div>

        <div v-if="deviceList.length === 0 && !isLoading" class="empty-result">
          <el-empty description="暂无设备数据">
            <template #image>
              <div class="empty-image">
                <el-icon size="80"><Monitor /></el-icon>
              </div>
            </template>
          </el-empty>
        </div>
      </div>

      <!-- 分页组件 -->
      <div v-show="total > 0" class="pagination-container">
        <el-pagination
            v-model:current-page="queryParams.pageNum"
            v-model:page-size="queryParams.pageSize"
            :page-sizes="[10, 20, 50, 100]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handlePageChange"
            background
        />
      </div>
    </div>

    <!-- 设备详情弹窗 -->
    <el-dialog
        v-model="detailDialogOpen"
        title="设备详情"
        width="900px"
        top="5vh"
        destroy-on-close
        class="detail-dialog"
    >
      <div v-if="currentDevice" class="device-detail">
        <!-- 基本信息 -->
        <div class="detail-section">
          <h3>基本信息</h3>
          <el-descriptions :column="3" border>
            <el-descriptions-item label="设备名称" :span="2">
              <el-tag type="primary" size="large" effect="dark">
                {{ currentDevice.deviceName }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="设备类型">
              <span>{{ deviceTypes[queryParams.device_type] || '未知' }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="设备状态" :span="3">
              <el-tag :type="getDeviceStatusType(currentDevice)" size="large" effect="dark">
                <el-icon style="margin-right: 4px;">
                  <CircleCheck v-if="getDeviceStatus(currentDevice) === '运行中'" />
                  <Warning v-else-if="getDeviceStatus(currentDevice) === '故障'" />
                  <Monitor v-else />
                </el-icon>
                {{ getDeviceStatus(currentDevice) }}
              </el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </div>

        <!-- 参数表格 -->
        <div class="detail-section">
          <h3>设备参数</h3>
          <el-table
              :data="getDetailTableData(currentDevice)"
              border
              max-height="450"
          >
            <el-table-column label="参数名称" prop="name" width="200">
              <template #default="scope">
                <div class="param-name">
                  <el-icon class="param-icon"><Setting /></el-icon>
                  <span>{{ scope.row.name }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="当前值" prop="value" align="center">
              <template #default="scope">
                <div class="param-value-cell">
              <span :class="['param-value', getCellClass(scope.row.name, scope.row.value)]">
                {{ formatDetailValue(scope.row.name, scope.row.value) }}
              </span>
                  <span v-if="scope.row.unit" class="param-unit">{{ scope.row.unit }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="状态" prop="status" width="120" align="center">
              <template #default="scope">
                <el-tag
                    :type="scope.row.status === '正常' ? 'success' : 'warning'"
                    size="default"
                    effect="dark"
                >
                  <el-icon style="margin-right: 4px;">
                    <CircleCheck v-if="scope.row.status === '正常'" />
                    <Warning v-else />
                  </el-icon>
                  {{ scope.row.status }}
                </el-tag>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailDialogOpen = false">关闭</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 组态信息弹窗 -->
    <el-dialog
        v-model="configurationDialogOpen"
        :title="`组态信息 - ${currentConfigDevice?.deviceName || ''}`"
        top="16vh"
    >
      <div class="configuration-iframe-container">
        <iframe
            v-if="currentVideoUrl"
            :src="currentVideoUrl"
            class="configuration-iframe"
            frameborder="0"
            allowfullscreen
        ></iframe>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="configurationDialogOpen = false">关闭</el-button>
          <el-button type="primary" @click="refreshConfiguration">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BuildingEquipmentMonitoring">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import {
  Search,
  Refresh,
  Monitor,
  CircleCheck,
  CircleClose,
  View,
  Setting,
  Warning,
  Close
} from '@element-plus/icons-vue';
import axios from 'axios';

// 响应式数据
const deviceTypes = ref({});
const deviceList = ref([]);
const total = ref(0);
const isLoading = ref(false);
const detailDialogOpen = ref(false);
const currentDevice = ref({});
const dynamicColumns = ref([]);
const tableHeight = ref(600);
const isCardView = ref(false);

// 组态信息相关
const configurationDialogOpen = ref(false);
const currentConfigDevice = ref(null);
const currentVideoUrl = ref('');

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  device_type: ''
});

// 生命周期
onMounted(() => {
  loadDeviceTypes();
  calculateTableHeight();
  window.addEventListener('resize', calculateTableHeight);
});

// 计算表格高度
const calculateTableHeight = () => {
  const windowHeight = window.innerHeight;
  tableHeight.value = windowHeight - 400;
};

// 加载设备类型
const loadDeviceTypes = async () => {
  try {
    const response = await axios.get(`${__LOCAL_API__}/intell/deviceType`);
    if (response.data.code === 200) {
      deviceTypes.value = response.data.data;
    }
  } catch (error) {
    ElMessage.error('加载设备类型失败');
    console.error(error);
  }
};

// 获取设备列表
const getList = async () => {
  if (!queryParams.device_type) {
    ElMessage.warning('请先选择设备类型');
    return;
  }

  isLoading.value = true;
  try {
    const params = {
      pageNum: queryParams.pageNum,
      pageSize: queryParams.pageSize,
      device_type: queryParams.device_type
    };

    const response = await axios.get(`${__LOCAL_API__}/intell/getDevices`, { params });

    if (response.data.code === 200) {
      const { data, total: totalCount } = response.data;
      total.value = totalCount;

      // 转换数据格式
      const convertedData = Object.entries(data).map(([deviceName, deviceData]) => ({
        deviceName,
        ...deviceData
      }));

      deviceList.value = convertedData;
      generateDynamicColumns(convertedData);
    }
  } catch (error) {
    ElMessage.error('获取设备数据失败');
  } finally {
    isLoading.value = false;
  }
};

// 获取组态信息 - 简化版本，只设置URL
const getConfigurationInfo = async (device) => {
  // 构建组态信息URL
  currentVideoUrl.value = `${__LOCAL_API__}/intell/configurationDiagram?id=${queryParams.device_type}-${device.deviceName}`;
};

// 刷新组态信息
const refreshConfiguration = () => {
  if (currentConfigDevice.value && currentVideoUrl.value) {
    // 通过重新设置src来刷新iframe
    const iframe = document.querySelector('.configuration-iframe');
    if (iframe) {
      const currentSrc = iframe.src;
      iframe.src = '';
      setTimeout(() => {
        iframe.src = currentSrc;
      }, 100);
    }
  }
};

// 生成动态表格列
const generateDynamicColumns = (data) => {
  if (data.length === 0) {
    dynamicColumns.value = [];
    return;
  }

  const columns = [];
  const firstDevice = data[0];

  Object.keys(firstDevice).forEach(key => {
    if (key === 'deviceName') return;

    const column = {
      prop: key,
      label: key,
      width: getColumnWidth(key),
      type: getColumnType(key),
      unit: getColumnUnit(key)
    };

    columns.push(column);
  });

  dynamicColumns.value = columns;
};

// 获取列宽度
const getColumnWidth = (columnName) => {
  const widthMap = {
    '冬夏季模式': 120,
    '运行状态': 120,
    '自动状态': 120,
    '故障报警': 120,
    '过滤网压差': 120,
    '防冻开关': 120,
    '风机压差': 120,
    '送风温度': 120,
    '回风温度': 120,
    '新风温度': 120,
    '送风湿度': 120,
    '回风二氧化碳': 140,
    '水阀反馈': 120,
    '回风阀反馈': 120,
    '新风阀反馈': 120
  };
  return widthMap[columnName] || 130;
};

// 获取列类型
const getColumnType = (columnName) => {
  const statusColumns = ['冬夏季模式', '运行状态', '自动状态', '故障报警', '过滤网压差', '防冻开关', '风机压差'];
  const numberColumns = ['送风温度', '回风温度', '新风温度', '送风湿度', '回风二氧化碳', '水阀反馈', '回风阀反馈', '新风阀反馈'];

  if (statusColumns.includes(columnName)) return 'status';
  if (numberColumns.includes(columnName)) return 'number';
  return 'text';
};

// 获取列单位
const getColumnUnit = (columnName) => {
  const unitMap = {
    '送风温度': '°C',
    '回风温度': '°C',
    '新风温度': '°C',
    '送风湿度': '%',
    '回风二氧化碳': 'ppm',
    '水阀反馈': '%',
    '回风阀反馈': '%',
    '新风阀反馈': '%'
  };
  return unitMap[columnName] || '';
};

// 获取单元格样式类
const getCellClass = (columnName, value) => {
  const type = getColumnType(columnName);

  if (type === 'status') {
    return value === '是' ? 'status-active' : 'status-inactive';
  }

  if (type === 'number') {
    const numValue = parseFloat(value);
    if (isNaN(numValue)) return 'value-invalid';
    if (numValue === 0) return 'value-zero';
    if (columnName.includes('温度') && (numValue < 18 || numValue > 35)) return 'value-warning';
    if (columnName.includes('湿度') && (numValue < 30 || numValue > 80)) return 'value-warning';
    if (columnName.includes('反馈') && numValue > 95) return 'value-high';
    return 'value-normal';
  }

  return '';
};

// 获取数字样式类
const getNumberClass = (columnName, value) => {
  const numValue = parseFloat(value);
  if (isNaN(numValue)) return 'number-invalid';
  if (numValue === 0) return 'number-zero';
  if (numValue > 90) return 'number-high';
  return 'number-normal';
};

// 格式化数字
const formatNumber = (value) => {
  const num = parseFloat(value);
  if (isNaN(num)) return value;
  return num.toFixed(2);
};

// 获取设备状态
const getDeviceStatus = (device) => {
  if (device.故障报警 === '是') return '故障';
  if (device.运行状态 === '是') return '运行中';
  if (device.自动状态 === '是') return '自动';
  return '待机';
};

// 获取设备状态类型
const getDeviceStatusType = (device) => {
  const status = getDeviceStatus(device);
  const typeMap = {
    '故障': 'danger',
    '运行中': 'success',
    '自动': 'primary',
    '待机': 'info'
  };
  return typeMap[status] || 'info';
};

// 获取卡片显示数据
const getCardDisplayData = (device) => {
  const importantKeys = ['运行状态', '故障报警', '送风温度', '水阀反馈', '自动状态', '过滤网压差'];
  const result = {};

  importantKeys.forEach(key => {
    if (device[key] !== undefined) {
      result[key] = device[key];
    }
  });

  const allKeys = Object.keys(device).filter(key => key !== 'deviceName');
  let count = Object.keys(result).length;

  for (const key of allKeys) {
    if (count >= 6) break;
    if (!result[key]) {
      result[key] = device[key];
      count++;
    }
  }

  return result;
};

// 格式化卡片值
const formatCardValue = (key, value) => {
  const type = getColumnType(key);
  if (type === 'number') {
    const unit = getColumnUnit(key);
    return `${formatNumber(value)}${unit}`;
  }
  return value;
};

// 获取详情表格数据
const getDetailTableData = (device) => {
  const data = [];
  Object.entries(device).forEach(([key, value]) => {
    if (key === 'deviceName') return;

    const type = getColumnType(key);
    let status = '正常';

    if (type === 'status' && value === '是' && (key.includes('故障') || key.includes('压差'))) {
      status = '异常';
    } else if (type === 'number') {
      const numValue = parseFloat(value);
      if (isNaN(numValue) || numValue === 0) {
        status = '异常';
      }
    }

    data.push({
      name: key,
      value: value,
      unit: getColumnUnit(key),
      status: status
    });
  });

  return data;
};

// 格式化详情值
const formatDetailValue = (name, value) => {
  const type = getColumnType(name);
  if (type === 'number') {
    return formatNumber(value);
  }
  return value;
};

// 表格行样式
const tableRowClassName = ({ row }) => {
  if (row.故障报警 === '是') return 'warning-row';
  if (row.运行状态 !== '是') return 'offline-row';
  return '';
};

// 事件处理
const handleSearch = () => {
  queryParams.pageNum = 1;
  getList();
};

const resetSearch = () => {
  queryParams.device_type = '';
  queryParams.pageNum = 1;
  deviceList.value = [];
  dynamicColumns.value = [];
  total.value = 0;
};

const handleDeviceTypeChange = () => {
  queryParams.pageNum = 1;
  if (queryParams.device_type) {
    getList();
  } else {
    deviceList.value = [];
    dynamicColumns.value = [];
    total.value = 0;
  }
};

const handlePageChange = (newPage) => {
  queryParams.pageNum = newPage;
  getList();
};

const handleSizeChange = (newSize) => {
  queryParams.pageSize = newSize;
  queryParams.pageNum = 1;
  getList();
};

const toggleTableView = () => {
  isCardView.value = !isCardView.value;
};

const handleRowClick = (row) => {
  currentDevice.value = row;
  detailDialogOpen.value = true;
};

const handleView = (device) => {
  currentDevice.value = device;
  detailDialogOpen.value = true;
};

// 查看组态信息
const handleViewConfiguration = (device) => {
  currentConfigDevice.value = device;
  configurationDialogOpen.value = true;
  getConfigurationInfo(device);
};

</script>

<style scoped>
/* ===== 基础样式 ===== */
.device-management-system {
  padding: 20px;
  background: #f5f7fa;
  min-height: 90vh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 1.6;
  animation: fadeIn 0.5s ease-in-out;
}

/* ===== 搜索表单样式 ===== */
.search-form {
  background: #fff;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  margin-bottom: 20px;
  border: 1px solid #e4e7ed;
  transition: box-shadow 0.3s ease;
}

.search-form:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}

.search-form .el-form {
  margin-bottom: 0;
}

.search-form .el-form-item {
  margin-bottom: 0;
}

.search-form .el-form-item__label {
  font-weight: 600;
  color: #303133;
}

/* ===== 表格容器样式 ===== */
.table-container {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  margin-bottom: 20px;
  transition: box-shadow 0.3s ease;
}

.table-container:hover {
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
}

/* ===== 表格头部样式 ===== */
.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #e4e7ed;
  background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-icon {
  font-size: 20px;
  color: #409eff;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.header-actions {
  display: flex;
  gap: 8px;
}

/* ===== 表格样式优化 ===== */
.table-view {
  padding: 0;
}

/* 表格基础样式 */
:deep(.el-table) {
  border-radius: 0;
  overflow: hidden;
}

/* 表格头部样式 */
:deep(.el-table th) {
  background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
  color: #303133;
  font-weight: 600;
  padding: 14px 12px;
  border-bottom: 2px solid #e4e7ed;
}

/* 表格单元格样式 */
:deep(.el-table td) {
  padding: 12px;
  border-bottom: 1px solid #f0f2f5;
}

/* 去掉隔行变色，统一背景色 */
:deep(.el-table__row) {
  background-color: #ffffff;
  transition: all 0.3s ease;
}

/* 悬停效果 */
:deep(.el-table__row:hover) {
  background-color: #f0f9ff !important;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

/* 选中行样式 - 加深颜色便于区分 */
:deep(.el-table__row.current-row) {
  background-color: #e6f4ff !important;
  border-left: 4px solid #409eff;
}

:deep(.el-table__row.current-row:hover) {
  background-color: #d4edff !important;
}

/* 表格行状态样式 */
:deep(.warning-row) {
  background-color: #fef0f0 !important;
  border-left: 4px solid #f56c6c;
}

:deep(.warning-row:hover) {
  background-color: #fde2e2 !important;
}

:deep(.offline-row) {
  background-color: #f5f7fa !important;
  color: #909399;
}

:deep(.offline-row:hover) {
  background-color: #ebeef5 !important;
}

/* ===== 设备信息样式 ===== */
.device-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.device-info:hover {
  background: rgba(64, 158, 255, 0.05);
  transform: translateY(-2px);
}

.device-name {
  font-weight: 600;
  color: #303133;
  font-size: 14px;
}

.device-type {
  color: #909399;
  font-size: 12px;
  background: #f0f2f5;
  padding: 2px 6px;
  border-radius: 4px;
  display: inline-block;
  width: fit-content;
}

/* ===== 单元格内容样式 ===== */
.cell-content {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
}

.number-display {
  display: flex;
  align-items: center;
  gap: 2px;
}

.number-value {
  font-weight: 600;
  transition: all 0.3s ease;
}

.number-value:hover {
  transform: scale(1.05);
}

.unit {
  font-size: 11px;
  color: #909399;
}

/* ===== 数值状态样式 ===== */
.value-normal .number-value {
  color: #67c23a;
}

.value-warning .number-value {
  color: #e6a23c;
}

.value-high .number-value {
  color: #f56c6c;
}

.value-zero .number-value {
  color: #909399;
}

.value-invalid .number-value {
  color: #f56c6c;
}

/* ===== 卡片视图样式 ===== */
.card-view {
  padding: 20px;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 20px;
  margin-bottom: 20px;
}

.device-card-wrapper {
  animation: cardFadeIn 0.6s ease-out;
}

.device-card {
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
  border: 1px solid #e4e7ed;
  overflow: hidden;
}

.device-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  border-color: #409eff;
}

.device-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0;
}

.device-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.device-avatar-large {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 20px;
}

.device-card-header .device-info {
  flex: 1;
}

.device-card-header .device-name {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

.device-card-header .device-type {
  font-size: 12px;
  color: #909399;
  margin: 4px 0 0 0;
}

.device-card-content {
  padding: 0;
}

.parameters-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.parameter-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.parameter-item:hover {
  background: #e3f2fd;
  transform: translateY(-2px);
}

.parameter-label {
  font-size: 12px;
  color: #909399;
  font-weight: 500;
}

.parameter-value {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
}

.device-card-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 0;
}

/* ===== 空状态样式 ===== */
.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: #909399;
}

.empty-content {
  max-width: 400px;
  margin: 0 auto;
}

.empty-icon {
  color: #c0c4cc;
  margin-bottom: 24px;
  animation: float 3s ease-in-out infinite;
}

.empty-title {
  font-size: 20px;
  color: #303133;
  margin: 0 0 12px 0;
  font-weight: 600;
}

.empty-description {
  font-size: 14px;
  color: #909399;
  margin: 0 0 24px 0;
  line-height: 1.6;
}

.empty-result {
  padding: 40px 20px;
  text-align: center;
}

.empty-image {
  color: #c0c4cc;
}

/* ===== 分页样式 ===== */
.pagination-container {
  display: flex;
  justify-content: flex-end;
  padding: 20px 24px;
  background-color: #fff;
  border-top: 1px solid #f0f2f5;
}

/* ===== 组态信息弹窗样式 ===== */
.configuration-iframe-container {
  width: 100%;
  height: 70vh; /* 调整高度 */
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  overflow: hidden;
  background: #f5f7fa;
}

.configuration-iframe {
  width: 100%;
  height: 100%;
  border: none;
  display: block;
}

/* 弹窗样式覆盖 */
:deep(.el-dialog) {
  margin: 0 auto !important;
  border-radius: 12px;
  overflow: hidden;
}
:deep(.el-dialog__header) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  padding: 20px 24px;
  margin: 0;
}
:deep(.el-dialog__title) {
  color: #fff;
  font-weight: 600;
  font-size: 18px;
}

:deep(.el-dialog__headerbtn .el-dialog__close) {
  color: #fff;
  font-size: 20px;
}

/* 组态弹窗特殊处理 */
:deep(.el-dialog__body) {
  padding: 16px !important; /* 减少内边距 */
  max-height: none;
  overflow: hidden;
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .configuration-iframe-container {
    height: 80vh;
  }
}

@media (max-width: 768px) {
  .configuration-iframe-container {
    height: 75vh;
  }

  :deep(.el-dialog) {
    width: 98% !important;
    top: 1vh !important;
  }
}

@media (max-width: 480px) {
  .configuration-iframe-container {
    height: 70vh;
  }

  :deep(.el-dialog) {
    width: 100% !important;
    top: 0 !important;
    margin: 0 !important;
  }
}

/* ===== 设备详情弹窗内容样式 ===== */
.device-detail {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.detail-section {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  border-left: 4px solid #409eff;
}

.detail-section h3 {
  margin: 0 0 16px 0;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.detail-section h3::before {
  content: '';
  width: 4px;
  height: 16px;
  background: #409eff;
  border-radius: 2px;
}

/* ===== 参数表格样式 ===== */
.param-name {
  display: flex;
  align-items: center;
  gap: 8px;
}

.param-icon {
  color: #409eff;
  font-size: 14px;
}

.param-value-cell {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: center;
}

.param-value {
  font-weight: 600;
}

.param-unit {
  font-size: 12px;
  color: #909399;
}

/* ===== 按钮样式优化 ===== */
.el-button {
  border-radius: 6px;
  font-weight: 500;
  transition: all 0.3s ease;
}

.el-button--primary {
  background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
  border: none;
  box-shadow: 0 2px 8px rgba(64, 158, 255, 0.3);
}

.el-button--primary:hover {
  background: linear-gradient(135deg, #3a8ee6 0%, #337ecc 100%);
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
  transform: translateY(-1px);
}

.el-button--success {
  background: linear-gradient(135deg, #67c23a 0%, #5daf34 100%);
  border: none;
  box-shadow: 0 2px 8px rgba(103, 194, 58, 0.3);
}

.el-button--success:hover {
  background: linear-gradient(135deg, #5daf34 0%, #529b2e 100%);
  box-shadow: 0 4px 12px rgba(103, 194, 58, 0.4);
  transform: translateY(-1px);
}

.el-button[circle] {
  transition: all 0.3s ease;
}

.el-button[circle]:hover {
  transform: rotate(180deg) scale(1.1);
}

/* ===== 标签样式优化 ===== */
.el-tag {
  border-radius: 4px;
  font-weight: 500;
  border: none;
  font-size: 11px;
  padding: 4px 8px;
  transition: all 0.3s ease;
}

.el-tag:hover {
  transform: scale(1.05);
}

.el-tag--success {
  background: linear-gradient(135deg, #4CAF50 0%, #009688 100%);
  color: #fff;
}

.el-tag--danger {
  background: linear-gradient(135deg, #f56c6c 0%, #f25c5c 100%);
  color: #fff;
}

.el-tag--warning {
  background: linear-gradient(135deg, #e6a23c 0%, #d19e3c 100%);
  color: #fff;
}

.el-tag--info {
  background: linear-gradient(135deg, #673AB7 0%, #9E9E9E 100%);
  color: #fff;
}

.el-tag--primary {
  background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
  color: #fff;
}

/* ===== 动画定义 ===== */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes cardFadeIn {
  from {
    opacity: 0;
    transform: translateY(20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes dialogSlideIn {
  from {
    opacity: 0;
    transform: scale(0.8) translateY(-50px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-10px);
  }
}

/* ===== 响应式设计 ===== */
@media (max-width: 1200px) {
  .device-management-system {
    padding: 16px;
  }

  .cards-grid {
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 16px;
  }

  .configuration-iframe-container {
    height: 70vh;
  }
}

@media (max-width: 768px) {
  .device-management-system {
    padding: 12px;
  }

  .search-form {
    padding: 16px;
  }

  .table-header {
    padding: 16px;
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .header-actions {
    align-self: flex-end;
  }

  .cards-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .parameters-grid {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .parameter-item {
    padding: 8px;
  }

  .device-card-actions {
    flex-direction: column;
    gap: 8px;
  }

  :deep(.el-table td) {
    padding: 8px 4px;
    font-size: 12px;
  }

  :deep(.el-table th) {
    padding: 8px 4px;
    font-size: 12px;
  }

  .device-info,
  .cell-content {
    font-size: 11px;
  }

  .pagination-container {
    padding: 16px;
    justify-content: center;
  }

  :deep(.el-dialog) {
    width: 95% !important;
    margin: 0 auto;
  }

  .detail-section {
    padding: 16px;
  }

  .configuration-iframe-container {
    height: 60vh;
  }
}

@media (max-width: 480px) {
  .device-management-system {
    padding: 8px;
  }

  .search-form {
    padding: 12px;
  }

  .search-form .el-form {
    display: flex;
    flex-direction: column;
  }

  .search-form .el-form-item {
    width: 100%;
  }

  .search-form .el-input,
  .search-form .el-select {
    width: 100% !important;
  }

  .table-header {
    padding: 12px;
  }

  .header-title {
    font-size: 16px;
  }

  .empty-state {
    padding: 40px 12px;
  }

  .empty-title {
    font-size: 18px;
  }

  .empty-description {
    font-size: 13px;
  }

  .device-card {
    margin: 0 4px;
  }

  .device-card-header {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }

  .configuration-iframe-container {
    height: 50vh;
  }

  .dialog-footer {
    flex-direction: column;
    gap: 8px;
  }

  .dialog-footer .el-button {
    width: 100%;
  }
}

/* ===== 滚动条样式 ===== */
:deep(.el-dialog__body)::-webkit-scrollbar,
:deep(.el-table__body-wrapper)::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

:deep(.el-dialog__body)::-webkit-scrollbar-track,
:deep(.el-table__body-wrapper)::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

:deep(.el-dialog__body)::-webkit-scrollbar-thumb,
:deep(.el-table__body-wrapper)::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

:deep(.el-dialog__body)::-webkit-scrollbar-thumb:hover,
:deep(.el-table__body-wrapper)::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* ===== 输入框和选择器样式优化 ===== */
:deep(.el-input__wrapper) {
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

:deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

:deep(.el-select .el-input__wrapper) {
  border-radius: 6px;
}

/* ===== 分页样式优化 ===== */
:deep(.el-pagination) {
  --el-pagination-button-color: #606266;
  --el-pagination-hover-color: #409eff;
}

:deep(.el-pagination .el-pager li) {
  border-radius: 6px;
  margin: 0 2px;
  transition: all 0.3s ease;
}

:deep(.el-pagination .el-pager li:hover) {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

:deep(.el-pagination .el-pager li.is-active) {
  background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
  color: #fff;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
}

:deep(.el-pagination__total) {
  margin-right: 16px;
  color: #606266;
  font-weight: 500;
}

:deep(.el-pagination__sizes) {
  margin-right: 16px;
}

/* ===== 描述列表样式优化 ===== */
:deep(.el-descriptions) {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-descriptions__label) {
  background: #fafbfc !important;
  font-weight: 600;
  color: #303133;
  border-right: 1px solid #e4e7ed;
}

:deep(.el-descriptions__content) {
  background: #fff !important;
  color: #606266;
}

/* ===== 表格固定列阴影 ===== */
:deep(.el-table__fixed-right) {
  box-shadow: -2px 0 8px rgba(0, 0, 0, 0.1);
}

:deep(.el-table__fixed-left) {
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
}

/* ===== 工具提示样式 ===== */
:deep(.el-tooltip__popper) {
  background: rgba(0, 0, 0, 0.8);
  border-radius: 6px;
  backdrop-filter: blur(10px);
}

/* ===== 加载动画优化 ===== */
:deep(.el-loading-mask) {
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);
}

:deep(.el-loading-spinner) {
  animation: loadingRotate 1.5s linear infinite;
}

@keyframes loadingRotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* ===== 表格边框优化 ===== */
:deep(.el-table--border) {
  border: 1px solid #ebeef5;
  border-radius: 0;
}

:deep(.el-table--border::after) {
  display: none;
}

/* ===== 空数据状态优化 ===== */
:deep(.el-table__empty-block) {
  padding: 60px 0;
}

:deep(.el-table__empty-text) {
  color: #909399;
  font-size: 14px;
}

/* ===== 焦点样式增强 ===== */
.el-button:focus,
.el-input:focus,
.el-select:focus {
  outline: 2px solid #409eff;
  outline-offset: 2px;
}

/* ===== 选择状态样式 ===== */
::selection {
  background: rgba(64, 158, 255, 0.3);
  color: #303133;
}

/* ===== 表单验证样式 ===== */
:deep(.el-form-item.is-error .el-input__wrapper) {
  box-shadow: 0 0 0 1px #f56c6c inset;
}

:deep(.el-form-item.is-success .el-input__wrapper) {
  box-shadow: 0 0 0 1px #67c23a inset;
}

/* ===== 性能优化 ===== */
.device-management-system * {
  box-sizing: border-box;
}

.device-card:hover,
:deep(.el-table__row:hover),
.parameter-item:hover {
  will-change: transform;
}

/* ===== 无障碍支持 ===== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== 高对比度模式 ===== */
@media (prefers-contrast: high) {
  .device-management-system {
    background: #fff;
    color: #000;
  }

  .search-form,
  .table-container,
  .device-card {
    border: 2px solid #000;
    background: #fff;
  }

  .el-button--primary {
    background: #000;
    color: #fff;
    border: 2px solid #000;
  }

  .el-button--success {
    background: #006400;
    color: #fff;
    border: 2px solid #006400;
  }

  .el-tag {
    border: 1px solid #000;
    color: #000;
    background: #fff;
  }

  :deep(.el-table__row.current-row) {
    background-color: #000 !important;
    color: #fff;
  }

  .configuration-iframe-container {
    border: 2px solid #000;
  }
}

/* ===== 打印样式 ===== */
@media print {
  .search-form,
  .pagination-container,
  .header-actions,
  .el-button,
  .dialog-footer {
    display: none !important;
  }

  .device-management-system {
    background: #fff;
    padding: 0;
  }

  .table-container {
    box-shadow: none;
    border: 1px solid #000;
  }

  .table-header {
    background: #f5f7fa !important;
    color: #000 !important;
    border-bottom: 2px solid #000;
  }

  :deep(.el-table th) {
    background: #f5f7fa !important;
    color: #000 !important;
    border: 1px solid #000;
  }

  :deep(.el-table td) {
    background: #fff !important;
    color: #000 !important;
    border: 1px solid #000;
  }

  :deep(.el-table__row) {
    background-color: #fff !important;
  }

  .device-card {
    break-inside: avoid;
    margin-bottom: 20px;
    border: 1px solid #000;
  }
}

/* ===== 最终样式确保 ===== */
.device-management-system {
  position: relative;
  z-index: 1;
}

/* 确保表格行选中状态优先级最高 */
:deep(.el-table tbody tr.current-row > td) {
  background-color: #e6f4ff !important;
}

:deep(.el-table tbody tr.current-row:hover > td) {
  background-color: #d4edff !important;
}

/* 确保警告行状态 */
:deep(.el-table tbody tr.warning-row > td) {
  background-color: #fef0f0 !important;
}

:deep(.el-table tbody tr.warning-row:hover > td) {
  background-color: #fde2e2 !important;
}

/* 确保离线行状态 */
:deep(.el-table tbody tr.offline-row > td) {
  background-color: #f5f7fa !important;
  color: #909399;
}

:deep(.el-table tbody tr.offline-row:hover > td) {
  background-color: #ebeef5 !important;
}

/* 确保弹窗层级正确 */
:deep(.el-overlay-dialog) {
  z-index: 2000;
  top: 10vh;
}

/* 确保按钮组在小屏幕上的布局 */
@media (max-width: 600px) {
  .device-card-actions {
    flex-direction: column;
    gap: 8px;
  }

  .device-card-actions .el-button {
    width: 100%;
    margin: 0;
  }
}

/* 最终确保所有交互元素的可访问性 */
.el-button,
.el-link,
.device-card,
.parameter-item {
  cursor: pointer;
  user-select: none;
}

.el-button:disabled,
.el-link:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

/* 确保文本选择的一致性 */
.device-name,
.parameter-label,
.parameter-value {
  user-select: text;
}

/* 最终的全局重置确保 */
* {
  box-sizing: border-box;
}

*:focus {
  outline: none;
}

*:focus-visible {
  outline: 2px solid #409eff;
  outline-offset: 2px;
}
</style>
