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


@Component({
  selector: 'app-edit-group',
  templateUrl: './edit-group.component.html',
  styleUrls: ['./edit-group.component.scss']
})
export class EditGroupComponent implements OnInit {
  icon: string;
  devUpdateId:any;
  useridd:any;
  be:any;
  errortext:any;
  name:any;
  status:any; 
  option:any;


  constructor(public thisDialogRef: MdDialogRef<EditGroupComponent>,private contactService:ContactService, @Inject(MD_DIALOG_DATA) public data: any) { 
    this.devUpdateId=data._id;
    this.name=data.name;
    this.option=data.status?"Active":"InActive";
    this.icon = data.logoPath;
    // console.log(this.devUpdateId);

  }

  foodds = [
   'Active','InActive'
  ];


  ngOnInit() {
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
   
   
  }


  car_status(){
    this.icon="car"
  }

  truck_status(){
    this.icon="truck"
  }
  bike_status(){
    this.icon="bike"
  }

  showError=false;
  group_payload:any;
  onCloseConfirm(name,option){
  
  
    // console.log("done")
    let a = name;
   
    if(option!=undefined){
      this.be =option;
    }else{
      this.be=option;
    }
   
 
    if((a==undefined)&&(this.be==undefined)){

     this.showError=true;
     this.errortext="Please provide details ";
    }
    if(a==undefined){
      this.showError=true;
     this.errortext="Please type group name";

    }else if(this.be==undefined){
      this.showError=true;
      this.errortext="Please select status";

    }else{

      
      // console.log("now call post service here and on success response close the dialog box")

      this.group_payload ={ 
      "name" : a,
      "status" : this.be=="Active"?true:false,
      "logopath" :this.icon,
      "_id": this.devUpdateId
      }

       this.contactService.editGroup(this.group_payload).subscribe(   
      res => {
        if(res){
            //console.log(res);
            //console.log('Group edited successfully');
            this.thisDialogRef.close("success");
        }  
        //console.log(res);    
      },
      err=> {
        //console.log("Check your Internet Connection");
      });
    } 
  }

  onCloseCancel(){
    //console.log("cancel button clicked");
    this.thisDialogRef.close('Cancel');
  }


}
