<!-- 各仓库库存弹窗 -->
<template>
    <ele-modal form :width="640" v-model="visible" title="库存">
        <div class="stock-header" v-if="bookInfo">
            <span class="book-name">{{ bookInfo.bookName || '-' }}</span>
            <span class="isbn">{{ bookInfo.isbn || '-' }}</span>
        </div>

        <el-table
            v-loading="loading"
            :data="tableData"
            border
            stripe
            style="width: 100%"
        >
            <el-table-column type="index" label="序号" width="80" align="center" />
            <el-table-column
                prop="repositoryName"
                label="仓库名称"
                min-width="200"
                align="center"
            />
            <el-table-column
                prop="stockNum"
                label="可用库存"
                width="140"
                align="center"
            >
                <template #default="{ row }">
                    {{ row.stockNum ?? row.num ?? 0 }}
                </template>
            </el-table-column>
        </el-table>

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

<script setup>
    import { ref } from 'vue';
    import { EleMessage } from 'ele-admin-plus/es';
    import request from '@/utils/request';

    const visible = defineModel({ type: Boolean });
    const loading = ref(false);
    const tableData = ref([]);
    const bookInfo = ref(null);

    async function handleOpen(row) {
        if (!row?.isbn) {
            EleMessage.warning('缺少商品ISBN');
            return;
        }
        bookInfo.value = row;
        visible.value = true;
        loading.value = true;
        tableData.value = [];
        try {
            const res = await request.post(
                `/activation/bookActivationInfo/getStockAllRepository/${row.isbn}`
            );
            if (res.data.code === 200) {
                tableData.value = res.data.data || [];
            } else {
                EleMessage.error(res.data.msg || '获取库存失败');
            }
        } catch (e) {
            EleMessage.error(e.message || '获取库存失败');
        } finally {
            loading.value = false;
        }
    }

    defineExpose({ handleOpen });
</script>

<style scoped>
    .stock-header {
        margin-bottom: 16px;
        font-size: 14px;
        color: #303133;
    }

    .book-name {
        font-weight: 500;
        margin-right: 12px;
    }

    .isbn {
        color: #909399;
    }
</style>
