import { Component, OnInit, ViewChild } 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';
import { DashboardPopupComponent } from '../../sales_executive/dashboard-popup/dashboard-popup.component';
import { CONFIG } from '../../config';
//import { $ } from 'protractor';
declare var $: any;
@Component({
  selector: 'app-dashboard-dse-details',
  templateUrl: './dashboard-dse-details.component.html',
  styleUrls: ['./dashboard-dse-details.component.scss']
})

export class DashboardDseDetailsComponent implements OnInit {
  imageSources = [];
  sales_date: any;
  user_dashboard: any;
  percentage: any;
  user_type: any;
  access_token: any;
  title:any;
  min_point:any;
  date_now: any;
  @ViewChild(DashboardPopupComponent) content_modal: DashboardPopupComponent;
  constructor(private server: HyperService, private router: Router) { 
    this.min_point=LocalStorage.getValue('user_data').min_redeem_point;
  //     this.sub = this.route.params.subscribe(params => {
  //     this.type = params['type'];
  //     this.rmmail = params['rmmail'];
  //     this.ammail = params['ammail'];
  // });
  }

  ngOnInit() {
    this.sales_date = moment().format("MMM YYYY")//current month and year
    this.date_now = moment(this.sales_date, "MMM YYYY").format("MMM YYYY");
    let m = moment(this.sales_date, "MMM YYYY").format("MM");
    let y = moment(this.sales_date, "MMM YYYY").format("YYYY");
    this.dashboardDetails(y, m);
    this.access_token = LocalStorage.getValue('token')
  }

  decreasMonth() {
    this.sales_date = moment(this.sales_date, "MMM YYYY").add(-1, 'months').format("MMM YYYY"); //- one month
    setTimeout(() => {
      let y = moment(this.sales_date, "MMM YYYY").format("YYYY");
      let m = moment(this.sales_date, "MMM YYYY").format("MM");
      this.dashboardDetails(y, m)
    }, 1200);
  }

  increasMonth() {
    let now = moment(this.sales_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.sales_date = moment(this.sales_date, "MMM YYYY").add(1, 'months').format("MMM YYYY"); //+ one month
      let y = moment(this.sales_date, "MMM YYYY").format("YYYY");
      let m = moment(this.sales_date, "MMM YYYY").format("MM");
      this.dashboardDetails(y, m)
    }
  }

  dashboardDetails(year, month) {
    LocalStorage.setValue("temp_mth",month)
    LocalStorage.setValue("temp_yr",year)
    this.server.get("dashboard/" + year + "/" + month + "")
      .then((data) => {
        if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {
          this.user_dashboard = data.result.data;
          this.getPercentage();
        } else if (data.result.status == 401 && data.result.response == 'failed') {
          this.router.navigateByUrl('');
        } else {
          // this.imageSources = [];
        }
      });
  }

  getPercentage() {
    this.percentage = Math.floor(this.user_dashboard.sales.achieved / this.user_dashboard.sales.target * 100);
    this.title = this.user_dashboard.sales.achieved + "/" + this.user_dashboard.sales.target;
  }

  showDetails(val) {
    let sendData:any={};
    sendData.date= this.sales_date;
    sendData.type=val;
    sendData.data_as_on=this.user_dashboard.sales_last_update
    this.content_modal.showcontentrModal('Result', {sendData}, (data) => {
    })
    // this.router.navigateByUrl('redeem');
  }
}
