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

            <a-form-model-item label="所属地区" prop="regionId">
                <UrlSelect
                    v-model="form.regionId"
                    url="/ranger/inspection/api/v1/region/list"
                    labelFiled="regionName"
                    valueFiled="regionId"
                    :disabled="isView"
                />
            </a-form-model-item>
            <a-form-model-item label="二维码" prop="qrCode">
                <a-input v-model="form.qrCode" :disabled="isView" />
            </a-form-model-item>
            <a-form-model-item label="二维码" prop="electronicTag">
                <a-input v-model="form.electronicTag" :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 { addPlaceApi, editPlaceApi, getPlaceInfoApi } from '@/api';

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

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