<template>
  <div class="tray-container">
    <!-- 标题区域 -->
    <div class="panel-title">
      <div class="title-with-status">
        <div class="title-left">
          <div class="title-text">实时数据看板</div>
          <div class="title-line"></div>
        </div>
      </div>

      <!-- 搜索区域 -->
      <div class="search-box">
        <el-input
          class="search-input"
          v-model="queryParams.fTrayCode"
          placeholder="请输入托盘编号"
          clearable
        >
        </el-input>
        <el-input
          class="search-input"
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
        >
        </el-input>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          class="search-button"
        >查询</el-button>
        <el-button
          icon="el-icon-refresh"
          @click="resetQuery"
          class="search-button reset-button"
        >重置</el-button>
      </div>
    </div>

    <!-- 表格内容区 -->
    <div class="tray-table-container">
      <!-- 带滚动条的表格容器 -->
      <div class="table-scroll-container">
        <el-table
          v-loading="loading"
          :data="list"
          border
          height="100%"
          style="width: 100%"
          :header-cell-style="headerCellStyle"
          :cell-style="cellStyle"
          class="tray-table"
        >
          <el-table-column label="托盘编号" prop="fTrayCode" align="center" width="150" fixed="left">
            <template slot-scope="scope">
              <div class="tray-id">{{ scope.row.fTrayCode }}</div>
            </template>
          </el-table-column>
          <el-table-column label="设备编号" prop="deviceCode" align="center" width="150" fixed="left">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.deviceCode }}</div>
            </template>
          </el-table-column>
