import { Component, OnInit,Inject } from '@angular/core';
import { MdDialogRef, MD_DIALOG_DATA, MdSnackBar} from '@angular/material';
import { ContactService } from '../../contact.service';
@Component({
  selector: 'app-gps-edit-master',
  templateUrl: './gps-edit-master.component.html',
  styleUrls: ['./gps-edit-master.component.scss']
})
export class GpsEditMasterComponent implements OnInit {
f_Name:any;
l_Name:any;
createdOn:any;
expOn:any;
iconType:any;
email:any;
phone:any;
os:any;
superAdmin:any;
dealerName:any;
  useridd: any;
  constructor(public dialogRef: MdDialogRef<GpsEditMasterComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService, public snackBar: MdSnackBar) {
      console.log(data);
      this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
      this.f_Name= data.userInfo?data.userInfo.first_name:"";
      this.l_Name =data.userInfo?data.userInfo.last_name:"";
      this.createdOn = data.userInfo.created_on?(new Date(data.userInfo.created_on).toISOString()):null;
      this.email = data.userInfo?data.userInfo.email:"";
      this.phone= data.userInfo?data.userInfo.phone:"";
      this.dealer = data.userInfo?data.userInfo.isDealer:false;
      this.superAdmin = data.userInfo.isSuperAdmin;
     
      if(data.userInfo.Dealer)
      this.dealerName =data.userInfo.Dealer.first_name;    
     }

  ngOnInit() {
    this.dealerDetail();
  }

  editDetails(){

    if((this.dealer == true)&&(this.superAdmin == true)){
      let message1="A user can only be Dealer or admin";
      let action:any;
      this.snackBar.open(message1, action, {
       duration: 5000,    
      });  
      return
    }

    console.log(this.dealerSelect)
    var userObj = {
      'first_name' : this.f_Name,
      'last_name'  : this.l_Name,
      'phone' :this.phone,
      'email' : this.email,
      'created_on' : new Date(this.createdOn).toISOString(),    
      'contactid' : this.data.userInfo._id, 
    }
    console.log("createdObj=>",userObj);
    if(this.dealerSelect.length!=0){
      userObj['Dealer'] = this.dealerSelect[0].dealer_id
    }

    if(this.dealer == true){
      userObj['isDealer'] = this.dealer ; 
      userObj['Dealer'] = this.data.userInfo._id;
    }
    if(this.superAdmin == true){
      userObj['isSuperAdmin'] = this.superAdmin ; 
    }
   


    this.contactService.editUsermaster(userObj)
      .subscribe(res=>{
        console.log(res);
        if(res){
          this.dialogRef.close("success");
        }
      },err=>{
          console.log(err.status);
      })
  }

cancelEdit(){
  this.dealers_info=[];
  this.dialogRef.close();

}
dealer:any;
onChange(dealer,$event){

}
dealers_info=[];

dealerDetail(){
  this.contactService.dealer_Info_dealer_list(this.useridd)
  .subscribe(res=>{
    console.log(res);
    this.dealers_info=res;
  })
  

}

dealerSelect:any=[];

filterDealerStates(dlr) {

  console.log(dlr);
  if (dlr) {

    const filterdealerValue:any = dlr;
    this.dealerSelect=[];
     this.dealerSelect = this.dealers_info.filter(function(p){
      var t = p.dealer_firstname.toLocaleLowerCase().indexOf(filterdealerValue.toLocaleLowerCase());
    //  this.option = this.options[i];
  
   
      return t > -1;    
    });
      console.log(this.dealerSelect);
      return this.dealerSelect;
  }

}
}
