shr.defineClass("shr.empcertification.HREmpCertificationBillHRListEx", shr.empcertification.HREmpCertificationBillHRList, {
    pageStep: 2,
    initalizeDOM: function () {

        shr.empcertification.HREmpCertificationBillHRListEx.superClass.initalizeDOM.call(this);
    },
    previewPrintAction : function () {
        var _self = this;
        var errorXhr = "";
        var selectedIds = $("#grid").jqGrid("getSelectedRows");
        var isSpecial = $('#grid').jqGrid("getCell", selectedIds[0], "isSpecial"); //是否其他模板
        if (selectedIds == undefined || selectedIds.length==0) {
            shr.showWarning({
                message: jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_16,
                hideAfter: null
            });
            return;
        }
        if(selectedIds.length>1){
            shr.showWarning({
                message: jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_19,
                hideAfter: null
            });
            return;
        }
        if(isSpecial=="1"){//是否其他模板
            shr.showWarning({
                message: jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_11,
                hideAfter: null
            });
            return false;
        }
        var rowData = $('#grid').jqGrid('getRowData', selectedIds);
        var drawUpMode = rowData.drawUpMode; //开具形式 1.纸质 2.电子
        if("2" == drawUpMode){
            shr.customer.gtiit.HREmpCertificationBillHRListEx.superClass.previewPrintAction.call(_self);
        }else {
            _self.previewPrintActionV2();
        }
    },
    previewPrintActionV2 : function () {
        var _self = this;
        var errorXhr = "";
        var selectedIds = $("#grid").jqGrid("getSelectedRows");
        var isSpecial = $('#grid').jqGrid("getCell", selectedIds[0], "isSpecial"); //是否其他模板
        if (selectedIds == undefined || selectedIds.length==0) {
            shr.showWarning({
                message: jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_16,
                hideAfter: null
            });
            return;
        }
        if(selectedIds.length>1){
            shr.showWarning({
                message: jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_19,
                hideAfter: null
            });
            return;
        }
        if(isSpecial=="1"){//是否其他模板
            shr.showWarning({
                message: jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_11,
                hideAfter: null
            });
            return false;
        }
        _self.remoteCall({
            method : "checkButtonDataPermission",
            param : {
                billId : selectedIds
            },
            async: false,
            success : function(data){

            },
            error: function (xhr, data) {
                errorXhr = xhr;
                return;
            }
        });
        if(errorXhr != undefined && errorXhr != null && errorXhr != ""){
            if(errorXhr.result == "error"){
                shr.showError({
                    message: errorXhr.summary,
                    hideAfter: null
                });
                return;
            }
        }
        var rowData = $('#grid').jqGrid('getRowData', selectedIds);
        var drawUpMode = rowData.drawUpMode; //开具形式 1.纸质 2.电子
        var certificationStatus = rowData.certificationStatus; //证明状态
        var isSign = rowData.isSign; // 是否已签署
        var signNo = rowData.signNO;
        var billId = rowData.id;
        if("1" == isSign){//已签署
            var newDate = new Date();
            var hourStr = newDate.getHours()<10?"0"+newDate.getHours().toString():newDate.getHours().toString();
            var minuteStr = newDate.getMinutes()<10?"0"+newDate.getMinutes().toString():newDate.getMinutes().toString();
            var secondStr = newDate.getSeconds()<10?"0"+newDate.getSeconds().toString():newDate.getSeconds().toString();
            var nowTime = hourStr+minuteStr+secondStr;
            var contractNo = rowData.number;
            shr.block.show();
            shr.callService({
                serviceName : 'empCreateCertificationService',
                param : {
                    billId : billId,
                    posNum : nowTime
                },
                async : false,
                success : function(response) {
                    console.log('createCertificationService success');
                    var _time3 = 0;
                    var showMsg = "";
                    var intervalID = setInterval(function() {
                        _time3++;
                        shr.callService({
                            serviceName : 'empPreViewECertificationV2Service',
                            param : {
                                contractNo : contractNo+"-"+nowTime,
                                billId : billId,
                                isDefault : "1"
                            },
                            async : false,
                            success : function(response) {
                                if (response.code == 0 && response.isSuccess) {
                                    var returnUrl = response.returnUrl;
                                    var pdfurl = _self.assemblePreViewUrl(returnUrl);
                                    _self.convertPdfToWord(contractNo+"-"+nowTime,returnUrl,"未盖章",intervalID);
                                } else {
                                    showMsg = response.msg;
                                }
                            },
                            error: function(xhr,data){

                            }
                        });
                        if (_time3 > 10){
                            shr.block.hide();
                            if (showMsg == "") {
                                showMsg = jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_13;
                            }
                            shr.showError({
                                message: showMsg
                            });
                            clearInterval(intervalID);
                        }
                    }, 500);
                },
                error: function(xhr,data){
                    shr.showError({
                        message: jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_13
                    });
                    return;
                }
            });
            return;
        }else{
            var newDate = new Date();
            var hourStr = newDate.getHours()<10?"0"+newDate.getHours().toString():newDate.getHours().toString();
            var minuteStr = newDate.getMinutes()<10?"0"+newDate.getMinutes().toString():newDate.getMinutes().toString();
            var secondStr = newDate.getSeconds()<10?"0"+newDate.getSeconds().toString():newDate.getSeconds().toString();
            var nowTime = hourStr+minuteStr+secondStr;
            var contractNo = rowData.number;
            shr.block.show();
            shr.callService({
                serviceName : 'empCreateCertificationService',
                param : {
                    billId : billId,
                    posNum : nowTime
                },
                async : false,
                success : function(response) {
                    console.log('createCertificationService success');
                    var _time3 = 0;
                    var showMsg = "";
                    var intervalID = setInterval(function() {
                        _time3++;
                        shr.callService({
                            serviceName : 'empPreViewECertificationV2Service',
                            param : {
                                contractNo : contractNo+"-"+nowTime,
                                billId : billId,
                                isDefault : "1"
                            },
                            async : false,
                            success : function(response) {
                                if (response.code == 0 && response.isSuccess) {
                                    var returnUrl = response.returnUrl;

                                    //shr.openNew(pdfurl);

                                    // pdfurl = "https://gtiit.kdeascloud.com"+pdfurl;
                                    //var downloadUrl = response.data[0].nosealInfo.downloadUrl;
                                    _self.convertPdfToWord(contractNo+"-"+nowTime,returnUrl,"未盖章",intervalID);
                                } else {
                                    showMsg = response.msg;
                                }
                            },
                            error: function(xhr,data){

                            }
                        });
                        if (_time3 > 20){
                            shr.block.hide();
                            if (showMsg == "") {
                                showMsg = jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_13;
                            }
                            shr.showError({
                                message: showMsg
                            });
                            clearInterval(intervalID);
                        }
                    }, 500);
                },
                error: function(xhr,data){
                    shr.showError({
                        message: jsBizMultLan.emp_HRCertificationBillList_28243376_i18n_13
                    });
                    return;
                }
            });
        }
    },
    convertPdfToWord: function(contractNo, attachId, isSign,intervalID){
        var _self = this;

        //ownloadUrl = encodeURIComponent(downloadUrl);
        const date = new Date();
        const year = date.getFullYear();
        const month = (date.getMonth() + 1).toString().padStart(2, "0");
        const day = date.getDate().toString().padStart(2, "0");
        var currentDate = year+month+day;
        var fileName = contractNo + isSign + "-" + currentDate + ".docx";

        //var urlinner = shr.getContextPath() + shr.dynamicURL + "?downloadUrl="+downloadUrl+"&fileName="+fileName+"&method=convertPdfToWord&handler=com.kingdee.shr.customer.gtiit.handler.HRcertificationbillListHandlerEx"+"&uipk="+shr.getUrlRequestParam('uipk');
        //var downloadLink = document.createElement('a');
        //downloadLink.href = urlinner;
        //document.body.appendChild(downloadLink);
        //downloadLink.click();
        //document.body.removeChild(downloadLink);
        shr.callHandler({
            handler: "com.kingdee.shr.customer.gtiit.handler.HRcertificationbillListHandlerEx",
            action: "convertPdfToWord",
            param: {
                attachId: attachId,
                fileName:fileName
            },
            type: "POST",
            async:false,
            success: function (res) {
                if (res != null&&res.code=="ok") {
                    var pdfurl = _self.assembleDownloadUrl(attachId);
                    shr.openNew(pdfurl);
                    shr.block.hide();
                    clearInterval(intervalID);
                }
            }
        });
    },
    assembleDownloadUrl:function(attachmentId, params) {
        var _self = this;
        var param = $.extend({
            method: 'download',
            id: attachmentId,
            permItemId: shr.getCurrentPagePermItemId(),
            billId:shr.getUrlRequestParam('billId'),
            uipk:shr.getUrlRequestParam('uipk'),
            assigmentId:shr.getAssignmentId(),
            ProcInstId:shr.getProcInstId(),
            msgId:shr.getMsgId(),
            shruicode:shr.getShruicode()
        }, params);
        var url = shr.getContextPath() + '/attachmentUpload.do?' + $.param(param);
        return url;
    },
    getContractParamsFromURL: function(url){
        // 定义正则表达式
        const regex = /[?&]contractNo(=([^&#]*)|&|#|$)/;
        // 使用正则表达式获取参数值
        const paramValue = decodeURIComponent(regex.exec(url)[2].replace(/\+/g, ' '));
        return paramValue;
    },
});