
<template>
    <Wraper :hidden="hidden" :refresh="refresh" :onOk="submit">
        <Table
            url="/ranger/inspection/api/v1/items"
            rowKey="itemId"
            :selected.sync="selected"
            ref="addTable"
            :otherQuery="otherQuery"
            :width="552"
        >
            <template #search="{ query }">
                <MoreItem label="项目名称">
                    <a-input v-model="query.inputValue" placeholder="请输入" />
                </MoreItem>
            </template>

            <a-table-column title="项目名称" data-index="itemName" />
            <a-table-column title="数据类型">
                <template #default="row">
                    <span> {{ dataType[row.dataType] }}</span>
                </template>
            </a-table-column>
            <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: {
        data: Object,
        row: Object,
        hidden: Function,
        refresh: Function,
    },
    watch: {
        row: {
            handler(val) {
                if (val) {
                    const { assembleObjectId, routeId, parentAssembleObjectId, regionId } = val;
                    this.otherQuery = { placeId: parentAssembleObjectId, unitId: assembleObjectId, regionId };
                    this.submitData = {
                        assembleObjectType: 'ITEM',
                        parentAssembleObjectType: 'UNIT',
                        parentAssembleObjectId: assembleObjectId,
                        routeId,
                    };
                }
            },
            deep: true,
            immediate: true,
        },
    },
    data() {
        return {
            selected: {
                keys: [],
            },
            otherQuery: {},
            submitData: {},
            dataType: {
                TEXT: '文本类型',
                OPTION: '选项类型',
                NUMERIC: '数值类型',
            },
        };
    },
    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>
