<template>
    <!-- 基础页面 -->
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" inline>
            <el-form-item label="申请日期" prop="timeRange">
                <el-date-picker
                    v-model="queryParams.timeRange"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    clearable
                    value-format="yyyy-MM-dd"
                    format="yyyy-MM-dd"
                />
            </el-form-item>
            <el-form-item label="关键字" prop="keyWord">
                <el-input
                    v-model="queryParams.keyWord"
                    placeholder="请输入关键字"
                    clearable
                    @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-table :data="tableList" fit highlight-current-row style="width: 100%">
            <!-- 多选 -->
            <el-table-column type="selection" width="55" align="center" @selection-change="selectionChange" />
            <el-table-column
                type="index"
                width="55"
                label="序号"
                align="center"
                :index="indexContinuation(queryParams.pageNum, queryParams.pageSize)"
            />
            <el-table-column
                v-for="(column, index) in tableColumns"
                show-overflow-tooltip
                :key="column.prop"
                :label="column.label"
                :prop="column.prop"
                align="center"
            >
                <!-- 插槽 -->
                <template v-slot="scope" v-if="column.prop == 'flowStatus'">
                    <el-tag v-if="scope.row.flowStatus == '0'" type="warning" size="mini">待审核</el-tag>
                    <el-tag v-else-if="scope.row.flowStatus == '1'" size="mini">审核中</el-tag>
                    <el-tag v-else-if="scope.row.flowStatus == '2'" type="success" size="mini">已通过</el-tag>
                    <el-tag v-else-if="scope.row.flowStatus == '3'" type="error" 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-search" @click="handleEdit(scope.row, 1)">查看</el-button> -->
                    <el-button
                        v-if="auditingShow(scope.row)"
                        type="text"
                        size="mini"
                        icon="el-icon-edit"
                        @click="handleEdit(scope.row, 2)"
                    >
                        审核
                    </el-button>

                    <el-button
                        type="text"
                        size="mini"
                        icon="el-icon-search"
                        v-if="scope.row.flowStatus == 2 || scope.row.flowStatus == 3"
                        @click="handleEdit(scope.row, 1)"
                    >
                        查看
                    </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"
        />
    </div>
</template>

<script>
import { getReceiveList, receiveDelete, getDerateList } from '@/api/business/index'

export default {
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                keyWord: '', // 关键字
                status: '', // 审核状态
                timeRange: [] // 日期范围
            },
            // 状态
            statusOptions: [
                { label: '待审核', value: '1' },
                { label: '审核中', value: '2' },
                { label: '已完成', value: '3' }
            ],
            total: 0, // 总条数
            // 表头
            tableColumns: [
                { label: '申请时间', prop: 'createTime' },
                { label: '申请人', prop: 'leaseMan' },
                { label: '转出单位', prop: 'backUnitName' },
                { label: '转出工程', prop: 'backProName' },
                { label: '转入单位', prop: 'leaseUnitName' },
                { label: '转入工程', prop: 'leaseProName' },
                // { label: '物资类型', prop: 'typeName' },
                { label: '状态', prop: 'flowStatus' }
            ],
            // 表格数据
            tableList: [],
            userId: ''
        }
    },
    created() {
        this.userId = sessionStorage.getItem('userId')
        this.getList()
    },
    methods: {
        // 查询
        handleQuery() {
            this.getList()
        },
        // 重置
        handleReset() {
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.$refs.queryForm.resetFields()
            this.getList()
        },
        // 获取列表
        async getList() {
            console.log('列表-查询', this.queryParams)
            try {
                const params = {
                    ...this.queryParams,
                    startTime: this.queryParams.timeRange[0] || '',
                    endTime: this.queryParams.timeRange[1] || ''
                }
                const res = await getDerateList(params)
                console.log('🚀 ~ 获取列表 ~ res:', res)
                this.tableList = res.rows
                this.total = res.total || 0
            } catch (error) {
                console.log('🚀 ~ 获取列表 ~ error:', error)
                this.tableList = []
                this.total = 0
            }
        },
        // 多选
        selectionChange(val) {
            console.log('selectionChange', val)
        },
        // 编辑
        handleEdit(row, type) {
            console.log('编辑', row)
            // 跳转审核详情页面
            this.$router.push({
                name: 'direct-rotation-apply',
                query: {
                    id: row.id,
                    flowId: row.flowId,
                    taskId: row.taskId,
                    type: type === 1 ? 'detail' : 'edit',
                    nodeId: row.nodeId
                }
            })
        },
        // 删除
        handleDelete(row) {
            console.log('删除', row)
            this.$confirm('是否删除该数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                const res = await receiveDelete(row.id)
                console.log('🚀 ~ 删除 ~ res:', res)
                this.getList()
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                })
            })
        },
        // 判断审核按钮显示隐藏
        auditingShow(row) {
            console.log('row.configValues', row.configValue)

            if (row.configValue) {
                return (row.flowStatus == 1 || row.flowStatus == 0) && row.configValue.includes(this.userId)
            }
        }
    }
}
</script>
