<template>
    <lj-win-common :title="title" :show-back="true">
        <div slot="body">
            <template>
                <div style="height: 35px;padding-left: 15px; font-size: 18px;display: table-cell; vertical-align:middle;">选打印机</div>
                <mu-data-table class="lj-muse-data-table" :stripe="true" :border="true" :checkbox-col-width="checkBoxWidth"
                                @row-click="handlePrinterRowClick"
                                :selects.sync="printerSelects" checkbox :columns="printerColumns" :data="printerList">
                    <template slot-scope="scope">
                        <td class="is-center" >{{scope.row.printername}}</td>
                    </template>
                </mu-data-table>
            </template>
            <template>
                <div style="height: 35px;padding-left: 15px; font-size: 18px;display: table-cell; vertical-align:middle;">选打印格式</div>
                <mu-data-table class="lj-muse-data-table" :stripe="true" :border="true" :checkbox-col-width="checkBoxWidth"
                               @row-click="handleChnnameRowClick"
                               :selects.sync="chnnameSelects" checkbox :columns="chnnameColumns" :data="chnnameList">
                    <template slot-scope="scope">
                        <td class="is-center">{{scope.row.chnname}}</td>
                    </template>
                </mu-data-table>
            </template>

        </div>
        <template slot="footer">
            <mu-col tag="mu-button" color="blue" @click="handleFooterClick">{{footerTitle}}</mu-col>
        </template>
    </lj-win-common>
</template>

<script>
    import ljWinCommon from 'components/lj-win-common'

    /**
     * 界面传参
     * wintype int 0-设置 1-保存设置并发送任务
     * keyname string 单据名
     * parm string 打印参数
     * parmflag 打印参数flag
     * openwinname 打开窗口名称
     */
    export default {
        name: "print-setting",
        components: {
            ljWinCommon,
        },
        created() {
            this.mainObject = $lj.getPageParam();
            switch (this.mainObject.wintype) {
                case 0:
                    this.footerTitle = "保存";
                    break;
                case 1:
                    this.footerTitle = "打印";
                    break;
                case 2:
                    this.footerTitle = "确认";
                    break;
            }
            var printHabit = $lj.getStorage(this.mainObject.keyname);
            if (printHabit !== undefined){
                this.habitPrinter = printHabit.habitPrinter;
                this.habitChnname = printHabit.habitChnname;
            }
        },
        mounted() {
            var self  = this;
            $lj.postLJRequest("GetGWPrintInfo", {
                token: $lj.getStorage("token"),
                keyname: this.mainObject.keyname,
            }, function (rsp) {
                if (rsp) {
                    for (var i in rsp.printernames){
                        var printernameObj = {printername: rsp.printernames[i]};
                        self.printerList.push(printernameObj);
                        if (self.habitPrinter == printernameObj.printername){
                            self.printerSelects.push(Number(i));
                        }
                    }
                    for (var i in rsp.chnnames){
                        var chnnameObj = {chnname: rsp.chnnames[i]};
                        self.chnnameList.push(chnnameObj);
                        if (self.habitChnname == chnnameObj.chnname){
                            self.chnnameSelects.push(Number(i));
                        }
                    }
                }
            })
        },
        data() {
            return {
                checkBoxWidth: 90,
                printerSelects: [],
                chnnameSelects: [],
                chnnameList: [],
                printerList: [],
                footerTitle: "",
                mainObject: {},
                habitPrinter: "",
                habitChnname: "",
                printerColumns: [{ title: '打印机名', name: 'pritername',width: screen.width - 90, align: 'center'}],
                chnnameColumns: [{ title: '打印格式名', name: 'chnname',width: screen.width - 90, align: 'center'}]

            }
        },
        methods: {
            handleFooterClick(){
                //DONE:保存成用户习惯
                if (this.printerSelects.length <= 0){
                    alert("请先选择打印机!");
                    return;
                }
                if (this.chnnameSelects.length <= 0){
                    alert("请先选择打印格式");
                    return;
                }
                let habitPrint = { habitPrinter: this.printerList[this.printerSelects[0]].printername, habitChnname: this.chnnameList[this.chnnameSelects[0]].chnname};
                $lj.setStorage(this.mainObject.keyname, habitPrint);
                if (this.mainObject.wintype === 0){
                    $lj.toast("保存成功！", 2000);
                    $lj.closeWin();
                    return;
                } else if (this.mainObject.wintype === 1){
                    var self = this;
                    $lj.pushPrintTask({
                        keyname: this.mainObject.keyname,
                        chnname: habitPrint.habitChnname,
                        parm: "," + this.mainObject.parm + ",",
                        parmflag: this.mainObject.parmflag,
                        printername: habitPrint.habitPrinter
                    },function (rsp) {
                        if (rsp) {
                            $lj.toast("打印成功！", 2000);
                            $lj.closeWin(self.mainObject.openwinname);
                            $lj.closeWin();
                        }
                    });
                } else if (this.mainObject.wintype === 2){
                    $lj.sendEvent("printer_select",
                        {
                            printer: habitPrint.habitPrinter,
                            chnname: habitPrint.habitChnname
                        }
                    );
                    $lj.closeWin();
                }
            },
            handlePrinterRowClick(index,row){
                this.printerSelects = [];
                this.printerSelects.push(index);
            },
            handleChnnameRowClick(index,row){
                this.chnnameSelects = [];
                this.chnnameSelects.push(index);
            }
        },
        computed: {
            title() {
                return "打印设置"
            }
        }
    }
</script>
<style>

</style>
