<template>
    <Wraper :hidden="hidden" :refresh="refresh" :onOk="submit">
        <Table
            url="/ranger/inspection/api/v1/units"
            rowKey="unitId"
            :selected.sync="selected"
            :otherQuery="otherQuery"
            :width="552"
        >
            <template #search="{ query }">
                <MoreItem label="搜索">
                    <a-input v-model="query.inputValue" placeholder="请输入单元名称/设备编号" />
                </MoreItem>
            </template>

            <a-table-column title="单元名称" data-index="unitName" />
            <a-table-column title="设备编号" data-index="equipmentCode" />
        </Table>
    </Wraper>
</template>

<script>
import Table from '@/components/table/table.vue';
import MoreItem from '@/components/table/more_item.vue';
import Wraper from '@/components/table/wraper.vue';
import { addRouteAssemblesListApi } from '@/api';

export default {
    components: { Table, MoreItem, Wraper },
    props: {
        row: Object,
        hidden: Function,
        refresh: Function,
    },
    watch: {
        row: {
            handler(val) {
                if (val) {
                    const { assembleObjectId, routeId, parentAssembleObjectId } = val;
                    this.otherQuery = { regionId: parentAssembleObjectId, placeId: assembleObjectId };
                    this.submitData = {
                        assembleObjectType: 'UNIT',
                        parentAssembleObjectType: 'PLACE',
                        parentAssembleObjectId: assembleObjectId,
                        routeId,
                    };
                }
            },
            deep: true,
            immediate: true,
        },
    },
    data() {
        return {
            selected: {
                keys: [],
            },
            otherQuery: {},
            submitData: {},
        };
    },
    methods: {
        async submit() {
            if (this.selected?.keys?.length !== 0) {
                return await addRouteAssemblesListApi({
                    ...this.submitData,
                    assembleObjectIdList: this.selected.keys,
                });
            }
            this.$message.warning('请选择');
            throw new Error('请选择');
        },
    },
};
</script>
