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

@Component({
  selector: 'app-dashboard-content',
  templateUrl: './dashboard-content.component.html',
  styleUrls: ['./dashboard-content.component.scss']
})
export class DashboardContentComponent implements OnInit {
  userIdd: any;

  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"},
  
  ];

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

  ngOnInit() {
  }


  savereportPref(){
 
    // 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);
      })
      

  }

  cancel(){
    this.dialogRef.close('close');
  }

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

  getCostumerDetail(){
    let that = this;
   
    this.contactService.getcustToken(this.userIdd).subscribe(res=>{
      var d_column = res.cust.dashboard_column;
      for(let dt in d_column){ 
        let arr = 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] ; 
        arr[0].dash_column = d_column[dt] ; 
        
        console.log('d_column[dt]',d_column[dt])
        // arr[0].Astatus = tempReport[dt].Astatus;
      }
    

      console.log('this.dashboard_column=>',this.dashboard_column);
   
    })
  }
}
