<template>
    <div>
        <el-table
            :data="bussinessTypeData"
            key="row.id"
            ref="fileTable"
            @selection-change="handleSelectionChange"
            style="width: 100%">

            <el-table-column type="selection" width="55"
            ></el-table-column>

            <el-table-column label="序号" type="index"></el-table-column>
            <el-table-column
                prop="action"
                label="单据类型编码"
                min-width="30%">
            </el-table-column>
            <el-table-column
                prop="name"
                label="单据类型名称"
                min-width="30%">
            </el-table-column>
        </el-table>
        <div style="text-align: center;margin-top: 20px">
            <el-button type="primary" size="small" icon="search" @click="onAddSubmit"
            >提交
            </el-button
            >
            <el-button type="primary" size="small" icon="search" @click="cancelDialog"
            >取消
            </el-button
            >
        </div>
    </div>
</template>

<script>

import {
    filterSelectInvUser, updateInvUser
} from "../../api/inventory/invRelBusTypes";


export default {
    name: "inputQuery",
    props: {
        inputQuery: {
            type: Object,
            required: true,
        },
        closeDialog: {
            type: Function,
            required: true,
        },
    },

    data() {
        return {
            filterQuery: {
                subInvCode: null,
                userId: null,
            },
            bussinessTypeData: null,
            multipleSelection: []
        }
    },
    methods: {
        getList() {
            this.filterQuery = {
                subInvCode: this.inputQuery.code,
                userId: this.inputQuery.userid,
            };
            filterSelectInvUser(this.filterQuery).then((res) => {
                if (res.code == 20000) {
                    this.bussinessTypeData = res.data;
                    this.$nextTick(() => {
                        for (let i = 0; i < this.bussinessTypeData.length; i++) {
                            this.$refs.fileTable.toggleRowSelection(this.bussinessTypeData[i], this.bussinessTypeData[i].select)
                        }
                    });

                } else {
                    this.$message.error(res.message);
                }
            }).catch((error) => {
            });
        },

        checkSelectable(row) {
            return !row.select;
        },

        cancelDialog() {
            this.closeDialog();
        },


        onAddSubmit() {

            let postRequesty = {
                subInvCode: this.inputQuery.code,
                userId: this.inputQuery.userid,
                invBusUserEntities: [],

            };


            let datas = [];
            this.multipleSelection.forEach((obj) => {
                datas.push(obj);
            });
            postRequesty.invBusUserEntities = datas;
            updateInvUser(postRequesty).then((res) => {
                if (res.code == 20000) {
                    this.closeDialog();
                } else {
                    this.$message.error(res.message);
                }

            });
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        }

    },

    created() {
        this.getList();
    },
};
</script>

