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

@Component({
  selector: 'app-report-setting',
  templateUrl: './report-setting.component.html',
  styleUrls: ['./report-setting.component.scss']
})
export class ReportSettingComponent implements OnInit {
  reportCheck :any;
  addressCheck :any;
  userSettingForm:FormGroup;
  selectedTab=1;
  dashboard_column = [
    {name:"GPS",dash_column:true,value:"gps_column"},
    {name:"AC",dash_column:true,value:"ac_column"},
    {name:"POWER",dash_column:true,value:"power_column"},
    {name :"GSM",dash_column:true,value:"gsm_column"},
    {name :"IGNITION",dash_column:true,value:"ignition_column"},
    {name:"External Volatage",dash_column:true,value:"ext_volt"},
    {name:"Door",dash_column:false,value:"door_column"},
    {name :"Temperature",dash_column:false,value:"temp_column"},
    {name :"Charging status",dash_column:false,value:"charging_column"},
  
  ];
  reportArray = [
    {name:"Daily Report",Rstatus:true,Astatus:false,value:"daily_report"},
    {name:"Daywise Report",Rstatus:true,Astatus:false,value:"daywise_report"},
    {name:"Speed Variation",Rstatus:true,Astatus:false,value:"speed_variation"},
    {name :"Fuel Report",Rstatus : true,Astatus:false,value:"fuel_report"},
    {name :"Idle Report",Rstatus : true,Astatus:false,value:"idle_report"},
    {name:"Fuel Consumption Report",Rstatus:true,Astatus:false,value:"fuel_consumption_report"},
    {name:"Trip Report",Rstatus:true,Astatus:false,value:"trip_report"},
    {name : "Travel Path Report",Rstatus : true,Astatus:false,value:"travel_path_report"},
    {name : "Summary Report",Rstatus : true,Astatus:false,value:"summary_report"},
    {name : "Geofence Report",Rstatus : true,Astatus:false,value:"geofence_report"},
    {name : "Overspeed Report",Rstatus : true,Astatus:false,value:"overspeed_report"},
    {name : "Route Violation Report",Rstatus : true,Astatus:false,value:"route_violation_report"},
    {name : "Stoppage Report",Rstatus : true,Astatus:true,value:"stoppage_report"},
    {name : "Ignition Report",Rstatus : true,Astatus:true,value:"ignition_report"},
    {name : "Distance Report",Rstatus : true,Astatus:false,value:"distance_report"},
    {name : "POI Report",Rstatus : true,Astatus:false,value:"poi_report"},
    {name : "SOS Report",Rstatus : true,Astatus:false,value:"sos_report"},
    {name : "AC Report",Rstatus : true,Astatus:false,value:"ac_report"},
    {name : "Driver Performance Report",Rstatus : true,Astatus:false,value:"driver_performance_report"},
    {name : "User Trip Report",Rstatus : true,Astatus:true,value:"user_trip_report"},
    {name : "Alert Report",Rstatus : true,Astatus:false,value:"alert_report"},
    {name : "Loading Unloading Trip",Rstatus : true,Astatus:false,value:"loading_unloading_trip"},
    {name : "Working Hours Reports",Rstatus : true,Astatus:false,value:"working_hours_report"},
    {name : "Maintenance Reports",Rstatus : true,Astatus:false,value:"maintanance_report"},
    {name:"Daily Logs",Rstatus:true,Astatus:false,value:"daily_logs"},
    {name : "Notification Center",Rstatus : true,Astatus:false,value:"notification_master"},
  ];
  userIdd: any;
  Address_Status_name: any = [];

  constructor(public dialogRef: MdDialogRef<ReportSettingComponent>,private fb:FormBuilder,
    @Inject(MD_DIALOG_DATA) public data: any,private contactService: ContactService,) { 
      console.log(data);
      console.log('data._id',data);
      this.userIdd = data._id;

    }
// ac,door,immoblizer,share,tow,parking
  ngOnInit() {
    this.userSettingForm=this.fb.group({
      ac:[],
      door:[],
      immoblizer:[],
      share:[],
      tow:[],
      parking:[],
      fuel:[],
      temp:[],
      satelite:[],
      zoomLevel:[""],
      relay_timer:[],
      errorCode:[],
      errorMessage:[],
      sateliteValue:['']
    })
    this.getCostumerDetail();
    // this.getUserSettings()
  }

