/**
 * CSES 蓝牙打印
 * 实现逻辑：通过获取数据库存存储的模板信息(理论上只需获取一次就可以,但是没有一个全局js,每次加载页面会重新加载main.js,导致每次都会重新加载此页面,无法实现单例),然后传入打印的界面,获取界面中上次存储或打印过的模板,按照模板提供的value获取值并打印
 * 待完善：
 * 1、
 * 方式一(用于属性值需要重新定义):
 * $BlueToothPrintTemplate.getAlreadySaveTemplateInfo().then(function(){
    let self2 = self;
    $BlueToothPrintTemplate.setBlutToothPrintUseModule(api.winName).then(function() {
        $BlueToothPrintTemplate.getBlueToothPrintUseTemplate();
        for(let i = 0;i < self2.printBarcodeList.length;i++){
            $BlueToothPrintTemplate.GetBlueToothPrint(self2.printBarcodeList[i]);
        }
        $BlueToothPrintTemplate.getBlueToothPrintUsePrinter().print();
    })
})
 * 方式二：
    $BlueToothPrintTemplate.BlueToothPrintRealPrint(api.winName,item) 也可以拓展新方法将item变list
 */
import BlueToothPrint from './bluetooth-print.js'
import {tableRow,tableCell} from './bluetooth-print.js'

// 单例模式，生成唯一打印类
let ProxyCreateBlutToothPrintSingleton = (function(){
    let instance;
    return function(w, h, x, y,s) {
        // 代理函数仅作管控单例
        if (instance) {
            instance.init(w, h, x, y,s);
            return instance;
        }
        return instance = new BlueToothPrint(w, h, x, y,s);
    }
})();

// 单例模式，生成唯一打印模板类
export let ProxyCreateTemplateSingleton = (function(){
    let instance;
    return function() {
        // 代理函数仅作管控单例
        if (instance) {
            return instance;
        }
        return instance = new BlueToothPrintTemplate();
    }
})();

const CSES_BLUETOOTH_PRINT_DWNAME = "CSES_BLUETOOTH_PRINT";
const CSES_BLUETOOTH_PRINT_ITEMNAME = "CSES_BLUETOOTH_PRINT_TEMPLATE";

// 对应的模块可使用的模板,list表示可供选择的模板
const CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE = [
    {text:"库存表",value:"mtrlware-test",list:["CSES_BLUETOOTH_PRINT_TEMPLATE_1","CSES_BLUETOOTH_PRINT_TEMPLATE_2"]},
    {text:"销售发货单",value:"outware-sale",list:["CSES_BLUETOOTH_PRINT_TEMPLATE_1","CSES_BLUETOOTH_PRINT_TEMPLATE_2"]},
    {text:"条码查询",value:"barcode-info",list:["CSES_BLUETOOTH_PRINT_TEMPLATE_1","CSES_BLUETOOTH_PRINT_TEMPLATE_2"]},
    {text:"条码调整单",value:"barcode-change",list:["CSES_BLUETOOTH_PRINT_TEMPLATE_1","CSES_BLUETOOTH_PRINT_TEMPLATE_2"]},
    {text:"条码变更单",value:"barcode-modify",list:["CSES_BLUETOOTH_PRINT_TEMPLATE_1","CSES_BLUETOOTH_PRINT_TEMPLATE_2"]},
    {text:"仓库调拨单",value:"outware-other50",list:["CSES_BLUETOOTH_PRINT_TEMPLATE_1","CSES_BLUETOOTH_PRINT_TEMPLATE_2"]},
    {text:"生产领料单",value:"outware-scllfrm",list:["CSES_BLUETOOTH_PRINT_TEMPLATE_1","CSES_BLUETOOTH_PRINT_TEMPLATE_2"]},
    {text:"采购收货单",value:"inware-buy-sptcheck-mx",list:["CSES_BLUETOOTH_PRINT_TEMPLATE_3"]},
]

