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

        <common-table ref="pageRef" :pageConfig="pageConfig" :columns="columns">
            <template #toolbar>
                <el-button
                    type="primary"
                    plain
                    :icon="PlusOutlined"
                    v-permission="'recycleService:stockOff:add'"
                    @click="handleAdd()"
                >
                    新增下架
                </el-button>
                <el-button
                    type="danger"
                    plain
                    v-permission="'recycleService:stockOff:batchClose'"
                    @click="handleBatchClose()"
                >
                    关闭作业
                </el-button>
                <el-button
                    type="warning"
                    plain
                    :icon="DownloadOutlined"
                    v-permission="'recycleService:stockOff:export'"
                    @click="handleExportExcel"
                >
                    导出
                </el-button>
            </template>

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

            <template #action="{ row }">
                <div>
                    <el-button
                        type="primary"
                        link
                        v-permission="'recycleService:stockOff:detail'"
                        @click="handleDetail(row)"
                    >
                        详情
                    </el-button>
                    <el-button
                        type="primary"
                        link
                        v-permission="'recycleService:stockOff:offline'"
                        @click="handleOperate(row, 1)"
                        v-if="row.taskStatus == 1"
                    >
                        确认下架
                    </el-button>
                    <el-button
                        type="primary"
                        link
                        v-permission="'recycleService:stockOff:end'"
                        @click="handleOperate(row, 2)"
                        v-if="row.taskStatus == 1"
                    >
                        结束
                    </el-button>
                    <el-button
                        type="danger"
                        link
                        v-permission="'recycleService:stockOff:close'"
                        @click="handleOperate(row, 3)"
                        v-if="row.taskStatus == 0"
                    >
                        关闭
                    </el-button>
                </div>
            </template>
        </common-table>

        <stock-off-add ref="addRef" @success="reload()" />
        <stock-off-detail ref="detailRef" />
    </ele-page>
</template>

<script setup>
    import { ref, reactive } from 'vue';
    import { ElMessageBox } from 'element-plus/es';
    import { EleMessage } from 'ele-admin-plus/es';
    import { DownloadOutlined, PlusOutlined } from '@/components/icons';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';
    import StockOffSearch from './components/stock-off-search.vue';
    import StockOffAdd from './components/stock-off-add.vue';
    import StockOffDetail from './components/stock-off-detail.vue';
    import request from '@/utils/request';

    defineOptions({ name: 'StockOffList' });

    /** 表格列配置 */
    const columns = ref([
        {
            type: 'selection',
            columnKey: 'selection',
            width: 50,
            align: 'center',
            fixed: 'left'
        },
        {
            label: '操作',
            prop: 'operation',
            align: 'center',
            slot: 'action',
            width: 200
        },
        {
            label: '作业状态',
            prop: 'taskStatus',
            slot: 'taskStatus',
            align: 'center',
            width: 100
        },
        { label: '仓库名称', prop: 'godownName', align: 'center' },
        { label: '作业编号', prop: 'taskCode', align: 'center', width: 180 },
        { label: '创建时间', prop: 'createTime', align: 'center', width: 160 },
        { label: '订单总数', prop: 'orderNum', align: 'center', width: 100 },
        { label: '完成时间', prop: 'finishTime', align: 'center', width: 160 },
        { label: '操作员', prop: 'taskUserName', align: 'center' },
        { label: '备注', prop: 'remark', align: 'center' }
    ]);

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

    const pageConfig = reactive({
        pageUrl: '/baseinfo/stocktask/pagelist',
        exportUrl: '/baseinfo/stocktask/export',
        fileName: '下架管理',
        cacheKey: 'stockOffTable'
    });

    //导出excel
    function handleExportExcel() {
        pageRef.value?.exportData('下架列表');
    }

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

    //新增
    function handleAdd() {
        addRef.value?.handleOpen();
    }

    //详情
    function handleDetail(row) {
        detailRef.value?.handleOpen(row);
    }

    //操作
    function handleOperate(row, type) {
        let message = {
            1: '作业单据状态将变更为已完成，未"下架"数量将完成，是否继续？',
            2: '作业单据状态将变更为已完成，未"下架"数量将忽略，是否继续？',
            3: '作业单据状态将变更为已关闭，是否继续？'
        }[type];
        let title = `结束单据：${row.taskCode}`;

        ElMessageBox.confirm(message, title, {
            type: 'warning'
        }).then(() => {
            request
                .post('/baseinfo/stocktask/operate', {
                    taskCode: row.taskCode,
                    operateType: type
                })
                .then(() => {
                    EleMessage.success(`操作成功`);
                    reload();
                });
        });
    }

    //批量关闭
    function handleBatchClose() {
        let selections = pageRef.value?.getSelections();
        let taskCodeList = selections.map((item) => item.taskCode);
        pageRef.value?.operatBatch({
            title: '当前选择的 作业单据状态将变更为已关闭，是否继续？',
            method: 'post',
            url: '/baseinfo/stocktask/batchOperate',
            data: { taskCodeList, operateType: 3 }
        });
    }
</script>
