<template>
  <div class="app-container">
    <template>
      <el-form
        :model="queryParams"
        ref="queryFormRef"
        inline
        label-width="auto"
        size="small"
      >
        <!-- 表单搜索 -->
        <el-row>
          <el-col :span="5">
            <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="2"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="申请时间" prop="applyTime">
              <el-date-picker
                type="date"
                style="width: 100%"
                value-format="YYYY-MM-DD"
                placeholder="请选择申请时间"
                v-model="queryParams.applyTime"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5" style="float:right;">
            <el-form-item>
              <el-button class="primary-lease" type="primary" @click="getEquipmentApplyList">
                查询
              </el-button>
              <el-button class="primary-lease" type="primary" @click="resetForm">
                重置
              </el-button>
              <el-button
                class="primary-lease"
                type="primary"
                @click="showEquipmentInput()"
              >
                装备录入
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 表格 -->
        <el-table :data="tableData">
          <el-table-column label="序号" align="center" width="55" type="index"/>
          <el-table-column prop="createUser" label="申请人" align="center"/>
          <el-table-column prop="createTime" label="申请时间" align="center"/>
          <el-table-column prop="devCount" 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="info">待审批</el-tag>
              <el-tag v-if="row.status == 1" size="small" type="warning">已审批</el-tag>
              <el-tag v-if="row.status == 3" size="small" type="success">草稿</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="{ row }">
              <el-button type="primary" size="small" @click="showEquipmentInput(row.id, true)">
                查看
              </el-button>
              <el-button
                type="primary"
                size="small"
                @click="showEquipmentInput(row.id, false)"
                v-if="row.status == '3'"
              >
                编辑
              </el-button>
<!--              <el-button type="primary" size="small"  v-if="row.status == '0'" @click="deleteItem(row)">
                删除
              </el-button>-->
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <pagination
          :total="total"
          @pagination="handlePageChange"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
        />
      </el-form>
    </template>
    <EquipmentEntryDialog
      :is-visible.sync="isVisible"
      :order-id="orderId"
      :is-add-visible="isAddVisible"
      :is-approval-visible="true"
      @submit="getEquipmentApplyList"
    />
  </div>
</template>

<script>
import { getEquipmentApplyListApi, equipmentDelApiNew } from '@/api/EquipmentEntryApply/index'
import EquipmentEntryDialog from '@/views/EquipmentEntryApply/equipmentInput/index1.vue'

export default {
  components: {
    EquipmentEntryDialog
  },
  data() {
    return {
      total: 0,
      orderId: '',
      isVisible: false,
      isAddVisible: false,
      queryParams: {
        status: '',
        applyTime: '',
        pageNum: 1,
        pageSize: 10
      },
      tableData: []
    }
  },
  mounted() {
    this.getEquipmentApplyList()
  },
  methods: {
    /**
     * 获取装备申请列表
     */
    getEquipmentApplyList() {
      getEquipmentApplyListApi(this.queryParams)
        .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()
    },

    /**
     * 显示装备录入/编辑/查看界面
     */
    showEquipmentInput(orderId, isAddVisible) {
      this.orderId = orderId
      this.isVisible = true
      this.isAddVisible = isAddVisible

    },

    /**
     * 删除项目
     */
    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('删除失败，请重试')
          }
        })
    }

  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 20px;
}

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

::v-deep .el-button {
  margin-right: 8px;
}
</style>
