<template>
    <el-row :gutter="20">
        <el-col :span="8">
            <el-form label-width="68px">
                <el-form-item label="批次">
                    <el-select v-model="batchId" clearable style="width: 227px">
                        <el-option v-for="b in batchList" :label="b.name" :value="b.batchId"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="考生类型">
                    <el-select v-model="examType" clearable style="width: 227px">
                        <el-option v-for="e in examTypes" :label="e.dictLabel" :value="e.dictValue"/>
                    </el-select>
                </el-form-item>
                <el-form-item label="科目">
                    <el-select v-model="subjectId" clearable style="width: 227px">
                        <el-option-group v-for="g in groupedSubjects" :label="g.label">
                            <el-option v-for="s in g.items" :label="s.subjectName" :value="s.subjectId"/>
                        </el-option-group>
                    </el-select>
                </el-form-item>
            </el-form>
        </el-col>
        <el-col :span="16">
            <class-statistic-table />
        </el-col>
    </el-row>
    <el-divider />
    <el-row v-if="!hasBuiltPaper" :gutter="20">
        <el-col :span="6">
            <knowledge-tree allow-multiple/>
        </el-col>
        <el-col :span="18">
            <question-intelligent @submit="handleSubmit" />
        </el-col>
    </el-row>
    <built-paper ref="built" @send="handleSubmit" />
</template>

<script setup name="PaperFullIntelligent">

import consts from "@/utils/consts.js";
import {useProvidePaperFullCondition} from "@/views/dz/papers/hooks/usePaperFullCondition.js";
import ClassStatisticTable from "@/views/dz/papers/components/plugs/class-statistic-table.vue";
import {useProvidePaperClassStatisticCondition} from "@/views/dz/papers/hooks/usePaperClassStatisticCondition.js";
import KnowledgeTree from "@/views/dz/papers/components/plugs/knowledge-tree.vue";
import {useProvidePaperKnowledgeCondition} from "@/views/dz/papers/hooks/usePaperKnowledgeCondition.js";
import QuestionIntelligent from "@/views/dz/papers/components/plugs/question-intelligent.vue";
import {ElMessage} from "element-plus";
import {buildPaperFullIntelligent} from "@/api/dz/papers.js";
import BuiltPaper from "@/views/dz/papers/components/plugs/built-paper.vue";

const type = consts.enums.buildType.FullIntelligent
const {
    batchId,
    batchList,
    examType,
    examTypes,
    subjectId,
    groupedSubjects,
    conditionArgs,
    onConditionReady
} = useProvidePaperFullCondition(type)
const {selectedClasses, classList, loadClassStatistic} = useProvidePaperClassStatisticCondition()
const {knowledgeNode, knowledgeCheckNodes, loadKnowledge} = useProvidePaperKnowledgeCondition()
const built = ref(null)
const hasBuiltPaper = computed(() => built.value?.hasPaper)

const handleSubmit = async (qTypes) => {
    // validation
    if (!batchId.value) return ElMessage.error('请选择批次')
    if (!knowledgeCheckNodes.value.length) return ElMessage.error('请选择知识点')
    if (!qTypes.value.length || qTypes.value.every(t => !t.count)) return ElMessage.error('请填写题量')
    const classIds = selectedClasses.value.map(c => c.classId)
    if (!classIds.length) return ElMessage.error('请选择班级')

    // build
    const commit = {
        buildType: type,
        batchId: toValue(batchId),
        examType: toValue(examType),
        subjectId: toValue(subjectId),
        knowledgeIds: knowledgeCheckNodes.value.map(k => k.id),
        types: qTypes.value.map(t => ({
            type: t.dictValue,
            title: t.dictLabel,
            count: t.count
        })),
        classIds
    }
    await buildPaperFullIntelligent(commit)
    ElMessage.success('生成成功')
    _loadClassStatistic()
}

let statArg = null
const _loadClassStatistic = async () => {
    selectedClasses.value = []
    classList.value = []

    await loadClassStatistic(statArg)
}
onConditionReady(async (payload) => {
    statArg = payload
    knowledgeNode.value = null
    knowledgeCheckNodes.value = []

    await loadKnowledge(payload)
    await _loadClassStatistic()
    await built.value.loadBuiltPaper(payload)
})

watch(conditionArgs, () => built.value.reset())
</script>

<style scoped>

</style>