import { Component, OnInit, ViewChild } from "@angular/core";
import { CommonService } from "src/app/hmg-common/services/common/common.service";
import { TranslatorService } from "src/app/hmg-common/services/translator/translator.service";
import { WorkListRequest } from "../models/workListRequest";
import * as moment from "moment";
import { WorKListResponse } from "../models/workListResponse";
import { WorklistService } from "../service/worklist.service";
import { IonInfiniteScroll } from '@ionic/angular';
import { WorklistMainService } from '../service/work-list.main.service';

@Component({
  selector: "app-home",
  templateUrl: "./home.component.html",
  styleUrls: ["./home.component.scss"]
})
export class HomeComponent implements OnInit {
  // @ViewChild(Navbar) navBar: Navbar;
  public static NOTIFICATION_DATA = 'notification_data';
  public static NOTIFICATION_ARR = 'notification_arr';
  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
  private WorkListObj: WorkListRequest;
  WorkListResObj: any; //WorKListResponse;
  notificationType: string = "1";
  pageNum: number = 1;
  rowsNo: number;
  noOfrows: number;
  selectedValue: string = "";
  FromUserName: string = "";
  ItemKeyDisplayName: string = "";
  SentDate: string = "";
  Subject: string = "";
  inputSearch: string = "";
  HideDateInput: boolean = false;
  HideTextInput: boolean = false;
  InputDate: string;
  filteredNotificationList: any;
  IsReachEnd: boolean = false;
  public noData: boolean = false;
  public isAll: boolean = true;
  public isPR: boolean = false;
  public isPO: boolean = false;
  public isMR: boolean = false;
  public isHR: boolean = false;
  public isITG: boolean = false;
  public isSearch: boolean = false;
  public direction = 'ltr';
  constructor(
    public common: CommonService,
    public ts: TranslatorService,
    public WorklistService: WorklistService,
    public workListService: WorklistMainService
  ) {
    this.WorkListObj = new WorkListRequest();
    this.WorkListObj.P_NOTIFICATION_TYPE = "1";
    this.WorkListObj.P_SEARCH_FROM_USER = "";
    this.WorkListObj.P_SEARCH_SUBJECT = "";
    this.WorkListObj.P_SEARCH_SENT_DATE = "";
    this.WorkListObj.P_SEARCH_ITEM_TYPE_DSP_NAME = "";
    this.WorkListObj.P_PAGE_NUM = 0;
    this.WorkListObj.P_PAGE_LIMIT = 50; //number
  }

  ngOnInit() {
    this.direction = TranslatorService.getCurrentDirection();
  }
  ionViewDidLoad() {
    // this.navBar.backButtonClick = () => {
    //   // you can set a full custom history here if you want
    //   let pages = [
    //     {
    //       page: "HomePage"
    //     }
    //   ];
    //   this.navCtrl.setPages(pages);
    // };
  }

  ionViewWillEnter() {
    this.getAllPushNotificationFun();
  }
  getValueSelected(Value) {
    this.selectedValue = Value.detail.value;
    if (this.selectedValue == "1") {
      this.HideDateInput = false;
      this.HideTextInput = true;
    } else if (this.selectedValue == "2") {
      this.HideDateInput = false;
      this.HideTextInput = true;
    } else if (this.selectedValue == "3") {
      this.HideDateInput = true;
      this.HideTextInput = false;
    } else if (this.selectedValue == "4") {
      this.HideDateInput = false;
      this.HideTextInput = true;
    } else if (this.selectedValue == "5") {
      this.HideDateInput = false;
      this.HideTextInput = false;
    }
  }

  getAllPushNotificationFun() {
    this.WorkListObj.P_PAGE_NUM = 1;
    this.IsReachEnd = false;

    if (this.selectedValue == "1") {
      this.WorkListObj.P_SEARCH_FROM_USER = this.inputSearch;
      this.WorkListObj.P_SEARCH_ITEM_TYPE_DSP_NAME = "";
      this.WorkListObj.P_SEARCH_SENT_DATE = "";
      this.WorkListObj.P_SEARCH_SUBJECT = "";
    } else if (this.selectedValue == "2") {
      this.WorkListObj.P_SEARCH_FROM_USER = "";
      this.WorkListObj.P_SEARCH_ITEM_TYPE_DSP_NAME = "";
      this.WorkListObj.P_SEARCH_SENT_DATE = "";
      this.WorkListObj.P_SEARCH_SUBJECT = this.inputSearch;
    } else if (this.selectedValue == "3") {
      this.WorkListObj.P_SEARCH_FROM_USER = "";
      this.WorkListObj.P_SEARCH_ITEM_TYPE_DSP_NAME = "";
      if (this.InputDate)
        this.WorkListObj.P_SEARCH_SENT_DATE = moment(this.InputDate).format(
          "DD-MMM-YYYY"
        );
      else this.WorkListObj.P_SEARCH_SENT_DATE = "";
      this.WorkListObj.P_SEARCH_SUBJECT = "";
    } else if (this.selectedValue == "4") {
      this.WorkListObj.P_SEARCH_FROM_USER = "";
      this.WorkListObj.P_SEARCH_ITEM_TYPE_DSP_NAME = this.inputSearch;
      this.WorkListObj.P_SEARCH_SENT_DATE = "";
      this.WorkListObj.P_SEARCH_SUBJECT = "";
    } else if (this.selectedValue == "5") {
      this.WorkListObj.P_SEARCH_FROM_USER = "";
      this.WorkListObj.P_SEARCH_ITEM_TYPE_DSP_NAME = "";
      this.WorkListObj.P_SEARCH_SENT_DATE = "";
      this.WorkListObj.P_SEARCH_SUBJECT = "";
    }

    this.WorklistService.getWorkList(this.WorkListObj).subscribe(
      (result: WorKListResponse) => {
        this.handleWorkListResult(result);
      }
    );
  } //End getNotifications

