var PENDING_PAYMENT_INVOICES_ERROR_BUCKET_ARRAY;
class pendingPaymentTable{
    getPendingPaymentDetailsTable(excelJson){
        PENDING_PAYMENT_INVOICES_ERROR_BUCKET_ARRAY=[]; // on every time on creating obj of this class mkng array empty.
        let tableBodyUI='';
        let initialisedArrayValues=()=>{
            this.errorBucket=[];
            this.errorBucket['requiredFieldMissingCnt']=0;
            this.errorBucket['mismatchCnt']=0;
            this.errorBucket['duplicateEntry']=false;
            this.errorBucket['invalidEntry']=false;
            this.errorBucket['requiredPaymentDateMissing']=false;
            this.errorBucket['requiredPaymentValueMissing']=false;
            this.errorBucket['invalidPaymentDate']=false;
            this.errorBucket['invalidPaymentValue']=false;
            this.errorBucket['invalidPaymentRemarks']=false;
        }
        for(let loopIndex=0; loopIndex < excelJson.length;loopIndex++){
            initialisedArrayValues();
            let currentLoopObj=excelJson[loopIndex];
            let nextLoopObj=excelJson[loopIndex+1];
            let lastLoopIndex=excelJson.length-1;
            // let uniqueKey=currentLoopObj.PONo+"_"+currentLoopObj.InVoiceNo;
            let uniqueKey=currentLoopObj.InVoiceNo;
            let inlineStyle='';
            let duplicate_invalidKeyword='';
            this.checkForDuplicateEntries(currentLoopObj, nextLoopObj, lastLoopIndex, loopIndex);
            this.checkForInvalidEntries(uniqueKey, currentLoopObj);
            if(currentLoopObj.duplicateEntry){
                this.errorBucket['duplicateEntry']=true;
                duplicate_invalidKeyword=`<span class="font-weight-bold text-danger d-block">Duplicate</span>`;
                inlineStyle=`style="background-color:var(--errorBgTableRow)"`;
            }
            if(currentLoopObj.invalidEntry){
                this.errorBucket['invalidEntry']=true;
                duplicate_invalidKeyword=`<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="${uniqueKey}" ${inlineStyle}>
                            <td>${loopIndex+1}</td>
                            <td>${currentLoopObj.PONo+" "+duplicate_invalidKeyword}</td>
                            ${this.getInvoiceDateTd(currentLoopObj["Invoice Date(yyyy-mm-dd)"], uniqueKey, currentLoopObj.invalidEntry)}
                            <td>${currentLoopObj.InVoiceNo}</td> 
                            ${this.getInvoiceValueTd(currentLoopObj["Invoice Value"],uniqueKey, currentLoopObj.invalidEntry)}
                            ${this.getPaymentDateTd(currentLoopObj, loopIndex)}
                            ${this.getPaymentValueTd(currentLoopObj["Payment Value"], currentLoopObj["Invoice Value"], loopIndex)}
                            ${this.getPaymentRemarksTd(currentLoopObj["Payment Remarks"], loopIndex)}
                            ${this.getRemainingBalanceTd(currentLoopObj, currentLoopObj["Invoice Value"], currentLoopObj["Payment Value"])}
                            </tr>`;
            PENDING_PAYMENT_INVOICES_ERROR_BUCKET_ARRAY.push(this.errorBucket);
        }
        return tableBodyUI;
                  // ${this.getReportDateTd(currentLoopObj["Report Date(yyyy-mm-dd)"], uniqueKey, currentLoopObj.invalidEntry)}
                // <td><textarea rows="1" disabled>${currentLoopObj["Report Remarks"]}</textarea></td>
    }
    // getReportDateTd(reportDate, uniqueKey, invalidEntry){
    //     let tdUi='';
    //     let reportDateInitially=this.downloadedTemplate_initialValues(uniqueKey,"REPORT_SUBMITED_TO_CUSTOMER_ON");
    //     if(reportDate){
    //         if((reportDate!=reportDateInitially) && (!invalidEntry)){
    //             tdUi=`<span>${reportDate}</span>
    //             <span class="text-warning font-weight-bold" data-toggle="Abbrevation_tooltip" title="You can't update invoice details only payment details will be updated">${reportDateInitially}</span>`;
    //             this.errorBucket['mismatchCnt']++;
    //         }else{
    //             tdUi=`${reportDate}`;
    //         }
    //     }else{
    //         tdUi=`<span class="font-weight-bold text-danger">Required</span>`;
    //         this.errorBucket['requiredFieldMissingCnt']++;
    //     }
    //     return `<td class="reportDate_Td">${tdUi}</td>`;
    // }
    getInvoiceDateTd(invoiceDate, uniqueKey, invalidEntry){
        let tdUi='';
        let invoiceDateInitially =this.downloadedTemplate_initialValues(uniqueKey, "INVOICE_RISED_ON");
        if(invoiceDate){
            if((invoiceDate!=invoiceDateInitially) && (!invalidEntry)){
                tdUi=`<span>${invoiceDate}</span>
                <span class="text-warning font-weight-bold" data-toggle="Abbrevation_tooltip" title="You can't update invoice details only payment details will be updated">${invoiceDateInitially}</span>`;
                this.errorBucket['mismatchCnt']++;
            }else{
                tdUi= `${invoiceDate}`;
            }
        }else{
            tdUi=`<span class="font-weight-bold text-danger">Required</span>`;
            this.errorBucket['requiredFieldMissingCnt']++;
        }
        return `<td class="invoiceDate_Td">${tdUi}</td>`;
    }
    
    getInvoiceValueTd(invoiceValue, uniqueKey, invalidEntry){
        let tdUi='';
        let invoiceValueInitially=this.downloadedTemplate_initialValues(uniqueKey, "INVOICE_VALUE");
        if(invoiceValue){
            if((invoiceValue!=invoiceValueInitially) && (!invalidEntry)){
                tdUi=`<span class="d-block">${invoiceValue}</span>
                      <span class="text-warning" data-toggle="Abbrevation_tooltip" title="You can't update invoice details only payment details will be updated">${invoiceValueInitially}</span>`;
                      this.errorBucket['mismatchCnt']++;
            }else{
                tdUi=`${invoiceValue}`;
            }
        }else{
            tdUi=`<span class="font-weight-bold text-danger">Required</span>`;
            this.errorBucket['requiredFieldMissingCnt']++;
        }
        return `<td class="invoiceValue_Td">${tdUi}</td>`;
    }
    getPaymentDateTd(currentLoopObj, currentLoopIndex){
        let paymentDate=currentLoopObj["Payment Date(yyyy-mm-dd)"];
        let tdUi='';
        if(paymentDate){
            let returnVal= validateDateFormate(paymentDate);  // function in invoices_reports.js
            if(returnVal['validDate']){
                currentLoopObj["Payment Date(yyyy-mm-dd)"]=returnVal['dateValue'] // update json value after correcting
                tdUi=`<input type="date" value="${returnVal['dateValue']}" class="form-control" min="2015-01-01"/>`;
            }else{
                this.errorBucket['invalidPaymentDate']=true;
                tdUi=`<input type="date" class="form-control border-danger" min="2015-01-01"/><span class="errorMsg text-danger font-weight-bold invalidPaymentDate">Invalid Date(${paymentDate})</span>`;
            }
        }else{
            tdUi=`<input type="date" class="form-control border-danger" min="2015-01-01"/><span class="errorMsg text-danger font-weight-bold requiredPaymentDateMissing">Required</span>`;
            this.errorBucket['requiredPaymentDateMissing']=true;
        }
        return `<td class="paymentDate_Td" data-index="${currentLoopIndex}">${tdUi}</td>`;
    }
    
    getPaymentValueTd(paymentValue, invoiceValue, currentLoopIndex){
        let tdUi='';
        let invoiceVal=Number(invoiceValue) ? Number(invoiceValue) :0;
        if(paymentValue && paymentValue!=0){
            if(!isNaN(paymentValue)){ //is number
                if(Number(paymentValue)<=invoiceVal){
                    tdUi=`<input type="number" value="${paymentValue}" class="form-control"/>`; 
                }else{
                    tdUi=`<input type="number" value="${paymentValue}" class="form-control border-danger"/><span class="errorMsg text-danger invalidPaymentValue">Payment value greater than Invoice value</span>`; 
                    this.errorBucket['invalidPaymentValue']=true;
                }
            }else{
                tdUi=`<input type="number"  class="form-control border-danger"/><span class="errorMsg text-danger invalidPaymentValue">Invalid Data(${paymentValue})</span>`;
                this.errorBucket['invalidPaymentValue']=true;
            }
        }else{
            tdUi=`<input type="number" value="0" class="form-control border-danger" min="0"/><span class="errorMsg text-danger font-weight-bold requiredPaymentValueMissing">Required</span>`;
            this.errorBucket['requiredPaymentValueMissing']=true;
        }
        return `<td class="paymentValue_Td" data-index="${currentLoopIndex}">${tdUi}</td>`;
    }
    
    getPaymentRemarksTd(paymentRemarks, currentLoopObj){
        let tdUi='';
        if(paymentRemarks){
            if(paymentRemarks.length <= 300){
                tdUi=`<textarea class="form-control" placeholder="Enter Payment Remarks" maxlength="300" rows="1">${paymentRemarks}</textarea>`;
            }else{
               tdUi=`<textarea class="form-control border-danger" placeholder="Enter Payment Remarks" maxlength="300">${paymentRemarks}</textarea><span class="errorMsg text-danger font-weight-bold invalidPaymentRemarks">Max Limit 300</span>`;
               this.errorBucket["invalidPaymentRemarks"]=true;
            }    
        }else{
            tdUi=`<textarea class="form-control" Placeholder="Enter Payment Remarks"></textarea>`;
        }
        return `<td class="paymentRemarks_Td" data-index="${currentLoopObj}">${tdUi}</td>`;
    }
    getRemainingBalanceTd(currentLoopObj, invoiceVal, paymentVal){
        let invoiceValue=Number(invoiceVal) ?  Number(invoiceVal) : 0;
        let paymentValue=Number(paymentVal) ? Number(paymentVal) : 0;
        let remaingBal=invoiceValue-paymentValue;
        //update json
        currentLoopObj['Balance']=remaingBal;
        
        return `<td class="remainigBalance_Td">${remaingBal}</td>`;
    }
    
    downloadedTemplate_initialValues(uniqueKey, keyValue){
        let returnVal='';
        if(PENDING_PAYMENT_INVOICE_DETAILS_RESPONSE_JSON_IN_OBJECT[uniqueKey]){
             returnVal=PENDING_PAYMENT_INVOICE_DETAILS_RESPONSE_JSON_IN_OBJECT[uniqueKey][keyValue];
        }
        return returnVal;
    }
    
    checkForDuplicateEntries(currentLoopObj, nextLoopObj, lastLoopIndex, currentLoopIndex){
        if(currentLoopIndex!=lastLoopIndex){
            if((currentLoopObj.PONo == nextLoopObj.PONo) && (currentLoopObj.InVoiceNo == nextLoopObj.InVoiceNo)){
                currentLoopObj.duplicateEntry=true;
                nextLoopObj.duplicateEntry=true;
            }
        }
    }
    checkForInvalidEntries(uniqueKey,currentLoopObj){
        let isExist=PENDING_PAYMENT_INVOICE_DETAILS_RESPONSE_JSON_IN_OBJECT[uniqueKey];
        if(!isExist){
            currentLoopObj.invalidEntry=true;
        }
    }
}