<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: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 #bookTag="{ row }">
                <dict-data
                    v-if="row.bookTag"
                    code="book_tag_ku"
                    type="text"
                    :model-value="row.bookTag"
                />
                <span v-else>未分配</span>
            </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="handleEdit(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, getCurrentInstance } from 'vue';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';
    import pageSearch from '@/views/recycle/processed/components/page-search.vue';
    import booksEdit from '@/views/data/books/components/books-edit.vue';
    import dealLog from '@/views/recycle/pending/components/deal-log.vue';
    import request from '@/utils/request';

    const { proxy } = getCurrentInstance();
    defineOptions({ name: 'Processed' });

    /** 表格列配置 */
    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: 'dealMark',
            slot: 'dealMark',
            align: 'center'
        },
        { label: '创建时间', prop: 'createTime', align: 'center' },
        {
            label: '是否检测',
            prop: 'checkStatus',
            slot: 'checkStatus',
            align: 'center'
        },
        {
            label: '处理人',
            prop: 'disposeName',
            align: 'center',
            formatter: (row) => row.disposeName || '-'
        },
        {
            label: '分类',
            prop: 'bookTag',
            align: 'center',
            slot: 'bookTag'
        },
        {
            columnKey: 'action',
            label: '操作',
            width: 200,
            slot: 'action',
            fixed: 'right'
        }
    ]);

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

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

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

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

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

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

    //编辑
    const editRef = ref(null);
    function handleEdit(row) {
        request
            .get('/book/bookInfo/getDetailByIsbn/' + row.isbn)
            .then((res) => {
                console.log(res, 'xxx');
                if (res.data.code == 200) {
                    editRef.value?.handleOpen(res.data.data);
                } else {
                    editRef.value?.handleOpen({ isbn: row.isbn });
                }
            });
    }

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