<template>
    <lj-win-common title="库位-选择">
        <template slot="header_sub">
            <div style="width: 100%;">
                <lj-searchbar hint="输入仓库名进行查找" @search="handleSearchbar" v-model="keyword"
                              style="width: 85%; float: left;"/>
                <mu-button icon @click="handleScanCode()"
                           style="width: 15%; float: left;"><i class="iconfont icon-qrcode-scan"></i></mu-button>
            </div>
        </template>
        <div slot="body">
            <div style="margin-top:10px">
                <lj-grid :table-data="columns" :columns="columnProperties" :editing="true"
                         @cell-click="handleGridCellClick" />
            </div>
        </div>
        <template slot="footer">
            <mu-button color="primary" @click="chooseCommit" style="width:100%">选择</mu-button>
        </template>
    </lj-win-common>
</template>

<script>
    import ljWinCommon from 'components/lj-win-common';
    import LjSearchbar from "components/lj-searchbar";
    import LjGrid from "components/lj-grid";

    export default {
        components: {
            ljWinCommon,
            LjSearchbar,
            LjGrid,
        },
        data() {
            return {
                columns: [],
                columnProperties: [
                    {
                        property: "checked",
                        width: screen.width * 0.1,
                        type: "selection",
                        editable: true,
                        headerType: "selection"
                    },
                    {property: "storagename", label: "仓库名", width: screen.width * 0.3,},
                    {property: "locacode", label: "库位编号", width: screen.width * 0.3,},
                    {property: "lcode", label: "库位", width: screen.width * 0.3,},
                ],
                locationSelect: [],
                keyword: "",
                item: {},
                ifshowall: false,
                parmsList: [],
            }
        },
        created() {
            this.parmsList = $lj.getPageParam();
            this.GetL1StorageLocaList();
        },
        methods: {
            handleScanCode() {
                let self = this;
                $lj.openScanner(function (ret) {
                    let si = ret.content.indexOf('{');
                    let ei = ret.content.indexOf('}');
                    if (si > 0 || ei > 0) {
                        let storageid = ret.content.substring(0, si);
                        let locacode = ret.content.substring(si + 1, ei);
                        self.GetL1StorageLocaListByScan(storageid, locacode);
                    }
                    else {
                        self.GetL1StorageLocaListByScan(ret.content, null);
                    }
                })
            },
            GetL1StorageLocaListByScan(storageid, locacode) {
                let self = this;
                let requestbody;
                if (locacode) {
                    requestbody = {
                        token: $lj.getStorage("token"),
                        storageid: storageid,
                        locacode: locacode
                    }
                }
                else {
                    requestbody = {
                        token: $lj.getStorage("token"),
                        storageid: storageid
                    }
                }
                $lj.postLJRequest("GetL1StorageLocaList", requestbody, function (ret) {
                    let extraObject = {}
                    extraObject.locaList = ret.locaList;
                    $lj.sendEvent("loca_select", extraObject);
                    $lj.closeWin();
                })
            },
            handleSearchbar() {
                this.GetL1StorageLocaList();
            },
            GetL1StorageLocaList() {
                let self = this;
                this.columns = [];
                $lj.postLJRequest("GetL1StorageLocaList", {
                    token: $lj.getStorage("token"),
                    keyword: this.keyword,
                }, function (ret) {
                    ret.locaList.forEach((col) => {
                        self.parmsList.forEach((par) => {
                            if (col.locacode != '') {
                                if (col.storageid == par.storageid && col.locacode == par.locacode) {
                                    col.checked = true;
                                }
                            }
                            else {
                                if (col.storageid == par.storageid) {
                                    col.checked = true;
                                }
                            }
                        })
                        self.columns.push(col)
                    })

                })
            },
            handleGridCellClick({column, row, index}) {
                if (!column) {
                    return;
                }
            },
            chooseCommit() {
                this.columns.forEach((item) => {
                    if (item.checked) {
                        this.locationSelect.push(item);
                    }
                })
                if (this.locationSelect.length <= 0) {
                    $lj.toast('请选择库位');
                    return;
                }
                let extraObject = {}
                extraObject.locaList = this.locationSelect;
                $lj.sendEvent("loca_select", extraObject);
                $lj.closeWin();
            }
        }
    }
</script>

<style>

</style>