<template>
    <lj-win-index-common :title="title" api-name="GetL1Sptcheck"
                         :data-reset-callback="handleDataReset"
                         :data-loaded-callback="handleDataLoaded"
                         ref="baseWin"
                         :query-hard-parms="queryHardParms"
                         :query-fields="queryFields"
                         :binding-parms="bindingParms">
        <mu-button icon slot="header_right" @click="handleScan">
            <i class="iconfont icon-qrcode-scan"></i></mu-button>
        <mu-paper :z-depth="3" slot="header_sub" style="background-color: white;">
            <div style="width: 100%;">
                <lj-searchbar hint="输入送检单号/订单号/相关号进行查找" @search="handleSearchbar" v-model="bindingParms.keyword"/>
            </div>
            <div style="margin-left: 10px;">待终审：{{secondAudit}}，已审核：{{audited}}</div>
        </mu-paper>
        <div slot="body_list">
            <div>
                <template v-for="item in allData">
                    <submit-item :item="item" @click.native="handleCellClick(item)"></submit-item>
                </template>
            </div>
        </div>
        <template slot="footer">
            <mu-row>
                <mu-col tag="mu-button" color="blue" @click="handleFooterClick">打印暂收单</mu-col>
                <mu-col tag="mu-button" color="blue" @click="handleSelectOrderMl">选指令单新建</mu-col>
            </mu-row>
        </template>
    </lj-win-index-common>
</template>

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

    export default {
        components: {
            LjSearchbar,
            LjWinIndexCommon,
            submitItem
        },
        data() {
            return {
                bindingParms: {keyword: ""},
                firstAudit: 0,
                secondAudit: 0,
                audited: 0,
                winMode: "",
                allData: [],
                queryHardParms: {scid: 0, billtype: 2},
                queryFields: [
                    {
                        title: "状态",
                        property: "flag",
                        selectOptions: {
                            options: [
                                {flag: -1, dscrp: "全部"},
                                {flag: 0, dscrp: "待审核"},
                                {flag: 4, dscrp: "待终审"},
                                {flag: 1, dscrp: "已审核"}
                            ],
                            valueMap: ["flag", "dscrp"]
                        },
                        disabled: false
                    },
                    {title: "收货人", property: "opemp"},
                    {title: "开始时间", property: "begindate", type: "date"},
                    {title: "结束时间", property: "enddate", type: "date"},
                    {title: "送检单号/订单号/相关号", property: "keyword"}
                ]
            }
        },
        created() {
            var userinfo = $lj.getStorage('user');
            this.queryFields[1].initValue = userinfo.username;
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
            }
        },
        methods: {
            handleDataReset(response) {
                this.allData.length = 0;
                this.addList(response.sptcheckList);
                this.firstAudit = 0;
                this.secondAudit = 0;
                this.audited = 0;
                for (var i in this.allData) {
                    switch (this.allData[i].flag) {
                        case 0:
                            this.firstAudit++;
                            break;
                        case 1:
                            this.audited++;
                            break;
                        case 4:
                            this.secondAudit++;
                            break;
                        default:
                            break;
                    }
                }
            },
            handleDataLoaded(response) {
                let moreItems = response.sptcheckList;
                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(Object.assign({keyword: text}, this.queryHardParms));
            },
            handleCellClick(mainObject) {
                if (this.isSelectMode) {
                    //选择界面
                } else {
                    $lj.openWin("sptcheck-zz-mx", mainObject);
                }
            },
            handleFooterClick() {
                $lj.openWin("sptcheck-print", {sptcheckList: this.allData})
            },
            handleScan() {
                var self = this;
                $lj.openScanner(function(ret) {
                    var ordercode = ret.content;
                    if (ordercode == "") {
                        alert("请先输入指令单号!");
                        return;
                    }
                    self.handleSaveL1ZZCheck(ordercode);
                });
            },
            handleSelectOrderMl() {
                let self = this;
                $lj.removeEventListener("orderml_select");
                $lj.addEventListener("orderml_select", function (ret) {
                    if (ret.value.selectedItems) {
                        self.handleSaveL1ZZCheck(ret.value.selectedItems[0].OrderCode);
                    }
                })

                $lj.openWin("orderml-selection", {winMode: "single-select"});
            },
            handleSaveL1ZZCheck(ordercode) {
                $lj.postLJRequest(
                    "SaveL1ZZCheck", {
                        token: $lj.getStorage("token"),
                        taskcode: ordercode,
                    },
                    function(rsp) {
                        if (rsp.ErrMsg === '') {
                            alert("指令单【" + ordercode + "】新建送检单成功！");
                            self.$refs.baseWin.refresh();
                        } else {
                            $lj.toast("未查询到条码【" + ordercode + "】该单号的信息！");
                        }
                    }
                );
            }
        },
        computed: {
            isSelectMode() {
                return this.winMode === "single-select";
            },
            title() {
                if (this.winMode === 'single-select') {
                    return "自制送检单选择";
                } else {
                    return "自制送检单";
                }
            }
        }
    }
</script>