<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="unitName">
                <a-input v-model="form.unitName" :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="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="equipmentCode">
                <SearchSelect
                    url="oceanus/api/v1/equipments/list"
                    searchField="searchKey"
                    v-model="form.equipmentCode"
                    labelFiled="equipmentCode"
                    valueFiled="equipmentCode"
                    :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 SearchSelect from '@/components/MySelect/search_select.vue';
import { addUnitApi, editUnitApi, getUnitInfoApi } from '@/api';

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

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