<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="exemptionType">
                <a-select class="tw-w-full" v-model="form.exemptionType" :disabled="isView">
                    <a-select-option value="PLACE">地点免检</a-select-option>
                    <a-select-option value="UNIT">单元免检</a-select-option>
                </a-select>
            </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="placeId">
                <UrlSelect
                    v-model="form.placeId"
                    :url="
                        form.regionId ? `/ranger/inspection/api/v1/place/regions/${form.regionId}/list` : ''
                    "
                    labelFiled="placeName"
                    valueFiled="placeId"
                    :disabled="isView"
                />
            </a-form-model-item>
            <a-form-model-item label="时间" prop="startTime">
                <RangeTime
                    :startTime.sync="form.startTime"
                    :endTime.sync="form.endTime"
                    showTime
                    :disabled="isView"
                />
            </a-form-model-item>

            <a-form-model-item label="免检原因" prop="exemptionReason">
                <a-textarea
                    placeholder="Basic usage"
                    :rows="4"
                    v-model="form.exemptionReason"
                    :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 RangeTime from '@/components/MySelect/range_time_select.vue';
import { addExemptionApi, updateExemptionApi, getExemptionApi } from '@/api';

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

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