import { Component, OnInit, ViewChild, ElementRef, NgModule } from '@angular/core';
import { HyperService } from '../../../core/services/http.service';
import { Router, RouterModule, Routes, ActivatedRoute } from '@angular/router';
import { FormGroup, FormBuilder, Validators, FormControl } from '@angular/forms';
import { DatePipe } from '@angular/common';
import * as moment from 'moment';
import { LocalStorage } from '../../../core/services/local_storage.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { DatepickerOptions } from 'ng2-datepicker';

import * as enLocale from 'date-fns/locale/en';
// declare var $: any;
import { Subscription } from "rxjs";
import { Observable } from "rxjs/Observable";
import { Location } from '@angular/common';
import { ToasterService } from '@service/toaster.service';
import $ from 'jquery';
import 'rxjs/add/observable/combineLatest';
import { THIS_EXPR } from '@angular/compiler/src/output/output_ast';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { IDayCalendarConfig, DatePickerComponent } from "ng2-date-picker";
declare var $: $;

@Component({
  selector: 'app-classroom-reports',
  templateUrl: './classroom-reports.component.html',
  styleUrls: ['./classroom-reports.component.scss']
})
export class ClassroomReportsComponent implements OnInit {

  constructor(private server: HyperService, private router: Router, private formBuilder: FormBuilder, private _location: Location) {
    if (this.type == 'mobile') {
      LocalStorage.setValue('token', this.user_id)
      this.getuserdetails() //Get Authentication token 

  } else{
    var date12 = new Date();
    var currentyear = date12.getFullYear();
     //==From Date==
  
    var firstDay = new Date(date12.getFullYear(), date12.getMonth(), 1);
    var date31 = new Date(firstDay);
    this.selectedfromdate = moment(date31);
    //==To Date ===
    var date29 = date12.setDate(date12.getDate() + 1);
    var date30 = new Date(Date.now());
    this.selectedtodate = moment(date30);
  //===
    
    this.user_role=LocalStorage.getValue('user_data').role ;
    if(this.user_role == '3'){
      this.getzmlist();
      this.selectedzm='';
    }else if((this.user_role == '4' )||(this.user_role == '5' )){
      this.getrmlist('1');
      this.selectedrm='';
      this.reloadcurrentpage();
    }else if(this.user_role == '6'){
      this.getamlist('1');
      this.selectedam='';
      this.reloadcurrentpage();
    }else if(this.user_role == '7'){
      this.getdealerlist('1');
      this.selecteddealer='';
      this.reloadcurrentpage();
    }else if(this.user_role == '14'){
      this.reloadcurrentpage();
    }
  }
  
  }
  user_id: any;
  user_role: any;
  type: any;
  userData: any = {};
  loader: boolean = false;
  downloadexcel:boolean = false;
  options_start: DatepickerOptions = {
    minYear: 2000,
    maxYear: 2030,
    displayFormat: 'DD-MMM-YYYY',
    barTitleFormat: 'MMMM YYYY',
    dayNamesFormat: 'dd',
    firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
    locale: enLocale,
    // minDate: new Date("Sun Dec 31 1899 17:58:53 GMT+0530 (India Standard Time)"), // Minimal selectable date
    //  minDate: new Date("Sun Dec 31 1899 17:58:53 GMT+0530 (India Standard Time)"), // Minimal selectable date

    maxDate: new Date(Date.now()),  // Maximal selectable date
    barTitleIfEmpty: 'Click to select a date',

  };
  options_end: DatepickerOptions = {
    minYear: 2000,
    maxYear: 2050,
    displayFormat: 'DD-MMM-YYYY',
    barTitleFormat: 'MMMM YYYY',
    dayNamesFormat: 'dd',
    firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
    locale: enLocale,
    // minDate: new Date("Sun Dec 31 1899 17:58:53 GMT+0530 (India Standard Time)"), // Minimal selectable date
    //  minDate: new Date("Sun Dec 31 1899 17:58:53 GMT+0530 (India Standard Time)"), // Minimal selectable date
    maxDate: new Date(Date.now()),  // Maximal selectable date
    barTitleIfEmpty: 'Click to select a date',

  };


  zmlist:any=[];
  rmlist:any=[];
  amlist:any=[];
  dealerlist:any=[];

