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

        <common-table ref="pageRef" :pageConfig="pageConfig" :columns="columns">
            <template #toolbar>
                <el-button
                    type="primary"
                    plain
                    :icon="PlusOutlined"
                    v-permission="'data:books:add'"
                    @click="handleUpdate()"
                >
                    新增图书
                </el-button>
                <el-button
                    type="primary"
                    plain
                    v-permission="'data:books:import'"
                    @click="handleImportExcel"
                    :icon="UploadOutlined"
                >
                    导入EXCEL
                </el-button>
                <el-button
                    type="success"
                    plain
                    v-permission="'data:books:export'"
                    @click="handleExportExcel"
                    :icon="DownloadOutlined"
                >
                    查询结果导出
                </el-button>
                <el-button
                    type="primary"
                    plain
                    v-permission="'data:books:uploadImage'"
                    @click="handleUploadImage"
                    :icon="CloudUploadOutlined"
                >
                    上传图片
                </el-button>
                <el-button
                    type="success"
                    plain
                    v-permission="'data:books:exportByIsbn'"
                    @click="handleExportByIsbn"
                    :icon="DownloadOutlined"
                >
                    根据ISBN导出
                </el-button>
                 <el-button
                    type="info"
                    plain
                    v-permission="'data:books:importLog'"
                    @click="handleImportLog"
                    :icon="LogOutlined"
                >
                    导入记录
                </el-button>
                <el-button
                    type="info"
                    plain
                    v-permission="'data:books:exportLog'"
                    @click="handleExportLog"
                    :icon="LogOutlined"
                >
                    导出记录
                </el-button>
                <div class="w-full mt-2">
                    <el-button
                        type="warning"
                        plain
                        v-permission="'data:books:importBatchUpdatePrice'"
                        @click="handleBatchUpdatePrice"
                        :icon="UploadOutlined"
                    >
                        批量更新售价
                    </el-button>
                    <el-button
                        type="danger"
                        plain
                        v-permission="'data:books:importBatchBlacklist'"
                        @click="handleBatchBlacklist"
                        :icon="UploadOutlined"
                    >
                        批量加入回收黑名单
                    </el-button>
                    <el-button
                        type="primary"
                        plain
                        v-permission="'data:books:importBatchAddRecycle'"
                        @click="handleBatchAddRecycle"
                        :icon="UploadOutlined"
                    >
                        批量加入回收书单
                    </el-button>
                    <el-button
                        type="primary"
                        plain
                        v-permission="'data:books:importBatchAddRecycleMulti'"
                        @click="handleBatchAddRecycleMulti"
                        :icon="UploadOutlined"
                    >
                        批量加入回收书单(多本回收)
                    </el-button>
                    <el-button
                        type="warning"
                        plain
                        v-permission="'data:books:importBatchPauseRecycle'"
                        @click="handleBatchPauseRecycle"
                        :icon="UploadOutlined"
                    >
                        批量暂停回收
                    </el-button>
                    <el-button
                        type="danger"
                        plain
                        v-permission="'data:books:importBatchModifyMaxNum'"
                        @click="handleBatchModifyMaxNum"
                        :icon="UploadOutlined"
                    >
                        批量修改最大回收数量
                    </el-button>
                </div>
            </template>
            <template #cover="{ row }">
                <el-image
                    style="width: 80px; height: 100px"
                    fit="cover"
                    :src="row.cover"
                    :preview-src-list="[row.cover]"
                    :initial-index="0"
                    preview-teleported
                />
            </template>

            <template #baseInfo="{ row }">
                <book-base-info :row="row" @click="handleUpdate" />
            </template>

            <template #action="{ row }">
                <div>
                    <el-button
                        type="warning"
                        link
                        v-permission="'data:books:changeLogs'"
                        @click="handleChangeLogs(row)"
                    >
                        [变动记录]
                    </el-button>
                    <el-button
                        type="success"
                        link
                        v-permission="'data:books:update'"
                        @click="handleUpdate(row)"
                    >
                        [编辑]
                    </el-button>
                </div>
            </template>
        </common-table>
        <books-edit ref="editRef" @success="reload()" />
        <books-import ref="importRef" v-model="showImport" @done="reload()" />
        <books-change-log ref="changeLogRef" />
        <upload-image ref="uploadImageRef" @done="reload()" />
        <book-export-log ref="exportLogRef" />
        <books-export-isbn ref="exportIsbnRef" v-model="showExportByIsbn" />
        <batch-import
            v-model="showBatchUpdatePrice"
            title="批量更新售价"
            template-name="批量更新售价模板"
            template-url="https://shuhi.oss-cn-qingdao.aliyuncs.com/default/sell_price_batch_import_template.xlsx"
            upload-url="/book/bookInfo/importBatchUpdatePrice"
            :instructions="[
                '支持通过导入来批量更新售价',
                '文件ISBN不存在或者错误，会自动过滤掉',
                '导入文件第一行需与模版完全一致'
            ]"
            @done="reload()"
        />
        <batch-import
            v-model="showBatchBlacklist"
            title="批量加入回收黑名单"
            template-name="批量操作模板"
            template-url="https://shuhi.oss-cn-qingdao.aliyuncs.com/default/recycle_black_batch_import_template.xlsx"
            upload-url="/book/bookInfo/importBatchBlacklist"
            :instructions="[
                '支持通过导入来批量加入回收黑名单',
                '文件ISBN不存在或者错误，会自动过滤掉',
                '导入文件第一行需与模版完全一致'
            ]"
            @done="reload()"
        />
        <batch-import
            v-model="showBatchAddRecycle"
            title="批量加入回收书单"
            template-name="批量操作模板"
            template-url="https://shuhi.oss-cn-qingdao.aliyuncs.com/default/recycle_discount_batch_import_template.xlsx"
            upload-url="/book/bookInfo/importBatchAddRecycle"
            :instructions="[
                '支持通过导入来批量加入回收书单',
                '文件ISBN不存在或者错误，会自动过滤掉',
                '导入文件第一行需与模版完全一致'
            ]"
            @done="reload()"
        />
        <batch-import
            v-model="showBatchAddRecycleMulti"
            title="批量加入回收书单(多本回收)"
            template-name="批量操作模板"
            template-url="https://shuhi.oss-cn-qingdao.aliyuncs.com/default/recycle_discount_batch_import_template.xlsx"
            upload-url="/book/bookInfo/importBatchAddRecycleMulti"
            :instructions="[
                '支持通过导入来批量加入回收书单，recycle_max_per_order=10',
                '文件ISBN不存在或者错误，会自动过滤掉',
                '导入文件第一行需与模版完全一致'
            ]"
            @done="reload()"
        />
        <batch-import
            v-model="showBatchPauseRecycle"
            title="批量暂停回收"
            template-name="批量操作模板"
            template-url="https://shuhi.oss-cn-qingdao.aliyuncs.com/default/recycle_stop_batch_import_template.xlsx"
            upload-url="/book/bookInfo/importBatchPauseRecycle"
            :instructions="[
                '支持通过导入来批量暂停回收',
                '文件ISBN不存在或者错误，会自动过滤掉',
                '导入文件第一行需与模版完全一致'
            ]"
            @done="reload()"
        />
        <batch-import
            v-model="showBatchModifyMaxNum"
            title="批量修改最大回收数量"
            template-name="批量修改最大回收数量模板"
            template-url="https://shuhi.oss-cn-qingdao.aliyuncs.com/default/recycle_update_max_num_batch_import_template.xlsx"
            upload-url="/book/bookInfo/importBatchModifyMaxNum"
            :instructions="[
                '支持通过导入来批量修改最大回收数量',
                '文件ISBN不存在或者错误，会自动过滤掉',
                '导入文件第一行需与模版完全一致'
            ]"
            @done="reload()"
        />
        <book-import-log ref="importLogRef" />
    </ele-page>
