<template>
    <Wraper :hidden="hidden" :onOk="submit" :refresh="refresh" :noFooter="isView">
        <a-form-model layout="vertical" :model="form" :rules="rules" ref="form">
            <a-form-model-item label="专业名称" prop="specialityName">
                <a-input v-model="form.specialityName" :disabled="isView" />
            </a-form-model-item>

            <a-form-model-item label="所属地区" prop="regionId">
                <UrlSelect
                    url="/ranger/inspection/api/v1/region/list"
                    v-model="form.regionId"
                    labelFiled="regionName"
                    valueFiled="regionId"
                    :disabled="isView"
                />
            </a-form-model-item>

            <a-form-model-item label="备注" prop="remark">
                <a-textarea placeholder="Basic usage" :rows="4" v-model="form.remark" :disabled="isView" />
            </a-form-model-item>
        </a-form-model>
    </Wraper>
</template>

<script>
import Wraper from '@/components/table/wraper.vue';
import UrlSelect from '@/components/MySelect/url_select.vue';
import { addMajorApi, updateMajorApi, getMajorInfoApi } from '@/api';

export default {
    components: { Wraper, UrlSelect },
    props: { hidden: Function, row: Object, type: String, refresh: Function },
    data: () => ({
        form: {},
        rules: {
            specialityName: [{ required: true }],
            regionId: [{ required: true }],
        },
    }),

    computed: {
        isView() {
            return this.type === 'view';
        },
        isEdit() {
            return this.type === 'edit';
        },
        isAdd() {
            return this.type === null;
        },
    },

    async mounted() {
        if (!this.isAdd) {
            this.form = await getMajorInfoApi(this.row.specialityId);
        }
    },

    methods: {
        async submit() {
            await this.$refs.form.validate();
            if (this.isAdd) return addMajorApi(this.form);
            if (this.isEdit) return updateMajorApi(this.form);
        },
    },
};
</script>
