<!-- 搜索表单 -->
<template>
    <simple-form-modal
        :title="title"
        :items="formItems"
        ref="editRef"
        :baseUrl="baseUrl"
        @success="(data) => emit('success', data)"
    />
</template>

<script setup>
    import {
        reactive,
        ref,
        defineEmits,
        getCurrentInstance,
        computed
    } from 'vue';
    import { useFormData } from '@/utils/use-form-data';
    import SimpleFormModal from '@/components/CommonPage/SimpleFormModal.vue';
    const { proxy } = getCurrentInstance();

    //获取省市
    const provinceList = ref([]);
    const cityList = ref([]);
    const title = ref('新增高校');
    const emit = defineEmits(['success']);

    const formItems = computed(() => {
        return [
            {
                type: 'input',
                label: '学校名称',
                prop: 'schoolName',
                required: true
            },
            {
                type: 'select',
                label: '省份',
                prop: 'provinceId',
                required: true,
                options: provinceList.value.map((d) => {
                    return { label: d.district, value: d.id };
                }),
                props: {
                    filterable: true,
                    onChange: (val) => {
                        getProviceList(val).then((res) => {
                            cityList.value = res.data.data;
                        });
                    }
                }
            },
            {
                type: 'select',
                label: '所在市',
                prop: 'cityId',
                required: true,
                options: cityList.value.map((d) => {
                    return { label: d.district, value: d.id };
                }),
                props: {
                    filterable: true
                }
            },
            {
                type: 'dictSelect',
                label: '办学层次',
                prop: 'schoolLevel',
                props: { code: 'school_level' },
                required: true
            },
            {
                type: 'input',
                label: '主管部门',
                prop: 'departmentName'
            },
            {
                type: 'dictSelect',
                label: '标记',
                prop: 'schoolTag',
                props: { code: 'school_tag' },
                required: true
            }
        ];
    });
    //默认值
    const baseUrl = reactive({
        add: '/baseinfo/schoolInfo/save',
        update: '/baseinfo/schoolInfo/edit'
    });
    const formData = ref();

    const editRef = ref(null);

    function handleOpen(data = {}) {
        title.value = data && data.id ? '编辑高校' : '新增高校';

        // 重置表单数据
        formData.value = JSON.parse(JSON.stringify({}));

        // 如果是编辑模式，才合并数据
        if (data && data.id) {
            formData.value = Object.assign({}, formData.value, data);
            formData.value.schoolTag = formData.value.schoolTag + '';
            formData.value.schoolLevel = formData.value.schoolLevel + '';
        }

        // 获取省份列表
        getProviceList().then((res) => {
            provinceList.value = res.data.data;
        });

        // 如果有省份ID，获取城市列表
        if (data && data.provinceId) {
            getProviceList(data.provinceId).then((res) => {
                cityList.value = res.data.data;
            });
        } else {
            cityList.value = [];
        }

        // 确保表单组件也重置
        editRef.value?.handleOpen(formData.value);
    }

    function getProviceList(id = 1) {
        return proxy.$http.get(`/baseinfo/districtInfo/findInfo/${id}`);
    }

    defineExpose({ handleOpen });
</script>
