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

@Component({
  selector: 'app-edit-dealer',
  templateUrl: './edit-dealer.component.html',
  styleUrls: ['./edit-dealer.component.scss']
})
export class EditDealerComponent implements OnInit {

  dealerInfo:any;
  dealerEditForm:any;
  firstName:any;
  lastName:any;
  email:any;
  PhoneNum:any;
  password:any;
  cnfrmPass:any;
  userId:any;
  address:any;
  date=new Date();
  expDate = new Date(this.date.getFullYear()+1,this.date.getMonth(), this.date.getDate()).toLocaleString();
  pageId: any;
  errorMsg: any;
  dealerStatus: any;
  superAdmin: any;

  constructor(public dialogRef: MdDialogRef<EditDealerComponent>,public thisDialogRef: MdDialogRef<EditDealerComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService) {
        this.dealerInfo=data;
        console.log(this.dealerInfo);   
        this.userId = this.dealerInfo.dealer_payload.user_id?this.dealerInfo.dealer_payload.user_id:'';
        this.firstName = this.dealerInfo.dealer_payload.first_name?this.dealerInfo.dealer_payload.first_name:'';
        this.lastName = this.dealerInfo.dealer_payload.last_name?this.dealerInfo.dealer_payload.last_name:'';
        this.email = this.dealerInfo.dealer_payload.email?this.dealerInfo.dealer_payload.email:'';
        this.PhoneNum = this.dealerInfo.dealer_payload.phone?this.dealerInfo.dealer_payload.phone:'';
        this.address =this.dealerInfo.dealer_payload.address?this.dealerInfo.dealer_payload.address:'';
        this.expDate = this.dealerInfo.dealer_payload.expire_date?this.dealerInfo.dealer_payload.expire_date:'';
        console.log(this.dealerInfo);
        // this.pageId = this.dealerInfo.id;
        // console.log("PAGEID=>",this.pageId);

       
     
     }

     useridd:any;

  ngOnInit() {
    this.pageId = this.dealerInfo.dealer_payload.ID;
    console.log("val=",this.pageId);
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
  }
  showError :boolean = false;

  editDealer(){
    var editDealerPayload = {
      first_name : this.firstName,
      last_name:this.lastName,
      address:this.address,
      user_id:this.userId,  
      contactid:this.dealerInfo.dealer_payload._id,
      expire_date:new Date(this.expDate),
     
    }
    // this.dealerInfo.dealer_payload.status
    if(this.PhoneNum !== this.dealerInfo.dealer_payload.phone){
      editDealerPayload['phone'] = this.PhoneNum
    }
    if(this.email!== this.dealerInfo.dealer_payload.email){
      editDealerPayload['email'] = this.email
    }
   
    this.contactService.editDealer(editDealerPayload)
      .subscribe(res=>{
      
        // console.log(res);
        if(res){
          // console.log(res);
          this.thisDialogRef.close("success");
      }  
      },err=>{
        var that = this;
        console.log(JSON.parse(err._body).message);
        var errorM = (JSON.parse(err._body).message).split(":");
        var mainErr =errorM[2];
        if(mainErr == " phone_1 dup key"){
          this.errorMsg = "Phone number already exists";
        }else if(mainErr == " email_1 dup key"){
          this.errorMsg = "Email id already exists";
        }else{
          this.errorMsg = "Internal server error";
        }

        this.showError  = true;
      
        var error =  setTimeout(function(){
          clearInterval(error);
          that.showError = false; 
        },5000)

      })

  }


  deleteResponse(action){
    if(action =='yes'){
      this.thisDialogRef.close("yes");
    }else{
      this.thisDialogRef.close("no");
    }
  
  }



}
