import { Component, OnInit } from '@angular/core';
import { BsDatepickerConfig } from 'ngx-bootstrap';
import * as moment from 'moment-timezone';
import { ContactService } from '../contact.service';
declare var $: any;

@Component({
  selector: 'app-fuel-price',
  templateUrl: './fuel-price.component.html',
  styleUrls: ['./fuel-price.component.scss']
})
export class FuelPriceComponent implements OnInit {
  currState: any;
  from_date;
  to_date= new Date();
   bsConfig: Partial<BsDatepickerConfig>;
   Indianstate = [ "Andhra Pradesh",
                "Arunachal Pradesh",
                "Assam",
                "Bihar",
                "Chhattisgarh",
                "Goa",
                "Gujarat",
                "Haryana",
                "Himachal Pradesh",
                "Jammu and Kashmir",
                "Jharkhand",
                "Karnataka",
                "Kerala",
                "Madhya Pradesh",
                "Maharashtra",
                "Manipur",
                "Meghalaya",
                "Mizoram",
                "Nagaland",
                "Odisha",
                "Punjab",
                "Rajasthan",
                "Sikkim",
                "Tamil Nadu",
                "Telangana",
                "Tripura",
                "Uttarakhand",
                "Uttar Pradesh",
                "West Bengal",
                "Andaman and Nicobar Islands",
                "Chandigarh",
                "Dadra and Nagar Haveli",
                "Daman and Diu",
                "Delhi",
                "Lakshadweep",
                "Puducherry"]
  tabelObj:any
  Load: boolean;
  useridd
  constructor(private contactService:ContactService) { }

  ngOnInit() {
    this.useridd = JSON.parse(window.atob(window.localStorage.token.split('.')[1]))._id;
    var wd = moment().startOf('week')
    this.from_date = new Date(wd);
    this.bsConfig = Object.assign({ dateInputFormat: 'DD-MM-YYYY, h:mm:ss a' }, { containerClass: 'theme-dark-blue' });
    this.testTable()
  }

  submit(){
    // if(this.currState){
     this.tabelObj.ajax.reload()

    // }else{

    // }
  }

  testTable() {
  const that = this;
  console.log("inside function");
  $(document).ready(function() {
     that.tabelObj = $('#deviceTable').DataTable({
    
          "processing": true,
          "searching": true, 
          pagingType: 'full_numbers',
          pageLength: 25,
          serverSide: false,
          "scrollY":'65vh',  
          "scrollCollapse": true,     
          lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]],
          ajax: (dataTablesParameters,callback) => {
            console.log('temptemptemptemptemp',dataTablesParameters);
              var deviceID;
              that.Load = true;
             
            var from_date = that.from_date;
            var to_date = that.to_date;
            // var user = that.useridd;
            var temp = {
            from_date : from_date,
            to_date : to_date,
            // user : user
            }
            var suburl = "";
           
            if(that.id==1){
              if((temp.from_date != undefined)&&(temp.to_date != undefined)&&(that.currState != undefined)){
                suburl += "/googleAddress/getFuelPrice?from_date=" + new Date(temp.from_date).toISOString() +'&to_date=' + new Date(temp.to_date).toISOString() + '&state=' + that.currState;
               
                that.contactService.get(suburl).subscribe((resp:any) => {   
                  // console.log(ignReport.length);  
                  if(resp.length>0){
                    that.Load = false;
                    callback({data : resp });
                  }else{
                     callback({data : [] });
                     that.Load = false;
                  }
              }, err => {   
                that.Load= false;    
                console.log("error",err) ;     
              });
  
              }else{
                let ta = [];
                that.Load = false;
                callback({data : ta });
              }

            }else{
              if(temp.to_date != undefined){
                suburl += "/googleAddress/getFuelPriceForAllState?&date=" + new Date(temp.to_date).toISOString()
               
                that.contactService.get(suburl).subscribe((resp:any) => {   
                  // console.log(ignReport.length);  
                  if(resp.length>0){
                    that.Load = false;
                    callback({data : resp });
                  }else{
                     callback({data : [] });
                     that.Load = false;
                  }
              }, err => {   
                that.Load= false;    
                console.log("error",err) ;     
              });
              }
            }

            // googleAddress/getFuelPrice?state=Arunachal%20Pradesh
          
        },  
     
          "columns": [
              {
                  "data": "date",
                  "defaultContent": "",
                  "render": function(data, type, row) {
                      
                    return data?data : '';                  
                  }
              },
              {
                  "data": "state",
                  "defaultContent": "",
                  render: function(data, type, row) {
                    return data?data:'';        
                  }
              },
              {
                  "data": "petrol",
                  "defaultContent": "",
                  render: function(data, type, row) {
                    return data?data:'';  
                
                  }
              },
               {
                  "data": "diesel",
                  "defaultContent": "",
                  render: function(data, type, row) {
                   return data?data:'';   
                
                  }
              },
                           
          ],  
          
      });

  });

}
id=1
selectOptions(id){
  this.id=id
}
}
