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

@Component({
  selector: 'app-myaccount',
  templateUrl: './myaccount.component.html',
  styleUrls: ['./myaccount.component.scss'],
  providers:[ContactService]
})
export class MyaccountComponent implements OnInit {

  constructor(public dialogRef: MdDialogRef<MyaccountComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService,private _flashMessagesService: FlashMessagesService) {

     }

    onNoClick(a): void {
     
     this.dialogRef.close(a);
    }
    text:any;
    symbol:any;
    contacts: Contact[]=[];
    contact: Contact;
    verify_email(){
      if(this.email_verfi == true){
        this.symbol = '\u2714';
        this.text = "Email Verified"
        this.final_counter1 = 0;
        return true
      }
      else{
        this.symbol =  'X'
        this.text = "Email Not Verified"
        this.final_counter1 = 1;
        return true
      }
    }
    symbol2:any;
    text2:any;
    sound:any;
    text3:any;
    symbol3:any;
    final_counter:any;
    final_counter1:any;
    deal(){
      if(this.custtype == true){
        this.symbol3 = '\u2714';
        this.text3 = "Dealer"
        this.final_counter = 0;
        return true
      }
      else{
        this.symbol3 =  'X'
        this.text3 = "Not a Dealer"
        this.final_counter =1;
        return true
      }
    }
    final_counter2:any;
    Edit(){
      
    }
    verify_phone(){
      if(this.phone_verfi == true){
        this.symbol2 = '\u2714';
        this.text2 = "Mobile Verified"
        this.final_counter2 = 0;
        return true
      }
      else{
        this.symbol2 =  'X'
        this.text2 = "Mobile Not Verified"
        this.final_counter2 =1;
        return true
      }
    }
    first_name:any;
    last_na;
    me:any;
    Load:any;
    GET_notif:any;
    data_descip:any;
tost1(divid){
  if(divid == "nofsls"){
    this.data_descip = "Please Enter Mandatory Fields";
    launch_toast();
  }
 else if(divid == "succ"){
  this.data_descip = "Succesfully Updated";
  launch_toast();
  this.onNoClick("succ");
 }
    function launch_toast() {
     // console.log(divid);
      var x = document.getElementById("toast")
      //console.log(x);
      x.className = "show";
      setTimeout(function(){ x.className = x.className.replace("show", ""); }, 1500);
  }
   }
  
    editAccount(){
      if(this.fs == "" || this.ls == "" || this.fs == " " || this.ls == " " ){
          this.tost1("nofsls")
      }
      else{
       
        const newContact ={
      
          fname: this.fs,
          lname: this.ls,
          org: this.or,
          noti: this.GET_notif,
          uid:this.useridd
            }
            this.contactService.updateuid(newContact)
            .subscribe(contact => {
              this.contacts.push(contact);
            //  console.log(contact.token)
              this.tost1("succ");
              window.localStorage['token'] = contact.token

              
             
            //  if(contact.message === "Registered Sucessfully"){
              //  this.Load=false
             // this.tost1("Confirm_Email")  
            //  }
            } , (err: any) => { 
              // console.log(err.status); 
              // console.log(err);
            if(err.status ==   500)
            {
            //  this.Load=false
             // this.dup_email = JSON.parse(err._body);
             // this.tost1("dup_email")
            }
            else{
             // this.Load=false
      
            }
            } 
          ); 
      }
  
    }
  fs:any;
  ls:any;
  emailid:any;
  or:any;
  useridd:any;
  custtype:any;
  mobile:any;
  email_verfi:any;
  phone_verfi:any;
/*   _orgName
  isDealer */

  ngOnInit() {
    this.fs = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).fn;
    this.ls = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).ln;
    this.emailid = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).email;
    this.or = JSON.parse(window.atob(window.localStorage.token.split(".")[1]))._orgName;
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;  
    this.custtype =  JSON.parse(window.atob(window.localStorage.token.split('.')[1])).isDealer;
    this.mobile = JSON.parse(window.atob(window.localStorage.token.split('.')[1])).phn; 
    this.email_verfi = JSON.parse(window.atob(window.localStorage.token.split('.')[1])).email_verfi; 
    this.phone_verfi = JSON.parse(window.atob(window.localStorage.token.split('.')[1])).phone_verfi; 
    this.GET_notif = JSON.parse(window.atob(window.localStorage.token.split('.')[1])).GET_notif; 
    //console.log(this.mobile) email_verfi phone_verfi
    // console.log(JSON.parse(window.atob(window.localStorage.token.split('.')[1])))
  }

}