// 默认模板1
var CSES_BLUETOOTH_PRINT_TEMPLATE_1 = 
[
    {
        text:"名称:",
        property: "signal0",
        type: "select",
        value: "mtrlname",
        test: "名称样式测试名称样式测试名称样式测试",
        useable: true
    },
    {
        text:"规格:",
        property: "signal1",
        type: "select",
        value: "mtrlmode",
        test: "规格样式测试",
        useable: true
    },
    {
        text:"批次:",
        property: "signal2",
        type: "select",
        value: "mtrlcuscode",
        test: "批次样式测试",
        useable: true
    },
    {
        text:"仓库:",
        property: "signal3",
        type: "select",
        value: "storagename",
        test: "仓库样式测试",
        useable: true
    },
    {
        text:"仓位:",
        property: "signal4",
        type: "select",
        value: "location",
        test: "仓位样式测试",
        useable: true
    },
    {
        text:"数量:",
        property: "signal5",
        type: "select",
        value: "p_qty",
        test: "1",
        useable: true
    },
    {
        text:"条码号",
        sublable: "barcode",
        property: "barcode",
        type: "barcode",
        value: "barcode",
        test: "条码号样式测试",
        range: ["mtrlcode","barcode"],
        useable: true
    }
]
// 默认模板2
var CSES_BLUETOOTH_PRINT_TEMPLATE_2 = 
[
    {
        text:"标题",
        property: "signal0",
        type: "select",
        value: "mtrlcode",
        test: "标题样式测试",
        useable: true
    },
    {
        text:"客户型号",
        sublable: "Customer Part",
        property: "signal1",
        type: "select",
        value: "cusmtrlname",
        test: "客户型号样式测试",
        useable: true
    },
    {
        text:"产品名称",
        sublable: "Product Name",
        property: "signal2",
        type: "select",
        value: "mtrlname",
        test: "产品名称样式测试",
        useable: true
    },
    {
        text:"批号",
        sublable: "Batch Number",
        property: "signal3",
        type: "select",
        value: "plancode",
        test: "批号样式测试",
        useable: true
    },
    {
        text:"数量",
        sublable: "Quantity",
        property: "signal4",
        type: "select",
        value: "p_qty",
        test: "1",
        useable: true
    },
    {
        text:"日期",
        sublable: "Date",
        property: "signal5",
        type: "datetime",
        value: "p_date",
        test: "2023-01-16",
        useable: true
    },
    {
        text:"装箱员:",
        sublable: "Packed By",
        property: "signal6",
        type: "text",
        value: "packedby",
        test: "装箱样式测试",
        useable: true
    },
    {
        text:"检验员:",
        sublable: "Inspector",
        property: "signal7",
        type: "text",
        value: "inspector",
        test: "检验员样式测试",
        useable: true
    },
    {
        text:"条码号",
        sublable: "barcode",
        property: "barcode",
        type: "barcode",
        value: "barcode",
        test: "条码号样式测试",
        range: ["mtrlcode","barcode"],
        useable: true
    },
    {
        text:"物料规格",
        sublable: "mtrlmode",
        property: "signal8",
        type: "select",
        value: "mtrlmode",
        test: "物料规格样式测试",
        useable: false
    }
]
// 默认模板3
var CSES_BLUETOOTH_PRINT_TEMPLATE_3 = 
[
    {
        text:"[编码]:",
        property: "signal0",
        type: "select",
        value: "mtrlcode",
        test: "编码样式测试",
        useable: true
    },
    {
        text:"[名称]:",
        sublable: "Customer Part",
        property: "signal1",
        type: "select",
        value: "p_mtrlname",
        test: "客户型号样式测试",
        useable: true
    },
    {
        text:"[Q  C]:",
        sublable: "Product Name",
        property: "signal2",
        type: "select",
        value: "first_auditemp",
        test: "产品名称样式测试",
        useable: true
    },
    {
        text:"[批号]:",
        sublable: "Batch Number",
        property: "signal3",
        type: "select",
        value: "relSaleTaskCode",
        test: "批号样式测试",
        useable: true
    },
    {
        text:"[数量]:",
        sublable: "Quantity",
        property: "signal4",
        type: "select",
        value: "factRecQty",
        test: "1",
        useable: true
    },
    {
        text:"日期",
        sublable: "Date",
        property: "signal5",
        type: "datetime",
        value: "p_date",
        test: "2023-01-16 17:00:00",
        useable: true
    },
    {
        text:"条码号",
        sublable: "barcode",
        property: "barcode",
        type: "barcode",
        value: "mtrlcode",
        test: "条码号样式测试",
        range: ["mtrlcode","barcode"],
        useable: true
    },
    {
        text:"物料规格",
        sublable: "mtrlmode",
        property: "signal8",
        type: "select",
        value: "mtrlmode",
        test: "物料规格样式测试",
        useable: false
    }
]

