import { Component, OnInit } from '@angular/core';
import * as moment from 'moment';
import { HyperService } from '../../core/services/http.service';
import { Router } from "@angular/router";
import { LocalStorage } from '../../core/services/local_storage.service';
@Component({
  selector: 'app-lederboard',
  templateUrl: './lederboard.component.html',
  styleUrls: ['./lederboard.component.scss']
})
export class LederboardComponent implements OnInit {
  sales_date: any;
  user_dashboard: any;
  percentage: any;
  loader: boolean;
  leader_deatail: any;
  user_data: any;
  total_count: any;
  leader_type :any;
  is_filter: boolean = true;
  filter_year: any;
  filter_month: any;
  filter_date: any;
  selected_type: any = 1;
  times = {
    year: 31557600,
    month: 2629746,
    day: 86400,
    hr: 3600,
    min: 60,
    sec: 1
  }
  user_role:any;
  rolewise: string = '';
  date_now:any;
  constructor(private server: HyperService, private router: Router) {

    if (LocalStorage.getValue('user_data') != undefined){
      this.user_role = LocalStorage.getValue('user_data').role;
  }
     if(this.user_role!=15){
      this.leader_type = [
        { title: "PERFORMANCE", id: "0" },
        { title: "ASSESSMENT", id: "1" }
      ]
     }else{
      this.selected_type=0;
      this.is_filter=false;
      this.leader_type = [
        { title: "ASSESSMENT", id: "1" }
      ]

     }

  }

  ngOnInit() {
    this.initFilter()
  }

  initFilter() {
    this.filter_date = moment().format("MMM YYYY")//current month and year
    let mm = moment(this.filter_date, "MMM YYYY").format("MM");
    let yy = moment(this.filter_date, "MMM YYYY").format("YYYY");
    this.date_now = moment(this.filter_date, "MMM YYYY").format("MMM YYYY");
    this.rolewise = "area";
    this.changeRole("area");
    this.changeRoleSales("area");
    if(this.user_role!=15){
      this.leader_type = [
        { title: "PERFORMANCE", id: "0" },
        { title: "ASSESSMENT", id: "1" }
      ]
     }else{
      this.selected_type=0;
      this.is_filter=false;
      this.leader_type = [
        { title: "ASSESSMENT", id: "1" }
      ]

     }
    this.getLeaderDetails(this.selected_type, yy, mm, "");
    console.log(mm, yy)
  }

  getLeaderDetails(type, year, month, rolewise) {
    console.log(type, year, month)
    let service:any;
    this.loader = true;
    let t = (type == 1) ? 'sales' : 'assessment';
    let _url;
    if (type == 1) {
      if(rolewise!='' && rolewise!='area'){
        _url = "leaderboard/" + t + "/"  + rolewise + "/" + year + "/" + month ;
      }else{
        _url = "leaderboard/" + t + "/"  + year + "/" + month;
      }
      service=this.server.get(_url);
      // _url = "leaderboard/" + t + "/" + year + "/" + month;
    }
    else {
      if(rolewise!='' && rolewise!='area'){
        _url = "leaderboard/" + t + "/"  + rolewise  ;
      }else{
        _url = "leaderboard/" + t ;
      }
      service=this.server.post(_url,{year:year,month:month})
    }

      service.then((data) => {
        if (data.status == 200 && data.result.response != 'failed' && data.result.data != null  ) {
          this.user_data = data.result.user_data;
          this.leader_deatail = data.result.data;
          this.total_count = data.result.total_count;
          this.loader = false
        }
        else if (data.result.status == 406  ) {
          this.loader = false
        }
        else if (data.result.status == 401 && data.result.response == 'failed') {
          this.router.navigateByUrl('');
        }
      });
  }



  decreasFilter() {
    this.loader = true;
    this.filter_date = moment(this.filter_date, "MMM YYYY").add(-1, 'months').format("MMM YYYY"); //- one month
    setTimeout(() => {
      let y = moment(this.filter_date, "MMM YYYY").format("YYYY");
      let m = moment(this.filter_date, "MMM YYYY").format("MM");
      this.getLeaderDetails(this.selected_type,y, m,this.rolewise)
    }, 1200);
  }

  increasFilter() {
    let now = moment(this.filter_date, "MMM YYYY").format("YYYY-MM-DD");
    let tdyDate = moment(new Date()).format('YYYY-MM-DD');
    if (moment(now).isSame(tdyDate, 'month') == false && moment(now).isSameOrBefore(tdyDate, 'year') == true) {
      this.loader = true;
      this.filter_date = moment(this.filter_date, "MMM YYYY").add(1, 'months').format("MMM YYYY"); //+ one month
      let y = moment(this.filter_date, "MMM YYYY").format("YYYY");
      let m = moment(this.filter_date, "MMM YYYY").format("MM");
      this.getLeaderDetails(this.selected_type,y, m,this.rolewise)
    }
  }

  selectTopic(type) {
    console.log(type)

    this.filter_date = moment().format("MMM YYYY")//current month and year
    let mm = moment(this.filter_date, "MMM YYYY").format("MM");
    let yy = moment(this.filter_date, "MMM YYYY").format("YYYY");
    if (type == 0) {
      this.getLeaderDetails(1, yy, mm, "");
      this.is_filter = true;
      this.rolewise = "area";
      this.selected_type=1;
    }
    else if (type == 1) {
      this.rolewise = "area";
      this.getLeaderDetails(2, yy, mm, "");
      this.is_filter = false;
      this.selected_type=2;
    }

  }

  formatMin(seconds) {
    if (seconds == 0) {
      return 0;
    }
    else {
      let time_string: string = '';
      let plural: string = '';
      for (var key in this.times) {
        if (Math.floor(seconds / this.times[key]) > 0) {
          if (Math.floor(seconds / this.times[key]) > 1) {
            plural = 's';
          }
          else {
            plural = '';
          }

          time_string += Math.floor(seconds / this.times[key]).toString() + ' ' + key.toString() + plural + ' ';
          seconds = seconds - this.times[key] * Math.floor(seconds / this.times[key]);

        }
      }
      return time_string;
    }


  }
  changeRole(event) {
    console.log(this.selected_type,event)
    let role:string='';
    this.selected_type='2';
    role= (event=="area")?"":event;
    console.log(role,"role")
    this.filter_date = moment().format("MMM YYYY")//current month and year
    this.date_now = moment(this.filter_date, "MMM YYYY").format("MMM YYYY");
    let mm = moment(this.filter_date, "MMM YYYY").format("MM");
    let yy = moment(this.filter_date, "MMM YYYY").format("YYYY");
    this.getLeaderDetails(this.selected_type, yy, mm, role);
  }
  changeRoleSales(event) {
    console.log(this.selected_type,event)
    let role:string='';
    this.selected_type='1';
    role= (event=="area")?"":event;
    console.log(role,"role")
    this.filter_date = moment().format("MMM YYYY")//current month and year
    this.date_now = moment(this.filter_date, "MMM YYYY").format("MMM YYYY");
    let mm = moment(this.filter_date, "MMM YYYY").format("MM");
    let yy = moment(this.filter_date, "MMM YYYY").format("YYYY");
    this.getLeaderDetails(this.selected_type, yy, mm, role);
  }
}
