<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
          @keyup.enter.native="searchTray"
        >
        </el-input>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchTray"
          class="search-button"
        >搜索</el-button>
      </div>
    </div>

    <!-- 表格内容区 -->
    <div class="tray-table-container">
      <!-- 带滚动条的表格容器 -->
      <div class="table-scroll-container">
        <el-table
          :data="filteredTrays"
          border
          style="width: 100%"
          @row-click="handleRowClick"
          :row-class-name="tableRowClassName"
          class="tray-table"
          :header-cell-style="headerCellStyle"
          :cell-style="cellStyle"
          height="100%"
        >
          <el-table-column prop="trayId" label="托盘编号" align="center">
            <template slot-scope="scope">
              <div class="tray-id">{{ scope.row.fTrayCode }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态" align="center">
            <template slot-scope="scope">
              <el-tag :type="statusTagType(scope.row.fStatus)" v-if="scope.row.fStatus === '1'" class="status-tag">运行中</el-tag>
              <el-tag :type="statusTagType(scope.row.fStatus)" v-else class="status-tag">空闲</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="boardCount" label="主板数量" align="center">
            <template slot-scope="scope">
              <div class="board-count">
                <span class="count-number">{{ (scope.row.boardCount == null || scope.row.boardCount == 0) ? '0' : scope.row.boardCount }}</span>
                <span class="count-label">块</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="location" label="位置" align="center">
            <template slot-scope="scope">
              <div class="location-text">{{ scope.row.location }}</div>
            </template>
          </el-table-column>
        </el-table>
      </div>

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

<script>
import { listTray } from "@/api/tray/tray"
export default {
  name: "TrayInformation",
  data() {
    return {
      searchKeyword: '',
      trays: [],

      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fTrayCode: undefined,
      },
    };
  },
  created() {
    this.fetchTrays();
  },
  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',
        // 关键：固定表头的样式设置
        'position': 'sticky',
        'top': '0',
        'z-index': '3'
      }
    },
    // 表格单元格样式
    cellStyle() {
      return {
        'border-bottom': '1px solid rgba(64, 158, 255, 0.2)',
        'padding': '16px 15px',
        'font-size': '15px',
        'transition': 'all 0.3s ease'
      }
    },
    // 计算过滤后的托盘数据
    filteredTrays() {
      let filtered = this.trays;
      if (this.searchKeyword) {
        filtered = filtered.filter(tray =>
          tray.trayId.includes(this.searchKeyword)
        );
      }
      return filtered;
    },
  },
  methods: {
    // 获取托盘数据
    fetchTrays() {
      listTray(this.queryParams).then(response => {
        this.trays = response.rows;
        this.total = response.total;
        this.loading = false;
      })
    },

    // 搜索托盘
    searchTray() {
      this.queryParams.pageNum = 1;
      this.fetchTrays();
    },

    // 处理行点击
    handleRowClick(row) {
      this.$emit('cabinet-click', row, 4);
    },

    // 设置行样式
    tableRowClassName() {
      return 'clickable-row';
    },

    // 状态标签类型
    statusTagType(status) {
      const statusMap = {
        '0': 'success',
        '1': 'primary',
      };
      return statusMap[status] || 'info';
    },

    // 分页处理
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.queryParams.pageNum = 1;
      this.fetchTrays();
    },

    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.fetchTrays();
    },
  }
}
</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;
}

.search-input {
  min-width: 200px;
  width: 300px;
}

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

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

.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-y: auto;
  overflow-x: hidden;
  position: relative;
}

/* 自定义滚动条样式，与整体风格统一 */
.table-scroll-container::-webkit-scrollbar {
  width: 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-button {
  display: none;
}

/* 表格样式 */
.tray-table >>> .el-table {
  background: transparent;
  color: #e0f0ff;
  border-radius: 10px 10px 0 0;
  overflow: hidden;
  width: 100%;
}

/* 表头样式增强 - 确保固定效果 */
.tray-table >>> .el-table__header {
  overflow: visible !important;
}

.tray-table >>> .el-table__header-wrapper {
  overflow: visible !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: sticky !important;
  top: 0 !important;
  z-index: 3 !important;
  /* 防止表头边框在滚动时被遮挡 */
  box-shadow: 0 1px 0 rgba(64, 158, 255, 0.5);
}

/* 处理表格边框在滚动时的显示问题 */
.tray-table >>> .el-table__body-wrapper {
  position: relative;
  z-index: 1;
}

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

.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-id {
  font-weight: bold;
  color: #6dc6ff;
}

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

.board-count {
  display: flex;
  align-items: center;
  justify-content: center;
}

.count-number {
  font-size: 18px;
  font-weight: bold;
  color: #ffcf5c;
  margin-right: 5px;
  text-shadow: 0 0 8px rgba(255, 207, 92, 0.3);
}

.count-label {
  color: #a0d0ff;
}

.location-text {
  color: #a0d0ff;
}

.clickable-row {
  cursor: pointer;
}

/* 分页样式 */
.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;
}

.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: 250px;
  }

  .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: 100%;
    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>
