<template>
  <div class="tray-container">
    <div class="tray-header">
      <div class="tray-title">
        <div class="title-text">托盘信息管理</div>
        <div class="title-line"></div>
      </div>

      <div class="search-box">
        <el-input
          class="search-input"
          v-model="searchKeyword"
          placeholder="请输入托盘编号"
          clearable
          @keyup.enter.native="searchTray"
        >
          <i slot="prefix" class="el-icon-search"></i>
        </el-input>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="searchTray"
          class="search-button"
        >搜索</el-button>
      </div>
    </div>

    <div class="tray-table-container">
      <el-table
        :data="filteredTrays"
        border
        style="width: 100%"
        @row-click="handleRowClick"
        :row-class-name="tableRowClassName"
        class="tray-table"
      >
        <el-table-column prop="trayId" label="托盘编号" width="200" align="center">
          <template slot-scope="scope">
            <div class="tray-id">{{ scope.row.fTrayCode }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="150" align="center">
          <template slot-scope="scope">
            <el-tag :type="statusTagType(scope.row.status)" class="status-tag">
              {{ scope.row.statusStr }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="boardCount" label="主板数量" width="150" align="center">
          <template slot-scope="scope">
            <div class="board-count">
              <span class="count-number">{{ 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 class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalTrays"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>

import {queryByDepartmentId} from "@/api/storey/storey";
export default {
  name: "TrayInformation",
  data() {
    return {
      searchKeyword: '',
      trays: [],
      currentPage: 1,
      pageSize: 10
    };
  },
  created() {
    this.fetchTrays();
  },
  computed: {
    // 计算过滤后的托盘数据
    filteredTrays() {
      let filtered = this.trays;
      if (this.searchKeyword) {
        filtered = filtered.filter(tray =>
          tray.trayId.includes(this.searchKeyword)
        );
      }
      // 分页处理
      const start = (this.currentPage - 1) * this.pageSize;
      const end = start + this.pageSize;
      return filtered.slice(start, end);
    },
    totalTrays() {
      if (this.searchKeyword) {
        return this.trays.filter(tray =>
          tray.trayId.includes(this.searchKeyword)
        ).length;
      }
      return this.trays.length;
    }
  },
  methods: {
    // 模拟API请求
    fetchTrays() {
      // 实际项目中替换为真实的API请求
      // 0 空闲
      // 1 运行中
      // 2 标检完成
      setTimeout(() => {
        this.trays = [
          {fTrayId:1,fTrayCode: 'TP20240001', status: '2',statusStr:"标检完成", boardCount: 0, location: 'A区-1号架'},
          {fTrayId:2,fTrayCode: 'TP20240002', status: '0',statusStr:"空闲",  boardCount: 8, location: 'B区-3号架'},
          {fTrayId:3,fTrayCode: 'TP20240003', status: '1',statusStr:"运行中",  boardCount: 12, location: 'C区-2号架'},
          {fTrayId:4,fTrayCode: 'TP20240004', status: '2',statusStr:"标检完成",  boardCount: 0, location: '维修区'},
          {fTrayId:5,fTrayCode: 'TP20240005', status: '0',statusStr:"空闲",  boardCount: 0, location: 'D区-4号架'},
          {fTrayId:6,fTrayCode: 'TP20240006', status: '1',statusStr:"运行中",  boardCount: 5, location: 'E区-1号架'},
          {fTrayId:7,fTrayCode: 'TP20240007', status: '2',statusStr:"标检完成",  boardCount: 12, location: 'F区-2号架'},
          {fTrayId:8,fTrayCode: 'TP20240008', status: '0',statusStr:"空闲",  boardCount: 0, location: 'G区-5号架'},
          {fTrayId:9,fTrayCode: 'TP20240009', status: '1',statusStr:"运行中",  boardCount: 10, location: 'H区-3号架'},
          {fTrayId:10,fTrayCode: 'TP20240110', status: '2',statusStr:"标检完成",  boardCount: 12, location: 'I区-1号架'}
        ];
      }, 500);
    },

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

    // 处理行点击
    handleRowClick(row) {
      // 触发事件传递 cabinetId 给父组件
      // 4 是 TrayBinding 的组件
      this.$emit('cabinet-click', row,4);
    },

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

    // 状态标签类型
    // 0 空闲
    // 1 运行中
    // 2 标检完成
    statusTagType(status) {
      const statusMap = {
        '0': 'success',
        '1': 'primary',
        '2': 'danger'
      };
      return statusMap[status] || 'info';
    },

    // 分页处理
    handleSizeChange(size) {
      this.pageSize = size;
      this.currentPage = 1;
    },

    handleCurrentChange(page) {
      this.currentPage = page;
    }
  }
}
</script>

<style scoped>
.tray-container {
  padding: 20px;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", Arial, sans-serif;
  background: rgba(4, 18, 57, 0.7);
  border-radius: 8px;
  box-shadow: 0 0 20px rgba(0, 153, 255, 0.2);
  border: 1px solid rgba(64, 158, 255, 0.3);
}

.tray-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid rgba(64, 158, 255, 0.3);
}

.tray-title {
  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;
  max-width: 500px;
}

.search-input {
  flex: 1;
  margin-right: 15px;
}

.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 {
  height: 40px;
  padding: 0 25px;
  background: linear-gradient(90deg, #409EFF, #1a73e8);
  border: none;
  border-radius: 4px;
  font-weight: bold;
  letter-spacing: 1px;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.5);
  transition: all 0.3s ease;
}

.search-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(64, 158, 255, 0.7);
}

.tray-table-container {
  margin-top: 15px;
}

.tray-table >>> .el-table {
  background: transparent;
  color: #e0f0ff;
}

.tray-table >>> .el-table th {
  background: rgba(10, 35, 80, 0.8);
  color: #a0d0ff;
  font-size: 16px;
  font-weight: bold;
  border-bottom: 1px solid #409EFF;
}

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

.tray-table >>> .el-table tr:hover {
  background: rgba(15, 45, 90, 0.7) !important;
}

.tray-table >>> .el-table td {
  border-bottom: 1px solid rgba(64, 158, 255, 0.2);
  padding: 12px 0;
}

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

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

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

.count-number {
  font-size: 18px;
  font-weight: bold;
  color: #ffcf5c;
  margin-right: 5px;
}

.count-label {
  color: #a0d0ff;
}

.location-text {
  color: #a0d0ff;
}

.pagination {
  margin-top: 25px;
  display: flex;
  justify-content: flex-end;
}

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

.pagination >>> .el-pagination button,
.pagination >>> .el-pager li {
  background: rgba(10, 35, 80, 0.6);
  color: #a0d0ff;
  border: 1px solid rgba(64, 158, 255, 0.3);
  margin: 0 3px;
  border-radius: 4px;
}

.pagination >>> .el-pagination button:hover,
.pagination >>> .el-pager li:hover {
  color: #409EFF;
}

.pagination >>> .el-pager li.active {
  background: linear-gradient(90deg, #409EFF, #1a73e8);
  color: #fff;
  border: none;
}

.clickable-row {
  cursor: pointer;
}

.clickable-row:hover {
  background-color: rgba(64, 158, 255, 0.1) !important;
}
</style>