  selectedzm:any='';
  selectedrm:any='';
  selectedam:any='';
  selecteddealer:any='';

  selectedfromdate:any='';
  selectedtodate:any='';

  headerlist:any=[];
  bodylist:any=[];
  footerlist:any=[];

  ngOnInit(){

  var date12 = new Date();
  var currentyear = date12.getFullYear();

  this.options_start={
    minYear: 2000,
    maxYear: currentyear,
    displayFormat: 'DD-MMM-YYYY',
    barTitleFormat: 'MMMM YYYY',
    dayNamesFormat: 'dd',
    firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
    locale: enLocale,
    minDate: new Date("Sat Jan 1 2000 17:58:53 GMT+0530 (India Standard Time)"),
    maxDate: new Date(Date.now()),  // Maximal selectable date
    barTitleIfEmpty: 'Click to select a date',
  }  

  this.options_end={
    minYear: 2000,
    maxYear: currentyear,
    displayFormat: 'DD-MMM-YYYY',
    barTitleFormat: 'MMMM YYYY',
    dayNamesFormat: 'dd',
    firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
    locale: enLocale,
    minDate: new Date("Sat Jan 1 2000 17:58:53 GMT+0530 (India Standard Time)"),
    maxDate: new Date(Date.now()),  // Maximal selectable date
    barTitleIfEmpty: 'Click to select a date',
  }
    
  //==From Date==
  
  var firstDay = new Date(date12.getFullYear(), date12.getMonth(), 1);
  var date31 = new Date(firstDay);
  this.selectedfromdate = moment(date31);
  //==To Date ===
  var date29 = date12.setDate(date12.getDate() + 1);
  var date30 = new Date(Date.now());
  this.selectedtodate = moment(date30);
  //===
   // this.getzmlist();
  }

  //== get ZM list
  getzmlist() {
   // this.loader = true;
    this.rmlist=[];
    this.amlist=[];
    this.dealerlist=[];
    this.selectedam='';
    this.selecteddealer='';
    this.selectedrm='';
    this.server.post("managerenquiry/getzmlist",{}).then((data) => {
      if (data.status == 200 && data.result.response != 'failed') {
        this.zmlist = data.result.data;
        this.selectedzm='';
        this.reloadcurrentpage();
      //  this.loader = false;
      } else if (data.result.status == 406) {
       
      }
      else if (data.result.status == 401 && data.result.response == 'failed') {

      }
    });
  }
  //==get AM list
  getrmlist(args) {
    this.amlist=[];
    this.selectedam='';
    this.dealerlist=[];
    this.selecteddealer='';
    this.selectedrm='';
    this.rmlist=[];

    LocalStorage.setValue('selectedzmvalue',this.selectedzm)
    LocalStorage.setValue('selectedrmvalue', this.selectedrm)
    LocalStorage.setValue('selectedamvalue', this.selectedam)
    LocalStorage.setValue('selecteddealervalue', this.selecteddealer)
    
    let tempvar='';
    if((args !='1')&&(args !='')){
      tempvar = args.target.options[args.target.selectedIndex].text;
      LocalStorage.setValue("setzmselectedname",tempvar);
    }else{
      tempvar = LocalStorage.getValue("setzmselectedname");
    }
    let req = {
      zmemail: this.selectedzm
    }
    this.server.post("managerenquiry/getrmlist", req).then((data) => {
      if (data.status == 200 && data.result.response != 'failed') {
        this.rmlist = data.result.data;
       // this.loader = false;
      } else if (data.result.status == 406) {
      }
      else if (data.result.status == 401 && data.result.response == 'failed') {

      }
      if((args !='1')&&(args !='')){
      this.reloadcurrentpage()
      }
    });

  }
  //==
  getamlist(args) {


    this.amlist=[];
    this.selectedam='';
    this.dealerlist=[];
    this.selecteddealer='';
    LocalStorage.setValue('selectedrmvalue',this.selectedrm)
   

    
    if(this.selectedrm !=''){
    let tempvar ='';
    if((args !='1')&&(args !='')){
      tempvar = args.target.options[args.target.selectedIndex].text;
      LocalStorage.setValue("setrmselectedname",tempvar);
    }else{
      tempvar = LocalStorage.getValue("setrmselectedname");
    }
  
    let req = {
      zmemail: this.selectedzm,
      rmemail: this.selectedrm
    }
    this.server.post("managerenquiry/getamlist", req).then((data) => {
      if (data.status == 200 && data.result.response != 'failed') {
        this.amlist = data.result.data;
       
      //  this.loader = false;
      } else if (data.result.status == 406) {
        
        //alert(data.result.message)
      }
      else if (data.result.status == 401 && data.result.response == 'failed') {

      }
    });
  }
  if((args !='1')&&(args !='')){
    this.reloadcurrentpage();
    } 
  }

