/**
 * Created by Tony on 2017/6/26.
 */
'use strict'

const PRE_PAGE_OFFSET = 150;
const NEXT_PAGE_OFFSET = 160;
const FIRST_PAGE_OFFSET = 50;
const LAST_PAGE_OFFSET = 60;

let fontSuffixMapObj = {"表标题": "title", "列标题": "column", "正文内容": "content", "合计": "summary", "表眉/表脚": "header_footer"};

let rptTplObj = {
    hasInitialized: false,
    iniPage: function() {
        let me = this;
        if (!me.hasInitialized) {
            zTreeOprObj.getCustomerCfg();
            zTreeOprObj.getReportTemplateTree();
            me.hasInitialized = true;
            let canvas = document.getElementById("rptCanvas");
            canvas.onclick = canvasOprObj.canvasOnClick;
            canvas.onmousemove = canvasOprObj.canvasOnMouseMove;
        }
    }
}

let zTreeOprObj = {
    treeObj: null,
    currentNode: null,
    checkedRptTplNodes: null,
    currentRptPageRst: null,
    reportPageCfg: null,
    defReportPageCfg: null,
    currentPage: 1,
    maxPages: 0,
    countChkedRptTpl: function () {
        let me = zTreeOprObj;
        if (me.treeObj) {
            me.checkedRptTplNodes = [];
            let chkNodes = me.treeObj.getCheckedNodes(true), cnt = 0, hasCurrentNode = false;
            for (let node of chkNodes) {
                if (node.nodeType === TPL_TYPE_TEMPLATE) {
                    cnt++;
                    me.checkedRptTplNodes.push(node);
                    if (me.currentNode === node) hasCurrentNode = true;
                }
            }
            if (!hasCurrentNode && cnt === 0 && me.currentNode !== null) {
                //这里根据实际需求再做处理
                cnt++;
                me.checkedRptTplNodes.push(me.currentNode);
            }
            $("#export_div").find("span").each(function(cIdx,elementSpan){
                elementSpan.innerText = cnt;
            });
            $("#print_div").find("span").each(function(cIdx,elementSpan){
                elementSpan.innerText = cnt;
            });
        }
    },
    getReportTemplateTree: function() {
        let me = zTreeOprObj, params = {};
        params.engineerId = projectInfoObj.projectInfo.property.engineering;
        let private_chk_hide = function (chkTplItem) {
            //考虑未来拓展，统一在此判断报表模板是否显示
            let rst = false;
            if (chkTplItem.hasOwnProperty('flags') && chkTplItem.flags.hasOwnProperty('taxType') && chkTplItem.flags['taxType'] !== null &&
                parseInt(chkTplItem.flags['taxType']) !== parseInt(projectInfoObj.projectInfo.property.taxType)) {
                rst = true;
            }
            return rst;
        };
        // projectInfoObj.projectInfo.property.taxType === 1 //1: 一般计税 2: 简易计税
        CommonAjax.postEx("report_tpl_api/getRptTplTree", params, 20000, true, function(result){
            let private_remove_hide_item = function (items) {
                if (items && items.length > 0) {
                    for (let i = items.length - 1; i >= 0; i--) {
                        if (!(items[i].released) && items[i].nodeType === 2) {
                            items.splice(i, 1);
                        } else if(private_chk_hide(items[i])) {
                            items.splice(i, 1);
                        } else {
                            if (items[i].items && items[i].items.length > 0) {
                                private_remove_hide_item(items[i].items);
                            }
                        }
                    }
                }
            }
            private_remove_hide_item(result);
            for (let topNode of result) {
                if (topNode.userId === "-100") {
                    topNode.name = topNode.name + " - 标准报表";
                } else {
                    topNode.name = topNode.name + " - 定制报表";
                }
            }
            zTreeHelper.createTreeDirectly(result, rpt_tpl_setting, "rptTplTree", me);
            me.refreshNodes();
        }, null, null);
    },
    getCustomerCfg: function() {
        let me = zTreeOprObj, params = {};
        params.engineerId = projectInfoObj.projectInfo.property.engineering;
        CommonAjax.postEx("report_tpl_api/getCustomizeCfg", params, 20000, true, function(result){
                if (result) {
                    me.defReportPageCfg = result[0];
                    me.reportPageCfg = result[1];
                    me.iniFontCfgDom(me.reportPageCfg);
                    me.renderRptCfg(result[1]);
                } else {
                    me.reportPageCfg = null;
                    me.defReportPageCfg = null;
                }
            }, null, null
        );
    },
    iniFontCfgDom: function (cfg) {
        for (let font of cfg.fonts) {
            let domArrs = [];
            let fontPropSuffix = fontSuffixMapObj[font.CfgDispName];
            domArrs.push("<div class='row mb-1'>");
            //1. label
            domArrs.push("<div class='col-3'>" + font.CfgDispName + "</div>");
            //2. font name
            domArrs.push("<div class='col-3'>");
            domArrs.push("<select class='form-control input-sm' id='fontName_" + fontPropSuffix + "' onchange='rptControlObj.changeFontMain(\"" + font.CfgDispName + "\", \"Name\", this)'>");
            domArrs.push("<option>宋体</option><option>楷体</option><option>黑体</option>");
            domArrs.push("</select>");
            domArrs.push("</div>");
            //3. font height
            domArrs.push("<div class='col-3'>");
            domArrs.push("<input class='form-control input-sm' id='fontHeight_" + fontPropSuffix + "' type='number' value='30' step='1' min='6' max='66' " +
                "onchange='rptControlObj.changeFontMain(\"" + font.CfgDispName + "\", \"FontHeight\", this)' " +
                "onkeyup='rptControlObj.changeFontMain(\"" + font.CfgDispName + "\", \"FontHeight\", this)'>");
            domArrs.push("</div>");
            //4. font bold italic underline
            domArrs.push("<div class='col-3'>");
            domArrs.push("<a id='font_bold_" + fontPropSuffix + "' class='btn btn-sm btn-outline-secondary' title='加粗' onclick='rptControlObj.changeFontAdhoc(\"" + font.CfgDispName + "\", \"FontBold\", this)'><i class='fa fa-bold'></i></a>");
            domArrs.push("<a id='font_italic_" + fontPropSuffix + "' class='btn btn-sm btn-outline-secondary' title='斜体' onclick='rptControlObj.changeFontAdhoc(\"" + font.CfgDispName + "\", \"FontItalic\", this)'><i class='fa fa-italic'></i></a>");
            domArrs.push("<a id='font_underline_" + fontPropSuffix + "' class='btn btn-sm btn-outline-secondary' title='下划线' onclick='rptControlObj.changeFontAdhoc(\"" + font.CfgDispName + "\", \"FontUnderline\", this)'><i class='fa fa-underline'></i></a>");
            domArrs.push("</div>");
            //
            domArrs.push("</div>");
            $(domArrs.join("")).insertBefore($("#font_cfg_blank_flag"));
        }
    },
    renderRptCfg: function (cfg) {
        $("#elementMargin_Left").get(0).value = cfg.margins.Left;
        $("#elementMargin_Right").get(0).value = cfg.margins.Right;
        $("#elementMargin_Top").get(0).value = cfg.margins.Top;
        $("#elementMargin_Bottom").get(0).value = cfg.margins.Bottom;

        for (let font of cfg.fonts) {
            let fontPropSuffix = fontSuffixMapObj[font.CfgDispName];
            document.getElementById("fontName_" + fontPropSuffix).value = font.Name;
            document.getElementById("fontHeight_" + fontPropSuffix).value = font.FontHeight;
            document.getElementById("font_bold_" + fontPropSuffix).className = (font.FontBold === "T")?"btn btn-sm btn-outline-secondary active":"btn btn-sm btn-outline-secondary";
            document.getElementById("font_italic_" + fontPropSuffix).className = (font.FontItalic === "T")?"btn btn-sm btn-outline-secondary active":"btn btn-sm btn-outline-secondary";
            document.getElementById("font_underline_" + fontPropSuffix).className = (font.FontUnderline === "T")?"btn btn-sm btn-outline-secondary active":"btn btn-sm btn-outline-secondary";
        }
        document.getElementById("rpt_vertical_line").checked = cfg.showVerticalLine;
        document.getElementById("rpt_narrow").checked = cfg.isNarrow;
        // document.getElementById("rpt_narrow").checked = false;
        document.getElementById("rpt_fill_zero").checked = cfg.fillZero;
    },
    extractRptCfg: function (cfg) {
        cfg.margins.Left = $("#elementMargin_Left").get(0).value;
        cfg.margins.Right = $("#elementMargin_Right").get(0).value;
        cfg.margins.Top = $("#elementMargin_Top").get(0).value;
        cfg.margins.Bottom = $("#elementMargin_Bottom").get(0).value;

        for (let font of cfg.fonts) {
            let fontPropSuffix = fontSuffixMapObj[font.CfgDispName];
            font.Name = document.getElementById("fontName_" + fontPropSuffix).value;
            font.FontHeight = document.getElementById("fontHeight_" + fontPropSuffix).value;
            font.FontBold = (document.getElementById("font_bold_" + fontPropSuffix).className === "btn btn-sm btn-outline-secondary active")?"T":"F";
            font.FontItalic = (document.getElementById("font_italic_" + fontPropSuffix).className === "btn btn-sm btn-outline-secondary active")?"T":"F";
            font.FontUnderline = (document.getElementById("font_underline_" + fontPropSuffix).className === "btn btn-sm btn-outline-secondary active")?"T":"F";
        }
        cfg.showVerticalLine = document.getElementById("rpt_vertical_line").checked;
        cfg.isNarrow = document.getElementById("rpt_narrow").checked;
        cfg.fillZero = document.getElementById("rpt_fill_zero").checked;
    },
    refreshNodes: function() {
        let me = this;
        let private_setupIsParent = function(node){
            node.isParent = (node.nodeType === RT.NodeType.NODE || node.level === 0);
            if (node.items && node.items.length) {
                for (let i = 0; i < node.items.length; i++) {
                    private_setupIsParent(node.items[i]);
                }
            }
        };
        let topNodes = me.treeObj.getNodes();
        for (let i = 0; i < topNodes.length; i++) {
            private_setupIsParent(topNodes[i]);
        }
        me.treeObj.refresh();
    },
    onCheck: function(event, treeId, treeNode) {
        zTreeOprObj.countChkedRptTpl();
    },
    onClick: function(event,treeId,treeNode) {
        let me = zTreeOprObj;
        if (treeNode.nodeType === TPL_TYPE_TEMPLATE && treeNode.refId > 0) {
            let params = {};
            let pageSize = rptControlObj.getCurrentPageSize();
            params.pageSize = pageSize;
            params.rpt_tpl_id = treeNode.refId;
            params.prj_id = projectInfoObj.projectInfo.ID;
            params.custCfg = me.reportPageCfg;
            me.currentNode = treeNode;
            me.requestReport(params);
            me.countChkedRptTpl();
        }
    },
    changePageSize: function(dom) {
        let me = zTreeOprObj,
            targetDom = document.getElementById("btnRptPageSize");
        let tmpStr = targetDom.innerHTML.trim();
        targetDom.innerHTML = dom.innerHTML.trim();
        dom.innerHTML = tmpStr;
        me.changeCfg();

    },
    changeOrientation: function(dom) {
        let me = zTreeOprObj,
            targetDom = document.getElementById("btnRptOrientation");
        let tmpStr = targetDom.innerHTML.trim();
        targetDom.innerHTML = dom.innerHTML.trim();
        dom.innerHTML = tmpStr;
        me.changeCfg();
    },
    changeCfg: function() {
        let me = zTreeOprObj;
        let params = {};
        params.pageSize = rptControlObj.getCurrentPageSize();
        params.orientation = rptControlObj.getCurrentOrientation();
        params.rpt_tpl_id = me.currentNode.refId;
        params.prj_id = projectInfoObj.projectInfo.ID;
        params.custCfg = me.reportPageCfg;
        me.requestReport(params);
    },
    resetAfter: function (pageRst) {
        let size = pageRst[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE].slice(0);
        if (size[0] > size[1]) {
            document.getElementById("btnRptOrientation").innerHTML = "横向";
            document.getElementById("hrefRptOrientation").innerHTML = "纵向";
        } else {
            document.getElementById("btnRptOrientation").innerHTML = "纵向";
            document.getElementById("hrefRptOrientation").innerHTML = "横向";
        }
    },
    requestReport: function (params) {
        let me = zTreeOprObj;
        hintBox.waitBox();
        CommonAjax.postEx("report_api/getReport", params, 15000, true,
            function(result){
                let pageRst = result;
                if (pageRst) {
                    me.resetAfter(pageRst);
                    let canvas = document.getElementById("rptCanvas");
                    me.currentRptPageRst = pageRst;
                    me.maxPages = pageRst.items.length;
                    me.currentPage = 1;
                    me.displayPageValue();
                    let size = JpcCanvasOutput.getReportSizeInPixel(me.currentRptPageRst, getScreenDPI());
                    canvas.width = size[0] + 20;
                    if (size[1] > size[0]) {
                        canvas.height = size[1] + 100;
                    } else {
                        canvas.height = size[1] + 50;
                    }
                    me.showPage(1, canvas);
                    hintBox.unWaitBox();
                }
            }, function(err){
                hintBox.unWaitBox();
            }, function(ex){
                hintBox.unWaitBox();
            }
        );
    },
    showPage: function (pageNum, canvas) {
        let me = zTreeOprObj;
        if (pageNum >= 1 && pageNum <= me.maxPages) {
            me.currentPage = pageNum;
            JpcCanvasOutput.cleanCanvas(canvas);
            JpcCanvasOutput.drawPageBorder(me.currentRptPageRst, canvas, getScreenDPI());
            JpcCanvasOutput.drawToCanvas(me.currentRptPageRst, canvas, me.currentPage);
        }
        me.displayPageValue();
    },
    displayPageValue: function() {
        let me = zTreeOprObj;
        $("#rpt_page_num").get(0).value = me.currentPage + "/" + me.maxPages;
    }
};