  savereportPref(){ 
   var report_preference_payload = [];
  console.log(this.reportArray);
  
   report_preference_payload =  JSON.parse(JSON.stringify(this.reportArray));
    var tempsemp ={};

    console.log(report_preference_payload);
    for(var index in report_preference_payload){
      var k = report_preference_payload[index].value;

      if(report_preference_payload[index].Rstatus == false){
        
        if(report_preference_payload[index].value!="trip_report" && report_preference_payload[index].value!="ignition_report" && report_preference_payload[index].value!="stoppage_report"){
          report_preference_payload[index].Astatus = false;

        }
      }
      tempsemp[k] = report_preference_payload[index];
      delete tempsemp[k].name;
      delete tempsemp[k].value;
      }
    console.log(tempsemp);
    var  reportPref  ={
      reportsArr : tempsemp,
      id :this.userIdd
    }
    this.contactService.setReportPrefrence(reportPref).subscribe(res=>{
            console.log(res);
            this.dialogRef.close('updated');
    },err=>{
        console.log("error",err);
    })
   }
  cancel(){
    this.dialogRef.close('close');
  }
  Status_name = [];
  selectedStatus(ind,ev,flag){
    if(flag == 'rVisibility'){
      this.reportArray[ind].Rstatus = ev.checked;
     
    }
    if(flag == 'aVisibility'){
      this.reportArray[ind].Astatus = ev.checked;
    
    }

    console.log('this.reportArray',this.reportArray);


  }

  getCostumerDetail(){
    let that = this;
    this.contactService.getcustToken(this.userIdd).subscribe(res=>{
      console.log("resresresresresresresresres",res);
      var tempReport = res.cust.report_preference;
      if(res.cust.user_settings){
        this.userSettingForm.patchValue(res.cust.user_settings);
      }
      
      for(let dt in tempReport){
        let arr = that.reportArray.filter(function(d){ 
          return d.value == dt;
        })
        if(dt=="trip_report" || dt=="ignition_report" || dt=="stoppage_report"){
            arr[0].Astatus = true;
          arr[0].Rstatus = tempReport[dt].Rstatus;
        }else{
          arr[0].Astatus = tempReport[dt].Astatus;
          arr[0].Rstatus = tempReport[dt].Rstatus;
        }
        
      }

       var d_column = res.cust.dashboard_column;
      for(let dt in d_column){ 
        let arr1 = that.dashboard_column.filter(function(d){ 
          return d.value == dt;
        })

        // arr[0].dash_column = d_column[dt].dash_column;
        // this.dashboard_column[dt].dash_column = d_column[dt] ; 
        arr1[0].dash_column = d_column[dt] ; 
        
        console.log('d_column[dt]',d_column[dt])
        // arr[0].Astatus = tempReport[dt].Astatus;
      }
      console.log("that.reportArray=>",that.reportArray);      
    })
  }
 

saveSettings(value){
  console.log(value);
  if(value==0){
    this.savereportPref()
  }else if(value==1){
    var reportPref={
       userSetting : this.userSettingForm.value,
        id :this.userIdd
    }
    this.contactService.setReportPrefrence(reportPref).subscribe(res=>{
              console.log(res);
              this.dialogRef.close('updated');
      },err=>{
          console.log("error",err);
      })

  }else{
      this.savereportPref1()
  }
}


  savereportPref1(){
 
    // var dashboard_content_payload  =[]
    var tempsemp = {}; 
    // dashboard_content_payload =  JSON.parse(JSON.stringify(this.dashboard_column));
    var tt =[];
    for(var index in this.dashboard_column){
    
      var k = this.dashboard_column[index].value;
      let pl = { };
      tempsemp[k] = this.dashboard_column[index].dash_column;
    
      delete tempsemp[k].name;
      delete tempsemp[k].value;
      }
      console.log(tempsemp);

      var payload = {
        dashboard_column : tempsemp, 
        user : this.userIdd
      }
      // console.log(payload);

      this.contactService.setDashboardContent(payload).subscribe(res=>{
     
        this.dialogRef.close('updated');

      },err=>{
        console.log(err);
      })
      

  }

  

  selectedStatus1(ind,ev,flag){ 
      this.dashboard_column[ind].dash_column = ev.checked;
  }

}