</template>

<script setup>
    import { ref, reactive } from 'vue';
    import {
        PlusOutlined,
        DeleteOutlined,
        DownloadOutlined,
        UploadOutlined,
        LogOutlined,
        CloudUploadOutlined
    } from '@/components/icons';
    import { dayjs } from 'element-plus';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';
    import booksEdit from '@/views/data/books/components/books-edit.vue';
    import booksSearch from '@/views/data/books/components/books-search.vue';
    import booksImport from '@/views/data/books/components/books-import.vue';
    import booksChangeLog from '@/views/data/books/components/books-change-log.vue';
    import bookBaseInfo from '@/views/data/books/components/book-base-info.vue';
    import uploadImage from '@/views/data/books/components/upload-image.vue';
    import bookExportLog from '@/views/data/books/components/book-export-log.vue';
    import bookImportLog from '@/views/data/books/components/book-import-log.vue';
    import booksExportIsbn from '@/views/data/books/components/books-export-isbn.vue';
import batchImport from '@/views/data/books/components/batch-import.vue';
    import { useDictData } from '@/utils/use-dict-data';

    defineOptions({ name: 'RecycleOrderCancelled' });

    const [perCheckDicts] = useDictData(['is_common_yes']);

    /** 表格列配置 */
    const columns = ref([
        {
            type: 'selection',
            columnKey: 'selection',
            width: 50,
            align: 'center',
            fixed: 'left'
        },
        {
            label: '图片',
            prop: 'cover',
            slot: 'cover',
            align: 'center'
        },
        {
            label: '基础信息',
            prop: 'baseInfo',
            slot: 'baseInfo',
            align: 'center',
            minWidth: 320
        },
        {
            label: '人工核实',
            prop: 'perCheck',
            formatter: (row) =>
                perCheckDicts.value.find((d) => d.dictValue == row.perCheck)
                    ?.dictLabel,
            align: 'center'
        },
        {
            columnKey: 'action',
            label: '操作',
            width: 200,
            align: 'center',
            slot: 'action'
        }
    ]);

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

    const pageConfig = reactive({
        pageUrl: '/book/bookInfo/list',
        exportUrl: '/book/bookInfo/export',
        fileName: '图书基础数据',
        cacheKey: 'books-base-data'
    });

    //导出excel
    function handleExportExcel() {
        pageRef.value?.exportData('图书基础数据');
    }

    //导入
    const showImport = ref(false);
    function handleImportExcel() {
        showImport.value = true;
    }

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

    //批量删除
    function handleBatchDelete(row) {
        let selections = row ? [row] : pageRef.value?.getSelections();
        let ids = selections.map((item) => item.id).join(',');
        console.log(ids, selections);
        let url = `/book/bookInfo/removeById/${ids}`;
        pageRef.value?.operatBatch({
            title: '确认删除？',
            method: 'post',
            url,
            row
        });
    }

    //新增编辑图书
    const editRef = ref(null);
    function handleUpdate(row) {
        editRef.value?.handleOpen(row);
    }

    //变动记录
    const changeLogRef = ref(null);
    function handleChangeLogs(row) {
        changeLogRef.value?.handleOpen(row);
    }

    //上传图片
    const uploadImageRef = ref(null);
    function handleUploadImage() {
        uploadImageRef.value?.handleOpen();
    }

    //导出记录
    const exportLogRef = ref(null);
    function handleExportLog() {
        exportLogRef.value?.handleOpen();
    }
    //导入记录
    const importLogRef = ref(null);
    function handleImportLog() {
        importLogRef.value?.handleOpen();
    }

    //根据isbn导出
    const showExportByIsbn = ref(false);
    function handleExportByIsbn() {
        showExportByIsbn.value = true;
    }

    //批量更新售价
    const showBatchUpdatePrice = ref(false);
    function handleBatchUpdatePrice() {
        showBatchUpdatePrice.value = true;
    }

    //批量加入回收黑名单
    const showBatchBlacklist = ref(false);
    function handleBatchBlacklist() {
        showBatchBlacklist.value = true;
    }

    //批量加入回收书单
    const showBatchAddRecycle = ref(false);
    function handleBatchAddRecycle() {
        showBatchAddRecycle.value = true;
    }

    //批量加入回收书单(多本回收)
    const showBatchAddRecycleMulti = ref(false);
    function handleBatchAddRecycleMulti() {
        showBatchAddRecycleMulti.value = true;
    }

    //批量暂停回收
    const showBatchPauseRecycle = ref(false);
    function handleBatchPauseRecycle() {
        showBatchPauseRecycle.value = true;
    }

    //批量修改最大回收数量
    const showBatchModifyMaxNum = ref(false);
    function handleBatchModifyMaxNum() {
        showBatchModifyMaxNum.value = true;
    }
</script>
