<template>
    <view class="page-content">
        <uv-loading-page :loading="loading"/>
        <large-header v-bind="headerBinding" :title="title" :scroll-top="scrollTop"/>
        <pretty-card/>
        <view class="px-20 fx-col gap-20 flex-1">
            <college-major-result v-for="m in prevData.universities" :key="m.num" :model="m"
                                  :results="filterResults(m)"/>
        </view>
        <view class="h-[80px] px-30 fx-row fx-cen-cen">
            <uv-button v-if="voluntaryRef" v-bind="gradientBtnBinding" text="生成报告" @click="handleDirectReport"/>
            <uv-button v-else :loading="submitting" v-bind="gradientBtnBinding" text="保存志愿表并查看报告"
                       @click="handleCommit"/>
        </view>
        <mx-popup-template ref="inputDialog" title="志愿表名称" left="" right="确定" @right="dialogInputConfirm">
            <uv-input v-model="commit.name" placeholder="请输入志愿表名称(选填)"/>
        </mx-popup-template>
    </view>
</template>

<script>
import {getCurrentInstance, computed} from 'vue';
import LargeHeader from "@/pages/ie/components/large-header.vue";
import PrettyCard from "@/pages/ie/components/card/pretty-card.vue";
import CollegeMajorResult from "@/pages/ie/entry-analysis/components/college-major-result.vue";
import {postMultipleResult, submitVoluntary} from "@/api/webApi/ie-voluntary";
import AIFormCommonStyle from "@/pages/ie/components/AIFormCommonStyle";
import MxConst from "@/common/MxConst";
import {sleep, toast} from "@/uni_modules/uv-ui-tools/libs/function";
import {useProvideUserSnapshot} from "@/pages/ie/hooks/useUserSnapshotInjection";
import {useProvidePageScroll} from "@/hooks/usePageScrollInjection";
import {useTransfer} from "@/hooks/useTransfer";

export default {
    components: {CollegeMajorResult, PrettyCard, LargeHeader},
    mixins: [AIFormCommonStyle],
    data() {
        return {
            // 志愿表模式能直接进入报告。
            // 因为VUE的JS与Template不能同时重写，所以这里定义这个属性进行控制。有点依赖倒置的现象
            voluntaryRef: null,
            loading: false,
            submitting: false,
            headerBinding: {
                mode: 'light',
                bgIcon: '',
                fullHeight: 80
            },
            results: [],
            commit: {
                year: '', // 缺省的话后台会补充
                name: '', // 缺省的话后台会补充
                voluntaryType: MxConst.enum.ai.voluntaryType.multiple,
            }
        }
    },
    setup() {
        const scrollTop = useProvidePageScroll()
        const {prevData, transferTo} = useTransfer()
        const instance = getCurrentInstance()
        const snapshot = computed(() => instance.proxy.voluntaryRef?.userSnapshot)
        useProvideUserSnapshot(snapshot)

        return {
            prevData,
            scrollTop,
            transferTo
        }
    },
    computed: {
        title() {
            return this.voluntaryRef?.name || '模拟志愿分析结果'
        }
    },
    mounted() {
        this.getAnalysisResult()
    },
    methods: {
        async getAnalysisResult() {
            this.loading = true
            try {
                const res = await postMultipleResult(this.prevData)
                this.results = res.data
            } catch (e) {
                console.log('get analysis result error', e)
            } finally {
                this.loading = false
            }
        },
        filterResults(model) {
            return this.results.filter(r => r.universityCode == model.code)
        },
        handleCommit() {
            if (!this.results?.length) {
                toast('异常数据')
                return
            }
            // input name first
            this.$refs.inputDialog.open()
        },
        async dialogInputConfirm() {
            this.$refs.inputDialog.close()
            // save core
            const data = {
                ...this.commit,
                request: this.prevData,
                details: this.results
            }
            this.submitting = true
            try {
                const res = await submitVoluntary(data)
                toast('保存成功')
                if (!res.data) return // 异常情况
                await sleep(2000)

                const url = '/pages/ie/entry-analysis/analysis-report'
                this.transferTo({url, params: {id: res.data, type: data.voluntaryType}, type: 'redirect'})
                this.submitting = false
            } catch (e) {
                console.log('submitVoluntary[multiple] error', e)
                this.submitting = false
            }
        },
        handleDirectReport() {
            const path = '/pages/ie/entry-analysis/analysis-report'
            this.transferTo(path, this.voluntaryRef, null, true)
        }
    }
}
</script>

<style>

</style>
