<template>
    <lj-win-index-common-list
        :title="title"
        ref="baseWin"
        api-name="GetSCWorkgroups"
        hint="工段编号/工段名称"
        ifShowSearchbar
        :data-reset-callback="handleDataReset"
        :data-loaded-callback="handleDataLoaded"
        :query-hard-parms="queryHardParms"
        :query-fields="queryFields"
        :searchText="keyword"
        v-on:update:searchText="emitChangeKeyword"
        :query-quick="queryQuick"
        :search-actions="searchActions"
        @scan-click="handleSearchScan"
    >
        <div slot="body_list">
            <mu-list class="lj-group">
                <wrkgrp-item v-for="(item, index) in allData" :key="index" :item="item" @click.native="handleCellClick(item)">
                </wrkgrp-item>
            </mu-list>
        </div>
    </lj-win-index-common-list>
</template>

<script>
    import LjSearchbar from "components/lj-searchbar";
    import WrkgrpItem from './wrkgrp-item';
    import LjWinIndexCommonList from "components/lj-win-index-common-list";

    export default {
        components: {
            LjSearchbar,
            LjWinIndexCommonList,
            WrkgrpItem
        },
        data() {
            return {
                winMode: "",
                allData: [],
                queryHardParms: {},
                queryFields: [
                    {
                        title: "编号",
                        property: "wrkgrpcode",
                        initValue: "",
                        group: "工段"
                    },
                    {
                        title: "名称",
                        property: "wrkgrpname",
                        initValue: "",
                        group: "工段"
                    },
                ],
                queryQuick: [],
                searchActions: [
                    { 
                        title: '全部',
                        property: 'keyword'
                    },
                    { 
                        title: '编号',
                        property: 'wrkgrpcode' 
                    },
                    { 
                        title: '名称',
                        property: 'wrkgrpname'
                    }
                ],
                keyword: ''
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
                this.queryHardParms = pageParam.queryParams || {};
            }
        },
        methods: {
            /**搜索栏-扫描 */
            handleSearchScan(searchState) {
                $lj.openScanner((ret) => {
                    setTimeout(() => {
                        let obj = {};
                        obj[searchState] = ret.content.trim();
                        this.$refs.baseWin.handleSearchbar(obj, true);
                    }, 200);
                })
            },
            /**回调绑定值 */
            emitChangeKeyword(val) {
                this.keyword = val;
            },
            handleDataReset(response) {
                this.allData.length = 0;
                this.addList(response.wrkgrpList);
            },
            handleDataLoaded(response) {
                let moreItems = response.wrkgrpList;
                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) {
                $lj.sendEvent("wrkgrp_select", mainObject);
                $lj.closeWin();
            },
        },
        computed: {
            isSelectMode() {
                return this.winMode === "single-select";
            },
            title() {
                return "工段选择";
            }
        }
    }
</script>