import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { HyperService } from '../../core/services/http.service';
import { Router, RouterModule, Routes, ActivatedRoute } from '@angular/router';
import { FormBuilder, FormGroup, Validators } 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 { CONFIG } from '../../config';
import { ConfirmPopupComponent } from '../../sales_executive/confirm-popup/confirm-popup.component';
import { EnquirypopupComponent } from '../enquirypopup/enquirypopup.component';
@Component({
  selector: 'app-nonsyncenquiry',
  templateUrl: './nonsyncenquiry.component.html',
  styleUrls: ['./nonsyncenquiry.component.scss']
})
export class NonsyncenquiryComponent implements OnInit {
  @ViewChild(EnquirypopupComponent) content_modal: EnquirypopupComponent;
  @ViewChild(ConfirmPopupComponent) confirm_modal: ConfirmPopupComponent;


  loader:boolean=false;
  is_modal_visible: boolean;
  screen_callback: any;
  sales_list: any = [];
  taskslist:any = [];
  CurrentTime: any;
  today: number = Date.now();
  sales_date: any;
  date_now: any;
  today_personal_visit: any;
  today_personal_testride: any;
  today_personal_call: any;
  today_pending_visit: any;
  today_pending_testride: any;
  today_pending_call: any;
  month_enquiry_target: any;
  month_enquiry_achieved: any;
  month_enquiry_month: any;
  month_enquiry_year: any;
  month_sales_target: any;
  month_sales_achieved: any;
  month_sales_month: any;
  month_sales_year: any;
  stage_testride: any;
  stage_booked: any;
  stage_invoiced: any;
  stage_lost: any;
  status_hot: any;
  status_warm: any;
  status_cold: any;
  id: any;
  pendingval:any;
  yearpresent:any;
  monthpresent:any;

  total_item: number;
  page_per_size: number = 10;
  current_page: number;
  total_page: number;
  activePage: number = 1;
  page_count: number;
  activeItem: number = -1;
  interval_time:any;

  titleenquiry:any;
  titlesales:any;
  varstatus:any;
  percentage_enquiry: any;
  percentage_sales: any;

  constructor(
    private server: HyperService, 
    private router: Router,
    private _eref: ElementRef,
    private route: ActivatedRoute
    ) {
      
        
    }

 

  ngOnInit() {
    this.route.params.subscribe(params => {
      let temp = params['id'];
      if((temp ==  'pending_call')||(temp ==  'pending_testride')||(temp ==  'pending_visit')){
        let toArray =  temp.split("_");
        this.pendingval=toArray[1];
       
        this.id='pending';
        if(temp ==  'pending_testride'){
          this.pendingval='test drive';
          this.id='pending';
        }
      }else{
        this.id=temp;
        this.pendingval='';
      }
    });
      //console.log(this.id);



    this.getDashboardstatic();
    // 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");temp_mth
    if((LocalStorage.getValue('user_data')!=undefined)&&(LocalStorage.getValue('user_data')!=undefined)){
      this.yearpresent=LocalStorage.getValue('temp_yr');
     
      this.monthpresent=LocalStorage.getValue('temp_mth');
    }
    // this.getDashboardmonthbased(this.yearpresent, this.monthpresent);
    this.getalllists(this.yearpresent, this.monthpresent,0);


    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.getDashboardmonthbased(y, m);
   
  }
  //==
  // public getalllists(url: string = "", data: any = {}, callBack: any = null) {
    public getalllists(year, month,page: number = 0) {
     // alert("year-->"+year)
    //  alert("month-->"+month)
    this.loader=true;
    this.is_modal_visible = true;
    // this.screen_callback = callBack;
    this.server.get("enquiry/checkunsave").then((data) => {
      this.loader=false;
      if (data.status == 200 && data.result.response != 'failed') {
     //   console.log("data--->",data.result.data.details);
       // return false;
        this.taskslist = data.result.data.details;

        this.total_item = Number(data.result.total);
  // console.log("this.tasklist--->",this.taskslist)

      } else if (data.result.status == 406) { }
      else if (data.result.status == 401 && data.result.response == 'failed') { }
    });
  //====


  
  }
  //==
  //===
  getDashboardstatic() {
    this.server.get("enquiry/dashboard")
    .then((data) => {
        if (data.status == 200 && data.result.response != 'failed') {
       //   console.log(data.result.data.today.visit)
           this.today_personal_visit = data.result.data.today.visit;
           this.today_personal_testride = data.result.data.today.testride;
           this.today_personal_call = data.result.data.today.call;
           //==
           this.today_pending_visit = data.result.data.pending.visit;
           this.today_pending_testride = data.result.data.pending.testride;
           this.today_pending_call = data.result.data.pending.call;
           

        }else if (data.result.status == 406 ) {
              //alert(data.result.message)
        }
         else if (data.result.status == 401 && data.result.response == 'failed') {}
    });
  }

