<template>
  <div class="app-container-con">
    <el-form :model="queryParams" ref="queryFormRef" inline label-width="auto" size="small">
      <!-- 表单搜索 -->
      <el-card class="search-box">
        <el-row>
          <el-col :span="18">
            <el-form-item prop="createUser" label="申请人：">
            <el-input clearable style="width: 240px" placeholder="请输入关键字" v-model.trim="queryParams.createUser" />
          </el-form-item>

          <el-form-item label="申请时间" prop="createTime">
            <el-date-picker type="daterange" style="width: 100%" unlink-panels range-separator="至"
              start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd"
              v-model="queryParams.createTime" />
          </el-form-item>

          <el-form-item label="审批状态" prop="status">
            <el-select clearable style="width: 100%" placeholder="请选择审批状态" v-model="queryParams.status">
              <el-option label="待审批" value="0" />
              <el-option label="已审批" value="1" />
              <el-option label="草稿" value="3" />
            </el-select>
          </el-form-item>
          </el-col>
          <el-col :span="6" style="text-align: right;">
            <el-button class="primary-lease" @click="resetForm"> 
              <i class="el-icon-refresh"></i>
              重置 
            </el-button>
            <el-button class="primary-lease" style="margin-right: 0;" type="primary" @click="getEquipmentApplyList">
              <i class="el-icon-search"></i>
              查询
            </el-button>
          </el-col>
        </el-row>
      </el-card>

      <el-card class="content-box">
        <el-row>
          <el-col>
            <el-button class="primary-lease" type="primary" @click="addEquipment()" style="margin-right: 0px">
              装备录入
            </el-button>

            <el-button class="primary-lease" type="primary" @click="showDetails('1')" style="margin-right: 0px">
              展开明细
            </el-button>
          </el-col>
        </el-row>

        <!-- 表格区域 -->
        <div class="table-container">
          <el-table :data="tableData" border stripe height="100%">
            <el-table-column label="序号" align="center" width="55" type="index" />
            <el-table-column prop="orderNumber" label="录入单号" align="center" />
            <el-table-column prop="devCount" label="申请录入数量" align="center" />
            <el-table-column prop="createUser" label="申请人" align="center" />
            <el-table-column prop="createTime" label="申请时间" align="center" />
            <el-table-column prop="status" align="center" label="审批状态">
              <template slot-scope="{ row }">
                <el-tag v-if="row.status == 0" size="small" type="warning">审批中</el-tag>
                <el-tag v-if="row.status == 1" size="small" type="success">已审批</el-tag>
                <el-tag v-if="row.status == 3" size="small" type="info">草稿</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="agree" label="已通过数量" align="center" />
            <el-table-column prop="reject" label="已驳回数量" align="center" />
            <el-table-column label="操作" align="center" min-width="100">
              <template slot-scope="{ row }">
                <el-button type="text" size="mini" @click="viewEquipment(row.id)">
                  <i class="el-icon-view"></i> 查看
                </el-button>
                <el-button type="text" size="mini" @click="editEquipment(row.id)" v-if="row.status === '3'">
                  <i class="el-icon-edit"></i> 编辑
                </el-button>
                <el-button type="text" size="mini" @click="deleteItem(row)" v-if="row.status === '3'">
                  <i class="el-icon-delete"></i> 删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!-- 分页 -->
        <div class="pagination-wrapper">
          <pagination :total="total" @pagination="handlePageChange" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" />
        </div>
      </el-card>
    </el-form>
  </div>
</template>

<script>
import { getEquipmentApplyListApi, equipmentDelApiNew } from '@/api/EquipmentEntryApply/index'