// 打印模板 
var CSES_BLUETOOTH_PRINT_TEMPLATE = {
    "CSES_BLUETOOTH_PRINT_TEMPLATE_1":CSES_BLUETOOTH_PRINT_TEMPLATE_1,
    "CSES_BLUETOOTH_PRINT_TEMPLATE_2":CSES_BLUETOOTH_PRINT_TEMPLATE_2,
    "CSES_BLUETOOTH_PRINT_TEMPLATE_3":CSES_BLUETOOTH_PRINT_TEMPLATE_3
}

// 设置纸张样式
var CSES_BLUETOOTH_PRINT_PAPERSTYLE = [
    {text: "连续纸",value: 0},
    {text: "间隔纸",value: 1}
]

// 可使用模板信息列表
var CSES_BLUETOOTH_PRINT_TEMPLATE_INFO = [
    {text:"模板1",value:"CSES_BLUETOOTH_PRINT_TEMPLATE_1",notes:"华宇",width:576,height:576,initX:20,initY:10,uses:[],style:CSES_BLUETOOTH_PRINT_PAPERSTYLE[1]},
    {text:"模板2",value:"CSES_BLUETOOTH_PRINT_TEMPLATE_2",notes:"路得",width:576,height:500,initX:20,initY:10,uses:[],style:CSES_BLUETOOTH_PRINT_PAPERSTYLE[1]},
    {text:"模板3",value:"CSES_BLUETOOTH_PRINT_TEMPLATE_3",notes:"路得",width:576,height:816,initX:20,initY:10,uses:[],style:CSES_BLUETOOTH_PRINT_PAPERSTYLE[1]},
]

class BlueToothPrintTemplate {
    // 当前选择打印的模块
    CSES_BLUETOOTH_PRINT_USE_MODULE;
    // 当前选择模板的可使用的模板信息列表
    CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST;
    // 当前打印使用模板信息
    CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO;
    // 当前打印使用模板
    CSES_BLUETOOTH_PRINT_USE_TEMPLATE;
    // 当前打印使用对象
    CSES_BLUETOOTH_PRINT_USE_PRINTER;
    // 当前打印使用参数
    CSES_BLUETOOTH_PRINT_USE_INIT_PARAM;

    constructor() {
        this.CSES_BLUETOOTH_PRINT_USE_MODULE = null;
        this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST = null;
        this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO = null;
        this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE = null;
        this.CSES_BLUETOOTH_PRINT_USE_PRINTER = null;
        this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM = null;
    }

    getBluePrintDwName() {
        return CSES_BLUETOOTH_PRINT_DWNAME;
    }
    getBluePrintItemName() {
        return CSES_BLUETOOTH_PRINT_ITEMNAME;
    }
    getBluePrintModule() {
        return CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE;
    }
    getBluePrintTemplateInfo() {
        return CSES_BLUETOOTH_PRINT_TEMPLATE_INFO;
    }
    getBlueToothPrintPaperStyle() {
        return CSES_BLUETOOTH_PRINT_PAPERSTYLE;
    }