<!--          <el-table-column label="行" prop="row" align="center" width="80" />-->
<!--          <el-table-column label="列" prop="col" align="center" width="80" />-->
          <el-table-column label="位置编号" prop="number" align="center" width="120" >
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.number }}</div>
            </template>
          </el-table-column>
          <el-table-column label="绑定时间" align="center" width="150">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.bindingTime ? formatDate(scope.row.bindingTime) : '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" width="120">
            <template slot-scope="scope">
              <el-tag
                :type="statusTagType(scope.row.status)"
                size="mini"
                class="status-tag"
              >
                {{ statusLabel(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="写时间状态" align="center" width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.writeTimeStatus === '1'" class="write-success">成功</span>
              <span v-else-if="scope.row.writeTimeStatus === '0'" class="write-failed">失败</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>
          <el-table-column label="时间运行状态" align="center" width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.runTimeStatus === '1'" class="write-success">正常</span>
              <span v-else-if="scope.row.runTimeStatus === '0'" class="write-failed">异常</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>
          <el-table-column label="浓度" prop="concentration" align="center" width="100">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.concentration || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="实时AD" align="center" width="120">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.realTimeAd || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="实时AD状态" align="center" width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.realTimeStatus === '1'" class="write-success">正常</span>
              <span v-else-if="scope.row.realTimeStatus === '0'" class="write-failed">异常</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>
<!--          <el-table-column label="零点AD" align="center" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <div class="device-code">{{ scope.row.adjustmentZeroAd || '-' }}</div>-->
<!--            </template>-->
<!--          </el-table-column>-->
<!--          <el-table-column label="零点AD状态" align="center" width="150">-->
<!--            <template slot-scope="scope">-->
<!--              <span v-if="scope.row.zeroStatus === '1'" class="write-success">正常</span>-->
<!--              <span v-else-if="scope.row.zeroStatus === '0'" class="write-failed">异常</span>-->
<!--              <span v-else class="write-unknown">-</span>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="标定浓度值" align="center" width="150">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.calibrationConcentration || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="标定状态" align="center" width="120">
            <template slot-scope="scope">
              <span :class="getCalibrationClass(scope.row.calibrationConcentrationStatus)">
                {{ getCalibrationText(scope.row.calibrationConcentrationStatus) }}
              </span>
            </template>
          </el-table-column>

          <el-table-column label="写自检状态" align="center" width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.writeSelfCheckStatus === 1" class="write-success">成功</span>
              <span v-else-if="scope.row.writeSelfCheckStatus === 0" class="write-failed">失败</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>
          <el-table-column label="继电器状态" align="center" width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.relayStatus === 0" class="write-failed">初始</span>
              <span v-else-if="scope.row.relayStatus === 1" class="write-success">动作</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>
          <el-table-column label="脉冲状态" align="center" width="120">
            <template slot-scope="scope">
              <span v-if="scope.row.pulseStatus === 0" class="write-failed">初始</span>
              <span v-else-if="scope.row.pulseStatus === 1" class="write-success">动作</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>
          <el-table-column label="模块状态" align="center" width="120">
            <template slot-scope="scope">
              <span v-if="scope.row.moduleStatus === 1" class="write-success">正常</span>
              <span v-else-if="scope.row.moduleStatus === 0" class="write-failed">异常</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>
          <el-table-column label="SIM卡状态" align="center" width="150">
            <template slot-scope="scope">
              <span v-if="scope.row.simCardStatus === 1" class="write-success">正常</span>
              <span v-else-if="scope.row.simCardStatus === 0" class="write-failed">异常</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>
          <el-table-column label="网络状态" align="center" width="120">
            <template slot-scope="scope">
              <span v-if="scope.row.networkStatus === 1" class="write-success">正常</span>
              <span v-else-if="scope.row.networkStatus === 0" class="write-failed">异常</span>
              <span v-else class="write-unknown">-</span>
            </template>
          </el-table-column>

          <el-table-column label="通信年" prop="recordYear" align="center" width="100">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.recordYear || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="通信月" prop="recordMonth" align="center" width="100">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.recordMonth || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="通信日" prop="recordDate" align="center" width="100">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.recordDate || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="通信时" prop="recordHour" align="center" width="100">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.recordHour || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column label="通信分" prop="recordMinute" align="center" width="100">
            <template slot-scope="scope">
              <div class="device-code">{{ scope.row.recordMinute || '-' }}</div>
            </template>
          </el-table-column>
        </el-table>
      </div>

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

<script>
import { listBinding } from "@/api/palletDeviceBinding/binding";
import { formatDate } from "@/utils/index";

export default {
  name: "RealTimeData",
  data() {
    return {
      // 表格数据
      list: [],
      total: 0,
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fTrayCode: "",
        deviceCode: "",
        status: "",
        bindingTime: ""
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    // 表格头部样式
    headerCellStyle() {
      return {
        'background': 'linear-gradient(to bottom, rgba(10, 35, 80, 0.9), rgba(8, 28, 65, 0.9))',
        'color': '#a0d0ff',
        'font-size': '16px',
        'font-weight': 'bold',
        'border-bottom': '1px solid rgba(64, 158, 255, 0.5)',
        'height': '60px',
        'padding': '0 15px'
      }
    },
    // 表格单元格样式
    cellStyle() {
      return {
        'border-bottom': '1px solid rgba(64, 158, 255, 0.2)',
        'padding': '16px 15px',
        'font-size': '15px',
        'transition': 'all 0.3s ease'
      }
    }
  },
  methods: {
    // 获取标定状态文本
    getCalibrationText(status) {
      if (status === '4') {
        return '正常'
      } else if (status) {
        return '异常'
      } else {
        return '-'
      }
    },

    // 获取标定状态样式类
    getCalibrationClass(status) {
      if (status === '4') {
        return 'write-success'
      } else if (status) {
        return 'write-failed'
      } else {
        return 'write-unknown'
      }
    },
    /** 获取数据列表 */
    getList() {
      this.loading = true;
      listBinding(this.queryParams).then(response => {
        this.list = response.rows || [];
        this.total = response.total || 0;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
        this.$message.error("数据加载失败，请重试");
      });
    },

    /** 状态标签类型映射 */
    statusTagType(status) {
      switch (status) {
        case "0":
          return "info"; // 预热-信息蓝
        case "1":
          return "success"; // 正常-成功绿
        case "3":
          return "warning"; // 传感器故障-警告黄
        case "4":
          return "danger"; // 报警-危险红
        case "5":
          return ""; // 通讯故障-使用自定义灰色
        default:
          return "";
      }
    },

    /** 状态中文映射 */
    statusLabel(status) {
      const statusMap = {
        "0": "预热",
        "1": "正常",
        "3": "传感器故障",
        "4": "报警",
        "5": "通讯故障"
      };
      return statusMap[status] || "-";
    },

    /** 格式化通信时间（年-月-日 时:分） */
    formatRecordTime(row) {
      const { recordYear, recordMonth, recordDate, recordHour, recordMinute } = row;
      if (recordYear && recordMonth && recordDate) {
        return `${recordYear}-${recordMonth.padStart(2, '0')}-${recordDate.padStart(2, '0')} ${recordHour.padStart(2, '0')}:${recordMinute.padStart(2, '0')}`;
      }
      return "-";
    },

    /** 日期格式化（兼容字符串和Date对象） */
    formatDate(date) {
      return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    },

    /** 处理查询 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置查询条件 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        fTrayCode: "",
        deviceCode: "",
        status: "",
        bindingTime: ""
      };
      this.getList();
    },

    /** 每页条数改变 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },

    /** 当前页改变 */
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    }
  }
};
</script>

<style scoped>
/* 主容器样式 */
.tray-container {
  width: 100%;
  height: 100%;
  min-height: 100%;
  background: rgba(10, 20, 40, 0.85);
  border-radius: 12px;
  padding: 20px;
  border: 1px solid rgba(64, 158, 255, 0.3);
  box-shadow: 0 0 20px rgba(0, 50, 120, 0.3);
  box-sizing: border-box;
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}

/* 标题区域样式 */
.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid rgba(64, 158, 255, 0.3);
  width: 100%;
}

.title-with-status {
  display: flex;
  align-items: center;
  gap: 15px;
}

.title-left {
  display: flex;
  flex-direction: column;
}

.title-text {
  font-size: 22px;
  font-weight: bold;
  color: #ffffff;
  letter-spacing: 1px;
  text-shadow: 0 0 10px rgba(64, 158, 255, 0.5);
}

.title-line {
  height: 3px;
  width: 100px;
  background: linear-gradient(to right, #409EFF, transparent);
  margin-top: 8px;
  border-radius: 2px;
}

/* 搜索区域样式 */
.search-box {
  display: flex;
  gap: 10px;
  width: auto;
  align-items: center;
  flex-wrap: wrap;
}

.search-input {
  min-width: 150px;
  width: 180px;
}

.search-input >>> .el-input__inner {
  background: rgba(10, 30, 70, 0.6);
  border: 1px solid #409EFF;
  color: #ffffff;
  height: 40px;
  border-radius: 4px;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.3) inset;
}

.search-input >>> .el-input__inner::placeholder {
  color: rgba(200, 200, 255, 0.5);
}

.search-button {
  background: linear-gradient(to right, #409EFF, #64c8ff);
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  display: flex;
  align-items: center;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  transition: all 0.3s;
  height: 40px;
}

.reset-button {
  background: linear-gradient(to right, #67c23a, #85ce61);
}

.search-button:hover {
  background: linear-gradient(to right, #64c8ff, #409EFF);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.4);
}

.reset-button:hover {
  background: linear-gradient(to right, #85ce61, #67c23a);
}

.search-button i {
  margin-right: 5px;
}

/* 表格容器样式 */
.tray-table-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid rgba(64, 158, 255, 0.3);
  box-shadow: 0 0 20px rgba(0, 153, 255, 0.15);
  margin-top: 10px;
}

/* 表格滚动容器 - 关键修改：只保留外部滚动条 */
.table-scroll-container {
  flex: 1;
  overflow: auto; /* 同时支持横向和纵向滚动 */
  position: relative;
  width: 100%;
}

/* 自定义滚动条样式 */
.table-scroll-container::-webkit-scrollbar {
  width: 8px;
  height: 8px; /* 横向滚动条高度 */
}

.table-scroll-container::-webkit-scrollbar-track {
  background: rgba(10, 35, 80, 0.5);
  border-radius: 4px;
}

.table-scroll-container::-webkit-scrollbar-thumb {
  background: rgba(64, 158, 255, 0.5);
  border-radius: 4px;
  border: 1px solid rgba(64, 158, 255, 0.2);
}

.table-scroll-container::-webkit-scrollbar-thumb:hover {
  background: rgba(64, 158, 255, 0.8);
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.5);
}

.table-scroll-container::-webkit-scrollbar-corner {
  background: rgba(10, 35, 80, 0.5);
}

/* 表格样式 - 关键修改：去掉内部滚动条 */
.tray-table >>> .el-table {
  background: transparent;
  color: #e0f0ff;
  border-radius: 10px 10px 0 0;
  overflow: hidden;
  width: 100%;
}

/* 关键修改：隐藏表格内部的滚动条 */
/*.tray-table >>> .el-table__body-wrapper {*/
/*  overflow: hidden !important; !* 隐藏内部滚动条 *!*/
/*}*/

/*.tray-table >>> .el-table__header-wrapper {*/
/*  overflow: hidden !important; !* 隐藏表头滚动条 *!*/
/*}*/

.tray-table >>> .el-table th {
  background: linear-gradient(to bottom, rgba(10, 35, 80, 0.9), rgba(8, 28, 65, 0.9));
  color: #a0d0ff;
  font-size: 16px;
  font-weight: bold;
  border-bottom: 1px solid rgba(64, 158, 255, 0.5);
  height: 60px;
  position: relative;
  box-shadow: 0 1px 0 rgba(64, 158, 255, 0.5);
}

.tray-table >>> .el-table tr {
  background: rgba(8, 28, 65, 0.5);
  transition: all 0.3s ease;
}

.tray-table >>> .el-table tr:hover {
  background: rgba(15, 45, 90, 0.7) !important;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 100, 255, 0.2);
}

.tray-table >>> .el-table td {
  border-bottom: 1px solid rgba(64, 158, 255, 0.2);
  padding: 16px 15px;
  font-size: 15px;
  white-space: nowrap; /* 防止文字换行 */
}

.tray-table >>> .el-table--border {
  border: none;
}

.tray-table >>> .el-table--border th,
.tray-table >>> .el-table--border td {
  border-right: 1px solid rgba(64, 158, 255, 0.2);
}

.tray-table >>> .el-table--border th:last-child,
.tray-table >>> .el-table--border td:last-child {
  border-right: none;
}

.tray-table >>> .el-table--border::after,
.tray-table >>> .el-table--group::after,
.tray-table >>> .el-table::before {
  background-color: rgba(64, 158, 255, 0.3);
}

/* 固定列样式 */
/*.tray-table >>> .el-table__fixed,*/
/*.tray-table >>> .el-table__fixed-right {*/
/*  background: rgba(8, 28, 65, 0.8);*/
/*}*/

/*.tray-table >>> .el-table__fixed-right-patch {*/
/*  background: linear-gradient(to bottom, rgba(10, 35, 80, 0.9), rgba(8, 28, 65, 0.9));*/
/*}*/

/* 表格内容样式 */
.tray-id, .device-code {
  font-weight: bold;
  color: #6dc6ff;
}

.binding-time, .record-time {
  color: #a0d0ff;
}

.concentration {
  font-weight: bold;
  color: #ffcf5c;
  text-shadow: 0 0 8px rgba(255, 207, 92, 0.3);
}

.adjustment-zero, .calibration-ad {
  color: #a0d0ff;
  font-family: 'Courier New', monospace;
}

.record-year, .record-month, .record-date, .record-hour, .record-minute {
  color: #a0d0ff;
  text-align: center;
}

.write-success {
  color: #67c23a;
  font-weight: bold;
}

.write-failed {
  color: #f56c6c;
  font-weight: bold;
}

.write-unknown {
  color: #a0d0ff;
}

.status-tag {
  font-weight: bold;
  border-radius: 12px;
  padding: 0 12px;
  height: 28px;
  line-height: 28px;
  min-width: 80px;
  text-align: center;
  box-shadow: 0 0 8px rgba(64, 158, 255, 0.3);
}

/* 通讯故障状态特殊样式 */
.status-tag:not([class*="el-tag--"]) {
  background-color: #c0c4cc;
  color: #fff;
  border-color: #c0c4cc;
}

/* 分页样式 */
.pagination-container {
  background: rgba(10, 35, 80, 0.7);
  padding: 15px 20px;
  border-top: 1px solid rgba(64, 158, 255, 0.3);
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-shrink: 0; /* 防止分页被压缩 */
}

.pagination-container >>> .el-pagination {
  color: #a0d0ff;
}

.pagination-container >>> .el-pagination__total {
  color: #a0d0ff;
  margin-right: 15px;
}

.pagination-container >>> .el-pagination button {
  background: transparent;
  color: #a0d0ff;
  border: 1px solid rgba(64, 158, 255, 0.3);
  border-radius: 6px;
  min-width: 32px;
  height: 32px;
  margin: 0 3px;
  transition: all 0.3s ease;
}

.pagination-container >>> .el-pagination button:hover {
  background: rgba(64, 158, 255, 0.2);
  color: #6dc6ff;
  border-color: rgba(64, 158, 255, 0.5);
}

.pagination-container >>> .el-pager li {
  background: transparent;
  color: #a0d0ff;
  min-width: 32px;
  height: 32px;
  line-height: 32px;
  margin: 0 3px;
  border-radius: 6px;
  border: 1px solid rgba(64, 158, 255, 0.3);
  transition: all 0.3s ease;
}

.pagination-container >>> .el-pager li:hover {
  color: #6dc6ff;
  background: rgba(64, 158, 255, 0.1);
  border-color: rgba(64, 158, 255, 0.5);
}

.pagination-container >>> .el-pager li.active {
  background: linear-gradient(135deg, #409EFF 0%, #1a73e8 100%);
  color: white;
  border: none;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.5);
}

.pagination-container >>> .el-pagination__sizes {
  margin-right: 15px;
}

.pagination-container >>> .el-pagination__sizes .el-input .el-input__inner {
  background: rgba(10, 30, 70, 0.6);
  border: 1px solid rgba(64, 158, 255, 0.3);
  color: #e0f0ff;
  border-radius: 6px;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .search-input {
    width: 160px;
  }

  .table-scroll-container {
    max-height: calc(100vh - 300px);
  }
}

@media (max-width: 800px) {
  .panel-title {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .search-box {
    width: 100%;
    justify-content: flex-start;
  }

  .search-input {
    width: calc(50% - 5px);
    min-width: auto;
  }

  .tray-table >>> .el-table th,
  .tray-table >>> .el-table td {
    padding: 12px 8px;
    font-size: 14px;
  }

  .table-scroll-container {
    max-height: calc(100vh - 340px);
  }
}

@media (max-width: 500px) {
  .title-text {
    font-size: 18px;
  }

  .title-line {
    width: 80px;
  }

  .pagination-container {
    padding: 10px;
  }

  .pagination-container >>> .el-pagination {
    font-size: 12px;
  }

  .pagination-container >>> .el-pagination__total {
    display: none;
  }

  .tray-table >>> .el-table th,
  .tray-table >>> .el-table td {
    padding: 8px 4px;
    font-size: 13px;
  }

  .status-tag {
    padding: 0 8px;
    min-width: 60px;
    font-size: 12px;
  }

  .table-scroll-container {
    max-height: calc(100vh - 320px);
  }
}
</style>
