import { Component, OnInit, ViewChild } from '@angular/core';
import {PayslipService} from '../service/payslip.service'
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { MenuResponse } from 'src/app/hmg-common/services/menu/models/menu-response';
import { SharedDataService } from 'src/app/hmg-common/services/shared-data-service/shared-data.service';
import { DeductionRequest } from '../model/deductionReq';
import { IonInfiniteScroll } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements OnInit {
  selEmp: string;
  respID: number;
  selMenu: MenuResponse;
  GetPayslipList:any='';
  GetSummaryOfPaymentList:any='';
  GetPaymentInformationList:any='';
  showPaySlip:any=[];
  ActionContextID:any;
  public static ACTION_CONTEXT_ID = "passActionContectID";
  GetEarningsList: any='';
  GetDeductionsList: any='';
  earingTotal: any;
  deductionTotal: any;
  firstPayslip: any;
  defultSelected: any;
  IsReachEnd: any;
  private deductionReq: DeductionRequest;
  doinfiniteDedReq:any;
  doinfinitEarReq:any;

  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;

  constructor(
    public payslipService:PayslipService,
    public ts: TranslatorService,
    public common: CommonService,
    public sharedData: SharedDataService
    ) {}

  ngOnInit() {
        // this.selEmp = this.common.sharedService.getSharedData(
        //   MenuResponse.SHARED_SEL_EMP,
        //   false
        // );
        // if(this.selEmp){}else{
        //   this.authService.loadAuthenticatedUser().subscribe((user: AuthenticatedUser) => {
        //     if (user) {
        //       this.selEmp = user.ASSIGNMENT_NUMBER;

        //     }
        //   });
        // }
       
        // console.log(" this.selEmp" + this.selEmp);
        // this.respID = this.common.sharedService.getSharedData(
        //   MenuResponse.SHARED_SEL_RESP_ID,
        //   false
        // );
        // if(this.respID){}else{
        //   this.respID=-999;
        // }
        // console.log(" this.respID" + this.respID);

        // this.selMenu = new MenuResponse();
        // this.selMenu = this.common.sharedService.getSharedData(
        //   MenuResponse.SHARED_DATA,
        //   false
        // );
        // console.log(" this.selMenu" + this.selMenu);

  
    this.getPayslip();
  }

  getPayslip(){
   // this.selEmp ="13777";
   console.log("this.selEmp????????"+this.selEmp)
    this.respID=-999;
    const request = {
      P_SELECTED_EMPLOYEE_NUMBER: this.selEmp,//'125346',//this.selEmp,
      P_MENU_TYPE:"E",// 'E',//this.selMenu.List_Menu.MENU_TYPE,
      P_SELECTED_RESP_ID: this.respID//"-999" //this.respID,
      // P_PAGE_NUM: this.P_PAGE_NUM,
      // P_PAGE_LIMIT: this.P_PAGE_LIMIT
    };
    this.payslipService.getPayslip(request, ()=> {} , this.ts.trPK('general', 'retry')).subscribe((result)=> 
    {     
       this.handleRespondGetPayslipResult(result);
    }
    );
  }

  handleRespondGetPayslipResult(result){
   
    if (result.GetPayslipList != null) {
      this.GetPayslipList = result.GetPayslipList;
      this.firstPayslip= this.GetPayslipList[0]
      this.defultSelected=this.firstPayslip.PAYSLIP_CHOICE                
    }
    this.showPaySlip = this.GetPayslipList[0];
    this.getAllPayslipData(this.GetPayslipList[0].ACTION_CONTEXT_ID);
  }

  // showPaymentInfo
  showInfo(event){
   console.log(event.detail.value);
  const index = parseInt(event.detail.value);
  this.showPaySlip =this.GetPayslipList[index];
  this.ActionContextID=this.GetPayslipList[index].ACTION_CONTEXT_ID;
  this.getAllPayslipData(this.ActionContextID);
  }

  getAllPayslipData(ActionContextID){
    this.common.sharedService.setSharedData(ActionContextID,HomeComponent.ACTION_CONTEXT_ID);
    this.getSummeryOfPayment(ActionContextID);
    this.getPaymentInfo(ActionContextID);
    this.getDeduction(ActionContextID);
    this.getEarings(ActionContextID);
  }

  getSummeryOfPayment(ActionContextID){
    const request = {
      P_ACTION_CONTEXT_ID: ActionContextID,
    };
    
    this.payslipService.getSummeryOFPayment(request, ()=> {} , this.ts.trPK('general', 'retry')).subscribe((result)=> 
    {     
       this.handleSummeryOfPaymentResult(result);
    }
    );
  }

  handleSummeryOfPaymentResult(result){
    if (result.GetSummaryOfPaymentList != null) {
      this.GetSummaryOfPaymentList = result.GetSummaryOfPaymentList[0];
      this.earingTotal =  this.GetSummaryOfPaymentList.TOTAL_EARNINGS_AMOUNT;
      this.deductionTotal =  this.GetSummaryOfPaymentList.TOTAL_DEDUCTIONS_AMOUNT;

    }
  }


getPaymentInfo(ActionContextID){
  const request = {
    P_ACTION_CONTEXT_ID: ActionContextID,
  };
  
  this.payslipService.getPaymentInfo(request, ()=> {} , this.ts.trPK('general', 'retry')).subscribe((result)=> 
  {     
     this.handleGetPaymentInfoResult(result);
  }
  );
}

handleGetPaymentInfoResult(result){
  if (result.GetPaymentInformationList != null) {
    this.GetPaymentInformationList = result.GetPaymentInformationList[0];
  }
}

openEarings(){
  this.common.openEarningsPage();

}
openDeductions(){
  this.common.openDeductionsPage();
}

getEarings(ActionContextID){
  console.log("getEarings");
 
  // this.deductionReq.P_ACTION_CONTEXT_ID=ActionContextID;
  // this.deductionReq.P_PAGE_NUM=1;
  // this.deductionReq.P_PAGE_LIMIT=100;
  const request ={
    P_ACTION_CONTEXT_ID:ActionContextID,
    P_PAGE_NUM:1,
    P_PAGE_LIMIT:100
   }
  this.doinfinitEarReq=request;
  this.payslipService.getEarings(request, ()=> {} , this.ts.trPK('general', 'retry')).subscribe((result)=> 
  {     
     this.handleGetEaringsResult(result);
  }
  );
}

handleGetEaringsResult(result){
  if (result.GetEarningsList != null) {
    this.GetEarningsList = result.GetEarningsList;
  }



}

getDeduction(ActionContextID){
  console.log("getDeduction");
  const request ={
   P_ACTION_CONTEXT_ID:ActionContextID,
   P_PAGE_NUM:1,
   P_PAGE_LIMIT:100
  }
  // this.deductionReq.P_ACTION_CONTEXT_ID=ActionContextID;
  // this.deductionReq.P_PAGE_NUM=1;
  // this.deductionReq.P_PAGE_LIMIT=100;
   this.doinfiniteDedReq=request;
    
    this.payslipService.getDeduction(request, ()=> {} , this.ts.trPK('general', 'retry')).subscribe((result)=> 
    {     
       this.handleGetDeductionResult(result);
    }
    );
  }

  handleGetDeductionResult(result){
    if (result.GetDeductionsList != null) {
      this.GetDeductionsList = result.GetDeductionsList;
    }
  }


  doInfiniteDeduction(infiniteScroll) {
    //this.pageNum= this.pageNum + 1;

    if (!this.IsReachEnd) {
      this.payslipService.getDeduction(this.doinfiniteDedReq).subscribe(
        (result: any) => {
          if (this.common.validResponse(result)) {
            this.doinfiniteDedReq.P_PAGE_NUM++;
            if (this.common.hasData(result.GetDeductionsList)) {
              result.GetDeductionsList.forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.GetDeductionsList.push(element);
              });
            } // if list length >0
            else {
              this.IsReachEnd = true;
            }
          } // if response == 1
          //this.pageNum++;
          this.infiniteScroll.complete();
        }
      );
    } else {
      if (this.infiniteScroll) this.infiniteScroll.complete();
    }
  } //end infiniteScroll


  doInfiniteEarning(infiniteScroll) {
    //this.pageNum= this.pageNum + 1;

    if (!this.IsReachEnd) {
      this.payslipService.getEarings(this.doinfinitEarReq).subscribe(
        (result: any) => {
          if (this.common.validResponse(result)) {
            this.doinfinitEarReq.P_PAGE_NUM++;
            if (this.common.hasData(result.GetEarningsList)) {
              result.GetEarningsList.forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.GetEarningsList.push(element);
              });
            } // if list length >0
            else {
              this.IsReachEnd = true;
            }
          } // if response == 1
          //this.pageNum++;
          this.infiniteScroll.complete();
        }
      );
    } else {
      if (this.infiniteScroll) this.infiniteScroll.complete();
    }
  } //end infiniteScroll



}
