var PENDING_ADDITIONAL_ADJUSTMENT_ERROR_BUCKET;
class AdditionalAdjustmentTable{
    constructor(){
        this.invalidEntry="Invalid Entries";
        this.transactionIdRequired="Transaction-Id Required";
        this.transactionAmountRequired="Transaction Amount Required";
        this.transactionDateRequired="Transaction Date Required";
        this.misMatched="MismatchedField";
        this.duplicateEntry="Duplicate_Emp_FN";
        this.duplicateTransactionId="Duplicate TransactionId";
        this.invalidTransactionAmount="Invalid Transaction Amount";
        this.invalidDateFormat="Invalid Date Format";
        this.exceedLimit="Max remark size exceed";
    }
    getAdditonalAdjustmentDetailsTable(excelJson){
        PENDING_ADDITIONAL_ADJUSTMENT_ERROR_BUCKET={};//initially make empty
        UPLOADED_EXCEL_TRANSACTIONS_ID_OBJECT={};// initially make empty
        let tableBody='';
        let lastLoopIndex=excelJson.length-1;
        for(let loopIndex=0; loopIndex<excelJson.length; loopIndex++){
            let currentLoopObj=excelJson[loopIndex];
            let nextLoopObj= excelJson[loopIndex+1];
            let empIdFortNightKey=currentLoopObj["EmpId"]+"_"+currentLoopObj["FortNight"];
            this.validateJson(loopIndex, lastLoopIndex, currentLoopObj, nextLoopObj, empIdFortNightKey);
            
            tableBody+=`<tr class="${convertToClassNameFormate(empIdFortNightKey)}"> // invoice_reports.js
                        <td>${(loopIndex+1)}</td>
                        ${this.getEmployeeId(currentLoopObj)}
                        <td>${currentLoopObj["EmpName"]}</td>
                        <td>${currentLoopObj["FortNight"]}</td>
                        <td>${this.getMismatchErrorOnMismatch(currentLoopObj, 'Finance Approved', 'Finance Approved')}</td>
                        <td>${this.getMismatchErrorOnMismatch(currentLoopObj, 'Total Advance', 'Total Advance')}</td>
                        <td>${this.getMismatchErrorOnMismatch(currentLoopObj, 'Prev Adjustment', 'Prev Adjustment')}</td>
                        <td>${this.getMismatchErrorOnMismatch(currentLoopObj, 'Final Payable', 'Final Payable')}</td>
                        <td>${this.getMismatchErrorOnMismatch(currentLoopObj, 'Total Addition Adjustments', 'Total Addition Adjustments')}</td>
                        <td>--</td>
                        <td class="transactionIdTd">${this.getTransactionId(currentLoopObj, empIdFortNightKey)}</td>
                        <td>${this.getTransactionAmount(currentLoopObj, loopIndex, empIdFortNightKey)}</td>
                        <td>${this.getTransactionDate(currentLoopObj, loopIndex, empIdFortNightKey)}</td>
                        <td>${this.getRemarks(currentLoopObj, loopIndex, empIdFortNightKey)}</td>
                       </tr>`;
        }
        return tableBody
    }
    getEmployeeId(currentLoopObj){
        let empIdTableData=`<td>${currentLoopObj["EmpId"]}</td>`;
        if(currentLoopObj.hasOwnProperty(this.invalidEntry)){ // if currentloop consist invalid key
            empIdTableData=`<td class="bgTableRow-danger">${currentLoopObj["EmpId"]} <p class="mb-0 errorMsgTag">Invalid Entry</p></td>`;
        }else if(currentLoopObj.hasOwnProperty(this.duplicateEntry)){
            empIdTableData=`<td class="bgTableRow-danger">${currentLoopObj["EmpId"]} <p class="mb-0 errorMsgTag">Duplicate Entry</p></td>`;
        }
        return empIdTableData;
    }
    
