<template>
    <div v-if="hasPaper">
        <div class="flex justify-center items-center gap-5">
            <el-button type="primary" size="large" @click="handleSend">发送试卷</el-button>
            <el-button type="warning" size="large" plain @click="handleSendNew">重新生成</el-button>
            <el-button type="danger" size="large" plain @click="handleSendForce">全部重新生成</el-button>
        </div>
        <el-collapse class="mt-5">
            <built-paper v-for="p in papers" :paper="p" />
        </el-collapse>
    </div>
</template>

<script setup name="BuiltPaperList">
import {ElMessage} from "element-plus";
import {useInjectGlobalLoading} from "@/views/hooks/useGlobalLoading.js";
import {getBuiltPaper} from "@/api/dz/papers.js";
import BuiltPaper from "@/views/dz/papers/components/plugs/built-paper.vue";

const emits = defineEmits(['send', 'send-new', 'send-force'])

const {loading} = useInjectGlobalLoading()
const papers = ref([])
const hasPaper = computed(() => !!papers.value?.length)

const loadBuiltPaper = async function (payload) {
    // 临时禁用全局 loading，避免显示遮罩
    const originalLoading = loading.value
    loading.value = false
    try {
        const res = await getBuiltPaper(payload)
        papers.value = [].concat(res.data)
    } finally {
        // 恢复全局 loading 状态
        loading.value = originalLoading
    }
}

const reset = function () {
    papers.value = []
}

const handleSend = function () {
    emits('send')
}
const handleSendNew = function () {
    return ElMessage.info('开发中，敬请期待...')
}
const handleSendForce = function () {
    return ElMessage.info('开发中，敬请期待...')
}

defineExpose({hasPaper, loadBuiltPaper, reset})
</script>

<style scoped>

</style>