
var datatable_content;
var detail_datatable_Content;

String.format = function (s) {
    var t = s;
    for (var i = 1; i < arguments.length; i++) {
        t = t.replace(eval("/\\{" + (i - 1) + "\\}/ig"), arguments[i]);
    }
    return t;
};

$(function(){
    $('#createTimeStart').jeDate({
    	format:"YYYY-MM-DD hh:mm:ss",
        hmsSetVal:{hh:00,mm:00,ss:00},
        festival : false,
        maxDate : '2099-06-30 23:59:59' //最大日期

    });
    $('#createTimeEnd').jeDate({    
    	format:"YYYY-MM-DD hh:mm:ss",
        hmsSetVal:{hh:23,mm:59,ss:59},
        festival : false,
        maxDate : '2099-06-30 23:59:59' //最大日期

    });
});

function initTable(){
    // var wmsPage = new WmsPage();
    WmsPage.fromId = "searchForm";//提前设置表单id
    datatable_content = $("#table-reprint").dataTable({
        "sAjaxSource": ctx + '/v3/output/express-sheet/reprint/data',
        //"fnServerData": callback,
        "fnServerData": WmsPage.callback,//WmsPage.callback是默认的数据回调处理方法，如果有个性化可以自己写。fromid需要使用默认的searchForm
        "iDisplayLength": 10, //每页数据
        "bPaginate": true, //翻页功能
        "bLengthChange": true, //改变每页显示数据数量
        "bFilter": false, //过滤功能
        "bSort": false, //排序功能
        "bInfo": true,//页脚信息
        "bAutoWidth": true,//自动宽度
        "bStateSave": false, //状态保存，使用了翻页或者改变了每页显示数据数量，会保存在cookie中，下回访问时会显示上一次关闭页面时的内容
        "bProcessing": true, //加载数据时显示正在加载信息
        "aoColumns":[
            {
            	"mData": null,
            	"mRender": function(data, display, row) {
            		return String.format('<input type="checkbox" name="chkOutOrderCode" value="{0}" data-wt="{1}" />', data.outOrderCode, data.workType); 
            	}
            },
            {"mData": "customerCode"},
            {"mData": "outOrderCode"},
            {"mData": "fromOrderCode"}, 
            {"mData": "expressCode"},
            {"mData": "expressName"},
            {
            	"mData": "workType",
            	"mRender": function(data) {
            		if (data == 'single') {
            			return "单件";
            		} else if (data == 'multi') {
            			return "多件";
            		} else if (data == 'db') {
            			return "调拨";
            		}
            		return data;
            	}
            },
            {"mData": "createTime"}
        ],
        "aoColumnDefs":[]
    });
}

function query() {
	if(!datatable_content ){
        initTable();
    }else{
        datatable_content.fnDraw();
    }
}

