<template>
    <!-- 报备丢失审核页面 -->
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" inline>
            <el-form-item label="申请日期" prop="startTime">
                <el-date-picker
                v-model="queryParams.startTime"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="开始日期"
                @change="() => queryParams.endTime = ''"
                style="width: 130px"
                />
            </el-form-item>
            <el-form-item>-</el-form-item>
            <el-form-item prop="endTime">
                <el-date-picker
                v-model="queryParams.endTime"
                value-format="yyyy-MM-dd"
                type="date"
                placeholder="结束日期"
                :picker-options="{ disabledDate: (t) => t.getTime() < new Date(queryParams.startTime).getTime() - 86400000}"
                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
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                        v-for="item in statusOptions"
                    />
                </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%" :max-height="650" v-loading="loading">
            <!-- 多选 -->
            <!-- <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 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="handleEdit(scope.row, 2)"
                    >
                        审核
                    </el-button>

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

<script>
import { getLoseAuditListApi } from '@/api/business/index'

export default {
    name: 'Lose-apply',
    data() {
        return {
            loading: false,
            userId: '',
            timeRange: [],
            // 表格数据
            tableList: [],
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                keyWord: '', // 关键字
                status: '1', // 审核状态
                startTime: '', // 开始时间
                endTime: '' // 结束时间
            },
            // 状态
            statusOptions: [
                { label: '待审核', value: '0' },
                { label: '审核中', value: '1' },
                { label: '已完成', value: '2' },
                { label: '已驳回', value: '3' }
            ],
            total: 0, // 总条数
            // 表头
            tableColumns: [
                { label: '申请时间', prop: 'createTime', showToolTip: true, width: '100' },
                { label: '申请人', prop: 'createBy', showToolTip: true,  },
                { label: '报备丢失单号', prop: 'code', showToolTip: true },
                { label: '报备丢失单位', prop: 'lossUnitName', showToolTip: false },
                { label: '报备丢失工程', prop: 'lossProName', showToolTip: false },
                { label: '状态', prop: 'status', showToolTip: true, width: '80' }
            ]
        }
    },
    created() {
        const end = new Date()
        let start = new Date()
        start.setMonth(start.getMonth() - 1)
        this.timeRange = [this.format(start), this.format(end)]
        this.userId = sessionStorage.getItem('userId')
        this.queryParams.startTime = this.format(start)
        this.queryParams.endTime = this.format(end)
        // // 设置默认状态为第一个选项（待审核）
        // if (this.statusOptions.length > 1) {
        //     this.queryParams.status = this.statusOptions[1].value
        // }
        this.getList()
    },
    methods: {
        format(date) {
            const y = date.getFullYear()
            const m = String(date.getMonth() + 1).padStart(2, '0')
            const day = String(date.getDate()).padStart(2, '0')
            return `${y}-${m}-${day}`
        },
        // 查询
        handleQuery() {
            this.getList()
        },
        // 重置
        handleReset() {
            const end = new Date()
            let start = new Date()
            start.setMonth(start.getMonth() - 1)
            this.timeRange = [this.format(start), this.format(end)]
            this.queryParams.startTime = this.format(start)
            this.queryParams.endTime = this.format(end)
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.$refs.queryForm.resetFields()
            this.getList()
        },
        // 获取列表
        async getList() {
            try {
                this.loading = true
                const params = {
                    ...this.queryParams,
                    // startTime: this.timeRange ? this.timeRange[0] : '',
                    // endTime: this.timeRange ? this.timeRange[1] : ''
                }
                const res = await getLoseAuditListApi(params)
                console.log('resxxxxxxxxxxxxxxxxx', res)
                this.tableList = res.data.rows
                this.total = res.data.total || 0
            } catch (error) {
                this.tableList = []
                this.total = 0
            } finally {
                this.loading = false
            }
        },
        // 多选
        selectionChange(val) {},
        // 编辑
        handleEdit(row, type) {
            // ----type---- 1. 查看 2. 审核
            // 跳转审核详情页面
            this.$router.push({
                name: 'lose-apply',
                query: {
                    id: row.id,
                    flowId: row.flowId,
                    taskId: row.taskId,
                    type: type === 1 ? 'detail' : 'edit',
                    nodeId: row.nodeId
                }
            })
        },
        // 判断审核按钮显示隐藏
        auditingShow(row) {
            if (row.directUserIds) {
                return (row.status == 1 || row.status == 0) && row.directUserIds.includes(this.userId)
            }
        }
    }
}
</script>