let canvasOprObj = {
    canvasOnMouseMove: function (event) {
        if (zTreeOprObj.currentNode) {
            let x = event.offsetX - JpcCanvasOutput.offsetX, canvas = event.originalTarget;
            if (!(canvas)) canvas = event.target; //chrome浏览器不认event.originalTarget,只认event.target或event.currentTarget
            if (x < FIRST_PAGE_OFFSET) {
                canvas.style.cursor = "url(/web/building_saas/img/FirstPageSimple.cur), auto";
            } else if (x < PRE_PAGE_OFFSET) {
                canvas.style.cursor = "url(/web/building_saas/img/PreviousPageSimple.cur), auto";
            } else if ((canvas.width - x) < LAST_PAGE_OFFSET) {
                canvas.style.cursor = "url(/web/building_saas/img/LastPageSimple.cur), auto";
            } else if ((canvas.width - x) < NEXT_PAGE_OFFSET) {
                canvas.style.cursor = "url(/web/building_saas/img/NextPageSimple.cur), auto";
            } else {
                canvas.style.cursor = "";
            }
        }
    },
    canvasOnClick: function(event){
        if (zTreeOprObj.currentNode) {
            let x = event.offsetX - JpcCanvasOutput.offsetX, canvas = event.originalTarget;
            if (!(canvas)) canvas = event.target; //chrome浏览器不认event.originalTarget,只认event.target或event.currentTarget
            if (x < FIRST_PAGE_OFFSET) {
                zTreeOprObj.showPage(1, canvas);
            } else if (x < PRE_PAGE_OFFSET) {
                zTreeOprObj.showPage(zTreeOprObj.currentPage - 1, canvas);
            } else if ((canvas.width - x) < LAST_PAGE_OFFSET) {
                zTreeOprObj.showPage(zTreeOprObj.maxPages, canvas);
            } else if ((canvas.width - x) < NEXT_PAGE_OFFSET) {
                zTreeOprObj.showPage(zTreeOprObj.currentPage + 1, canvas);
            }
        }
    }
};

