<!-- 推送记录弹窗 -->
<template>
    <ele-modal
        v-model="visible"
        title="推送记录"
        :width="800"
        destroy-on-close
        @open="handleOpen"
    >
        <ele-data-table
            ref="tableRef"
            row-key="id"
            :columns="columns"
            :data="dataList"
            :loading="loading"
        >
            <template #pushRes="{ row }">
                <dict-data
                    code="book_stat_push_status"
                    type="text"
                    :model-value="row.pushRes"
                />
            </template>
        </ele-data-table>
        <template #footer>
            <el-button @click="handleClose">关闭</el-button>
        </template>
    </ele-modal>
</template>

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

    defineOptions({ name: 'PushLog' });

    /** 弹窗是否打开 */
    const visible = ref(false);

    /** 加载状态 */
    const loading = ref(false);

    /** 表格数据 */
    const dataList = ref([]);

    /** 当前行数据 */
    const currentRow = ref(null);

    /** 表格列配置 */
    const columns = [
        { label: 'ID', prop: 'id', align: 'center', width: 80 },
        { label: '推送时间', prop: 'createTime', align: 'center' },
        { label: '推送人', prop: 'pushUserName', align: 'center' },
        { label: '推送结果', prop: 'pushRes', align: 'center', slot: 'pushRes' }
    ];

    /** 获取推送记录数据 */
    const fetchPushLogs = () => {
        if (!currentRow.value || !currentRow.value.id) return;

        loading.value = true;

        request
            .get('/bookpush/pusherp/log?pushId=' + currentRow.value.id)
            .then((res) => {
                console.log(res, 'xxx');
                if (res.data.code === 200) {
                    dataList.value = res.data.data || [];
                } else {
                    EleMessage.error(res.data.msg);
                }
            })
            .finally(() => {
                loading.value = false;
            });
    };

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

    /** 弹窗打开事件 */
    const handleOpen = () => {
        fetchPushLogs();
    };

    /** 打开弹窗 */
    const open = (row) => {
        currentRow.value = row;
        visible.value = true;
    };

    // 暴露方法给父组件调用
    defineExpose({
        open
    });
</script>
