import { Component, OnInit } from '@angular/core';
import {ContactService} from '../contact.service';
import { Params,Router, ActivatedRoute, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { MyaccountComponent } from '../myaccount/myaccount.component';
import {MdDialog, MdDialogRef, MD_DIALOG_DATA} from '@angular/material';
import { AddGroupComponent } from '../add-group/add-group.component';
import { EditGroupComponent } from '../edit-group/edit-group.component';
import { DeleteGroupComponent } from '../delete-group/delete-group.component';


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

 icon: string;
 dev: any;

 constructor(private contactService: ContactService,private router: Router,public dialog: MdDialog) { 
 

 }
 before:any;
 logoutbut:boolean;
 cust:boolean=false;
 logo:any;
 text:any;
 fs:any;ls:any;emailid:any;or:any;useridd:any;mb:any;custtype:any;dealer:any;
 showImage:any;
 groups=[];
 group_info:any;

 deviceId:any;
 device_update:any;
 superAdmin:Boolean = false;

 statusList = ['ASSIGNED','LOADING','TRANSIT','UNLOADING','UNASSIGNED'];
 statusArr:any=[0,0,0,0,0];
 total:any = 0;
 t:any=0;
 public doughnutChartType:string = 'doughnut';

 options = { 
 legend: {
 display: false
 },
 cutoutPercentage: 75
 };
 
 ngOnInit() {
 
 this.superAdmin = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).isSuperAdmin;

 this.logo=window.localStorage['logo'];
 this.text=window.localStorage['text']; 
 window.localStorage['DataUpdate'] = 'True'
 
 
 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.mb = JSON.parse(window.atob(window.localStorage.token.split('.')[1])).phn;
 
 this.custtype = JSON.parse(window.atob(window.localStorage.token.split('.')[1])).isDealer;
 //this.getdev();
 if(this.custtype == true){
 this.cust = true;
 } 
 let foods = [ ];
 
 if(this.mb.charAt(0)=="n"){
 this.mb = ' '
 }
 if( window.localStorage['Custumer'] == 'ON'){
 this.logoutbut = false
 this.dealer = true
 }
 else{
 this.logoutbut = true
 }
 this.getGroupWithStatus();
 }

 mydealer(){
//  console.log('Dealer')
 }
 delDev:any
 deletegroup(devId){
//  console.log(devId);
 

 let dialogRef = this.dialog.open(DeleteGroupComponent, {
 height:'300px',
 width:'430px',
 
 data: {devId}
 });
 
 dialogRef.afterClosed().subscribe(result => {
 
//  console.log(result);
 if(result=="success"){
 this.getGroupWithStatus();
 }else if(result=="Cancel"){
//  console.log("close dialog only");
 }
 });


 }

 myaccount(){

 
 this.cond = false
 this.router.navigateByUrl("accountSettings");
 
 
 
 // let dialogRef = this.dialog.open(MyaccountComponent, {
 // width: '903px',
 // data: {}
 // });
 
 // dialogRef.afterClosed().subscribe(result => {
 
 // if(result == "succ"){
 // console.log("Updated")
 // }
 
 // });
 
 
 }
 addcus(){
 this.router.navigateByUrl("add");
 }
 geo(){
 this.router.navigateByUrl("geofencing");
 
 }

openNav() {
 /* document.getElementById("myNav").style.width = "100%";
*/
 this.router.navigateByUrl("location");

}


vehicleRoute(){
 this.cond = false
 this.router.navigateByUrl("vehicleRoute");

}

route_map(){
 this.router.navigateByUrl("routeMapping");

}

notification(){
 this.cond = false
 this.router.navigateByUrl("notifications");
}

dealerInfo(){
 this.cond = false
 this.router.navigateByUrl("dealerInfo");
}

logout(){
 window.localStorage.clear();
 this.router.navigateByUrl("login");
}
devi(){
 this.router.navigateByUrl("dashboard");

}
soon(){
 this.router.navigateByUrl("const?_i="+window.localStorage.token);
 
}
new(){
 this.router.navigateByUrl("new");
 
}

group(){
 this.cond = false
 this.router.navigateByUrl("group_view");
}


