<template>
    <view class="flex-1 fx-col">
        <ie-form-picker :model-value="major.name" :desc="major.majorDirection" label="选择专业"
                        @click="handleShowPicker"/>
        <major-picker ref="picker" :limit="1" :university-code="universityCode" @confirm="handleConfirm"/>
    </view>
</template>

<script>
import IeFormPicker from "@/pages/ie/components/picker/ie-form-picker.vue";
import MajorPicker from "@/pages/ie/components/picker/major-picker.vue";
import AIFormFieldBase from "@/pages/ie/components/ai-form/items/AIFormFieldBase";
import {toast} from "@/uni_modules/uv-ui-tools/libs/function";

export default {
    name: "ai-form-college-major",
    components: {MajorPicker, IeFormPicker},
    mixins: [AIFormFieldBase],
    emits: ['update:enrollCode'],
    props: {
        universityCode: {
            type: String,
            default: ''
        },
        enrollCode: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            show: false,
            major: {}
        }
    },
    watch: {
        universityCode() {
            // university changes, clear major selected
            this.major = {}
            this.$emit('update:modelValue', '')
            this.$emit('update:enrollCode', '')
        }
    },
    methods: {
        async handleShowPicker() {
            if (!this.universityCode) {
                toast('请先选择学校')
                return
            }
            this.$refs.picker.open()
        },
        handleConfirm(e) {
            this.major = e
            this.show = false
            this.$emit('update:modelValue', e.code)
            this.$emit('update:enrollCode', e.enrollCode)
            this.clearValidate()
        }
    }
}
</script>

<style scoped>

</style>