<template>
  <!-- 直转审核页面 -->
  <div class="app-container">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" size="small" inline>
      <el-form-item label="申请日期" prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          type="date"
          placeholder="开始日期"
          clearable
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          style="width: 130px"
        />
      </el-form-item>
      <el-form-item label="">-</el-form-item>
      <el-form-item label="" prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          type="date"
          placeholder="结束日期"
          clearable
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          style="width: 130px"
        />
      </el-form-item>
      <el-form-item label="关键字" prop="keyWord">
        <el-input
          clearable
          placeholder="请输入关键字"
          v-model="queryParams.keyWord"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>

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

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>
    <el-table :data="tableList" fit highlight-current-row style="width: 100%" :max-height="650" v-loading="isLoading">
      <!-- 多选 -->
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuation(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column
        v-for="column in tableColumns"
        :show-overflow-tooltip="column.showTooltip"
        :key="column.prop"
        :label="column.label"
        :prop="column.prop"
        :width="column.width"
        align="center"
      >
        <!-- 插槽 -->
        <template v-slot="scope" v-if="column.prop == 'status'">
          <el-tag v-if="scope.row.status == '0'" type="warning" size="mini">待审核</el-tag>
          <el-tag v-else-if="scope.row.status == '1'" size="mini">审核中</el-tag>
          <el-tag v-else-if="scope.row.status == '2'" type="success" size="mini">已通过</el-tag>
          <el-tag v-else-if="scope.row.status == '3'" type="danger" size="mini">已驳回</el-tag>
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            v-if="auditingShow(scope.row)"
            @click="handleReview(scope.row, 2)"
          >
            审核
          </el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-edit"
            v-if="(scope.row.status == 0 || scope.row.status == 3) && scope.row.userId == userId"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            v-if="(scope.row.status == 0 || scope.row.status == 3) && scope.row.userId == userId"
            @click="handleDelete(scope.row)"
            style="color: red"
          >
            删除
          </el-button>
          <el-button type="text" size="mini" icon="el-icon-search" @click="handleReview(scope.row, 1)">查看</el-button>
          <el-button type="text" size="mini" icon="el-icon-document" @click="handleStraightforward(scope.row.id)">直转单</el-button>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { getUseListApi, deleteUseApi } from '@/api/materialsStation'
import { formatTime } from '@/utils/bonus.js'
import StraightforwardBills from './components/StraightforwardBills.vue'

export default {
  name: 'straightTransferReview',
  components: { StraightforwardBills },
  data() {
    return {
      isLoading: false,
      showSearch: true,
      userId: '',
      timeRange: [],
      // 表格数据
      tableList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startTime: '',
        endTime: '',
        keyWord: '', // 关键字
        status: '' // 审核状态
      },
      // 状态
      statusOptions: [
        { label: '待审核', value: 0 },
        { label: '审核中', value: 1 },
        { label: '已通过', value: 2 },
        { label: '已驳回', value: 3 }
      ],
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '直转单号', prop: 'code' },
        { label: '申请时间', prop: 'createTime', showToolTip: true, width: '100' },
        { label: '申请人', prop: 'createBy', showToolTip: true, width: '80' },
        { label: '转出工程', prop: 'backProName', showToolTip: false },
        { label: '转出班组', prop: 'backTeamName', showToolTip: false },
        { label: '转入工程', prop: 'leaseProName', showToolTip: false },
        { label: '转入班组', prop: 'leaseTeamName', showToolTip: false },
        { label: '物资类型', prop: 'typeName', showToolTip: true },
        { label: '状态', prop: 'status', showToolTip: true, width: '80' }
      ]
    }
  },
  created() {
    this.userId = sessionStorage.getItem('userId')
    console.log('🚀 ~ created ~ this.userId:', this.userId)
    this.getList()
  },
  methods: {
    // 查询
    handleQuery() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.timeRange = []
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    async getList() {
      try {
        this.isLoading = true
        const params = {
          ...this.queryParams,
          // startTime: this.timeRange ? this.timeRange[0] : '',
          // endTime: this.timeRange ? this.timeRange[1] : ''
        }
        const res = await getUseListApi(params)
        this.tableList = res.data.rows
        this.total = res.data.total || 0
      } catch (error) {
        this.tableList = []
        this.total = 0
      } finally {
        this.isLoading = false
      }
    },
    // 审核/详情
    handleReview(row, type) {
      // ----type---- 1. 查看 2. 审核
      // 跳转审核详情页面
      this.$router.push({
        path: '/materialsStation/straight/reviewDetails',
        query: {
          ...row,
          taskId: row.id,
          type: type === 1 ? 'detail' : 'edit'
        }
      })
    },
    handleStraightforward(id) {
      console.log('🚀 ~ handleStraightforward ~ id:', id)
      this.$refs.straightforwardBills.openDialog(id)
    },
    // 编辑
    handleEdit(row, type) {
      this.$router.push({
        path: '/materialsStation/straight/straightTransfer',
        query: {
          id: row.id,
          type: 'edit'
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          this.isLoading = true
          await deleteUseApi({ id: row.id })
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } catch (error) {
          console.log('🚀 ~ handleDelete ~ error:', error)
        } finally {
          this.isLoading = false
          this.getList()
        }
      })
    },
    // 判断审核按钮显示隐藏
    auditingShow(row) {
      if (row.directUserIds) {
        return (row.status == 1 || row.status == 0) && row.directUserIds.includes(this.userId)
      }
    },
    // 导出数据
    handleExport() {
      // 提示
      this.$message({
        type: 'warning',
        message: '导出功能开发中，敬请期待！'
      })
      try {
        let fileName = `导出_${formatTime(new Date())}.xLsx`
        let url = '/material/material_direct/export'
        const params = { ...this.queryParams }
        console.log('🚀 ~ 导出 ~ params:', params)
        this.download(url, params, fileName)
      } catch (error) {
        console.log('导出数据失败', error)
      }
    }
  }
}
</script>