cond:Boolean = false;
point : any = 0;
 aa(){
//  console.log(this.point);
 if(this.point == 0){
 this.cond = true;
 this.point ++;
 }
 else if(this.point % 2 == 0){
 this.cond = true;
 this.point ++;
 }
 else{
 this.cond = false
 this.point ++;
 }

 
}
report(){
 this.cond = false
 this.router.navigateByUrl("device-report");

}
routeViolation(){
 this.cond = false
 this.router.navigateByUrl("device-report/routeViolation");

}
report_speed(){
//  console.log("report speed call")
 this.cond = false
 this.router.navigateByUrl("device-report/device-speed-report");
}


alert_report(){
 this.cond = false
 this.router.navigateByUrl("device-report/alert-report");

}

summaryReport(){

 this.cond = false
 this.router.navigateByUrl("device-report/summary-report");
}

overspeed(){
 this.cond = false
 this.router.navigateByUrl("device-report/overspeed");

}
stoppage_report(){
 this.cond = false
 this.router.navigateByUrl("device-report/stoppage_report");

}
ignition_report(){
 this.cond = false
 this.router.navigateByUrl("device-report/ignition_report");


}

geofancingReport(){
 this.cond = false
 this.router.navigateByUrl("device-report/geofancing");
}


driverPerformance(){
 this.cond = false
 this.router.navigateByUrl("device-report/Driver-performance");
}



distance_report(){
this.cond = false
this.router.navigateByUrl("device-report/distance_report");
}

trip_report(){
 this.cond = false
 this.router.navigateByUrl("device-report/trip_report");

}

getGroupWithStatus(){
 this.groups = [];
 this.contactService.getGroupWithStatus(this.useridd).subscribe( 
 res => {
//  console.log(res);
 
 this.group_info = res;
 for(var i = 0;i<this.group_info.length;i++) {
 let group_logs = this.group_info[i];
 let _id = group_logs._id.Groupid;
 let group_name = group_logs._id.GroupName.toUpperCase();
 let statusListArr = group_logs.TripStatus ? group_logs.TripStatus : [];
 this.total = 0;
 this.t=0;
 this.statusArr = [0,0,0,0,0];
 for(var j=0;j<statusListArr.length;j++){
 var index = this.statusList.indexOf( statusListArr[j].Status);
 if(index>-1){
 this.statusArr[index] = (statusListArr[j].Count);
 this.t += statusListArr[j].Count;
 }
 this.total += statusListArr[j].Count;
 }

if(this.total==0){
    this.total = "No Device";
    // console.log("totalDevice=>",this.total)
}

 this.groups.push({
 "_id":_id,
 "name":group_name,
 "statusCount" : this.statusArr,
 "totalCount" : this.total,
 "statusList" : this.statusList,
 "t":this.t,
 "isShowGroupOptions":false
 })
//  console.log("group");
//  console.log(this.groups);
 }
//  console.log(this.groups);
 },
 err=> {
//  console.log("Check your Internet Connection");
 });
}

addGroupComponent(){
 let dialogRef = this.dialog.open(AddGroupComponent, {
 height:'300px',
 width:'430px',
 
 data: {}
 });

 dialogRef.afterClosed().subscribe(result => {
 
//  console.log(result);
 if(result=="success"){
 this.getGroupWithStatus();

 }else if(result=="Cancel"){
//  console.log("close dialog only");
 }
 });
}



editgroupComponent(group){



 
 
 
 // var _id = group._id.Groupid;
 // var name = group._id.GroupName;
 
 
 let dialogRef_1 = this.dialog.open(EditGroupComponent, {
 height:'300px',
 width:'430px',
 data: group
 });


 dialogRef_1.afterClosed().subscribe(result => {
 
//  console.log(result);
 if(result=="success"){
 this.getGroupWithStatus();
 }else if(result=="Cancel"){
//  console.log("close dialog only");
 }
 });
}

showVehiclesByGroup = function(group){

 group.dataSource = "GROUPS";
 this.contactService.setService(group);
 this.router.navigateByUrl("const?_i="+window.localStorage.token);
}

Ddetail(){
 this.router.navigateByUrl("driverdetail");
}
DModel(){
 this.router.navigateByUrl("deviceModel");
}
VType(){
 this.router.navigateByUrl("VehicleType");
}

showGroupOptions(group){

 group.isShowGroupOptions = true;
}

hideGroupOptions(group){

 group.isShowGroupOptions = false;
}

editGroupById(groupId){

 this.contactService.getGroupById(groupId).subscribe( 
 res => {
//  console.log(res);
 this.editgroupComponent(res[0]);
 },
 err=> {
//  console.log("Check your Internet Connection");
 });
}



}