import { Component, OnInit, Inject } from '@angular/core';
import { ContactService } from '../contact.service';
import { MD_DIALOG_DATA, MdDialogRef } from '@angular/material';
import { FlashMessagesService } from 'angular2-flash-messages';

@Component({
  selector: 'app-notification-setting',
  templateUrl: './notification-setting.component.html',
  styleUrls: ['./notification-setting.component.scss']
})
export class NotificationSettingComponent implements OnInit {
  isAddEmail:boolean = false;
  isAddPhone:boolean =false;
  emailList: any = [];
  phonelist:any=[];
  useridd: any;
  notifType: any;
  femail: any;
  fphone: any;
  constructor(public dialogRef: MdDialogRef<NotificationSettingComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,public contactService :ContactService,private _flashMessagesService: FlashMessagesService) { 
      this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;  
        console.log(data.notifData);
        this.notifType = data.notifData.notifType;
        this.data = data.notifData.compData;
        console.log(this.data);
        console.log("Notification type =>",this.notifType);
        if(data.notifData.buttonClick == 'email'){
          this.isAddEmail = true;
        }
        if(data.notifData.buttonClick == 'phone'){
          this.isAddPhone = true;
        }

       
    }

  ngOnInit() {
    this.emailList=this.data[this.notifType].emails;
    this.phonelist=this.data[this.notifType].phones;
  }
  fData:any={};
  addEmail(id,index){
    if(id == 'add'){
      if(!this.data[this.notifType].emails){
        this.data[this.notifType].emails=[];
      }
      this.data[this.notifType].emails.push(this.femail);
      console.log(this.data);
      this.fData.contactid = this.useridd;
      this.fData.alert = this.data;
      console.log("fData object =>",this.fData);
      this.contactService.editDealer(this.fData).subscribe(resp => {
          console.log(resp);
          this.femail=null;
       //   this.dialogRef.close("success");
        // toastr.success("Setting Updated");
        // this.router.navigateByUrl("/account")
        // this.dataOriginal = JSON.parse(JSON.stringify(this.data));
      }, err => {
        console.log("err", err);
      })
    }
    if(id == 'delete'){
      var splicedmail =  this.data[this.notifType].emails[index];   
      console.log("spliceNumber =>",splicedmail)
       this.data[this.notifType].emails.splice(index,1);
 
       this.fData.contactid = this.useridd;
       this.fData.alert = this.data;
       this.contactService.editDealer(this.fData).subscribe(resp => {
      //   this.dialogRef.close("deleted");
      
       }, err => {
         this.data[this.notifType].emails.push(splicedmail);
         this._flashMessagesService.show("Internal server error, please try again!!!", { cssClass: 'alert-danger', timeout: 2000 });
         console.log("err", err);
       })
       // this.dialogRef.close("success");
     }

   

  }
 
  addPhone(id,index){
    if(id == 'add'){
      if(!this.data[this.notifType].phones){
        this.data[this.notifType].phones=[];
      }
      this.data[this.notifType].phones.push(this.fphone);
      console.log("in addd",this.fphone);
      this.fData.contactid = this.useridd;
      console.log("This.data=>",this.data);
      this.fData.alert = this.data;
      console.log("finalData=>",this.fData);
      this.contactService.editDealer(this.fData).subscribe(resp => {
        this.fphone=null;
     //   this.dialogRef.close("success");
     
        // toastr.success("Setting Updated");
        // this.router.navigateByUrl("/account")
        // this.dataOriginal = JSON.parse(JSON.stringify(this.data));
      }, err => {
        console.log("err", err);
      })
    }
    if(id == 'delete'){
     var splicedNum =  this.data[this.notifType].phones[index];   
     console.log("spliceNumber =>",splicedNum)
      this.data[this.notifType].phones.splice(index,1);

      this.fData.contactid = this.useridd;
      this.fData.alert = this.data;
      this.contactService.editDealer(this.fData).subscribe(resp => {
     //   this.dialogRef.close("deleted");
      }, err => {
        this.data[this.notifType].phones.push(splicedNum);
        this._flashMessagesService.show("Internal server error, please try again!!!", { cssClass: 'alert-danger', timeout: 2000 });
        console.log("err", err);
      })
      // this.dialogRef.close("success");
    }
  }

  Close(){
        this.dialogRef.close();
  }
}
