<template>
    <div>
        <!-- <lj-win-index-common :title="title" api-name="GetCustList"
                             :data-reset-callback="handleDataReset"
                             :data-loaded-callback="handleDataLoaded"
                             ref="baseWin"
                             :addable="addable"
                             @add-click="addcus"
                             :query-hard-parms="queryHardParms"
                             :query-fields="queryFields"
                             @query-field-click="handleQueryFieldClick"> -->

        <lj-win-index-common-list
            :title="title"
            ref="baseWin"
            api-name="GetCustList"
            hint="编号/名称/电话号码"
            ifShowSearchbar
            :addable="addable"
            :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"
            @add-click="addcus"
        >
            <!-- <lj-searchbar slot="header_sub" hint="输入客户名称/电话进行查找" @search="handleSearchbar" style="width: 100%"/> -->
            <div slot="body_list">
                <mu-list class="lj-group" toggle-nested :nested-indent="false">
                    <cust-item v-for="(item, index) in allData" :key="index" :item="item" :ifaccount="ifaccount" @click.native="handleCellClick(item)"></cust-item>
                </mu-list>
            </div>
        </lj-win-index-common-list>
    </div>
</template>

<script>
    import LjSearchbar from "components/lj-searchbar";
    import custItem from 'components/L1APP/public/cust-item';
    import LjWinIndexCommonList from "components/lj-win-index-common-list";

    export default {
        components: {
            LjSearchbar,
            LjWinIndexCommonList,
            custItem
        },
        data() {
            return {
                winMode: "",
                allData: [],
                queryHardParms: {},
                // queryFields: [
                //     {
                //         title: "区域",
                //         property: "cusareaid",
                //         selectOptions: {
                //             optionsApi: "GetQ5Cusarea",
                //             valueMap: ["CusareaID", "Cusareaname"],
                //             extraOptions: [{CusareaID: -1, Cusareaname: "全部", areaname: "全部", sonflag: 0, parentid: 0}]
                //         },
                //         // disabled: true
                //     },
                //     {title: "客户名称/电话", property: "keyword"}
                // ],
                queryFields: [
                    {
                        title: "编号",
                        property: "cuscode",
                        initValue: "",
                        group: "客户"
                    },
                    {
                        title: "名称",
                        property: "name",
                        initValue: "",
                        group: "客户"
                    },
                    {
                        title: "电话号码",
                        property: "tele",
                        initValue: "",
                        group: "客户"
                    },
                ],
                queryQuick: [
                    {
                        title: "全部区域",
                        property: "cusareaid",
                        type: 'cascader',
                        initValue: -1,
                        selectOptions: {
                            optionsApi: "GetL1Cusarea",
                            resultName: "RecurionList",
                            requestbody: {RecurionType: 1},
                            valueMap: { text: 'text', value: 'value', children: 'children' }
                        }
                    },
                    {
                        title: "客户状态",
                        property: "inuse",
                        type: 'radio2',
                        initValue: -1,
                        selectOptions: {
                            options: [
                                {flag: 1, dscrp: "有效"},
                                {flag: 0, dscrp: "无效"},
                            ], 
                            valueMap: { text: 'dscrp', value: 'flag'},
                            // valueMap: ["flag", "dscrp"],·
                        }
                    },
                ],
                searchActions: [
                    { 
                        title: '全部',
                        property: 'keyword'
                    },
                    { 
                        title: '编号',
                        property: 'cuscode' 
                    },
                    { 
                        title: '名称',
                        property: 'name'
                    },
                    { 
                        title: '电话号码',
                        property: 'tele'
                    },
                ],
                keyword: '',
                addable: true,
                ifaccount:false
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
                this.queryHardParms = pageParam.queryParams || {};
                if (pageParam.hasOwnProperty('addable')) {
                    this.addable = pageParam.addable;
                }
                if(pageParam.hasOwnProperty('ifaccount')){
                    this.ifaccount = pageParam.ifaccount;
                    if(this.ifaccount){
                        this.queryFields.push({
                                title:"户名",
                                property:"accountname",
                                initValue:'',
                                group:"客户账户"
                            },
                            {
                                title:"账户",
                                property:"account",
                                initValue:'',
                                group:"客户账户"
                            },
                            {
                                title:"备注",
                                property:"accountdscrp",
                                initValue:'', 
                                group:"客户账户"
                            })  
                    }
                }
            }
        },
        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) {
                // alert(JSON.stringify(response.custList));
                // alert(JSON.stringify(response));
                this.allData.length = 0;
                this.addList(response.custList);
            },
            handleDataLoaded(response) {
                let moreItems = response.custList;
                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("cust_select", mainObject);
                    $lj.closeWin();
                } else {
                    $lj.openWin("cust-detail", {mainObject: mainObject});
                }
            },
            addcus() {
                $lj.openWin("cust-detail");
            },
            handleQueryFieldClick({property}) {
                if (property === 'cusareaid') {
                    this.$refs.areaChooseDialog.open();
                }
            },
            handleAreaChosen(item) {
                if (item && item.CusareaID) {
                    this.$refs.baseWin.setQueryParam("cusareaid", item.CusareaID);
                }
            },
        },
        computed: {
            isSelectMode() {
                return this.winMode === "single-select";
            },
            title() {
                if (this.winMode === 'single-select') {
                    return "客户选择";
                } else {
                    return "客户管理";
                }
            }
        }
    }
</script>