  //== get dealer list
  getdealerlist(args) {

    this.dealerlist=[];
    this.selecteddealer='';
     LocalStorage.setValue('selectedamvalue', this.selectedam)
    if(this.selectedam !=''){
    let tempvar ='';
    if((args !='1')&&(args !='')){
      tempvar = args.target.options[args.target.selectedIndex].text;
     
      LocalStorage.setValue("setamselectedname",tempvar);
    }else{
      tempvar = LocalStorage.getValue("setamselectedname");
    }


  //  this.loader = true;
    let req = {
      zmemail: this.selectedzm,
      rmemail: this.selectedrm,
      amemail: this.selectedam
    }
    this.server.post("managerenquiry/getdealerlist", req).then((data) => {
    //  this.loader = false;
      if (data.status == 200 && data.result.response != 'failed') {
        this.dealerlist = data.result.data;  
      } else if (data.result.status == 406) {
      
      }
      else if (data.result.status == 401 && data.result.response == 'failed') {

      }
    });
  }
  if((args !='1')&&(args !='')){
    this.reloadcurrentpage();
  } 
  }

  getdashboarddetails(){

    this.loader=true;

    let tmpenddate=moment(this.selectedtodate).format('YYYY-MM-DD');
    let tmpstart = moment(this.selectedfromdate).format('YYYY-MM-DD');

    let req = {
      zmemail: this.selectedzm,
      rmemail: this.selectedrm,
      amemail: this.selectedam,
      dealer_code:this.selecteddealer,
      from:tmpstart,
      to:tmpenddate


    }
    this.server.post("trainingreport/classroom", req).then((data) => {
      this.loader=false;
      if (data.status == 200 && data.result.response != 'failed') {
        this.headerlist=data.result.data.header;
        this.bodylist=data.result.data.value;
        this.footerlist=data.result.data.footer;
      } else if (data.result.status == 406) {
      }
      else if (data.result.status == 401 && data.result.response == 'failed') {

      }
     
    });
  }
  //==dashboard details ==
  reloadcurrentpage(){
    this.getdashboarddetails()
    
  }
  changestartdate(params){
    let tempfromdate=this.selectedfromdate;
    let todate= moment(this.selectedtodate);
    let fromdate= moment(this.selectedfromdate);
    if(fromdate > todate){
      alert("From Date should not be greater than to date")
      var date12 = new Date();
      var currentyear = date12.getFullYear();

      this.options_start={
        minYear: 2000,
        maxYear: currentyear,
        displayFormat: 'DD-MMM-YYYY',
        barTitleFormat: 'MMMM YYYY',
        dayNamesFormat: 'dd',
        firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
        locale: enLocale,
        minDate: new Date("Sat Jan 1 2000 17:58:53 GMT+0530 (India Standard Time)"),
        maxDate: new Date(Date.now()),  // Maximal selectable date
        barTitleIfEmpty: 'Click to select a date',
      }  

      this.options_end={
        minYear: 2000,
        maxYear: currentyear,
        displayFormat: 'DD-MMM-YYYY',
        barTitleFormat: 'MMMM YYYY',
        dayNamesFormat: 'dd',
        firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
        locale: enLocale,
        minDate: new Date("Sat Jan 1 2000 17:58:53 GMT+0530 (India Standard Time)"),
        maxDate: new Date(Date.now()),  // Maximal selectable date
        barTitleIfEmpty: 'Click to select a date',
      }

      //==From Date==
  
    var firstDay = new Date(date12.getFullYear(), date12.getMonth(), 1);
    var date31 = new Date(firstDay);
    this.selectedfromdate = moment(date31);
    //==To Date ===
    var date29 = date12.setDate(date12.getDate() + 1);
    var date30 = new Date(Date.now());
    this.selectedtodate = moment(date30);

    }else{
      let date12 = new Date(tempfromdate);
      var currentyear = date12.getFullYear();
      this.options_end={
        minYear: 2000,
        maxYear: currentyear,
        displayFormat: 'DD-MMM-YYYY',
        barTitleFormat: 'MMMM YYYY',
        dayNamesFormat: 'dd',
        firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
        locale: enLocale,
        // minDate: new Date("Sat Jan 1 2000 17:58:53 GMT+0530 (India Standard Time)"),
        minDate: new Date(tempfromdate),
        maxDate: new Date(Date.now()),  // Maximal selectable date
        barTitleIfEmpty: 'Click to select a date',
      }
      //==
      var date29 = date12.setDate(date12.getDate() + 1);
      var date30 = new Date(Date.now());
      this.selectedtodate = moment(date30);
    }
  
    
  }
  //==
  changeenddate(params){

    let tempfromdate=this.selectedfromdate;
    let todate= moment(this.selectedtodate);
    let fromdate= moment(this.selectedfromdate);
    if(fromdate > todate){
      alert("From Date should not be greater than to date");
      var date30 = new Date(Date.now());
      this.selectedtodate = moment(date30);
    }else{
      let date12 = new Date(tempfromdate);
      var currentyear = date12.getFullYear();
      this.options_end={
        minYear: 2000,
        maxYear: currentyear,
        displayFormat: 'DD-MMM-YYYY',
        barTitleFormat: 'MMMM YYYY',
        dayNamesFormat: 'dd',
        firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
        locale: enLocale,
        // minDate: new Date("Sat Jan 1 2000 17:58:53 GMT+0530 (India Standard Time)"),
        minDate: new Date(tempfromdate),
        maxDate: new Date(Date.now()),  // Maximal selectable date
        barTitleIfEmpty: 'Click to select a date',
      }
      //==
    }
  
    
  }
  resetdate(){
    var date12 = new Date();
    var currentyear = date12.getFullYear();

    this.options_start={
      minYear: 2000,
      maxYear: currentyear,
      displayFormat: 'DD-MMM-YYYY',
      barTitleFormat: 'MMMM YYYY',
      dayNamesFormat: 'dd',
      firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
      locale: enLocale,
      minDate: new Date("Sat Jan 1 2000 17:58:53 GMT+0530 (India Standard Time)"),
      maxDate: new Date(Date.now()),  // Maximal selectable date
      barTitleIfEmpty: 'Click to select a date',
    }  

    this.options_end={
      minYear: 2000,
      maxYear: currentyear,
      displayFormat: 'DD-MMM-YYYY',
      barTitleFormat: 'MMMM YYYY',
      dayNamesFormat: 'dd',
      firstCalendarDay: 0, // 0 - Sunday, 1 - Monday
      locale: enLocale,
      minDate: new Date("Sat Jan 1 2000 17:58:53 GMT+0530 (India Standard Time)"),
      maxDate: new Date(Date.now()),  // Maximal selectable date
      barTitleIfEmpty: 'Click to select a date',
    }

      //==From Date==

    var firstDay = new Date(date12.getFullYear(), date12.getMonth(), 1);
    var date31 = new Date(firstDay);
    this.selectedfromdate = moment(date31);
    //==To Date ===
    var date29 = date12.setDate(date12.getDate() + 1);
    var date30 = new Date(Date.now());
    this.selectedtodate = moment(date30);

  }
  godateval(){
    this.getdashboarddetails();
  }
  //==
  getuserdetails() {
    this.loader = true;
    this.updateMobileView();
}
updateMobileView() {
    this.server.get("user/profile").then((data) => {
        if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {
            this.userData = data.result.data;
            LocalStorage.setValue('user_data', this.userData)
            this.user_role = this.userData.role;
            console.log("here" + this.user_role)
            if (this.user_role != undefined && this.user_role < 4) {
                this.getzmlist();
            } else if (this.user_role != undefined && this.user_role < 6) {
                this.getrmlist('1');
            } else if (this.user_role != undefined && this.user_role < 7) { 
               
                this.getamlist('1');
            } else {
              this.reloadcurrentpage()
            }
        }
        else if (data.result.status == 401 && data.result.response == 'failed') {
            this.router.navigateByUrl('');
        }
        else {
            alert("Oops something happened!");
        }
    })
}
}

