import { Component, OnInit } from '@angular/core';
import { AddEitResponse } from 'src/app/eit/models/add.eit.response';
import { EitRequest } from 'src/app/eit/models/eit.request';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { MenuResponse } from 'src/app/hmg-common/services/menu/models/menu-response';
import { TranslatorService } from 'src/app/hmg-common/services/translator/translator.service';
import { ProfileService } from '../service/profile.service';


@Component({
  selector: 'app-conatact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.scss'],
})
export class ConatactComponent implements OnInit {
  public phoneNumbers: any = [];
  public objectValue: any = [];
  direction: any;
  selEmp: any;
  respID: any;
  selMenu: any;
  submitrequest: any;
  constructor(public cs: CommonService, public ts: TranslatorService, public profileService: ProfileService,) {
    this.direction = TranslatorService.getCurrentLanguageName();
    this.selEmp = this.cs.sharedService.getSharedData(
      MenuResponse.SHARED_SEL_EMP,
      false
    );
    this.respID = this.cs.sharedService.getSharedData(
      MenuResponse.SHARED_SEL_RESP_ID,
      false
    );
    this.selMenu = new MenuResponse();
    this.selMenu = this.cs.sharedService.getSharedData(
      MenuResponse.SHARED_DATA,
      false
    );

  }


  ngOnInit() {
    this.phoneNumbers = this.cs.sharedService.getSharedData(ProfileService.EMP_PHONE, false);
    this.setIsUpdated();
    this.direction = TranslatorService.getCurrentLanguageName();
    this.getObjectValue();
  }
  setIsUpdated() {
    this.phoneNumbers.forEach(element => {
      element['ACTION'] = 'UPDATE_ROW';
    });
  }

  getObjectValue() {
    this.profileService.getObjectValue({
      'P_OBJECT_TYPE': 'LOOKUP',
      'P_OBJECT_NAME': 'PHONE_TYPE',
      P_SELECTED_EMPLOYEE_NUMBER: this.selEmp
    }, () => { }, this.ts.trPK('general', 'retry')).subscribe((response) => {
      if (this.cs.validResponse(response)) {
        console.log(response);
        this.objectValue = response['GetObjectValuesList'];
      }
    })
  }
  setIsDeletedStatus() {
    this.phoneNumbers.forEach(element => {
      if (element['DELETE_ROW'] == true) {
        element['ACTION'] = 'DELETE_ROW';
      }
    });
  }

  updatePhone() {
    this.setIsDeletedStatus();
    console.log(this.phoneNumbers);
    this.submitrequest = {
      P_MENU_TYPE: this.selMenu.List_Menu.MENU_TYPE,
      P_SELECTED_RESP_ID: this.respID, //this.selMenu.List_Menu.RESP_ID,//-999,
      P_FUNCTION_NAME: this.selMenu.GetMenuEntriesList.FUNCTION_NAME,
      P_MBL_PHONES_TBL: this.phoneNumbers,
      P_SELECTED_EMPLOYEE_NUMBER: this.selEmp
    };
    this.profileService.submitPhoneTransaction(this.submitrequest, () => { }, this.ts.trPK('general', 'retry')).subscribe((response) => {
      if (this.cs.validResponse(response)) {
        console.log(response);
        this.handleSubmitEitResult(response);
      }
    })
    //}
  }

  addNewRow() {
    this.phoneNumbers.push({ 'PHONE_NUMBER': '', 'PHONE_TYPE': '', 'ACTION': 'NEW_ROW', 'OBJECT_VERSION_NUMBER': null, 'PHONE_ID': null })
  }
  deleteRow(row) {
    row['DELETE_ROW'] = true;
    // this.phoneNumbers =
    //   this.phoneNumbers.filter(item => item !== row);
    console.log(this.phoneNumbers);

  }
  handleSubmitEitResult(result) {
    if (this.cs.validResponse(result)) {
      this.cs.sharedService.setSharedData(
        this.submitrequest,
        EitRequest.SHARED_DATA
      );
      this.cs.sharedService.setSharedData(result, AddEitResponse.SHARED_DATA);
      this.cs.sharedService.setSharedData(true, AddEitResponse.isContact);
      this.cs.sharedService.setSharedData(
        { isResubmit: false },
        "confirmAddEITData"
      );
      this.cs.openConfirmAddAddress();
    }
  }
}
