<template>
  <!-- 基础页面 -->
  <div class="app-container">
    <el-card v-show="showSearch" style="margin-bottom: 20px">
      <el-form :model="queryParams" ref="queryForm"  inline label-width="100px">
          <el-form-item label="项目名称" prop="proCode">
            <el-select
              clearable
              placeholder="请选择项目名称"
              v-model="queryParams.proCode"
              style="width: 200px"
            >
              <el-option
                :key="item.proCode"
                :label="item.proName"
                :value="item.proCode"
                v-for="item in useProjectList"
              />
            </el-select>
          </el-form-item>

        <el-form-item label="任务状态" prop="taskStatus">
          <el-select
            v-model="queryParams.taskStatus"
            placeholder="请选择任务状态"
            clearable
            filterable
            style="width: 200px"
          >
            <el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>


        <!-- 日期范围 -->
        <el-form-item label="申请日期">
          <el-date-picker
            v-model="timeRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            clearable
            unlink-panels
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            style="width: 200px"
          />
        </el-form-item>



        <!-- 表单按钮 -->
        <el-form-item style="float: right">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card>
      <el-row :gutter="10" class="mb8" justify="end">
        <el-col :span="4">
          <span style="font-size: 20px; font-weight: 800">自用申请列表</span>
        </el-col>
        <el-col :span="20" style="display: flex; justify-content: flex-end">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新建申请</el-button>
        </el-col>
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" /> -->
      </el-row>

      <el-table
        v-loading="isLoading"
        :data="tableList"
        highlight-current-row
        border
        stripe
        :max-height="650"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          width="55"
          label="序号"
          align="center"
          :index="(index) => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
        />
        <el-table-column label="申请单号" align="center" prop="code" :show-overflow-tooltip="true" />
        <el-table-column label="项目名称" align="center" prop="proName" :show-overflow-tooltip="true" />
        <el-table-column label="申请装备数" align="center" prop="devNum" :show-overflow-tooltip="true" width="120px"/>
        <el-table-column label="申请工具数" align="center" prop="toolNum" :show-overflow-tooltip="true" />
        <el-table-column label="任务状态" align="center" prop="taskStatus" :show-overflow-tooltip="true" >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.taskStatus == '0'" type="info">待审批</el-tag>
              <el-tag v-if="scope.row.taskStatus == '1'" type="warning">审批中</el-tag>
              <el-tag v-if="scope.row.taskStatus == '2'" type="success">已审批</el-tag>
              <el-tag v-if="scope.row.taskStatus == '3'" type="danger">审批驳回</el-tag>
            </template>
        </el-table-column>
        <el-table-column label="申请人" align="center" prop="createBy" :show-overflow-tooltip="true" width="140px"/>
        <el-table-column label="申请时间" align="center" prop="createTime"  width="160px"/>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button  size="mini" type="text" icon="el-icon-zoom-in" @click="handleView(row)">查看</el-button>
            <el-button v-if="row.taskStatus == 0" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(row)">编辑</el-button>
            <el-button v-if="row.taskStatus == 0" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)" style="color: red"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { getToolApplyListApi, deleteToolApplyApi } from '@/api/toolsManage'
import { getListApi,deleteApplyApi } from '@/api/business'
import {
  getUseProjectListAPI,
} from '@/api/EquipmentLedger/equ-out'
export default {
  name: 'ApplyList',
  data() {
    return {
      isLoading: false,
      showSearch: true,
      timeRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskStatus: null,
        createBy: null,
        startTime: null,
        endTime: null,
      },
      statusList: [
        { label: '待审批', value: '0' },
        { label: '审批中', value: '1' },
        { label: '已审批', value: '2' },
        { label: '审批驳回', value: '3' },
      ],
      total: 0, // 总条数
      // 表格数据
      tableList: [],
      useProjectList: [], // 使用项目下拉选
    }
  },
  created() {
    this.getUseProjectList()
    this.getList()
  },
  methods: {
    // 获取使用项目的下拉选
    async getUseProjectList() {
      const res = await getUseProjectListAPI()
      this.useProjectList = res.data
    },


    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.queryParams.parentId = '0'
      this.queryParams.level = '1'
      this.queryParams.taskStatus = null
      this.timeRange = []
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    async getList() {
      console.log('列表-查询', this.queryParams)
      this.isLoading = true
      this.queryParams.startTime = this.timeRange && this.timeRange[0] ? this.timeRange[0] : null
      this.queryParams.endTime = this.timeRange && this.timeRange[1] ? this.timeRange[1] : null
      try {
        const params = { ...this.queryParams }
        const res = await getListApi(params)
        this.tableList = res.data.rows || []
        this.total = res.data.total || 0
      } catch (error) {
        this.tableList = []
        this.total = 0
      } finally {
        this.isLoading = false
      }
    },
    // 新增
    handleAdd() {
      this.$router.push({ path: '/business/addApply',query: {isEdit: true } })
    },
    // 查看
    handleView(row) {
      this.$router.push({ path: '/business/addApply', query: { id: row.id, isView: true } })
    },
    // 编辑
    handleEdit(row) {
      this.$router.push({ path: '/business/addApply', query: { id: row.id, isEdit: true } })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        this.isLoading = true
        try {
          await deleteApplyApi({ id: row.id })
          // 提示
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
          this.getList()
        } catch (error) {
          console.log('🚀 ~ error:', error)
        } finally {
          this.isLoading = false
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 0; /* 取消form-item的默认下边距 */
}
</style>
