import { Component, OnInit } from '@angular/core';
import { FormArray, FormBuilder, FormGroup } from '@angular/forms';
import { BsDatepickerConfig } from 'ngx-bootstrap';
import { ContactService } from '../contact.service';
declare var XLSX:any;
@Component({
  selector: 'app-add-fuel-price',
  templateUrl: './add-fuel-price.component.html',
  styleUrls: ['./add-fuel-price.component.scss']
})
export class AddFuelPriceComponent implements OnInit {
  petrolPrice
  dieselPrice
  faileduploadDevices;
  stateArray
Indianstate = [
  {state:"Andhra Pradesh",petrol:"",diesel:'',date:''},
  {state:"Arunachal Pradesh",petrol:"",diesel:'',date:''},
  {state:"Assam",petrol:"",diesel:'',date:''},
  {state:"Bihar",petrol:"",diesel:'',date:''},
  {state:"Chhattisgarh",petrol:"",diesel:'',date:''},
  {state:"Goa",petrol:"",diesel:'',date:''},
  {state:"Gujarat",petrol:"",diesel:'',date:''},
  {state:"Haryana",petrol:"",diesel:'',date:''},
  {state:"Himachal Pradesh",petrol:"",diesel:'',date:''},
  {state:"Jammu and Kashmir",petrol:"",diesel:'',date:''},
   {state:"Jharkhand",petrol:"",diesel:'',date:''},
    {state:"Karnataka",petrol:"",diesel:'',date:''},
  {state:"Kerala",petrol:"",diesel:'',date:''},
  {state:"Madhya Pradesh",petrol:"",diesel:'',date:''},
  {state:"Maharashtra",petrol:"",diesel:'',date:''},
  {state:"Manipur",petrol:"",diesel:'',date:''},
  {state:"Meghalaya",petrol:"",diesel:'',date:''},
  {state:"Mizoram",petrol:"",diesel:'',date:''},
  {state:"Nagaland",petrol:"",diesel:'',date:''},
  {state:"Odisha",petrol:"",diesel:'',date:''},
  {state:"Punjab",petrol:"",diesel:'',date:''},
  {state:"Rajasthan",petrol:"",diesel:'',date:''},
  {state:"Sikkim",petrol:"",diesel:'',date:''},
  {state:"Tamil Nadu",petrol:"",diesel:'',date:''},
  {state:"Telangana",petrol:"",diesel:'',date:''},
   {state:"Tripura",petrol:"",diesel:'',date:''},
    {state:"Uttarakhand",petrol:"",diesel:'',date:''},
    {state:"Uttar Pradesh",petrol:"",diesel:'',date:''},
  {state:"West Bengal",petrol:"",diesel:'',date:''},
  {state:"Andaman and Nicobar Islands",petrol:"",diesel:'',date:''},
  {state:"Chandigarh",petrol:"",diesel:'',date:''},
  {state:"Dadra and Nagar Haveli",petrol:"",diesel:'',date:''},
  {state:"Daman and Diu",petrol:"",diesel:'',date:''},
  {state:"Lakshadweep",petrol:"",diesel:'',date:''},
  {state:"Delhi",petrol:"",diesel:'',date:''},
   {state:"Puducherry",petrol:"",diesel:'',date:''},
]
bsConfig: Partial<BsDatepickerConfig>;
  date=new Date();
  uploadJSON;
  showErrorupload_1
  failedFiles;
  useridd
  constructor( private contactService:ContactService) { }

  ngOnInit() {
    this.stateArray=this.Indianstate
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
       this.bsConfig = Object.assign({ dateInputFormat: 'DD-MM-YYYY' }, { containerClass: 'theme-dark-blue' });
        let that = this;
     $("#excelFile").change(function(e){
      let _e:any = e;
      that.failedFiles=[]
      console.log("Inside function");
        var reader = new FileReader();
        console.log(reader);
        reader.onload = function(e) {
         
          let _E:any = e;
          var data = _E.target.result;
       
        
        let workbook = XLSX.read(data, {type: 'binary' });
      
         let first_sheet_name = workbook.SheetNames[0];
      
       let worksheet = workbook.Sheets[first_sheet_name];
       let d:any = XLSX.utils.sheet_to_json(worksheet,{raw:true});
         
       that.uploadJSON=d.map(function(d){  

         if((isFinite(Number(d.lat)) && Math.abs(Number(d.lat)) <= 90)&&(isFinite(d.long) && Math.abs(d.long) <= 180)){
            console.log(d.lat);
            return d;
          }else{
            if(!(that.failedFiles.includes(d))){
              console.log(d);
              d.diesel=d.diesel.substring(0, (d.diesel.length - 4));
              d.petrol=d.petrol.substring(0, (d.petrol.length - 4))
                that.failedFiles.push(d)
              }
          }
        
        });
        console.log("file=>",that.uploadJSON);
        that.stateArray=that.failedFiles
        console.log("Failed",that.stateArray);
        
        that.faileduploadDevices=that.uploadJSON;
        that.showErrorupload_1=true
        };
           reader.readAsBinaryString(_e.target.files[0]); 
      
      });
  }
petrolChange(ev,i){
  // console.log(ev,i);
  
}

dieselChange(ev,i){
  // console.log(ev,i);
  
}
  
submit(){
  console.log(this.stateArray);
  for(var i=0;i<this.stateArray.length;i++){
    this.stateArray[i].date=this.date.toISOString().substr(0, 10)
  }
  var request={
    data:this.stateArray,
    date:this.date
  }
  console.log(request);
  
  this.contactService.post('/googleAddress/addFuelPrice',request).subscribe((res:any)=>{
    console.log(res);
    this.stateArray=this.Indianstate
  })
  
  
}
  uploadExcel(){
     for(var i=0;i<this.stateArray.length;i++){
    this.stateArray[i].date=this.date.toISOString().substr(0, 10)
  }
    var request={
    data:this.stateArray,
    date:this.date
  }
  console.log(request);
  
  this.contactService.post('/googleAddress/addFuelPrice',request).subscribe((res:any)=>{
    console.log(res);
     this.stateArray=this.Indianstate
  })
  }
}
