<template>
    <view class="bg-white p-30 mx-card" @click="goDetail">
        <view class="fx-row fx-bet-cen gap-20">
            <text class="flex-1 text-main text-xl">{{ item.name }}</text>
            <text class="text-content">{{ createDate }}</text>
            <view class="fx-row fx-cen-cen gap-10">
                <uv-icon name="trash" size="24" color="primary" stop @click="handleDelete"/>
                <uv-icon name="arrow-right" size="20" color="primary"/>
            </view>
        </view>
        <view class="mt-10 text-sm text-content fx-row items-center gap-20">
            <text v-if="false">分数显示待定</text>
            <text>{{ examType }}</text>
            <text v-if="majorCategory">{{ majorCategory.name }}</text>
        </view>
    </view>
</template>

<script>
import MxConst from "@/common/MxConst";
import _ from 'lodash';
import {removeVoluntary} from "@/api/webApi/ie-voluntary";
import {findTreeNode} from "@/utils/tree-helper";
import {useInjectMajorTreeService} from "@/pages/ie/hooks/useMajorTreeInjection";
import {useTransfer} from "@/hooks/useTransfer";
import {confirmAsync} from "@/utils/uni-helper";
import {toast} from "@/uni_modules/uv-ui-tools/libs/function";

export default {
    name: "ai-list-item",
    emits: ['delete'],
    props: {
        item: {
            type: Object,
            default: () => ({})
        }
    },
    data() {
        return {
            type: MxConst.enum.ai.voluntaryType.ai
        }
    },
    setup() {
        const {majorTree} = useInjectMajorTreeService()
        const {transferTo} = useTransfer()

        return {
            majorTree,
            transferTo
        }
    },
    computed: {
        majorCategory() {
            if (!this.majorTree) return {}
            return findTreeNode(this.majorTree, n => n.code == this.item.request.majorCategory) || {}
        },
        examType() {
            return this.item.userSnapshot?.examType
        },
        createDate() {
            return _.first(this.item.createTime?.split(' '))
        }
    },
    methods: {
        async handleDelete() {
            const {id} = this.item
            if (!id) return
            await confirmAsync(`确认删除此条志愿记录么？\r\n删除后不可恢复!`)
            await removeVoluntary(id)
            toast('删除成功')
            this.$emit('delete', this.item)
        },
        goDetail() {
            const path = '/pages/ie/entry-ai-detail/entry-ai-detail'
            this.transferTo(path, this.item, null, true)
        }
    }
}
</script>

<style scoped>

</style>