export default {
  name: 'EquipmentMainList',

  data() {
    return {
      total: 0,
      orderId: '',
      isVisible: false,
      isAddVisible: false,
      queryParams: {
        createUser: '',
        createTime: '',
        status: '',
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],

      totalDetails: 0,
      orderIdDetails: '',
      isVisibleDetails: false,
      isAddVisibleDetails: false,
      queryParamsDetails: {
        status: '',
        applyTime: '',
        pageNum: 1,
        pageSize: 10,
      },
      tableDataDetails: [],
      isDetails: '0',
    }
  },
  mounted() {
    this.getEquipmentApplyList()
  },
  methods: {
    /**
     * 获取装备申请列表
     */
    getEquipmentApplyList() {
      this.queryParams.startCreateTime = this.queryParams.createTime ? this.queryParams.createTime[0] : ''
      this.queryParams.endCreateTime = this.queryParams.createTime ? this.queryParams.createTime[1] : ''
      const params = { ...this.queryParams }
      delete params.createTime
      getEquipmentApplyListApi(params)
        .then((res) => {
          this.tableData = res.data.rows
          this.total = res.data.total
        })
        .catch((error) => {
          console.error('获取装备申请列表失败:', error)
        })
    },

    /**
     * 分页变化处理
     */
    handlePageChange() {
      this.getEquipmentApplyList()
    },

    /**
     * 重置表单
     */
    resetForm() {
      this.$refs.queryFormRef.resetFields()
      this.queryParams.pageNum = 1
      this.getEquipmentApplyList()
    },

    /**
     * 删除项目
     */
    deleteItem(row) {
      this.$confirm('是否确定删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => equipmentDelApiNew({ id: row.id }))
        .then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.getEquipmentApplyList()
          } else {
            this.$message.error('删除失败')
          }
        })
        .catch((error) => {
          if (error !== 'cancel') {
            // 排除取消操作的错误
            console.error('删除失败:', error)
            this.$message.error('删除失败，请重试')
          }
        })
    },

    //展示明细
    showDetails(num) {
      this.$emit('switch-view', num)
    },

    // 查看模式
    viewEquipment(orderId) {
      this.$router.push({
        name: 'equipmentInput',
        query: {
          orderId: orderId,
          isAddVisible: true,
          isApprovalVisible: false,
        },
      })
    },

    // 编辑模式
    editEquipment(orderId) {
      this.$router.push({
        name: 'equipmentInput',
        query: {
          orderId: orderId,
          isAddVisible: false,
          isApprovalVisible: false,
        },
      })
    },

    // 新增模式
    addEquipment() {
      this.$router.push({
        name: 'equipmentInput',
        query: {
          orderId: '',
          isAddVisible: false,
          isApprovalVisible: false,
        },
      })
    },
  },
}
</script>

<style scoped lang="scss">
.view-btn {
  color: #409eff;
  font-size: 14px;
  font-weight: bold;
}

.edit-btn {
  color: #67c23a;
  font-size: 12px;
  font-weight: bold;
}

.delete-btn {
  color: #f56c6c;
  font-size: 16px;
  font-weight: bold;
}

.app-container-con {
  height: calc(100vh - 84px);
  // padding: 20px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  overflow-y: hidden;
}

::v-deep .el-pagination {
  margin-top: 15px;
  text-align: right;
}

::v-deep .el-button {
  margin-right: 8px;
}

.search-box {
  margin-bottom: 20px;
  border-radius: 8px;
}

.content-box {
  border-radius: 8px;
  height: calc(100vh - 230px);
  display: flex;
  flex-direction: column;
  overflow: hidden;

  ::v-deep .el-card__body {
    display: flex !important;
    flex-direction: column !important;
    height: 100% !important;
    padding: 20px;
  }

  .el-row:first-child {
    margin-bottom: 16px;
    flex-shrink: 0;

    .el-col {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      gap: 12px;
    }
  }

  .table-container {
    flex: 1;
    overflow: hidden;
    margin-bottom: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  .pagination-wrapper {
    flex-shrink: 0;
    padding-top: 6px;
    margin-top: auto;

    ::v-deep  .pagination-container {
      padding: 0px 20px !important;
      margin-bottom: 30px;
    }
  }

  ::v-deep .el-table {
    // 启用斑马纹
    &.el-table--striped .el-table__body {
      tr.el-table__row--striped td {
        background-color: #F6FBFA !important; // 浅紫色
      }
    }

    .el-table__header {
      background: #E9F0EE;

      th {
        background: #E9F0EE !important;
        color: #606266;
        font-weight: 600;
        height: 50px;
      }
    }

    &.el-table--striped .el-table__body tr.el-table__row:hover>td.el-table__cell {
      background-color: #CCF1E9 !important;
    }
  }
}
</style>
