<template>
    <div>
        <lj-win-index-common :title="title" api-name="GetL1SptList"
                             :data-reset-callback="handleDataReset"
                             :data-loaded-callback="handleDataLoaded"
                             ref="baseWin"
                             :addable="!isSelectMode"
                             @add-click="addcus"
                             :query-hard-parms="queryHardParms"
                             :query-fields="queryFields"
                             @query-field-click="handleQueryFieldClick">
            <lj-searchbar slot="header_sub" hint="输入供应商编号/名称进行查找" @search="handleSearchbar" style="width: 100%"/>
            <div slot="body_list">
                <mu-list class="lj-group">
                    <template v-for="item in allData">
                        <spt-item :item="item" @click.native="handleCellClick(item)"></spt-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 sptItem from './spt-item';

    export default {
        components: {
            LjSearchbar,
            LjWinIndexCommon,
            sptItem
        },
        data() {
            return {
                winMode: "",
                allData: [],
                queryHardParms: { sptid:-1, spttypeid:-1 },
                queryFields: [
                    {title: "供应商编号/名称", property: "keyword"}
                ]
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
                this.queryHardParms = Object.assign(pageParam.queryParams || {}, this.queryHardParms);
            }
        },
        methods: {
            handleDataReset(response) {
                // alert(JSON.stringify(response.sptList));
                // alert(JSON.stringify(response));
                this.allData.length = 0;
                this.addList(response.sptList);
            },
            handleDataLoaded(response) {
                let moreItems = response.sptList;
                if (moreItems.length > 0) {
                    this.addList(moreItems);
                    return true;
                }
                return false;
            },
            addList(list) {
                // if (!list) {
                // alert(JSON.stringify(list));
                // return;
                // }
                // for (let item of list){
                //     this.allData.push(item);
                // }
                this.allData = this.allData.concat(list);
            },
            handleSearchbar(text) {
                this.$refs.baseWin.refresh({keyword: text});
            },
            handleCellClick(mainObject) {
                if (this.isSelectMode) {
                    $lj.sendEvent("spt_select", mainObject);
                    $lj.closeWin();
                } else {
                    $lj.openWin("spt-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>