<template>
    <div>
        <el-form :inline="true" :model="filterQuery" size="mini">
            <el-row>
                <el-form-item class="query-form-item">
                    <el-input
                        v-model="filterQuery.inventoryCode" clearable
                        placeholder="存货编码"
                    ></el-input>
                </el-form-item>
                <el-form-item class="query-form-item">
                    <el-input
                        v-model="filterQuery.inventoryName" clearable
                        placeholder="存货名称"
                    ></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button-group>
                        <el-button type="primary" icon="el-icon-refresh" @click="onReset"></el-button>
                        <el-button type="primary" icon="search" @click="getList">查询</el-button>
                        <el-button type="primary" icon="search" @click="genIncode"
                        >生成内部码
                        </el-button>
                        <el-button type="primary" icon="search" @click="genAllIncode"
                        >一键全部生成
                        </el-button>
                    </el-button-group>
                </el-form-item>
            </el-row>
        </el-form>
        <el-table v-loading="loading" :data="list" style="width: 100%"    @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="序号" type="index" width="70"></el-table-column>
            <el-table-column label="产品编码" prop="code" show-overflow-tooltip></el-table-column>
            <el-table-column label="产品名称" prop="name" show-overflow-tooltip></el-table-column>
            <el-table-column label="规格型号" prop="spec" show-overflow-tooltip></el-table-column>
            <el-table-column label="批次号" prop="batchNo" show-overflow-tooltip></el-table-column>
            <el-table-column label="仓位" prop="warehouseName" show-overflow-tooltip></el-table-column>
            <el-table-column label="货位" prop="spaceName" show-overflow-tooltip></el-table-column>
            <el-table-column label="注册证号" prop="registerCertNo" show-overflow-tooltip></el-table-column>
            <el-table-column label="生产日期" prop="manufacturingDate" show-overflow-tooltip></el-table-column>
            <el-table-column label="失效日期" prop="expirationDate" show-overflow-tooltip></el-table-column>
        </el-table>
        <el-pagination
            :page-size="filterQuery.limit"
            @current-change="handleCurrentChange"
            layout="prev, pager, next"
            :total="total"
        ></el-pagination>
    </div>
</template>

<script>
    import {postAllByInvInfo, postByInvInfo} from "../../api/inventory/inCodelog";
    import {getOnhands} from "../../api/inventory/onHand.js";

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

        },
        data() {
            return {
                filterQuery: {
                    inventoryCode: null,
                    inventoryName: null,
                    thirdSys: null,
                    page: 1,
                    limit: 20,
                },
                list: [],
                total: 0,
                multipleSelection: [],
            };
        },
        methods: {
            onReset() {
                this.$router.push({
                    path: "",
                });
                this.filterQuery = {
                    inventoryCode: null,
                    inventoryName: null,
                    thirdSys: null,
                    page: 1,
                    limit: 20,
                };
                this.getList();
            },
            getList() {
                this.loading = true;
                console.log("this.thirdSys = " + this.thirdSys);
                this.filterQuery.thirdSys = this.thirdSys;
                getOnhands(this.filterQuery)
                    .then((response) => {
                        this.loading = false;
                        this.list = response.data.list || [];
                        this.total = response.data.total || 0;
                    })
                    .catch(() => {
                        this.loading = false;
                        this.list = [];
                        this.total = 0;
                    });
            },
            genIncode() {
                let selectData = this.multipleSelection;
                var ids = [];
                selectData.forEach((obj) => {
                    ids.push(obj.code);
                });
                let query = {
                    ids: ids,
                    thirdSys: this.thirdSys
                }

                postByInvInfo(query)
                    .then((response) => {
                        if (response.code == 20000) {
                            this.closeDialog();
                            this.$message.success(response.data);
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                    });
            },
            genAllIncode() {
                this.loading = true;
                let query = {
                    thirdSysFk: this.thirdSys
                };
                postAllByInvInfo(query)
                    .then((response) => {
                        this.loading = false;
                        if (response.code == 20000) {
                            this.closeDialog();
                            this.$message.success(response.data);
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                        this.loading = false;
                    });
            },
            handleSelectionChange(val) {
                console.log("----" + val);
                this.multipleSelection = val;
            },
            handleCurrentChange(val) {
                this.filterQuery.page = val;
                this.getList();
            },
        }
    }

</script>

<style scoped>

</style>
