import { Component, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ContactService } from '../contact.service';

@Component({
  selector: 'app-report-schedule',
  templateUrl: './report-schedule.component.html',
  styleUrls: ['./report-schedule.component.scss']
})
export class ReportScheduleComponent implements OnInit {
  useridd: any;
  userData:any
  form: FormGroup;
   submitted = false;
  constructor(public contactService: ContactService,private fb: FormBuilder) {}


  ngOnInit() {
    this.form = this.fb.group({
      Geofencing: [],
      scheduled_excel_report: [],
       fuel: [],
       ignition: [],
        overspeed: [],
        rViolation: [],
         sos: [],
          stoppage: [],
          summary: [],
           trip: [],
           distance:[],
           daily:[],
           email1: ['',Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')],
           email2: ['',Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')],
           email3: ['',Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')],
           email4: ['',Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')],
           email5: ['',Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$')],
        
    })
     this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id; 
     this.getUserData() 
  }
  get f() { return this.form.controls; }
  patchValue(){
    this.form.patchValue({
       Geofencing: this.userData.report_config.Geofencing,
        scheduled_excel_report: this.userData.scheduled_excel_report,
       fuel: this.userData.report_config.fuel,
       ignition: this.userData.report_config.ignition,
        overspeed: this.userData.report_config.overspeed,
        rViolation: this.userData.report_config.rViolation,
         sos: this.userData.report_config.sos,
          stoppage: this.userData.report_config.stoppage,
          summary: this.userData.report_config.summary,
          daily: this.userData.report_config.Daily,
          distance: this.userData.report_config.Distance,
           trip: this.userData.report_config.trip,
           email1:  (this.userData.report_emailid[0]?this.userData.report_emailid[0]:null),
           email2:  (this.userData.report_emailid[1]?this.userData.report_emailid[1]:null),
           email3:  (this.userData.report_emailid[2]?this.userData.report_emailid[2]:null),
           email4:  (this.userData.report_emailid[3]?this.userData.report_emailid[3]:null),
           email5:  (this.userData.report_emailid[4]?this.userData.report_emailid[4]:null),
    })
  }

  getUserData(){
    this.contactService.getUserObj(this.useridd).subscribe((res:any)=>{
      this.userData=res
      console.log(this.userData);
       this.patchValue()
    })
  }
   submit(){
    this.submitted = true;

        // stop here if form is invalid
        if (this.form.invalid) {
            return;
        }
     var emailArray=[this.form.value.email1?this.form.value.email1:null,this.form.value.email1?this.form.value.email2:null,
      this.form.value.email1?this.form.value.email3:null,this.form.value.email1?this.form.value.email4:null,this.form.value.email1?this.form.value.email5:null]
     console.log(emailArray,"sdsdsds")
     
      for(var i=0;i<emailArray.length;i++){
        if(emailArray[i]==null){
          emailArray.splice(i,1);
        }
      }
   

     const newContact ={
      uid:this.useridd,
      reportSchedule:true,
      scheduled_excel_report:this.form.value.scheduled_excel_report?this.form.value.scheduled_excel_report:false,
      report_config:{
         fuel: this.form.value.fuel?this.form.value.fuel:false,
         ignition: this.form.value.ignition?this.form.value.ignition:false,
         overspeed: this.form.value.overspeed?this.form.value.overspeed:false,
         rViolation: this.form.value.rViolation?this.form.value.rViolation:false,
         sos: this.form.value.sos?this.form.value.sos:false,
         stoppage: this.form.value.stoppage?this.form.value.stoppage:false,
         summary: this.form.value.summary?this.form.value.summary:false,
         trip: this.form.value.trip?this.form.value.trip:false,
         Geofencing:this.form.value.trip?this.form.value.Geofencing:false,
         Distance:this.form.value.distance?this.form.value.distance:false,
         Daily:this.form.value.daily?this.form.value.daily:false,

      },
      report_emailid:emailArray
        }

        console.log(newContact);
        this.contactService.updateuid(newContact) .subscribe(contact => {
          console.log(contact);
          this.getUserData()

        })

     
   }

}
