import { vacationRuleResponse } from './../model/vacationRule.Respond';
import { notificationTypeRequest } from './../model/notification.Request';
import { DeleteVacationRuleRequest } from './../model/deleteVacationRule.Request';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { VacationRuleServiceService } from './../service/vacation-rule-service.service';
import { VacationRuleRequest } from './../model/VacationRuleRequest';
import { Component, OnInit, ViewChild } from '@angular/core';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { Router } from '@angular/router';
import { ButtonInput } from 'src/app/uI-elements/button.input';
import { IonInfiniteScroll } from '@ionic/angular';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
})
export class HomeComponent implements OnInit {
  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;

  userName: any;
  P_PAGE_NUM: number;
  P_PAGE_LIMIT: number;
  GetVacationRulesList: any = [];
  isUpdate: boolean = false;
  IsReachEnd: boolean = false;
  RespondAttributeList: any;
  add:any='add';
  button: ButtonInput;
  direction = '';
  notBadge = '';
  // dateTime: DateTimeInput;
  // date: DateInput;
  // numberInput: NumberInput;
  // selectInput: SelectInput;
  // textAreaInput: TextAreaInput;
  // timeInput: TimeInput;


  constructor(public vacationRuleService: VacationRuleServiceService, public ts: TranslatorService, public cs: CommonService, private router: Router) {
    this.P_PAGE_NUM = 1;
    this.P_PAGE_LIMIT = 50;
    
    //this.button = new ButtonInput('btnSubmit', 'Submit', 'containerDiv', 'Y');   
    // this.dateTime = new DateTimeInput('Start Date', 'SDate', '', 'containerDiv', 'Y', 'Y', 'Y');
    // this.date = new DateInput('Start Date', 'SDate', '', 'containerDiv', 'Y', 'Y', 'Y');
    // this.numberInput = new NumberInput('Roll No.:', 'rollNo', '', 'containerDiv', 'Y', 'Y', 'Y');
    // this.selectInput = new SelectInput('Gender', 'genderSelect', 'm', 'containerDiv', 'Y', 'Y', 'Y');
    // this.textAreaInput = new TextAreaInput('Message:', 'messageText', '', 'containerDiv', 'Y', 'Y', 'Y');
    // this.timeInput = new TimeInput('Time', 'SDate', '', 'containerDiv', 'Y', 'Y', 'Y');

  }

  showAlert() {
    this.cs.presentAlert('Button Clicked');
  }

  ngOnInit() {
    console.log('OnInit');
    this.getVacationRules();
    // let elem = document.getElementById('btnSubmit');

   // const elemDiv = document.createElement('div');
    // elemDiv.id = this.elementID;
   // elemDiv.className = '';
    
    //elemDiv.innerHTML = this.button.getTemplate();
    // elemDiv.innerHTML = this.dateTime.getTemplate();
    // elemDiv.innerHTML = this.date.getTemplate();
    // elemDiv.innerHTML = this.numberInput.getTemplate();
    // elemDiv.innerHTML = this.selectInput.getTemplate();
    // elemDiv.innerHTML = this.textAreaInput.getTemplate();
    // elemDiv.innerHTML = this.timeInput.getTemplate();

    //document.getElementById('containerDiv').appendChild(elemDiv);

   // let elem = document.getElementById("btnSubmit");
    // console.log(elem);
    // elem.addEventListener("click", (e) => {
    //   this.showAlert();
    //   e.stopImmediatePropagation();
    // });

  }

  Vacation_Type() {
    this.cs.navigateForward('/vacation-rule/vacation-type');
  }

  getVacationRules() {
    this.GetVacationRulesList = [];
    this.P_PAGE_NUM = 1;
    this.IsReachEnd = false;
    let request: VacationRuleRequest = new VacationRuleRequest();
    request.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    request.P_PAGE_NUM = this.P_PAGE_NUM;

    this.vacationRuleService.getVacationRule(request, () => {
      this.getVacationRules();
    }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      if (this.cs.validResponse(result)) {
        this.GetVacationRulesList = result.GetVacationRulesList;
        this.P_PAGE_NUM++;
      } else {
        this.cs.presentAlert(result.ErrorEndUserMessage);
      }
    });
  }

  deleteFunc(i) {
    let alert = this.cs.confirmAlertDialog(() => {
      this.deleteVacationRule(i);
    }, this.ts.trPK('general', 'ok'), () => {
    }, this.ts.trPK('general', 'cancel'), this.ts.trPK('vacation-rule', 'confirmation'), this.ts.trPK('vacation-rule', 'deleteVR'));
  }

  deleteVacationRule(i) {
    let request: DeleteVacationRuleRequest = new DeleteVacationRuleRequest();
    request.P_RULE_ID = this.GetVacationRulesList[i].RULE_ID;
    this.vacationRuleService.deleteVacationRule(request, () => {
      this.deleteVacationRule(i);
    }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      if (this.cs.validResponse(result)) {
        this.handleDeleteVacationRuleResult(result)
      } else {
        this.cs.presentAlert(result.ErrorEndUserMessage);
      }
    });
  }

  handleDeleteVacationRuleResult(result) {
    if (this.cs.validResponse(result)) {
      //this.navCtrl.push('HomePage');
      this.cs.JustAlertDialog(this.ts.trPK('general', 'ok'), this.ts.trPK('vacation-rule', 'rule-delete-success'));
      this.getVacationRules();
    }
  }

  updateFunc(i) {
    this.respondAttributes(i);

    this.cs.sharedService.setSharedData(this.GetVacationRulesList[i], vacationRuleResponse.SHARED_DATA);
    this.isUpdate = true;

    this.cs.sharedService.setSharedData(this.isUpdate, 'isUpdate');
    // this.router.navigate(['/vacation-rule/create-vacation-rule']);
    this.cs.navigateForward('/vacation-rule/vacation-type');

  }


  respondAttributes(i) {
    let request: notificationTypeRequest = new notificationTypeRequest();
    request.P_NOTIFICATION_NAME = this.GetVacationRulesList[i].NOTIFICATION_NAME;
    request.P_ITEM_TYPE = this.GetVacationRulesList[i].ITEM_TYPE;
    this.vacationRuleService.respondAttributes(request, () => {
      this.respondAttributes(i);
    }, this.ts.trPK('general', 'retry')).subscribe((result) => {
      this.handlerespondAttributesResult(result, i);
    });

  }
  handlerespondAttributesResult(result, i) {
    // console.log("Respond att: ")
    if (this.cs.validResponse(result)) {
      // console.log(result.RespondAttributesList);
      this.RespondAttributeList = result.RespondAttributesList;
      this.GetVacationRulesList[i].RespondAttributeList = this.RespondAttributeList;
    }
  }

  doInfinite(infiniteScroll) {
    let request: VacationRuleRequest = new VacationRuleRequest();
    request.P_PAGE_LIMIT = this.P_PAGE_LIMIT;
    request.P_PAGE_NUM = this.P_PAGE_NUM;
    if (!this.IsReachEnd) {
      this.vacationRuleService.getVacationRule(request, () => {
        // this.getVacationRules();
      }, this.ts.trPK('general', 'retry')).subscribe((result) => {
          if (this.cs.validResponse(result)) {
            this.P_PAGE_NUM++;
            request.P_PAGE_NUM = this.P_PAGE_NUM;

            if (this.cs.hasData(result.GetVacationRulesList)) {
              result.GetVacationRulesList.forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.GetVacationRulesList.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


}
