<template>
    <div>
        <lj-win-index-common :title="title" api-name="GetL1StorageList"
                             :data-reset-callback="handleDataReset"
                             :data-loaded-callback="handleDataLoaded"
                             ref="baseWin"
                             :addable="!isSelectMode"
                             @add-click="addcus"
                             :query-hard-parms="queryHardParms"
                             :query-fields="queryFields"
                             :binding-parms="bindingParms"
                             @query-field-click="handleQueryFieldClick">
            <lj-searchbar slot="header_sub" hint="输入仓库编号/名称进行查找" v-model="bindingParms.keyword"
                          @search="handleSearchbar" style="width: 100%"/>
            <div slot="body_list">
                <mu-list class="lj-group">
                    <template v-for="item in allData">
                        <storage-item :item="item" @click.native="handleCellClick(item)"></storage-item>
                    </template>
                </mu-list>
            </div>
        </lj-win-index-common>
    </div>
</template>

<script>
    import LjWinIndexCommon from "components/lj-win-index-common";
    import LjSearchbar from "components/lj-searchbar";
    import storageItem from './storage-item';

    export default {
        components: {
            LjSearchbar,
            LjWinIndexCommon,
            storageItem
        },
        data() {
            return {
                bindingParms: {keyword: ""},
                winMode: "",
                allData: [],
                queryHardParms: { sptid:-1, spttypeid:-1},
                queryFields: [
                    {title: "仓库编号/名称", property: "keyword"}
                ]
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
                // this.queryHardParms = pageParam.queryParams || {};
            }
        },
        methods: {
            handleDataReset(response) {
                // alert(JSON.stringify(response.sptList));
                // alert(JSON.stringify(response));
                this.allData.length = 0;
                this.addList(response.storageList);
            },
            handleDataLoaded(response) {
                // let moreItems = response.storageList;
                // if (moreItems.length > 0) {
                //     this.addList(moreItems);
                //     return true;
                // }
                return false;
            },
            addList(list) {
                this.allData = this.allData.concat(list);
            },
            handleSearchbar(text) {
                this.$refs.baseWin.refresh({keyword: text});
            },
            handleCellClick(mainObject) {
                if (this.isSelectMode) {
                    $lj.sendEvent("storage_select", mainObject);
                    $lj.closeWin();
                } else {
                    $lj.openWin("storage-detail", {mainObject: mainObject});
                }
            },
            addcus() {
                $lj.openWin("spt-detail");
            },
            handleQueryFieldClick({property}) {
                if (property === 'cusareaid') {
                    this.$refs.areaChooseDialog.open();
                }
            },
        },
        computed: {
            isSelectMode() {
                return this.winMode === "single-select";
            },
            title() {
                if (this.winMode === 'single-select') {
                    return "仓库选择";
                } else {
                    return "仓库管理";
                }
            }
        }
    }
</script>