<!-- 出库明细弹窗 -->
<template>
    <ele-modal form :width="1200" v-model="visible" title="出库明细">
        <common-table
            ref="tableRef"
            :pageConfig="pageConfig"
            :columns="columns"
            :tools="false"
            :bodyStyle="{ padding: 0 }"
        />

        <template #footer>
            <el-button @click="handleCancel">关闭</el-button>
        </template>
    </ele-modal>
</template>

<script setup>
    import { ref, reactive, watch } from 'vue';
    import CommonTable from '@/components/CommonPage/CommonTable.vue';

    /** 弹窗是否打开 */
    const visible = defineModel({ type: Boolean });

    /** 当前ISBN */
    const currentIsbn = ref('');

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

    /** 页面配置 */
    const pageConfig = reactive({
        pageUrl: '/activation/bookActivationInfo/pageOutLogs',
        fileName: '出库明细',
        cacheKey: 'outDetailTable',
        params: {}
    });

    /** 表格列配置 */
    const columns = ref([
        { label: 'ISBN', prop: 'isbn', width: 140, align: 'center' },
        { label: '书名', prop: 'bookName', minWidth: 200, align: 'left' },
        {
            label: '仓库',
            prop: 'repositoryName',
            width: 120,
            align: 'center',
            formatter: (row) => row.repositoryName || '-'
        },
        { label: '数量', prop: 'num', width: 80, align: 'center' },
        { label: '金额', prop: 'totalPrice', width: 100, align: 'center' },
        {
            label: '状态',
            prop: 'status',
            width: 100,
            align: 'center',
            formatter: (row) => {
                const statusMap = {
                    1: '已出库',
                    2: '已作废'
                };
                return statusMap[row.status] || '未知';
            }
        },
        { label: '操作人', prop: 'operatorName', width: 120, align: 'center' },
        {
            label: '描述',
            prop: 'remark',
            minWidth: 150,
            align: 'left',
            formatter: (row) => row.remark || '-'
        },
        {
            label: '使用次数',
            prop: 'useTimes',
            width: 100,
            align: 'center',
            formatter: (row) => {
                return row.useTimes === 0 ? '未使用' : row.useTimes;
            }
        },
        { label: '操作时间', prop: 'operatorTime', width: 160, align: 'center' }
    ]);

    /** 关闭弹窗 */
    const handleCancel = () => {
        visible.value = false;
    };

    /** 弹窗打开事件 */
    const handleOpen = (isbn) => {
        if (isbn) {
            pageConfig.pageUrl =
                '/activation/bookActivationInfo/pageOutLogs/' + isbn;
            currentIsbn.value = isbn;
            visible.value = true;
            // 设置查询参数
            pageConfig.params = { isbn };
            // 重新加载数据
            tableRef.value?.reload();
        }
    };

    /** 监听弹窗关闭，清理数据 */
    watch(visible, (newVal) => {
        if (!newVal) {
            currentIsbn.value = '';
            pageConfig.params = {};
        }
    });

    defineExpose({
        handleOpen
    });
</script>