    getMismatchErrorOnMismatch(currentLoopObj, errorType, key){
        let returnUI=currentLoopObj[key];
        let currentLoopObjConsistAnyMismatchs=currentLoopObj.hasOwnProperty(this.misMatched);
            if(currentLoopObjConsistAnyMismatchs){
                let fieldMismatched=currentLoopObj[this.misMatched].includes(errorType); //if exist means finance Approved amount got modified from main excel
                if(fieldMismatched){
                    returnUI=`${currentLoopObj[key]} <p class="mb-0 errorMsgTag">Modified Value</p>`; // if error present ui overided
                }
            }
        return returnUI;
    }
    getTransactionId(currentLoopObj, empIdFortNightKey){
        let transactionId=currentLoopObj["T-Id"];
        let transactionIdUi;
        if(transactionId){
            transactionIdUi=`<textarea  rows="1" class="form-control" readonly>${transactionId}</textarea>`;
        }else{
            transactionIdUi='<p class="mb-0 errorMsgTag">Required</p>';
            this.updateErrorBucket(empIdFortNightKey, this.TransactionIdRequired);
        }
        return transactionIdUi;
    }
    getTransactionAmount(currentLoopObj, currentLoopIndex, empIdFortNightKey){
        let transactionAmount=parseInt(currentLoopObj["Transaction Amount"]);
        let finalPayableValue= parseInt(currentLoopObj["Final Payable"]);
        let transactionAmountUi=`<input type="number" class="form-control transactionAmountInput"  value="${transactionAmount}" data-index="${currentLoopIndex}"/>`;
        if(transactionAmount){
            if(transactionAmount > finalPayableValue){
                transactionAmountUi=`<input type="number" class="form-control transactionAmountInput"  value="${transactionAmount}" data-index="${currentLoopIndex}"/> <p class="mb-0 errorMsgTag">Transaction Amount greater than final Amount</p>`;
                this.updateErrorBucket(empIdFortNightKey, this.invalidTransactionAmount);
            }
        }else{
            transactionAmountUi=`<input type="number" class="form-control transactionAmountInput"  value="${transactionAmount}" data-index="${currentLoopIndex}"/><p class="mb-0 errorMsgTag">Required</p>`;
            this.updateErrorBucket(empIdFortNightKey, this.transactionAmountRequired);
        }
        return transactionAmountUi;
    }
    getTransactionDate(currentLoopObj , currentLoopIndex, empIdFortNightKey){
        let transactionDate=currentLoopObj["Transaction Date"];
        let transactionDateUi;
        if(transactionDate){
            let returnValue=validateDateFormate(transactionDate); // in invoice.js
            currentLoopObj["Transaction Date"]=returnValue["dateValue"];//update json Date
            if(returnValue["validDate"]){
                transactionDateUi=`<input type="date" class="form-control transactionDate" value="${returnValue["dateValue"]}" data-index="${currentLoopIndex}"/>`;
            }else{
                transactionDateUi=`<input type="date" class="form-control transactionDate" value=""  data-index="${currentLoopIndex}"/> <p class="mb-0 errorMsgTag">Invalid date format(${transactionDate}) </p>`;
                this.updateErrorBucket(empIdFortNightKey, this.invalidDateFormat);
            }
        }else{
            transactionDateUi=`<input type="date" class="form-control transactionDate" value="" data-index="${currentLoopIndex}" /> <p class="mb-0 errorMsgTag">Required</p>`;
            this.updateErrorBucket(empIdFortNightKey, this.transactionDateRequired);
        }
        return transactionDateUi;
    }
    getRemarks(currentLoopObj, currentLoopIndex, empIdFortNightKey){
        let remarks=currentLoopObj["Remarks"].replace(/[`]/g,'').trim();
        currentLoopObj["Remarks"]=remarks;
        let remarksUi=`<textarea rows="1" class="form-control transactionRemarks" maxlength="300" data-index="${currentLoopIndex}">${remarks}</textarea>`;
        if(remarks.length >300){
            remarksUi=`<textarea  rows="1" class="form-control transactionRemarks" maxlength="300" data-index="${currentLoopIndex}">${remarks}</textarea><p class="errorMsgTag">Max 300 char</p>`;
            this.updateErrorBucket(empIdFortNightKey, this.exceedLimit);
        }
        return remarksUi;
    }
    
    validateJson(currentLoopIndex, lastLoopIndex, currentLoopObj, nextLoopObj, empIdFortNightKey){
        let mismatchEntries=()=>{
            let tempArray=[];
            let currentObjFrom_downloadedExcel=RESPONSE_FN_ADJUSTMENT_FORTNIGHT_CLAIMS_OBJ[empIdFortNightKey]; // in additionalAdjustment.js
            let valuesOfThisFieldsNotTobeModifyInUploadedExcel=['Finance Approved', 'Total Advance', 'Prev Adjustment', 'Final Payable', 'Total Addition Adjustments'];
            for(let innerLoopIndex=0; innerLoopIndex<valuesOfThisFieldsNotTobeModifyInUploadedExcel.length; innerLoopIndex++){
                let value=valuesOfThisFieldsNotTobeModifyInUploadedExcel[innerLoopIndex];
                if(currentObjFrom_downloadedExcel[value] != parseFloat(currentLoopObj[value]).toFixed(2)){ //currentLoopObj = currentObjFrom_uploadedExcel // string => float => 2 decical
                    this.updateErrorBucket(empIdFortNightKey, this.misMatched, value);       //update errorBucket
                    tempArray.push(value);                                              
                }
                currentLoopObj[this.misMatched]=tempArray;                                     // update currentLoop Obj
            }
        };
        let duplicateTransactionId=()=>{
            currentLoopObj["T-Id"]=currentLoopObj["T-Id"].replace(/[`]/g,'').trim();
            if(UPLOADED_EXCEL_TRANSACTIONS_ID_OBJECT.hasOwnProperty(currentLoopObj["T-Id"])){ //if already exist ,its mean already with same transId having in obj,then this  is duplicate T-id
                UPLOADED_EXCEL_TRANSACTIONS_ID_OBJECT[currentLoopObj["T-Id"]].push(convertToClassNameFormate(empIdFortNightKey)); // invoices_reports.js
                this.updateErrorBucket(empIdFortNightKey, this.duplicateTransactionId);
            }else{
                UPLOADED_EXCEL_TRANSACTIONS_ID_OBJECT[currentLoopObj["T-Id"]]=[convertToClassNameFormate(empIdFortNightKey)];  // invoices_reports.js
            }
        }
        let duplicateEmpId_fn=()=>{
            if(currentLoopIndex!=lastLoopIndex){
                if(currentLoopObj["EmpId"]==nextLoopObj["EmpId"] && currentLoopObj["FortNight"] == nextLoopObj["FortNight"]){
                    this.updateErrorBucket(empIdFortNightKey, this.duplicateEntry); //update errorBucket
                    currentLoopObj[this.duplicateEntry]=true;                             // update currentLoop Obj
                    nextLoopObj[this.duplicateEntry]=true;                                // update nextLoop Obj
                }else{ //if not duplicate entry check duplicate trans'n-Id
                    duplicateTransactionId();
                }
            }else{ //on last loop check duplicateT_id
                duplicateTransactionId();
            }
        };
        let invalidEntry=()=>{
            if(!RESPONSE_FN_ADJUSTMENT_FORTNIGHT_CLAIMS_OBJ.hasOwnProperty(empIdFortNightKey)){
                this.updateErrorBucket(empIdFortNightKey,this.invalidEntry); // update errorbucket
                currentLoopObj[this.invalidEntry]=true;                       // update currentLoop obj
            }else{ // if validEntry
                mismatchEntries();
                duplicateEmpId_fn();
            }
        };
        invalidEntry();
        