  //  ==
  // public onPageChange(event) {
  //   this.activeItem = -1;
  //   this.activePage = event;
  //   this.current_page=this.activePage;
  //   this.current_page=this.activePage;
  //   console.log('pagech', this.activePage)
  // }
  // //==
  // public getServerData(event) {
  //   let mvalue = LocalStorage.getValue("temp_mth")
  //   let yearvalue = LocalStorage.getValue("temp_yr")
  //   console.log(event)
  //   this.page_count = (event - 1);
  //   this.getalllists(this.yearpresent, this.monthpresent,this.page_count);
  //   this.onPageChange(event);
  // }

  public getServerData(event) {
    this.page_count = (event - 1);
    this.getalllists(this.yearpresent, this.monthpresent,this.page_count);
    this.onPageChange(event);
  }

  public onPageChange(event) {
    this.activeItem = -1;
    this.activePage = event;
  }

  //===
  getDashboardmonthbased(year, month){
    LocalStorage.setValue("temp_mth",month)
    LocalStorage.setValue("temp_yr",year)

    // this.server.get("enquiry/enquirysales/2019/02")
    this.server.get("enquiry/enquirysales/" + year + "/" + month + "")
    .then((data) => {
        if (data.status == 200 && data.result.response != 'failed') {
          //  this.pending_tasks=data.result.data;
          // console.log(this.pending_tasks.enquiry.target);

          this.month_enquiry_target=data.result.data.enquiry.target;
          this.month_enquiry_achieved=data.result.data.enquiry.achieved;
          this.month_enquiry_month=data.result.data.enquiry.month;
          this.month_enquiry_year=data.result.data.enquiry.year;
          //==
          this.month_sales_target=data.result.data.sales.target;
          this.month_sales_achieved=data.result.data.sales.achieved;
          this.month_sales_month=data.result.data.sales.month;
          this.month_sales_year=data.result.data.sales.year;
          //==
          this.stage_testride=data.result.data.stage.testride;
          this.stage_booked=data.result.data.stage.booked;
          this.stage_invoiced=data.result.data.stage.invoiced;
          this.stage_lost=data.result.data.stage.lost;
          //==
          this.status_hot=data.result.data.status.hot;
          this.status_warm=data.result.data.status.warm;
          this.status_cold=data.result.data.status.cold;
          //==
          this.getPercentage();
          this.getPercentage_sales();
        }else if (data.result.status == 406 ) {
              //alert(data.result.message)
        }
          else if (data.result.status == 401 && data.result.response == 'failed') {}
    });
  }
  //==
  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.getDashboardmonthbased(y, m);
      
    }, 1200);
  }
  //==
  increasMonth() {
    // alert("aaasas")
    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.getDashboardmonthbased(y, m);
      
    //}
  }

    //==
    getPercentage() {
      this.percentage_enquiry = Math.floor(this.month_enquiry_achieved / this.month_enquiry_target * 100);
      this.titleenquiry = this.month_enquiry_achieved + "/" + this.month_enquiry_target;
  }
  getPercentage_sales() {
      this.percentage_sales = Math.floor(this.month_sales_achieved / this.month_sales_target * 100);
      this.titlesales = this.month_sales_achieved + "/" + this.month_sales_target;
   
   }

  // getPercentage() {
  //   throw new Error("Method not implemented.");
  // }
  // getPercentage_sales() {
  //   throw new Error("Method not implemented.");
  // }
 

  ///Ram code------------

  backtoenquiry(id){
    const found = this.taskslist.find(element => element.id == id);
    LocalStorage.setValue("non-syncenquiry",found);
    this.router.navigateByUrl("create-enquiry/non-sync")   
  }

  deletetask(id){ 
   
    this.confirm_modal.deletenonsyncmodal(id);
  }
  sendtolist(temp){

  }
  testridestatus(param,mobileno){
    LocalStorage.setValue("sessionstatusvalue",'nonsyncenquiry');
    this.content_modal.testridepopup(param,mobileno);
  }



}

