<template>
  <div class="temperature-device-page">

    <!-- 查询条件表单 -->
    <div class="search-form">
      <el-form :model="searchForm" inline>
        <el-form-item label="设备名称">
          <el-input
              v-model="searchForm.query"
              placeholder="请输入设备名称"
              clearable
              prefix-icon="Search"
          />
        </el-form-item>

        <el-form-item label="设备状态">
          <el-select
              v-model="searchForm.devices_enabled"
              placeholder="请选择设备状态"
              clearable
          >
            <el-option label="全部" :value="-1" />
            <el-option label="在线" :value="1" />
            <el-option label="离线" :value="0" />
          </el-select>
        </el-form-item>

        <el-form-item label="网关">
          <el-select
              v-model="searchForm.gateway_id"
              placeholder="请选择网关"
              clearable
              filterable
          >
            <el-option
                v-for="item in gatewayList"
                :key="item.gateway_id"
                :label="item.gateway_name"
                :value="item.gateway_id"
            >
              <div class="gateway-option">
                <span class="gateway-name">{{ item.gateway_name }}</span>
                <el-tag
                    size="small"
                    :type="item.gateway_status === 1 ? 'success' : 'danger'"
                    class="gateway-status"
                >
                  {{ item.gateway_status === 1 ? '在线' : '离线' }}
                </el-tag>
              </div>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSearch" icon="Search">查询</el-button>
          <el-button @click="handleReset" icon="Refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <el-table
          :data="deviceList"
          v-loading="loading"
          border
          stripe
          style="width: 100%"
          :row-class-name="tableRowClassName"
      >
        <el-table-column prop="devices_id" label="设备ID" width="120" align="center" />

        <el-table-column label="设备信息" width="300">
          <template #default="scope">
            <div class="device-info">
              <div class="device-name">{{ scope.row.devices_name }}</div>
              <div class="device-type">{{ scope.row.devices_type_name }}</div>
              <div class="device-udid">{{ scope.row.devices_udid }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="位置信息" width="250">
          <template #default="scope">
            <div class="location-info">
              <div class="building">
                <el-icon><OfficeBuilding /></el-icon>
                {{ scope.row.building_name }}
              </div>
              <div class="region">{{ scope.row.full_region_name }}</div>
              <div class="room">房间: {{ scope.row.room_name }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="网关信息" width="160">
          <template #default="scope">
            <div class="gateway-info">
              <div class="gateway-name">{{ scope.row.gateway_name }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="设备状态" width="120" align="center">
          <template #default="scope">
            <div class="device-status">
              <el-tag
                  :type="scope.row.devices_enabled ? 'success' : 'info'"
                  effect="dark"
              >
                {{ scope.row.devices_enabled ? '在线' : '离线' }}
              </el-tag>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="温度信息" width="140" align="center">
          <template #default="scope">
            <div class="temperature-info">
              <div class="current-temp">
                <span class="temp-label">当前:</span>
                <span class="temp-value" :class="getTempClass(getDeviceData(scope.row).dis_temp)">
                  {{ getDeviceData(scope.row).dis_temp || '--' }}°C
                </span>
              </div>
              <div class="set-temp">
                <span class="temp-label">设定:</span>
                <span class="temp-value">
                  {{ getDeviceData(scope.row).temp_setting || '--' }}°C
                </span>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="运行状态" width="120" align="center">
          <template #default="scope">
            <div class="run-status">
              <div class="switch-status">
                <el-tag
                    :type="getDeviceData(scope.row).switch_status ? 'success' : 'info'"
                    size="small"
                >
                  {{ getDeviceData(scope.row).switch_status ? '开启' : '关闭' }}
                </el-tag>
              </div>
              <div class="run-mode">
                <el-tag
                    size="small"
                    :type="getRunModeType(getDeviceData(scope.row).run_mode)"
                >
                  {{ getRunModeText(getDeviceData(scope.row).run_mode) }}
                </el-tag>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="设备详情" width="160" align="center">
          <template #default="scope">
            <div class="device-details">
              <div class="fan-info">
                <span class="detail-label">风扇:</span>
                <el-tag
                    size="small"
                    :type="getDeviceData(scope.row).fan_status ? 'success' : 'info'"
                >
                  {{ getDeviceData(scope.row).fan_status ? '运行' : '停止' }}
                </el-tag>
              </div>
              <div class="valve-info">
                <span class="detail-label">阀门:</span>
                <el-tag
                    size="small"
                    :type="getDeviceData(scope.row).valve_status ? 'success' : 'info'"
                >
                  {{ getDeviceData(scope.row).valve_status ? '开启' : '关闭' }}
                </el-tag>
              </div>
              <div class="fan-setting">
                <span class="detail-label">风速:</span>
                <span class="detail-value">{{ getDeviceData(scope.row).fan_setting || '--' }}</span>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="温度范围" width="120" align="center">
          <template #default="scope">
            <div class="temp-range">
              <div class="temp-min">最低: {{ getDeviceData(scope.row).temp_min || '--' }}°C</div>
              <div class="temp-max">最高: {{ getDeviceData(scope.row).temp_max || '--' }}°C</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column prop="devices_last_request_time" label="最后请求时间"  align="center">
          <template #default="scope">
            <div class="time-info">
              <div class="date">{{ parseTime(scope.row.devices_last_request_time) }}</div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120"  align="center">
          <template #default="scope">
            <el-button
                type="primary"
                size="small"
                @click="handleView(scope.row)"
                icon="View"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
          v-model:current-page="searchForm.currentPage"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <!-- 设备详情弹窗 -->
    <el-dialog
        v-model="dialogVisible"
        title="设备详情"
        width="900px"
        :before-close="handleCloseDialog"
    >
      <div v-if="selectedDevice" class="device-detail">
        <!-- 基本信息 -->
        <div class="detail-section">
          <h3>基本信息</h3>
          <el-descriptions :column="3" border>
            <el-descriptions-item label="设备ID">{{ selectedDevice.devices_id }}</el-descriptions-item>
            <el-descriptions-item label="设备名称">{{ selectedDevice.devices_name }}</el-descriptions-item>
            <el-descriptions-item label="设备类型">{{ selectedDevice.devices_type_name }}</el-descriptions-item>
            <el-descriptions-item label="设备UDID">{{ selectedDevice.devices_udid }}</el-descriptions-item>
            <el-descriptions-item label="网关名称">{{ selectedDevice.gateway_name }}</el-descriptions-item>
            <el-descriptions-item label="请求间隔">{{ selectedDevice.devices_req_interval }}秒</el-descriptions-item>
            <el-descriptions-item label="建筑">{{ selectedDevice.building_name }}</el-descriptions-item>
            <el-descriptions-item label="区域">{{ selectedDevice.full_region_name }}</el-descriptions-item>
            <el-descriptions-item label="房间">{{ selectedDevice.room_name }}</el-descriptions-item>
          </el-descriptions>
        </div>

        <!-- 状态信息 -->
        <div class="detail-section">
          <h3>状态信息</h3>
          <el-descriptions :column="3" border>
            <el-descriptions-item label="设备状态">
              <el-tag :type="selectedDevice.devices_enabled ? 'success' : 'danger'">
                {{ selectedDevice.devices_enabled ? '在线' : '离线' }}
              </el-tag>
            </el-descriptions-item>
<!--            <el-descriptions-item label="通信状态">
              <el-tag :type="selectedDevice.on_line ? 'success' : 'warning'">
                {{ selectedDevice.on_line ? '通信正常' : '通信异常' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="网关状态">
              <el-tag :type="selectedDevice.gateway_status === 1 ? 'success' : 'danger'">
                {{ selectedDevice.gateway_status === 1 ? '网关在线' : '网关离线' }}
              </el-tag>
            </el-descriptions-item>-->
          </el-descriptions>
        </div>

        <!-- 温控数据 -->
        <div v-if="selectedDevice.devices_json_object" class="detail-section">
          <h3>温控数据</h3>
          <el-descriptions :column="3" border>
            <el-descriptions-item label="当前温度">
              <span :class="getTempClass(getDeviceData(selectedDevice).dis_temp)">
                {{ getDeviceData(selectedDevice).dis_temp }}°C
              </span>
            </el-descriptions-item>
            <el-descriptions-item label="设定温度">{{ getDeviceData(selectedDevice).temp_setting }}°C</el-descriptions-item>
            <el-descriptions-item label="运行模式">
              <el-tag :type="getRunModeType(getDeviceData(selectedDevice).run_mode)">
                {{ getRunModeText(getDeviceData(selectedDevice).run_mode) }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="开关状态">
              <el-tag :type="getDeviceData(selectedDevice).switch_status ? 'success' : 'info'">
                {{ getDeviceData(selectedDevice).switch_status ? '开启' : '关闭' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="风扇状态">
              <el-tag :type="getDeviceData(selectedDevice).fan_status ? 'success' : 'info'">
                {{ getDeviceData(selectedDevice).fan_status ? '运行' : '停止' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="阀门状态">
              <el-tag :type="getDeviceData(selectedDevice).valve_status ? 'success' : 'info'">
                {{ getDeviceData(selectedDevice).valve_status ? '开启' : '关闭' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="风扇设定">{{ getDeviceData(selectedDevice).fan_setting }}</el-descriptions-item>
            <el-descriptions-item label="温度范围">{{ getDeviceData(selectedDevice).temp_min }}°C - {{ getDeviceData(selectedDevice).temp_max }}°C</el-descriptions-item>
            <el-descriptions-item label="回差设定">{{ getDeviceData(selectedDevice).backlash_setting }}°C</el-descriptions-item>
          </el-descriptions>
        </div>

        <!-- 高级设置 -->
        <div v-if="selectedDevice.devices_json_object" class="detail-section">
          <h3>高级设置</h3>
          <el-descriptions :column="3" border>
            <el-descriptions-item label="传感器选择">{{ getDeviceData(selectedDevice).sensor_selection }}</el-descriptions-item>
            <el-descriptions-item label="按键锁定">
              <el-tag :type="getDeviceData(selectedDevice).key_lock ? 'warning' : 'success'">
                {{ getDeviceData(selectedDevice).key_lock ? '已锁定' : '未锁定' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="防冻保护">
              <el-tag :type="getDeviceData(selectedDevice).frozen_enable ? 'success' : 'info'">
                {{ getDeviceData(selectedDevice).frozen_enable ? '启用' : '禁用' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="防冻温度">{{ getDeviceData(selectedDevice).frozen_temp }}°C</el-descriptions-item>
            <el-descriptions-item label="外部温度">{{ getDeviceData(selectedDevice).out_temp }}°C</el-descriptions-item>
            <el-descriptions-item label="温度校准">{{ getDeviceData(selectedDevice).out_temp_calibration }}°C</el-descriptions-item>
          </el-descriptions>
        </div>

        <!-- 时间信息 -->
        <div class="detail-section">
          <h3>时间信息</h3>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="创建时间">{{ parseTime(selectedDevice.devices_created) }}</el-descriptions-item>
            <el-descriptions-item label="最后请求时间">{{ parseTime(selectedDevice.devices_last_request_time) }}</el-descriptions-item>
            <el-descriptions-item label="网关最后请求">{{ parseTime(selectedDevice.gateway_last_request_time) }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Wkq">
import { ref, reactive, onMounted, computed } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import {
  Search,
  Refresh,
  Connection,
  Close,
  VideoPlay,
  Monitor,
  OfficeBuilding,
  View
} from '@element-plus/icons-vue'

// 响应式数据
const loading = ref(false)
const deviceList = ref([])
const gatewayList = ref([])
const total = ref(0)
const dialogVisible = ref(false)
const selectedDevice = ref(null)

// 搜索表单
const searchForm = reactive({
  currentPage: 1,
  pageSize: 10,
  device_type_id: '',
  devices_enabled: -1,
  query: '',
  gateway_id: '',
  devices_udid: ''
})

// 计算统计数据
const onlineCount = computed(() => {
  return deviceList.value.filter(device => device.devices_enabled === 1).length
})

const offlineCount = computed(() => {
  return deviceList.value.filter(device => device.devices_enabled === 0).length
})

const runningCount = computed(() => {
  return deviceList.value.filter(device => {
    const data = getDeviceData(device)
    return data.switch_status === 1
  }).length
})

// 创建FormData的函数
const createFormData = (params) => {
  const formData = new FormData()

  // 必传参数
  formData.append('currentPage', params.currentPage)
  formData.append('pageSize', params.pageSize)

  // 可选参数，只有在有值时才添加
  if (params.device_type_id && params.device_type_id.toString().trim() !== '') {
    formData.append('device_type_id', params.device_type_id)
  }

  if (params.devices_enabled !== '' && params.devices_enabled !== null && params.devices_enabled !== undefined) {
    formData.append('devices_enabled', params.devices_enabled)
  }

  if (params.query && params.query.trim() !== '') {
    formData.append('query', params.query.trim())
  }

  if (params.gateway_id && params.gateway_id.toString().trim() !== '') {
    formData.append('gateway_id', params.gateway_id)
  }

  if (params.devices_udid && params.devices_udid.trim() !== '') {
    formData.append('devices_udid', params.devices_udid.trim())
  }

  return formData
}

// 获取设备列表
const getDeviceList = async () => {
  loading.value = true
  try {
    // 创建FormData
    const formData = createFormData(searchForm)

    const response = await axios.post(`${__LOCAL_API__}/temperature/getBaseecic`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })

    if (response.data.code === 200) {
      deviceList.value = response.data.data.devices || []
      total.value = response.data.data.count || 0
    } else {
      ElMessage.error(response.data.message || '获取设备列表失败')
    }
  } catch (error) {
    console.error('获取设备列表失败:', error)
    ElMessage.error('获取设备列表失败')
  } finally {
    loading.value = false
  }
}

// 获取网关列表
const getGatewayList = async () => {
  try {
    const response = await axios.get(`${__LOCAL_API__}/temperature/gatewayFind`)

    if (response.data.code === 200) {
      gatewayList.value = response.data.data.gatewayAndBacnet || []
    } else {
      ElMessage.error(response.data.message || '获取网关列表失败')
    }
  } catch (error) {
    console.error('获取网关列表失败:', error)
    ElMessage.error('获取网关列表失败')
  }
}

// 解析设备JSON数据
const getDeviceData = (device) => {
  try {
    return device.devices_json_object ? JSON.parse(device.devices_json_object) : {}
  } catch (error) {
    console.error('解析设备数据失败:', error)
    return {}
  }
}

// 获取运行模式文本
const getRunModeText = (mode) => {
  const modeMap = {
    1: '制冷',
    2: '制热',
    3: '通风'
  }
  return modeMap[mode] || '未知'
}

// 获取运行模式类型
const getRunModeType = (mode) => {
  const typeMap = {
    1: 'primary',
    2: 'danger',
    3: 'waring',
  }
  return typeMap[mode] || 'info'
}

// 获取温度样式类
const getTempClass = (temp) => {
  if (!temp) return ''
  if (temp < 18) return 'temp-cold'
  if (temp > 26) return 'temp-hot'
  return 'temp-normal'
}

// 表格行样式
const tableRowClassName = ({ row }) => {
  if (!row.devices_enabled) return 'offline-row'
  return ''
}

const formatDate = (dateTime) => {
  if (!dateTime) return '--'
  const date = new Date(dateTime)
  return date.toLocaleDateString('zh-CN')
}

const formatTime = (dateTime) => {
  if (!dateTime) return '--'
  const date = new Date(dateTime)
  return date.toLocaleTimeString('zh-CN')
}

// 搜索
const handleSearch = () => {
  searchForm.currentPage = 1
  getDeviceList()
}

// 重置
const handleReset = () => {
  Object.assign(searchForm, {
    currentPage: 1,
    pageSize: 10,
    device_type_id: '',
    devices_enabled: -1,
    query: '',
    gateway_id: '',
    devices_udid: ''
  })
  getDeviceList()
}

// 分页大小改变
const handleSizeChange = (size) => {
  searchForm.pageSize = size
  searchForm.currentPage = 1
  getDeviceList()
}

// 当前页改变
const handleCurrentChange = (page) => {
  searchForm.currentPage = page
  getDeviceList()
}

// 查看详情
const handleView = (device) => {
  selectedDevice.value = device
  dialogVisible.value = true
}

// 关闭弹窗
const handleCloseDialog = () => {
  dialogVisible.value = false
  selectedDevice.value = null
}

// 组件挂载时获取数据
onMounted(() => {
  getGatewayList()
  getDeviceList()
})
</script>

<style scoped>
.temperature-device-page {
  padding: 20px;
  background: #f5f7fa;
  min-height: 90vh;
}

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

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

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

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

.search-form .el-input {
  width: 200px;
}

.search-form .el-select {
  width: 180px;
}

/* 网关选项样式 */
.gateway-option {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.gateway-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.gateway-status {
  margin-left: 8px;
}

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

:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table th) {
  background: linear-gradient(135deg, #eff2f3 100%, #f8f9fa 100%);
  color: #333;
  font-weight: 600;
  padding: 12px 12px;
}

:deep(.el-table td) {
  padding: 12px 0;
}

:deep(.el-table__row:hover) {
  background-color: #f5f7fa;
}

/* 表格行状态样式 */
:deep(.offline-row) {
  background-color: #fef0f0 !important;
}

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

/* 设备信息样式 */
.device-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.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;
}

.device-udid {
  color: #909399;
  font-size: 11px;
  font-family: 'Courier New', monospace;
}

/* 位置信息样式 */
.location-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.building {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 600;
  color: #303133;
  font-size: 13px;
}

.region {
  color: #606266;
  font-size: 12px;
  padding-left: 16px;
}

.room {
  color: #909399;
  font-size: 12px;
  padding-left: 16px;
}

/* 网关信息样式 */
.gateway-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.gateway-name {
  font-weight: 500;
  color: #303133;
  font-size: 13px;
}

/* 设备状态样式 */
.device-status {
  display: flex;
  justify-content: center;
}

/* 温度信息样式 */
.temperature-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
}

.current-temp,
.set-temp {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
}

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

.temp-value {
  font-weight: 600;
  font-size: 13px;
}

.temp-cold {
  color: #409eff;
}

.temp-normal {
  color: #67c23a;
}

.temp-hot {
  color: #f56c6c;
}

/* 运行状态样式 */
.run-status {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
}

.switch-status,
.run-mode {
  width: 100%;
  display: flex;
  justify-content: center;
}

/* 设备详情样式 */
.device-details {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
}

.fan-info,
.valve-info,
.fan-setting {
  display: flex;
  align-items: center;
  gap: 4px;
}

.detail-label {
  color: #909399;
  font-size: 11px;
  min-width: 32px;
}

.detail-value {
  font-weight: 500;
  color: #303133;
}

/* 温度范围样式 */
.temp-range {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  text-align: center;
}

.temp-min,
.temp-max {
  color: #606266;
}

/* 时间信息样式 */
.time-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-align: center;
}

.date {
  font-weight: 500;
  color: #303133;
  font-size: 12px;
}

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

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

:deep(.el-pagination) {
  --el-pagination-button-color: #606266;
  --el-pagination-hover-color: #409eff;
}

/* 弹窗样式 */
:deep(.el-dialog) {
  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: 24px;
  max-height: 70vh;
  overflow-y: auto;
}

/* 设备详情弹窗内容样式 */
.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;
}

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

:deep(.el-descriptions__header) {
  background: #f5f7fa;
  padding: 12px 16px;
  border-bottom: 1px solid #e4e7ed;
}

:deep(.el-descriptions__body .el-descriptions__table) {
  border-collapse: separate;
  border-spacing: 0;
}

: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;
}

/* 按钮样式优化 */
.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-tag {
  border-radius: 4px;
  font-weight: 500;
  border: none;
  font-size: 11px;
  padding: 2px 6px;
}

.el-tag--success {
  background: linear-gradient(135deg, #67c23a 0%, #5daf34 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, #909399 0%, #82848a 100%);
  color: #fff;
}

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

/* 输入框和选择器样式优化 */
: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-loading-mask) {
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);
}

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

  .search-form .el-input,
  .search-form .el-select {
    width: 160px;
  }
}

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

  .search-form {
    padding: 16px;
  }

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

  .search-form .el-input,
  .search-form .el-select {
    width: 140px;
  }

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

  .device-info,
  .location-info,
  .temperature-info,
  .device-details {
    font-size: 11px;
  }
}

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

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

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

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

/* 动画效果 */
.temperature-device-page {
  animation: fadeIn 0.5s ease-in-out;
}

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

.search-form,
.table-container,
.pagination-container {
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 表格行悬停效果增强 */
:deep(.el-table tbody tr) {
  transition: all 0.3s ease;
}

:deep(.el-table tbody tr:hover) {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* 卡片阴影效果 */
.search-form,
.table-container,
.pagination-container {
  transition: box-shadow 0.3s ease;
}

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

/* 状态指示器样式 */
.device-status .el-tag {
  position: relative;
  overflow: hidden;
}

.device-status .el-tag::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s ease;
}

.device-status .el-tag:hover::before {
  left: 100%;
}

/* 温度显示增强效果 */
.temp-value {
  position: relative;
  transition: all 0.3s ease;
}

.temp-value:hover {
  transform: scale(1.1);
}

.temp-cold {
  text-shadow: 0 0 8px rgba(64, 158, 255, 0.5);
}

.temp-hot {
  text-shadow: 0 0 8px rgba(245, 108, 108, 0.5);
}

.temp-normal {
  text-shadow: 0 0 8px rgba(103, 194, 58, 0.5);
}

/* 网关状态指示 */
.gateway-status {
  position: relative;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(64, 158, 255, 0.7);
  }
  70% {
    box-shadow: 0 0 0 10px rgba(64, 158, 255, 0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(64, 158, 255, 0);
  }
}

/* 详情按钮特效 */
.el-button--primary.is-plain {
  background: transparent;
  border: 2px solid #409eff;
  color: #409eff;
  position: relative;
  overflow: hidden;
  z-index: 1;
}

.el-button--primary.is-plain::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #409eff 0%, #3a8ee6 100%);
  transition: left 0.3s ease;
  z-index: -1;
}

.el-button--primary.is-plain:hover::before {
  left: 0;
}

.el-button--primary.is-plain:hover {
  color: #fff;
  border-color: #409eff;
}

/* 表格头部渐变增强 */
:deep(.el-table th) {
  position: relative;
  overflow: hidden;
}

:deep(.el-table th::before) {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.8s ease;
}

:deep(.el-table th:hover::before) {
  left: 100%;
}

/* 设备信息卡片效果 */
.device-info,
.location-info,
.gateway-info,
.temperature-info,
.device-details {
  padding: 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

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

/* 分页按钮增强 */
: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-dialog) {
  animation: dialogSlideIn 0.4s ease-out;
}

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

/* 详情区块动画 */
.detail-section {
  animation: sectionFadeIn 0.6s ease-out;
  animation-fill-mode: both;
}

.detail-section:nth-child(1) { animation-delay: 0.1s; }
.detail-section:nth-child(2) { animation-delay: 0.2s; }
.detail-section:nth-child(3) { animation-delay: 0.3s; }
.detail-section:nth-child(4) { animation-delay: 0.4s; }
.detail-section:nth-child(5) { animation-delay: 0.5s; }

@keyframes sectionFadeIn {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

/* 搜索表单项动画 */
.search-form .el-form-item {
  animation: formItemSlide 0.5s ease-out;
  animation-fill-mode: both;
}

.search-form .el-form-item:nth-child(1) { animation-delay: 0.1s; }
.search-form .el-form-item:nth-child(2) { animation-delay: 0.2s; }
.search-form .el-form-item:nth-child(3) { animation-delay: 0.3s; }
.search-form .el-form-item:nth-child(4) { animation-delay: 0.4s; }

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

/* 加载状态优化 */
:deep(.el-loading-spinner) {
  animation: loadingRotate 1.5s linear infinite;
}

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

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

.empty-state .empty-icon {
  font-size: 64px;
  color: #c0c4cc;
  margin-bottom: 16px;
}

.empty-state .empty-text {
  font-size: 16px;
  margin-bottom: 8px;
}

.empty-state .empty-description {
  font-size: 14px;
  color: #c0c4cc;
}

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

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

/* 状态徽章样式 */
.status-badge {
  position: relative;
  display: inline-block;
}

.status-badge::after {
  content: '';
  position: absolute;
  top: -2px;
  right: -2px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #67c23a;
  border: 2px solid #fff;
  animation: statusPulse 2s infinite;
}

.status-badge.offline::after {
  background: #f56c6c;
}

.status-badge.warning::after {
  background: #e6a23c;
}

@keyframes statusPulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.2);
    opacity: 0.7;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

/* 深色模式支持 */
@media (prefers-color-scheme: dark) {
  .temperature-device-page {
    background: #1a1a1a;
    color: #e4e7ed;
  }

  .search-form,
  .table-container,
  .pagination-container {
    background: #2d2d2d;
    border-color: #4c4d4f;
  }

  .detail-section {
    background: #363636;
  }

  :deep(.el-table th) {
    background: linear-gradient(135deg, #4c4d4f 0%, #5a5b5d 100%);
  }

  :deep(.el-table td) {
    background: #2d2d2d;
    border-color: #4c4d4f;
  }
}

/* 打印样式 */
@media print {
  .search-form,
  .pagination-container,
  .el-table-column--selection,
  .el-button {
    display: none !important;
  }

  .temperature-device-page {
    background: #fff;
    padding: 0;
  }

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

  :deep(.el-table th) {
    background: #f5f7fa !important;
    color: #000 !important;
  }
}
</style>