    // getter
    getBlueToothPrintUseModule() {return this.CSES_BLUETOOTH_PRINT_USE_MODULE;}
    getBlueToothPrintUseTemplateInfoList() {return this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST;}
    getBlueToothPrintUseTemplateInfo() {return this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO;}
    getBlueToothPrintUseTemplate() {return this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE;}
    getBlueToothPrintUsePrinter() {return this.CSES_BLUETOOTH_PRINT_USE_PRINTER;}
    getBlueToothPrintUseInitParam() {return this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM;}
    

    getAlreadySaveTemplateInfo() {
        let self = this;
        return new Promise(function(resolve,reject) {
            let self2 = self;
            self.getSysUserFileString(CSES_BLUETOOTH_PRINT_DWNAME,CSES_BLUETOOTH_PRINT_ITEMNAME,function(itemvalue){
                if(itemvalue != '') {
                    self2.setBlueToothPrintTemplateInfo(JSON.parse(itemvalue))
                }

                resolve();
            })
        })
    }

    // 设置全局模板信息,判断是否值有改变,有就按照出厂设置
    setBlueToothPrintTemplateInfo(infos) {
        var Obj = new Object();
        for(let i = 0;i < infos.length;i++) {
            Obj[infos[i].value] = infos[i];
        }

        console.log("obj>>>>",JSON.stringify(Obj))

        for(let i = 0;i < CSES_BLUETOOTH_PRINT_TEMPLATE_INFO.length;i++) {
            if(CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i].value in Obj) {
                // 判断固定的值是否有改变
                infos.text = CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i].value;
                infos.width = CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i].width;
                infos.height = CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i].height;
                infos.initX = CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i].initX;
                infos.initY = CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i].initY;
            } else {
                // 出厂设置新增了新的信息
                infos.push(CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i]);
            }
        }

        CSES_BLUETOOTH_PRINT_TEMPLATE_INFO = infos;
    }

    setBlueToothPrintPaperStyle(s) {
        this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.style = s;
    }

    /**
     * 设置模块后,自动获取对应模块可使用的模板信息列表
     * 查找列表中,useable = true的模板,并设置为当前打印使用模板信息
     * 如果useable都为false,则设置第一个为当前打印使用模板信息
     * 然后通过GetSysUserFileString接口,获取保存过的模板
     * 如果返回空值，证明没有保存过模板,则使用默认模板
     * 最后初始化打印
     * @param {模块名} m 
     */
    setBlutToothPrintUseModule(m) {
        let self = this;

        return new Promise(function(resolve,reject) {
            // 设置模块
            for(let i = 0;i < CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE.length;i++) {
                if(CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE[i].value == m) {
                    self.CSES_BLUETOOTH_PRINT_USE_MODULE = CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE[i];
                    break;
                }
            }

            if(self.CSES_BLUETOOTH_PRINT_USE_MODULE == null) {
                alert("模块没有适配任何打印功能,请联系管理员");
                return;
            }

            // console.log("self.CSES_BLUETOOTH_PRINT_USE_MODULE.>>>>>>",JSON.stringify(self.CSES_BLUETOOTH_PRINT_USE_MODULE))

            let self2 = self;

            // 然后通过GetSysUserFileString接口,获取保存过的模板
            self.getSysUserFileString(CSES_BLUETOOTH_PRINT_DWNAME,self.CSES_BLUETOOTH_PRINT_USE_MODULE.value,function(itemvalue){

                if(itemvalue != '') {
                    self2.setBlueToothPrintTemplate(JSON.parse(itemvalue));
                }

                self2.setBlueToothPrintUseTemplateInfoList();

                resolve();
            })
        })
    }
    // 设置全局模板,判断是否值有改变,有就按照出厂设置
    setBlueToothPrintTemplate(templates) {
        for(var key in CSES_BLUETOOTH_PRINT_TEMPLATE) {
            if(!(key in templates)) {
                templates.key = CSES_BLUETOOTH_PRINT_TEMPLATE[key];
                continue;
            }

            // 已存在模板,判断属性值是否有 出现改变,待补充,因实现会影响效率
        }

        CSES_BLUETOOTH_PRINT_TEMPLATE = templates;
    }

    // 设置对应模块可使用的模板信息列表
    setBlueToothPrintUseTemplateInfoList() {
        this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST = [];
        // 自动获取对应模块可使用的模板信息列表
        var list = null;
        console.log("CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE>>>",JSON.stringify(CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE))
        console.log("CSES_BLUETOOTH_PRINT_USE_MODULE>>>",JSON.stringify(this.CSES_BLUETOOTH_PRINT_USE_MODULE))
        for(let i = 0;i < CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE.length;i++) {
            if(CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE[i] == this.CSES_BLUETOOTH_PRINT_USE_MODULE) {
                list = CSES_BLUETOOTH_PRINT_MODULE_USEABLE_TEMPLATE[i].list;
                break;
            }
        }
        
        console.log("CSES_BLUETOOTH_PRINT_TEMPLATE_INFO>>>",JSON.stringify(CSES_BLUETOOTH_PRINT_TEMPLATE_INFO))
        for(let i = 0;i < CSES_BLUETOOTH_PRINT_TEMPLATE_INFO.length;i++) {
            if(list.includes(CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i].value)) {
                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST.push(CSES_BLUETOOTH_PRINT_TEMPLATE_INFO[i]);
            }
        }

        if(this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST.length <= 0) {
            alert("当前模块没有可使用的模板信息,请联系管理员!");
            return;
        }
        // 查找列表中,useable = true的模板,并设置为当前打印使用模板信息
        var isExist = false;
        for(let i = 0;i < this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST.length;i++) {
            isExist = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[i].uses.includes(this.CSES_BLUETOOTH_PRINT_USE_MODULE.value);

            if(isExist) {
                this.setBlueToothPrintUseTemplateInfo(this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[i]);
                break;
            }
        }
        // 如果useable都为false,则设置第一个为当前打印使用模板信息
        if(!isExist) {
            this.setBlueToothPrintUseTemplateInfo(this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[0]);
        }
    }

    /**
     * 设置当前使用模板信息
     * 遍历模块中可使用的模板信息列表,查找对应的模板
     * @param {模板信息} info 
     */
    setBlueToothPrintUseTemplateInfo(info) {
        // console.log("info>>>>>>>>>>>>",JSON.stringify(info));
        for(let i = 0;i < this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST.length;i++) {
            if(this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[i].value == info.value) {
                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[i];

                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.width = info.width;
                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.height = info.height;
                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.initX = info.initX;
                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.initY = info.initY;
                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.style = info.style;

                // 如果uses存在此模块,跳过
                if(this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[i].uses.includes(this.CSES_BLUETOOTH_PRINT_USE_MODULE.value)) {
                    continue;
                }

                // 不存在,添加
                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[i].uses.push(this.CSES_BLUETOOTH_PRINT_USE_MODULE.value);
            } else {
                // 其他模块信息如果存在当前使用模块，则删除
                var index = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[i].uses.indexOf(this.CSES_BLUETOOTH_PRINT_USE_MODULE.value)
                if(index < 0) {
                    continue;
                }

                this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO_LIST[i].uses.splice(index,1);
            }
        }
        
        // 根据模板信息,获取对应模板
        this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE = CSES_BLUETOOTH_PRINT_TEMPLATE[this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.value];

        this.initBlueToothPrintParam();
    }

    setBlueToothPrintTemplateValue(oIndex,nIndex) {
        var temp = new Object();

        if(this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].type == "barcode") {
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].value = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].range[nIndex]
        } else {
            temp.text = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].text;
            temp.sublable = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].sublable;
            temp.value = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].value;
            temp.test = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].test;
            temp.type = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].type;

            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].text = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].text;
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].sublable = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].sublable;
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].value = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].value;
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].test = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].test;
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[oIndex].type = this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].type;

            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].text = temp.text;
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].sublable = temp.sublable;
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].value = temp.value;
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].test = temp.test;
            this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[nIndex].type = temp.type;
        }

        this.CSES_BLUETOOTH_PRINT_USE_PRINTER.clearCanvas();
    }

    // 初始化打印参数,必须优先使用
    initBlueToothPrintParam() {
        this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM = 
        {
            width: this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.width,
            height: this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.height,
            initX: this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.initX,
            initY: this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.initY,
            style: this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.style.value
        }

        if(this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM.style === 1) {
            this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM.height = 0;
        }

        this.CSES_BLUETOOTH_PRINT_USE_PRINTER = new ProxyCreateBlutToothPrintSingleton(this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM.width,this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM.height,this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM.initX,this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM.initY,this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM.style);
    }

    SaveBlueToothTemplateInfo() {
        var info = JSON.stringify(CSES_BLUETOOTH_PRINT_TEMPLATE_INFO)
        this.setSysUserFileString(CSES_BLUETOOTH_PRINT_DWNAME,CSES_BLUETOOTH_PRINT_ITEMNAME,info,
            () => {
                $lj.toast("保存蓝牙打印模板信息成功!");
            })
    }

    SaveBlueToothModuleTemplate() {
        var info = JSON.stringify(CSES_BLUETOOTH_PRINT_TEMPLATE)
        this.setSysUserFileString(CSES_BLUETOOTH_PRINT_DWNAME,this.CSES_BLUETOOTH_PRINT_USE_MODULE.value,info,
            () => {
                $lj.toast("保存蓝牙打印模板成功!");
            })
    }

    // 获取通过模板使用的属性汇总打印信息
    GetBlueToothPrint(obj) {
        var ifTest = false
        if(obj == null || Object.keys(obj).length <= 0) {
            ifTest = true;
        }

        var content = new Object();
    
        for(let i = 0;i < this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE.length;i++) {
    
            if(!this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[i].useable) continue;
    
            content[this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[i].property] = {
                text: this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[i].text,
                sublable: this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[i].sublable,
                value: ifTest ? this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[i].test : (`${obj[this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE[i].value]}` || ' ')
            }
        }

        // console.log("content>>>>",JSON.stringify(content));
        
        this.AdapteBlueToothPrintFunction(content);
    }
    
    // 适配模板的打印方法
    AdapteBlueToothPrintFunction (content) {
    
        if(this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE == null) return;
    
        switch (this.CSES_BLUETOOTH_PRINT_USE_TEMPLATE_INFO.value) {
            case "CSES_BLUETOOTH_PRINT_TEMPLATE_1":
                this.BlueToothPrintTemplate_1Fun(content)
                break;
            case "CSES_BLUETOOTH_PRINT_TEMPLATE_2":
                this.BlueToothPrintTemplate_2Fun(content);
                break;
            case "CSES_BLUETOOTH_PRINT_TEMPLATE_3":
                this.BlueToothPrintTemplate_3Fun(content);
                break;
            default:
                alert("请先设置打印格式!");
                break;
        }
    }

    /*****************************             工具                       ***************************/
    getSysUserFileString(dwname,itemname,callback) {
        let requestBody = {
            token: $lj.getStorage("token"),
            empid: -1,
            dwname: dwname,
            itemname: itemname,
            ifcompress: 0
        };
        $lj.postLJRequest("GetSysUserFileString", requestBody, function (ret) {
            callback && callback(ret.itemvalue);
        });
    }

    setSysUserFileString(dwname,itemname,itemvalue,callback){
        var requestBody = {
            token : $lj.getStorage("token"),
            empid : -1,
            dwname : dwname,
            itemname : itemname,
            itemvalue : itemvalue
        }
        $lj.postLJRequest('SetSysUserFileString', requestBody, function (ret) {
            if(ret){
                console.log("保存成功>>>",dwname,itemname);
                callback && callback(ret);
            }
        }, function() {}, true);
    }

    // 模板2的打印方法
    BlueToothPrintTemplate_1Fun(content) {
        let init_param = this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM
        let printer = this.CSES_BLUETOOTH_PRINT_USE_PRINTER;
        let u = 10;
        let qrwidth = u * 21;
        let qrheight = u * 21;

        printer.setBerlinWall(init_param.width - init_param.initX * 2 - qrwidth);
        //
        printer.drawText(true,false,7,0,0,0,content.signal0.text);
        printer.drawTextBox(false,true,7,0,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal0.value);
        printer.clearOffsetX();
        printer.drawText(true,false,7,0,0,0,content.signal1.text);
        printer.drawTextBox(false,true,7,0,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal1.value);
        printer.clearOffsetX();
        printer.drawText(true,false,7,0,0,0,content.signal2.text);
        printer.drawTextBox(false,true,7,0,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal2.value);
        printer.clearOffsetX();
        printer.drawText(true,false,7,0,0,0,content.signal3.text);
        printer.drawTextBox(false,true,7,0,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal3.value);
        printer.clearOffsetX();
        if(content.signal4.value != '') {
            printer.drawText(true,false,7,0,0,0,content.signal4.text);
            printer.drawTextBox(false,true,7,0,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal4.value);
            printer.clearOffsetX();
        }
        printer.drawText(true,false,7,0,0,0,content.signal5.text);
        printer.drawTextBox(false,true,7,0,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal5.value);
        //
        printer.clearOffsetX();
        printer.clearOffsetY();
        printer.drawQrBarcode(false,true,init_param.width - init_param.initX * 2 - qrwidth,0,qrwidth,qrheight,content.barcode.value,null);

        printer.putMapToArrayList();
    }

    BlueToothPrintTemplate_2Fun(content) {
        let init_param = this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM
        let printer = this.CSES_BLUETOOTH_PRINT_USE_PRINTER;
        let u = 7;
        let qrwidth = u * 5 * 4;
        let qrheight = u * 5 * 4;
        let tableRow1 = new tableRow(0);
        tableRow1.addTableCell(new tableCell("text",content.signal0.value,init_param.width - init_param.initX * 2 - qrwidth,0,4,3));
        //客户型号
        let tableRow2 = new tableRow(0);
        let rowcell2 = new tableCell("text",content.signal1.text,120,0,1,0);
        let cell2_child = new tableCell("text",content.signal1.sublable,0,0,0,0);
        cell2_child.setPadding(0);
        rowcell2.addChild(cell2_child);
        tableRow2.addTableCell(rowcell2);
        tableRow2.addTableCell(new tableCell("text",content.signal1.value,init_param.width - init_param.initX * 2 - qrwidth - 120,0,7,0));
        //产品名称
        let tableRow3 = new tableRow(0);
        let rowcell3 = new tableCell("text",content.signal2.text,120,0,1,0);
        let cell3_child = new tableCell("text",content.signal2.sublable,0,0,0,0);
        cell3_child.setPadding(0);
        rowcell3.addChild(cell3_child);
        tableRow3.addTableCell(rowcell3);
        tableRow3.addTableCell(new tableCell("text",content.signal2.value,init_param.width - init_param.initX * 2 - qrwidth - 120,0,7,0));
        printer.addTableRowList([tableRow1,tableRow2,tableRow3]);
        printer.drawTable();
        // 二维码
        var curY = printer.getOffsetY();
        let tableRow0 = new tableRow(0);
        let rowcell1 = new tableCell("barcode",content.barcode.value,qrwidth,curY - init_param.initY - 10,0,0)
        rowcell1.setOffsetX(init_param.width - init_param.initX * 2 - qrwidth)
        rowcell1.setOffsetY(-(curY - init_param.initY))
        rowcell1.setImage(null);
        tableRow0.addTableCell(rowcell1);
        printer.addTableRow(tableRow0);
        printer.drawTable();
        //批号
        printer.clearOffsetX();
        printer.clearOffsetY();
        printer.addOffsetY(curY - init_param.initY);
        let tableRow4 = new tableRow(0);
        let rowcell4 = new tableCell("text",content.signal3.text,120,0,1,0);
        let cell4_child = new tableCell("text",content.signal3.sublable,0,0,0,0);
        cell4_child.setPadding(0);
        rowcell4.addChild(cell4_child);
        tableRow4.addTableCell(rowcell4);
        tableRow4.addTableCell(new tableCell("text",content.signal3.value,init_param.width - init_param.initX * 2 - 120,0,7,0));
        //数量
        let tableRow5 = new tableRow(0);
        let rowcell5 = new tableCell("text",content.signal4.text,120,0,1,0);
        let cell5_child = new tableCell("text",content.signal4.sublable,0,0,0,0);
        cell5_child.setPadding(0);
        rowcell5.addChild(cell5_child);
        tableRow5.addTableCell(rowcell5);
        tableRow5.addTableCell(new tableCell("text",content.signal4.value,80,0,7,0));
        //日期
        let rowcell6 = new tableCell("text",content.signal5.text,120,0,1,0);
        let cell6_child = new tableCell("text",content.signal5.sublable,0,0,0,0);
        cell6_child.setPadding(0);
        rowcell6.addChild(cell6_child);
        tableRow5.addTableCell(rowcell6);
        tableRow5.addTableCell(new tableCell("text",content.signal5.value,init_param.width - init_param.initX * 2 - 320,0,7,0));
    
        printer.addTableRowList([tableRow4,tableRow5]);
        printer.drawTable();
        //装箱员
        printer.addOffsetY(20);
        printer.drawText(false,false,1,0,0,0,content.signal6.text);
        printer.drawText(false,true,1,0,(init_param.width - init_param.initX * 2) >> 1,0,content.signal7.text);
        //检验员
        printer.drawText(false,false,0,0,0,0,content.signal6.sublable);
        printer.drawText(false,true,0,0,(init_param.width - init_param.initX * 2) >> 1,0,content.signal7.sublable);
    
        printer.putMapToArrayList();
    }

    BlueToothPrintTemplate_3Fun(content) {
        let init_param = this.CSES_BLUETOOTH_PRINT_USE_INIT_PARAM
        let printer = this.CSES_BLUETOOTH_PRINT_USE_PRINTER;
        let u = 8;
        let qrwidth = u * 21;
        let qrheight = u * 21;

        printer.setBerlinWall(0);
        //
        printer.drawText(true,false,55,3,0,0,content.signal0.text);
        printer.drawTextBox(false,true,55,3,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal0.value);
        printer.clearOffsetX();
        printer.drawText(true,false,55,3,0,0,content.signal1.text);
        printer.drawTextBox(false,true,24,3,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal1.value);
        printer.clearOffsetX();
        printer.drawText(true,false,55,3,0,0,content.signal2.text);
        printer.drawTextBox(false,true,55,3,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal2.value);
        printer.clearOffsetX();
        printer.drawText(true,false,55,3,0,0,content.signal3.text);
        printer.drawTextBox(false,true,55,3,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal3.value);
        printer.clearOffsetX();
        printer.drawText(true,false,55,3,0,0,content.signal4.text);
        printer.drawTextBox(false,true,55,3,0,0,init_param.width - init_param.initX * 2 - qrwidth,0,content.signal4.value);
        
        //
        printer.clearOffsetX();
        printer.drawQrBarcode(true,true,init_param.width - init_param.initX * 2 - qrwidth,0,qrwidth,qrheight,content.barcode.value,null);
        printer.drawPoText(true,true,7,0,0,0,content.signal5.value);

        printer.putMapToArrayList();
    }

    BlueToothPrintRealPrint(winName,item) {
        let self = this;
        this.getAlreadySaveTemplateInfo().then(function(){
            let self2 = self;
            self.setBlutToothPrintUseModule(winName).then(function() {
                self2.getBlueToothPrintUseTemplate();
                self2.GetBlueToothPrint(item);
                self2.getBlueToothPrintUsePrinter().print();
            })
        })
    }
}

export default BlueToothPrintTemplate