<template>
    <ele-page flex-table>
        <page-search @search="reload" />

        <common-table
            ref="pageRef"
            :pageConfig="pageConfig"
            :columns="columns"
            :tools="false"
        >
            <template #toolbar>
                <el-button
                    type="success"
                    plain
                    v-permission="'recycle:pending:oneDetection'"
                    @click="handleOneDetection()"
                >
                    一键检测
                </el-button>
                <el-button
                    type="success"
                    plain
                    v-permission="'recycle:pending:exprot'"
                    @click="handleExportExcel()"
                >
                    导出Excel
                </el-button>
                <el-button
                    type="warning"
                    plain
                    v-permission="'recycle:pending:clear'"
                    @click="handleClear()"
                >
                    清空
                </el-button>
            </template>

            <template #checkStatus="{ row }">
                <dict-data
                    code="is_common_yes"
                    type="tag"
                    :model-value="row.checkStatus"
                />
            </template>

            <template #status="{ row }">
                <dict-data
                    code="dispose_status"
                    type="text"
                    :model-value="row.status"
                />
            </template>

            <template #dealMark="{ row }">
                <el-tooltip :content="row.exportTimes" v-if="row.exportTimes">
                    <el-tag type="success">导</el-tag>
                </el-tooltip>
                <el-tooltip :content="row.crawlerTimes" v-if="row.crawlerTimes">
                    <el-tag type="danger" style="margin-left: 10px">爬</el-tag>
                </el-tooltip>
                <span v-if="!row.exportTimes && !row.crawlerTimes">-</span>
            </template>

            <template #action="{ row }">
                <el-button
                    type="primary"
                    link
                    v-permission="'recycle:pending:delete'"
                    @click="handleDelete(row)"
                >
                    [删除]
                </el-button>
                <el-button
                    type="danger"
                    link
                    v-permission="'recycle:pending:deal'"
                    @click="handleDeal(row)"
                >
                    [去处理]
                </el-button>
                <el-button
                    type="success"
                    link
                    v-permission="'recycle:pending:detection'"
                    @click="handleDetectionById(row)"
                >
                    [检测]
                </el-button>
                <el-button
                    type="warning"
                    link
                    v-permission="'recycle:pending:dealLog'"
                    @click="handleDealLog(row)"
                >
                    [处理日志]
                </el-button>
            </template>
        </common-table>

        <books-edit ref="editRef" />
        <deal-log ref="dealLogRef" />
    </ele-page>
</template>

<script setup>
    import { ref, reactive } from 'vue';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';
    import pageSearch from './components/page-search.vue';
    import booksEdit from '@/views/data/books/components/books-edit.vue';
    import dealLog from './components/deal-log.vue';
    import request from '@/utils/request';

    defineOptions({ name: 'PendingDetection' });

    /** 表格列配置 */
    const columns = ref([
        {
            type: 'selection',
            columnKey: 'selection',
            width: 50,
            align: 'center',
            fixed: 'left'
        },
        { label: 'ID', prop: 'id', width: 120, align: 'center' },
        { label: 'ISBN', prop: 'isbn', align: 'center' },
        { label: '扫描次数', prop: 'scanTimes', align: 'center' },
        {
            label: '处理标记',
            prop: 'dealMark',
            slot: 'dealMark',
            align: 'center'
        },
        { label: '创建时间', prop: 'createTime', align: 'center', width: 160 },
        {
            label: '是否检测',
            prop: 'checkStatus',
            slot: 'checkStatus',
            align: 'center'
        },
        {
            label: '处理人',
            prop: 'disposeName',
            align: 'center',
            formatter: (row) => row.disposeName || '-'
        },
        { label: '状态', prop: 'status', slot: 'status', align: 'center' },
        {
            columnKey: 'action',
            label: '操作',
            width: 260,
            slot: 'action',
            fixed: 'right'
        }
    ]);

    /** 页面组件实例 */
    const pageRef = ref(null);

    const pageConfig = reactive({
        pageUrl: '/book/bookNoData/unDispose/pagelist',
        exportUrl: '/book/bookNoData/unDispose/export',
        fileName: '待处理数据库',
        cacheKey: 'pendingTable'
    });

    //刷新表格
    function reload(where) {
        pageRef.value?.reload(where);
    }

    //导出excel
    function handleExportExcel() {
        pageRef.value?.exportData('待处理数据库');
    }

    //一键检测
    function handleOneDetection() {
        pageRef.value?.messageBoxConfirm({
            message: '确定要执行一键检测吗？',
            fetch: () => request.post('/book/bookNoData/unDispose/check')
        });
    }

    //去处理
    const editRef = ref(null);
    function handleDeal(row) {
        editRef.value?.handleOpen({ isbn: row.isbn });
    }

    //删除
    function handleDelete(row) {
        pageRef.value?.messageBoxConfirm({
            message: '确定删除吗？',
            fetch: () =>
                request.post('/book/bookNoData/deleteById', { id: row.id })
        });
    }

    //修改类型
    function handleModifyType(row) {}

    //清空
    function handleClear() {
        let selections = pageRef.value?.getSelections();
        pageRef.value?.operatBatch({
            title: '确定要执行清空吗？',
            method: 'post',
            url: '/book/bookNoData/unDispose/clear',
            data: { ids: selections.map((item) => item.id) }
        });
    }

    //根据ID检测
    function handleDetectionById(row) {
        pageRef.value?.messageBoxConfirm({
            message: '确定要检测该条记录吗？',
            fetch: () =>
                request.post(`/book/bookNoData/unDispose/checkId/${row.id}`)
        });
    }

    //处理日志
    const dealLogRef = ref(null);
    function handleDealLog(row) {
        dealLogRef.value?.handleOpen(row.id);
    }
</script>
