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

@Component({
  selector: 'app-add-group',
  templateUrl: './add-group.component.html',
  styleUrls: ['./add-group.component.scss']
})
export class AddGroupComponent implements OnInit {
  be: any;
  errortext: any;
  name:any;
  status:any;
  useridd:any;
  icon="car"
  foodds = [
  'Active',"InActive"
  ];
  option:any;

  constructor(public thisDialogRef: MdDialogRef<AddGroupComponent>,private contactService:ContactService) { }

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

  showError=false;
  group_payload:any;
  car_icon:any;
  bike_icon:any;
  truck_icon:any;

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

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

  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 ={
      "uid" :this.useridd,
      "name" : a,
      "status" : this.be,
      "logopath" : this.icon
      }
  
       this.contactService.addGroup(this.group_payload).subscribe(   
      res => {
        if(res){
            // console.log(res);
            // console.log("Group added 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');
  }
 

}
