import { Component, Inject, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MdDialogRef, MD_DIALOG_DATA } from '@angular/material';
import * as moment from 'moment';
import { BsDatepickerConfig } from 'ngx-bootstrap';
import { ContactService } from '../../contact.service';
declare var $: any, DataTable: any, swal: any;

@Component({
  selector: 'app-renew-vehicle',
  templateUrl: './renew-vehicle.component.html',
  styleUrls: ['./renew-vehicle.component.scss']
})
export class RenewVehicleComponent implements OnInit {
  renewForm:FormGroup;
  useridd;
  disabled=true;
  isFirst=true;
  validTillDate=new Date()
  bsConfig: Partial<BsDatepickerConfig>;
  constructor(public dialogRef: MdDialogRef<RenewVehicleComponent>,
    @Inject(MD_DIALOG_DATA) public data: any,private fb:FormBuilder,private contactService:ContactService) {
      console.log(data);
      
     }

  ngOnInit() {
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
    this.renewForm=this.fb.group({
      name:[this.data.Device_Name],
      expDate:[moment(this.data.expiration_date).format('lll')],
      sim:[this.data.sim_number],
      plan:[''],
      validTill:[new Date()]
    })

    this.renewForm.controls.validTill.disable()
  }

  submit(){
    console.log(this.renewForm.value);
    if(this.renewForm.value.plan=="custom"){
      this.validTillDate=this.renewForm.value.validTill
    }
    
    if(this.isDatesEqual(new Date(this.validTillDate),new Date())){
      swal({
  icon: 'warning',
  title: 'Oops...',
  text: 'Please choose plan or set valid till date',

})
    }else{
      var request={
         _id: this.data._id,
            devicename: this.renewForm.value.name,
            sim: this.renewForm.value.sim,
            deviceid: this.data.Device_ID,
            device_model: this.data.device_model._id,
            vehicleType:this.data.vehicleType._id,
            expdate:new Date(this.validTillDate),
            renew_by:this.useridd
      }
  
       swal({
               title: '<strong>Renew vehicle</strong>',
               icon: 'info',
               html: 'Please confirm to Renew Vehicle ?',
               showCloseButton: true,
               showCancelButton: true,
               focusConfirm: false,
               confirmButtonText: "Renew Vehicle",
               confirmButtonAriaLabel: 'Thumbs up, great!',
                             
               }).then((result: { value: boolean; }) => {
                 if (result) {
                  console.log(request);
                   this.contactService.editdevice(request)
                      .subscribe(device => {
                        this.dialogRef.close('succ');
                      })
                }
              })

    }




    

  }

  isDatesEqual(date1, date2) {
    console.log(date1,date2);
    
  return date1.getFullYear() === date2.getFullYear() &&
    date1.getMonth() === date2.getMonth() &&
    date1.getDate() === date2.getDate();
}

  changePlan(event){
    console.log(event);
    if(event=='custom'){
      this.disabled=false;
      console.log("in");
      this.validTillDate=new Date();
      // this.renewForm.controls.validTill.enable();
    this.renewForm.value.validTill=new Date();
      this.renewForm.controls.validTill.enable();
    }else{
      this.validTillDate=new Date();
      // this.renewForm.controls.validTill.enable();
    this.renewForm.value.validTill=new Date();
    this.renewForm.patchValue({
      validTill:new Date(moment(this.renewForm.value.validTill, "DD-MM-YYYY").add('days', Number(event)).format('YYYY/MM/DD HH:mm:ss'))
    })
    this.validTillDate=new Date(moment(this.validTillDate, "DD-MM-YYYY").add('days', Number(event)).format('YYYY/MM/DD HH:mm:ss'))
    // this.renewForm.value.validTill= new Date(moment(this.renewForm.value.validTill, "DD-MM-YYYY").add('days', Number(event)).format('YYYY/MM/DD HH:mm:ss'));
    console.log(this.renewForm.value.validTill,this.validTillDate)
    this.renewForm.controls.validTill.disable()
    }
    
  }

}