        // Invalid Entry
        // Mismatch Entries
        // Duplicate EmpId_fortNight
        // Duplicate TransactionId
    }
    
    updateErrorBucket(empIdFn, errorType, errorKey){
        if(!PENDING_ADDITIONAL_ADJUSTMENT_ERROR_BUCKET.hasOwnProperty(empIdFn)){ // check emp_fortnight in object exist or not , not exist create obj
             PENDING_ADDITIONAL_ADJUSTMENT_ERROR_BUCKET[empIdFn]={};
        }
            if(errorType!="MismatchedField"){
                PENDING_ADDITIONAL_ADJUSTMENT_ERROR_BUCKET[empIdFn][errorType]=true;
            }else{
                if((PENDING_ADDITIONAL_ADJUSTMENT_ERROR_BUCKET[empIdFn]).hasOwnProperty(errorType)){ // checking is errorType="MismatchedField" array already exist  if not create else push
                    PENDING_ADDITIONAL_ADJUSTMENT_ERROR_BUCKET[empIdFn][errorType].push(errorKey)
                }else{
                    PENDING_ADDITIONAL_ADJUSTMENT_ERROR_BUCKET[empIdFn][errorType]=[errorKey];
                }
            }
       
    }
}
// -------------------------------------------------

/*Aim 
check uploaded excel 
1) should not be any invalid enries. ie:- only need to update adjustment that downloaded from pending Table
2) should not be modify any static Data(Finance final amount, Advance amount, Addjustment, ...).
3) should not be any Duplicate entries like same "empId and fortnight" not to repeate.
4) should not be any Duplicate Transaction ids


// process
while looping each obj, updating currentloopObj by errors if exist(invalid, duplicates, mismatches) 
And maintaining ErrorBucket object for current loop (emp_fortnight) obj if error exist.

//storing all transaction-id in Global Varaible "UPLOADED_EXCEL_TRANSACTIONS_ID_OBJECT" 
example:-
UPLOADED_EXCEL_TRANSACTIONS_ID_OBJECT={
    transact_123:["emp_123_2022-January 1st Fn"],
    transact_124:["emp_123_2022-January 1st Fn","emp_123_2022-January 1st Fn"], // duplicate entries
    transact_125:["emp_123_2022-January 1st Fn"],
}

appending Duplicate transaction-Id key work while looping whose transaction Id array consists more tham one entry
*/


// ;;