import { Component, OnInit, ViewChild, Input, } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MdSnackBar } from '@angular/material';

import {ContactService} from '../contact.service';
 

declare var google: any,swal: any;
@Component({
  selector: 'app-soon',
  templateUrl: './soon.component.html',
  styleUrls: ['./soon.component.css'],
  providers: [ContactService],
})

export class SoonComponent implements OnInit  {
  emailId: any;
  searchForm:FormGroup;
  deleteShow:boolean=false;
   submitted = false;
  userId: any;
  deviceForm:FormGroup
  data: any;
  deleted:boolean=false;
  // @ViewChild(BaseChartDirective) public chart: BaseChartDirective;
  constructor(private contactService:ContactService,private fb:FormBuilder,public snackBar: MdSnackBar){
    
    }
  ngOnInit(): void {
    this.searchForm=this.fb.group({
      imei:['',Validators.required]
    })
    this.deviceForm=this.fb.group({
      imei:[''],
      registrationNumber:[''],
      sim1:[''],
      sim2:[''],
      dealaer:[''],
      user:[''],
      deviceModel:[''],
      vehicleType:[''],
      expirationDate:[''],
      creationDate:[''],
      superAdmin:[''],
      lastRenewal:[''],
      status:[''],
      deleted:[''],
      deletedBy:[''],
      deletedAt:[''],
      deletedFrom:['']

// 352887076587769
    })
     this.emailId = JSON.parse(window.atob(window.localStorage.token.split(".")[1])).email;
    this.userId = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
  }
  
 get f() { return this.searchForm.controls; }

  submit(){
    console.log(this.searchForm.value);
    
      this.submitted = true;

        // stop here if form is invalid
        if (this.searchForm.invalid) {
            return;
        }
        this.contactService.searchByImei(this.searchForm.value).subscribe((res:any)=>{
          console.log(res);
          this.data=res[0];
          if(res.length!=0){
            this.deleteShow=true
            if(res[0].deletedDevice){
              this.deleteShow=false;
              this.deleted=true;
            }else{

              this.deleted=false;
            }
            this.deviceForm.patchValue({
              imei:this.data.Device_ID,
              registrationNumber:this.data.Device_Name,
              sim1:this.data.sim_number?this.data.sim_number:'',
              sim2:this.data.sim_number2?this.data.sim_number2:'',
              dealaer:this.data.Dealer?(this.data.Dealer.first_name?this.data.Dealer.first_name:'')+' '+(this.data.Dealer.last_name?this.data.Dealer.last_name:''):'',
              user:this.data.user?(this.data.user.first_name?this.data.user.first_name:'')+' '+(this.data.user.last_name?this.data.user.last_name:''):'',
              deviceModel:this.data.device_model?this.data.device_model.device_type:'',
              vehicleType:this.data.vehicleType?(this.data.vehicleType.brand?this.data.vehicleType.brand:'')+' '+(this.data.vehicleType.model?this.data.vehicleType.model:''):'',
              expirationDate:this.data.expiration_date?this.data.expiration_date:'',
              superAdmin:this.data.supAdmin?(this.data.supAdmin.first_name?this.data.supAdmin.first_name:'')+' '+(this.data.supAdmin.last_name?this.data.supAdmin.last_name:''):'',
              lastRenewal:this.data.renew_at?this.data.renew_at:'',
              creationDate:this.data.created_on?this.data.created_on:'',
              status:this.data.status,
              deleted:this.data.deletedDevice?'Yes':'No',
              deletedBy:this.data.device_deleted_by?this.data.device_deleted_by.first_name+" "+this.data.device_deleted_by.last_name:' ',
              deletedAt:this.data.device_delete_on,
              deletedFrom:this.data.login_type,
            })

          }else{
             this.deleteShow=false;
             this.deviceForm.reset();
             this.snackBar.open("No Data Found", '', {
                duration: 4000,
          });
          }
          
        })

  }

             deleteDevice(){
                      swal({                 
                             title: '<strong>Are you sure?</strong>',
                            icon: 'warning',
                            html: "You won't be able to revert this!",
                            showCloseButton: true,
                            focusConfirm: false,
                            // confirmButtonText: 'Reset today ODO',
                            confirmButtonText: 'Yes, delete it!',
                            // cancelButtonText: 'Reset total ODO',
                            // cancelButtonAriaLabel: 'Thumbs down'
                            cancelButtonText: 'No, cancel!',
                        }).then((result: { value: boolean; }) => {
                            if (result) {
                              var req={
                                device:this.data.Device_ID,
                                userId:this.userId
                          }
                              this.contactService.deldev(req).subscribe(res=>{
                                swal({
                                 title: 'Deleted!',
                                 html:'Your file has been deleted.',
                                   icon:'success'
                                })
                              })
                              this.deviceForm.reset();
                            }
                          }
                        )
  }

    deleteDevicePermenatly(){
                      swal({                 
                             title: '<strong>Are you sure?</strong>',
                            icon: 'warning',
                            html: "Device will be deleted permanantly",
                            showCloseButton: true,
                            focusConfirm: false,
                            // confirmButtonText: 'Reset today ODO',
                            confirmButtonText: 'Yes, delete it!',
                            // cancelButtonText: 'Reset total ODO',
                            // cancelButtonAriaLabel: 'Thumbs down'
                            cancelButtonText: 'No, cancel!',
                        }).then((result: { value: boolean; }) => {
                            if (result) {
                              var req={
                                device:this.data.Device_ID,
                                userId:this.userId,
                                permenant:true
                          }
                              this.contactService.deldev(req).subscribe(res=>{
                                swal({
                                 title: 'Deleted!',
                                 html:'Your file has been deleted.',
                                   icon:'success'
                                })
                              })
                              this.deviceForm.reset();
                            }
                          }
                        )
  }

 
  }
  
 
   
   

                       
  
                                 
                               
                               