let rptControlObj = {
    currentOutputType: "Excel",
    currentDownloadUrl: null,
    currentDownloadIdx: 0,
    getCurrentPageSize: function() {
        // let rst = "A4";
        let rst = document.getElementById("btnRptPageSize").innerHTML.trim();
        //btnRptPageSize
        return rst;
    },
    getCurrentOrientation: function() {
        // let rst = "横向";
        let rst = document.getElementById("btnRptOrientation").innerHTML.trim();
        return rst;
    },
    getCurrentReportOption: function() {
        //
    },
    changeType: function(newType) {
        let me = rptControlObj;
        let excelDom = document.getElementById("EXCEL_TYPE");
        let pdfDom = document.getElementById("PDF_TYPE");
        if (newType === "Excel") {
            excelDom.className = "btn btn-block btn-primary";
            pdfDom.className = "btn btn-block btn-outline-secondary";
            me.currentOutputType = newType;
        } else if (newType === "PDF") {
            excelDom.className = "btn btn-block btn-outline-secondary";
            pdfDom.className = "btn btn-block btn-primary";
            me.currentOutputType = newType;
        } else {
            //me.currentOutputType = newType;
        }
    },
    outputRpt: function() {
        let me = rptControlObj;
        if (me.currentOutputType === "Excel") {
            me.getExcel();
        } else if (me.currentOutputType === "PDF") {
            me.getPDF();
        } else {
            //other types if needed.
        }
    },
    getAllInOneBook: function () {
        if (zTreeOprObj.checkedRptTplNodes && zTreeOprObj.checkedRptTplNodes.length > 0) {
            let me = rptControlObj;
            let orgRptName = projectInfoObj.projectInfo.name;
            let refRptTplIds = [];
            for (let node of zTreeOprObj.checkedRptTplNodes) {
                refRptTplIds.push(node.refId);
            }
            let params = {};
            params.prj_id = projectInfoObj.projectInfo.ID;
            params.rpt_ids = refRptTplIds;
            params.rptName = orgRptName;
            params.pageSize = me.getCurrentPageSize();
            params.orientation = ((zTreeOprObj.checkedRptTplNodes.length > 1)?null:me.getCurrentOrientation());
            params.custCfg = zTreeOprObj.reportPageCfg;
            params.option = "normal";
            CommonAjax.postEx("report_api/createExcelFilesInOneBook", params, 20000, true, function(result){
                    if (result) {
                        let uuIdUrls = [];
                        let uuIdUrl =  "/report_api/getFileByUUID/" + result.uuid + "/" + stringUtil.replaceAll(result.reportName, "#", "_") + "/xlsx";
                        uuIdUrls.push(uuIdUrl);
                        downloadReport(uuIdUrls);
                    } else {
                        //
                    }
                }, null, null
            );
        }
    },
    getAllIndividualExcelBook: function () {
        let me = rptControlObj;
        if (zTreeOprObj.checkedRptTplNodes && zTreeOprObj.checkedRptTplNodes.length > 0) {
            let rpt_ids = [], rpt_names = [];
            for (let tplNode of zTreeOprObj.checkedRptTplNodes) {
                rpt_ids.push(tplNode.refId);
                rpt_names.push(tplNode.name)
            }
            let params = {};
            params.prj_id = projectInfoObj.projectInfo.ID;
            params.rpt_ids = rpt_ids;
            params.rpt_names = rpt_names;
            params.pageSize = me.getCurrentPageSize();
            params.orientation = ((zTreeOprObj.checkedRptTplNodes.length > 1)?null:me.getCurrentOrientation());
            params.isOneSheet = true;
            params.custCfg = zTreeOprObj.reportPageCfg;
            params.option = "normal";
            CommonAjax.postEx("report_api/createExcelFiles", params, 20000, true, function(result){
                    if (result) {
                        let uuIdUrls = [];
                        for (let uuIdObj of result) {
                            let uuIdUrl =  "/report_api/getFileByUUID/" + uuIdObj.uuid + "/" + stringUtil.replaceAll(uuIdObj.reportName, "#", "_") + "/xlsx";
                            uuIdUrls.push(uuIdUrl);
                        }
                        downloadReport(uuIdUrls);
                    } else {
                        //
                    }
                }, null, null
            );
        }
    },
    checkAndGetExcel: function () {
        if (zTreeOprObj.treeObj) {
            let chkNodes = zTreeOprObj.treeObj.getCheckedNodes(true);
            if (chkNodes.length > 0) {
                $("#show_excel_output_cfg").trigger("click");
            } else {
                rptControlObj.getAllIndividualExcelBook();
            }
        }
    },
    getExcel: function () {
        let me = rptControlObj;
        if ($("#excelExportType_AllInOneBook").get(0).checked) {
            me.getAllInOneBook();
        } else if ($("#excelExportType_IndividualBook").get(0).checked) {
            me.getAllIndividualExcelBook();
        }
    },
    getPDF: function() {
        let me = rptControlObj;
        if (zTreeOprObj.checkedRptTplNodes && zTreeOprObj.checkedRptTplNodes.length > 0) {
            let rpt_ids = [], rpt_names = [];
            for (let tplNode of zTreeOprObj.checkedRptTplNodes) {
                rpt_ids.push(tplNode.refId);
                rpt_names.push(tplNode.name)
            }
            let params = {};
            params.prj_id = projectInfoObj.projectInfo.ID;
            params.rpt_ids = rpt_ids;
            params.rpt_names = rpt_names;
            params.pageSize = me.getCurrentPageSize();
            params.orientation = ((zTreeOprObj.checkedRptTplNodes.length > 1)?null:me.getCurrentOrientation());
            params.custCfg = zTreeOprObj.reportPageCfg;
            params.option = "normal";
            CommonAjax.postEx("report_api/createPdfFiles", params, 20000, true, function(result){
                    if (result) {
                        let uuIdUrls = [];
                        for (let uuIdObj of result) {
                            let uuIdUrl =  "/report_api/getFileByUUID/" + uuIdObj.uuid + "/" + stringUtil.replaceAll(uuIdObj.reportName, "#", "_") + "/pdf";
                            uuIdUrls.push(uuIdUrl);
                        }
                        downloadReport(uuIdUrls);
                    } else {
                        //
                    }
                }, null, null
            );
        }
    },
    firstPage: function(dom) {
        let canvas = document.getElementById("rptCanvas");
        zTreeOprObj.showPage(1, canvas);
    },
    prePage: function(dom) {
        let canvas = document.getElementById("rptCanvas");
        zTreeOprObj.showPage(zTreeOprObj.currentPage - 1, canvas);
    },
    nextPage: function(dom) {
        let canvas = document.getElementById("rptCanvas");
        zTreeOprObj.showPage(zTreeOprObj.currentPage + 1, canvas);
    },
    lastPage: function(dom) {
        let me = zTreeOprObj;
        let canvas = document.getElementById("rptCanvas");
        zTreeOprObj.showPage(me.maxPages, canvas);
    },
    onKeydown: function (event, dom) {
        let me = zTreeOprObj, keyPressed = null;
        if (window.event) {
            keyPressed = window.event.keyCode; // IE/Chrome
        } else {
            keyPressed = event.which; // Firefox
        }
        if (keyPressed === 13) {
            let pageNum = 1;
            try {
                pageNum = parseInt(dom.value);
            } catch (e) {
                pageNum = 1;
            }
            let canvas = document.getElementById("rptCanvas");
            if (pageNum < 1) {
                pageNum = 1;
            } else if (pageNum > me.maxPages) {
                pageNum = me.maxPages;
            }
            zTreeOprObj.showPage(pageNum, canvas);
            return false;
        }
    },
    changeMargin: function(marginPropStr, marginDom) {
        zTreeOprObj.reportPageCfg.margins[marginPropStr] = marginDom.value;
    },
    changeFontMain: function(CfgDispName, fontProperty, fontDom) {
        for (let font of zTreeOprObj.reportPageCfg.fonts) {
            if (font["CfgDispName"] === CfgDispName) {
                font[fontProperty] = fontDom.value;
                break;
            }
        }
    },
    changeCfgOption: function (optStr, dom) {
        zTreeOprObj.reportPageCfg[optStr] = dom.checked;
    },
    changeFontAdhoc: function(CfgDispName, fontProperty, fontDom) {
        for (let font of zTreeOprObj.reportPageCfg.fonts) {
            if (font["CfgDispName"] === CfgDispName) {
                if (font[fontProperty] === 'T') {
                    font[fontProperty] = 'F';
                    fontDom.className = "btn btn-sm btn-outline-secondary";
                } else {
                    font[fontProperty] = 'T';
                    fontDom.className = "btn btn-sm btn-outline-secondary active";
                }
                break;
            }
        }
    },
    restoreCustCFG: function () {
        let me = this;
        zTreeOprObj.renderRptCfg(zTreeOprObj.defReportPageCfg);
        zTreeOprObj.extractRptCfg(zTreeOprObj.reportPageCfg);
        me.saveCustCfg();
    },
    saveCustCfg: function() {
        let params = {};
        params.custCfg = zTreeOprObj.reportPageCfg;
        CommonAjax.postEx("report_tpl_api/saveCustomerCfg", params, 20000, true, function(result){
                // alert("Save successfully!");
                $("#update_msg_response")[0].style.color = "green";
                $("#update_msg_response")[0].innerHTML = " (保存成功!)";
                setTimeout(function(){
                    $("#update_msg_response")[0].innerHTML = "";
                }, 1000);
            }, function (failRst) {
                $("#update_msg_response")[0].style.color = "red";
                $("#update_msg_response")[0].innerHTML = " (保存失败!)";
                setTimeout(function(){
                    $("#update_msg_response")[0].innerHTML = "";
                }, 1000);
            }, null
        );
    },
    confirmCfgChange: function() {
        let treeNode = zTreeOprObj.currentNode;
        if (treeNode.nodeType === TPL_TYPE_TEMPLATE && treeNode.refId > 0) {
            let params = {};
            params.pageSize = rptControlObj.getCurrentPageSize();
            params.rpt_tpl_id = treeNode.refId;
            params.prj_id = projectInfoObj.projectInfo.ID;
            params.custCfg = zTreeOprObj.reportPageCfg;
            zTreeOprObj.requestReport(params);
        }

    }
};

function downloadReport(urls) {
    //考虑到多个报表下载，一些浏览器（如chrome）不允许一下子下载多个文件，得缓缓处理，统一在这处理
    rptControlObj.currentDownloadUrl = null;
    rptControlObj.currentDownloadIdx = 0;
    let private_download = function() {
        if (rptControlObj.currentDownloadIdx >= 0 && rptControlObj.currentDownloadIdx < urls.length) {
            rptControlObj.currentDownloadUrl = urls[rptControlObj.currentDownloadIdx];
            rptControlObj.currentDownloadIdx++;
            window.location = rptControlObj.currentDownloadUrl;
            setTimeout(private_download, 2000);
        }
    }
    private_download();
}