function mailReprint() {
	if ($('[name=chkOutOrderCode]:checked').length == 0) {
		jAlert('请选择需要打印的面单', '提示');
		return;
	}
	var codes = $('[name=chkOutOrderCode]:checked').map(function() {
		return $(this).val();
	}).get();
	
	jQuery.ajax({
		url: ctx + '/v3/output/express-sheet/reprint/mail',
		data: {orderCodes: codes.join(',')},
		success: function(resp) {
            jAlert(resp.message,"提示");
			if(resp.status == 1){
				//打印快递面单
			    initLodop();//初始化lodop
			    jQuery.each(resp.content || [], function(i, v) {
			    	var modelContent = v.printTemplateDTO;
			    	var expressLogistics=v.expressLogistics;
			    	if(expressLogistics==""){
			    		jAlert("面单补打获取物流渠道失败。")
						return;
					}
				    if(modelContent){
				        var render = template.compile(modelContent.content);
				        var html = render(v.addresseeInfoBo);
                       // jAlert(v.addresseeInfoBo.expressNo);
                       // jAlert(expressLogistics);
                        //return
                       // LODOP.SET_LICENSES("深圳市鲸仓科技有限公司", "BB720FD5AB0D521EB007D05CDE36CE65", "", "");
                        if(expressLogistics=="YUNDA"){
                          LODOP.ADD_PRINT_BARCODE((27)+"mm", (77)+"mm", "20mm", "8mm", "128AUTO", v.addresseeInfoBo.expressNo+"");
                            LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);
                            LODOP.ADD_PRINT_BARCODE((69)+"mm", (20)+"mm", "70mm", "17mm", "128AUTO", v.addresseeInfoBo.expressNo+"");
                            LODOP.ADD_PRINT_BARCODE((112)+"mm", (45)+"mm", "50mm", "12mm", "128AUTO", v.addresseeInfoBo.expressNo+"");
                        }else if(expressLogistics=="JDKY"){
                            LODOP.ADD_PRINT_BARCODE((5)+"mm", (10)+"mm", "70mm", "12mm", "128AUTO", v.addresseeInfoBo.expressNo+"");
                            LODOP.ADD_PRINT_BARCODE((78)+"mm", (5)+"mm", "50mm", "8mm", "128AUTO", v.addresseeInfoBo.expressNo+"");
                        }else if(expressLogistics=="SF"){
                            LODOP.ADD_PRINT_BARCODE((16)+"mm", (6)+"mm", "70mm", "12mm", "128AUTO", v.addresseeInfoBo.expressNo+"");
                            LODOP.ADD_PRINT_BARCODE((93)+"mm", (32)+"mm", "70mm", "12mm", "128AUTO", v.addresseeInfoBo.expressNo+"");
                        }else if(expressLogistics=="YTO"){
                            LODOP.ADD_PRINT_BARCODE((31)+"mm", (70)+"mm", "33mm", "8mm", "128AUTO", v.addresseeInfoBo.expressNo+"");//圆通上方条码  第一条不显示下面的数字
                            LODOP.SET_PRINT_STYLEA(0,"ShowBarText",0);
                            LODOP.ADD_PRINT_BARCODE((70)+"mm", (12)+"mm", "70mm", "15mm", "128AUTO", v.addresseeInfoBo.expressNo+"");//圆通中间条码
                            LODOP.ADD_PRINT_BARCODE((111)+"mm", (34)+"mm", "60mm", "8mm", "128AUTO", v.addresseeInfoBo.expressNo+"");//圆通最下方条码
                        }
				        LODOP.SET_PRINT_PAGESIZE(1, modelContent.width+"mm", modelContent.height+"mm", "");
				        LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%",html);
                        LODOP.SET_LICENSES("深圳市鲸仓科技有限公司", "BB720FD5AB0D521EB007D05CDE36CE65", "", "");

                        if (LODOP.SET_PRINTER_INDEXA(modelContent.printerName)) {
				            LODOP.PRINT();
				        }
				    }
			    })
			}
		}
	})
	
}

function listReprint() {
	if ($('[name=chkOutOrderCode]:checked').length == 0) {
		jAlert('请选择需要打印的清单', '提示');
		return;
	}
	var codes = $('[name=chkOutOrderCode]:checked').map(function() {
		return $(this).val();
	}).get();
	
	console.log(codes);
	
	jQuery.ajax({
		url: ctx + '/v3/output/express-sheet/reprint/list',
		data: {orderCodes: codes.join(',')},
		success: function(resp) {
            jAlert(resp.message,"提示");
			if(resp.status == 1){
				initLodop();
				jQuery.each(resp.content || [], function(i, v) {
					var type = v.type;
				    var modelContent = v.printTemplateDTO;
				    var render = template.compile(modelContent.content);
					if (type == 'db') {
					    for(var i=0; i < v.urdbTemplates.length; i++){
	                        var html = render(v.urdbTemplates[i]);
                            LODOP.ADD_PRINT_BARCODE((20)+"mm", (152)+"mm", "45mm", "12mm", "128Auto", v.urdbTemplates[i].fromNo+"");
	                        LODOP.SET_PRINT_PAGESIZE(1, modelContent.width+"mm", modelContent.height+"mm", "");
	                        LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%",html);
                            LODOP.SET_LICENSES("深圳市鲸仓科技有限公司", "BB720FD5AB0D521EB007D05CDE36CE65", "", "");
                            if (LODOP.SET_PRINTER_INDEXA(modelContent.printerName)) {
	                            LODOP.PRINT();
	                        }
	                    }
					} else {
					    var html = render(v.urTemplate);
					    LODOP.ADD_PRINT_BARCODE((2)+"mm", (2)+"mm", "45mm", "10mm", "128Auto", v.urTemplate.fromNo+"");
					    LODOP.SET_PRINT_PAGESIZE(1, modelContent.width+"mm", modelContent.height+"mm", "");
					    LODOP.ADD_PRINT_HTM(0, 0, "100%", "100%",html);
                        LODOP.SET_LICENSES("深圳市鲸仓科技有限公司", "BB720FD5AB0D521EB007D05CDE36CE65", "", "");
                        if (LODOP.SET_PRINTER_INDEXA(modelContent.printerName)) {
					     	LODOP.PRINT();
					    }
					}
				    
				});
			}
		}
	})
	
}

$(function() {
	$('#chk-all').click(function() {
		if ($(this).is(':checked')) {
			$(this).parents('table').find('input[type=checkbox]').prop('checked', true)
		} else {
			$(this).parents('table').find('input[type=checkbox]').prop('checked', false)
		}
	});
	query();
})
