<template>
    <lj-win-index-common-list :title="title" api-name="GetL1SaletaskOutware"
                        :data-reset-callback="handleDataReset"
                        :data-loaded-callback="handleDataLoaded"
                        ref="baseWin"
                        hint="输入订单号/车牌号进行查找"
                        ifShowSearchbar
                        :searchText="keyword"
                        v-on:update:searchText="emitChangeKeyword"
                        :query-quick="queryQuick"
                        :search-actions="searchActions"
                        :query-hard-parms="queryHardParms"
                        @scan-click="handleSearchScan"
                        :closeWinHandler="handleCloseWin"
                        :query-fields="queryFields">
        <div slot="body_list" class="body_list_normal">
            <lj-text :datas="outwareInfo" v-if="outwareInfo && ifSummary"/>
            <saletask-outware-item v-for="(item, idx) in allData" :item="item" :key="idx" @callFuntion="handleCellClick"></saletask-outware-item>
        </div>
    </lj-win-index-common-list>
</template>

<script>
    import LjWinIndexCommonList from "components/lj-win-index-common-list";
    import LjText from "../../sale/saletask/lj-text";
    import SaletaskOutwareItem from './saletask-outware-item';
    
    export default {
        components: {
            LjWinIndexCommonList,
            LjText,
            SaletaskOutwareItem
        },
        data() {
            return {
                title: "销售发货单",
                bindingParms: {keyword: ""},
                allData: [],
                userFunids: [],
                queryHardParms: {},
                outwareInfo:[],
                billtype: 1,
                thflag: 0,
                keyword: "",
                queryQuick: [
                    {
                        title: "仓库",
                        property: "storageid",
                        type: 'radio2',
                        initValue: -1,
                        selectOptions: {
                            valueMap: 
                            { text: 'storagename', value: 'storageid'},
                        }
                    },
                    {
                        title: "审核状态",
                        property: "flag",
                        type: 'radio2',
                        initValue: -1,
                        selectOptions: {
                            options: [
                                {flag: 0, dscrp: "待仓审"},
                                {flag: 1, dscrp: "待财审"},
                                {flag: 2, dscrp: "已财审"}
                            ], 
                            valueMap: { text: 'dscrp', value: 'flag'},
                        }
                    }, 
                    {
                        title: "确认状态",
                        property: "priceflag",
                        type: 'radio2',
                        initValue: -1,
                        selectOptions: {
                            options: [
                                {flag: 0, dscrp: "待确认"},
                                {flag: 2, dscrp: "初确认"},
                                {flag: 1, dscrp: "已确认"}
                            ], 
                            valueMap: { text: 'dscrp', value: 'flag'},
                        }
                    }, 
                ],
                searchActions: [
                    { 
                        title: '全部',
                        property: 'keyword'
                    },
                    { 
                        title: '订单号',
                        property: 'outwarecode' 
                    },
                    { 
                        title: '客户名称',
                        property: 'cusname' 
                    },
                    { 
                        title: '车牌号码',
                        property: 'carcode'
                    },
                ],
                queryFields: [
                    {title: "发货日期开始", property: "begindate", type: "date",group: "发货单"},
                    {title: "发货日期结束", property: "enddate", type: "date",group: "发货单"},
                ],
            }
        },
        created() {
            this.userFunids = $lj.getStorage('user').rsltFunids;
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
                this.billtype = pageParam.billtype;
                this.thflag = pageParam.thflag;
                this.queryHardParms = pageParam.queryParams || {billtype: this.billtype,thflag: this.thflag};
                this.title = pageParam.title
            }

            var self = this;
            $lj.addEventListener("saletask_outware_refresh", function () {
                self.$refs.baseWin.refresh();
            });

            let storageInfo = $lj.getStorage("storageInfo") || {};
            if (storageInfo.storagePower == null || storageInfo.storagePower == "") {
                $lj.toast("该用户的仓库权限不存在 / 该用户不存在");
                return false;
            }
            if (storageInfo.storagePower == "-1") {
                $lj.toast("你没有仓库权限");
                return false;
            }
            this.$set(this.queryQuick[0].selectOptions, 'options', storageInfo.storageList);
        },
        methods: {
            handleCloseWin() {
                $lj.closeWin();
            },
            /**搜索栏-扫描 */
            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;
                console.log('库存表 emitChangeKeyword this.keyword :>>>>>>>>>>>>>>>>>>>>>', JSON.stringify(this.keyword));
            },
            handleDataReset(response) {
                this.allData = [];
                if (response.outwareList && response.outwareList.length > 0) {
                    this.addList(response.outwareList);
                }
                if(response.outwareInfo && response.outwareInfo.length > 0 ){
                    for(let i = 0;i < response.outwareInfo.length;i++) {
                        response.outwareInfo[i].mainvalue = Number(response.outwareInfo[i].mainvalue);
                    }
                    this.outwareInfo = response.outwareInfo
                }
            },
            handleDataLoaded(response) {
                let moreSaletasks = response.outwareList;
                if (moreSaletasks.length > 0) {
                    this.addList(moreSaletasks);
                    return true;
                }
                return false;
            },
            addList(list) {
                this.allData = this.allData.concat(list);
            },
            handleSearchbar(text) {
                this.$refs.baseWin.refresh(Object.assign({keyword: text}, this.queryHardParms));
            },
            handleCellClick(mainObject) {
                if (this.winMode === "single-select") {
                    $lj.sendEvent("saletask_outware_select", mainObject);
                    $lj.closeWin();
                } else {
                    $lj.openWin("saletask-outware-detail", {mainObject: mainObject});
                }
            }
        },
        computed: {
            queryFields() {
                let result = [];
                if (true) {
                    result = [
                        {title: "仓库", property: "storageid", selectOptions: {
                                optionsApi: "GetL1StorageList",
                                valueMap: ["storageid", "storagename"],
                                extraOptions: [{
                                    storageid: -1,
                                    storagename: "全部"
                                }]
                            }
                        },
                        {title: "审核状态", property: "flag", selectOptions: {
                                options: [
                                    {flag: -1, dscrp: "全部"},
                                    {flag: 0, dscrp: "待仓审"},
                                    {flag: 1, dscrp: "待财审"},
                                    {flag: 2, dscrp: "已财审"},
                                ],
                                valueMap: ["flag", "dscrp"]
                            }
                        },
                        {title: "确认状态", property: "priceflag", selectOptions: {
                                options: [
                                    {flag: -1, dscrp: "全部"},
                                    {flag: 0, dscrp: "待确认"},
                                    {flag: 2, dscrp: "初确认"},
                                    {flag: 1, dscrp: "已确认"},
                                ],
                                valueMap: ["flag", "dscrp"]
                            }
                        },
                        {title: "发货日期/要求交期开始", property: "begindate", type: "date"},
                        {title: "发货日期/要求交期结束", property: "enddate", type: "date"},
                        {title: "客户名称", property: "cusname"}, 
                        {title: "车牌",property: "carcode"}
                    ]
                }
                else {
                    result = [
                        {title: "仓库", property: "storageid", selectOptions: {
                                optionsApi: "GetL1StorageList",
                                valueMap: ["storageid", "storagename"],
                                extraOptions: [{
                                    storageid: -1,
                                    storagename: "全部"
                                }]
                            }
                        },
                        {title: "审核状态", property: "flag", selectOptions: {
                                options: [
                                    {flag: -1, dscrp: "全部"},
                                    {flag: 0, dscrp: "待仓审"},
                                    {flag: 1, dscrp: "待财审"},
                                    {flag: 2, dscrp: "已财审"},
                                ],
                                valueMap: ["flag", "dscrp"]
                            }
                        },
                        {title: "确认状态", property: "priceflag", selectOptions: {
                                options: [
                                    {flag: -1, dscrp: "全部"},
                                    {flag: 0, dscrp: "待确认"},
                                    {flag: 1, dscrp: "已确认"},
                                ],
                                valueMap: ["flag", "dscrp"]
                            }
                        },
                        {title: "发货日期/要求交期开始", property: "begindate", type: "date"},
                        {title: "发货日期/要求交期结束", property: "enddate", type: "date"},
                        {title: "客户名称", property: "cusname"}, 
                    ]
                }
                return result;
            },
            ifSummary(){
                if(this.billtype == 2) return false;
                if ($lj.checkPower(122) && $lj.getStorage('user').empid != 0) {
                    return false;  
                }
                else{
                    return true;
                }
            },
            isShowCusname(data) {
                let empid = $lj.getStorage("userInfo").empid;
                return function (data) {
                    let msg = "";
                    if (empid != 0 && $lj.checkPower(1210)) {
                        msg = data.cuscode;
                    } else {
                        msg = data.cusname;
                    }
                    return msg;
                }
            },
        }
    }
</script>