<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="regionName">
                <a-input v-model="form.regionName" :disabled="isView" />
            </a-form-model-item>

            <a-form-model-item label="所属组织" prop="organizationId">
                <UrlSelect
                    url="/api/v1/users/organizations"
                    labelFiled="orgName"
                    valueFiled="orgId"
                    v-model="form.organizationId"
                    :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 { addRegionApi, editRegionApi, getRegionInfoApi } from '@/api';

export default {
    components: { Wraper, UrlSelect },
    props: { hidden: Function, row: Object, type: String, refresh: Function },
    data: () => ({
        form: {},
        rules: {
            regionName: [{ required: true }],
            organizationId: [{ 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 getRegionInfoApi(this.row.regionId);
        }
    },

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