  private handleWorkListResult(result) {
    if (this.common.validResponse(result)) {
      // this.sharedData.setSharedData(result, WorKListResponse.SHARED_DATA);
      if (this.common.hasData(result.GetWorkList)) {
        this.WorkListObj.P_PAGE_NUM++;
        this.WorkListResObj = result.GetWorkList;
        this.filteredNotificationList = this.WorkListResObj;
        this.common.sharedService.setSharedData(this.WorkListResObj, HomeComponent.NOTIFICATION_ARR);
        let lastItemIndex = this.WorkListResObj.length - 1;
        if (result.GetWorkList[lastItemIndex]) {
          let lastitem = result.GetWorkList[lastItemIndex];
          if (lastitem.NO_OF_ROWS == lastitem.ROW_NUM) {
            this.IsReachEnd = true;
          } else {
            this.IsReachEnd = false;
          }
          // this.navCtrl.push('SmsAuthenticatePage');
        }
      } else {
        this.WorkListResObj = [];
      }
    }
  }

  openNotificationDetail(obj) {
    console.log(obj);
    this.common.sharedService.setSharedData(obj, HomeComponent.NOTIFICATION_DATA);
    //this.common.openWorklistMainPage();
    if(obj.REQUEST_TYPE == "PR"){
    this.common.openWorklistMainPRPage();
    }
    else if(obj.REQUEST_TYPE == "PO"){
      this.common.openWorklistMainPOPage();
      }
      else if(obj.REQUEST_TYPE == "MO"){
      this.common.openWorklistMainMRPage();
      }
      
    else{
       this.common.openWorklistMainPage();

    }

    //this.navCtrl.push("WorkListMainPage", { passNotificationList: obj });
  }
  onChangeView(selectedValue: any) {
    this.WorkListObj.P_NOTIFICATION_TYPE = selectedValue.detail.value;
  }

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

    if (!this.IsReachEnd) {
      this.WorklistService.getWorkList(this.WorkListObj).subscribe(
        (result: any) => {
          if (this.common.validResponse(result)) {
            this.WorkListObj.P_PAGE_NUM++;
            if (this.common.hasData(result.GetWorkList)) {
              result.GetWorkList.forEach(element => {
                if (element.ROW_NUM == element.NO_OF_ROWS) {
                  this.IsReachEnd = true;
                } else {
                  this.IsReachEnd = false;
                }
                this.WorkListResObj.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

  Count() {
    this.workListService.getITGCount()
      .subscribe((result: any) => {
        console.log("ENAD ITG COUNT:")
        console.log(result);
      });
  }

  filterNotificationByRequestType(reqType) {
    let filterdType = [];
    if (reqType == "All") {
      this.filteredNotificationList = this.WorkListResObj;
      if (this.filteredNotificationList == [] || this.filteredNotificationList == null || this.filteredNotificationList == "") {
        this.noData = true;
        return false;
      } else {
        this.noData = false;
        return false;
      }
    }
    for (let i = 0; i < this.WorkListResObj.length; i++) {
      if(reqType=="HR"){
        if ("EIT" == this.WorkListResObj[i].REQUEST_TYPE || "ABSENCE" == this.WorkListResObj[i].REQUEST_TYPE) {
          filterdType.push(this.WorkListResObj[i]);
        }
      }else{
        if (reqType == this.WorkListResObj[i].REQUEST_TYPE) {
          filterdType.push(this.WorkListResObj[i]);
        }
      }
     
    }

    this.filteredNotificationList = filterdType;
    if (this.filteredNotificationList == [] || this.filteredNotificationList == null || this.filteredNotificationList == "") {
      this.noData = true;
      return false;
    } else {
      this.noData = false;
      return false;
    }
  }

  Details() {
    this.workListService.getITGDetails()
      .subscribe((result: any) => {
        console.log("ENAD ITG Details:")
        console.log(result);
      });
  }


  AllNotification() {
    this.filterNotificationByRequestType("All");
    this.isAll = true;
    this.isPR = false;
    this.isPO = false;
    this.isMR = false;
    this.isHR = false;
    this.isITG = false;
  }
  PRNotification() {
    this.filterNotificationByRequestType("PR");
    this.isAll = false;
    this.isPR = true;
    this.isPO = false;
    this.isMR = false;
    this.isHR = false;
    this.isITG = false;
  }
  PONotification() {
    this.filterNotificationByRequestType("PO");
    this.isAll = false;
    this.isPR = false;
    this.isPO = true;
    this.isMR = false;
    this.isHR = false;
    this.isITG = false;

  }
  MRNotification() {
    this.filterNotificationByRequestType("MO");
    this.isAll = false;
    this.isPR = false;
    this.isPO = false;
    this.isMR = true;
    this.isHR = false;
    this.isITG = false;
  }
  HRNotification() {
    this.filterNotificationByRequestType("HR");
    this.isAll = false;
    this.isPR = false;
    this.isPO = false;
    this.isMR = false;
    this.isHR = true;
    this.isITG = false;
  }
  ITGNotification() {
    // this.Details();
    this.isAll = false;
    this.isPR = false;
    this.isPO = false;
    this.isMR = false;
    this.isHR = false;
    this.isITG = true;
  }

  searchBtn() {
    this.isSearch = !this.isSearch;
  }

}
