import { Component, OnInit ,ViewChild, Output,ElementRef, EventEmitter, AfterViewInit, ChangeDetectorRef} from '@angular/core';
import { HyperService } from '../../core/services/http.service';
import { Router, RouterModule, Routes, ActivatedRoute } from '@angular/router';
import { LocalStorage } from '../../core/services/local_storage.service';

import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { DatePipe } from '@angular/common';
import * as moment from 'moment';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { CONFIG } from '../../config';
declare var $: any;

@Component({
  selector: 'app-enquiry-process',
  templateUrl: './enquiry-process.component.html',
  styleUrls: ['./enquiry-process.component.scss']
})
export class EnquiryProcessComponent implements OnInit {
  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;
  task_name:any='';
  page:any;

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



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

 

  ngOnInit() {
    this.current_page = 0;
    this.activePage = 0;

    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;
        if(temp=='hot'){
          this.task_name='HOT';
        }else if(temp=='warm'){
          this.task_name='WARM';
        }
        else if(temp=='cold'){
          this.task_name='COLD';
        }
        else if(temp=='today'){
          this.task_name='TODAYS TASK';
        }
        else if(temp=='pending'){
          this.task_name='PENDING TASK';
        }
        else if(temp=='enquirystatus'){
          this.task_name='ENQUIRY STATUS';
        }
        // this.task_name=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);
    
   
  }
  //==
  // public getalllists(url: string = "", data: any = {}, callBack: any = null) {
    public getalllists(year, month,pagevalue: number = 0) {
     // alert("year-->"+year)
    //  alert("month-->"+month)
    this.is_modal_visible = true;
    // this.screen_callback = callBack;
    this.server.post("enquiry/listdata?page="+pagevalue+"&limit=10&filter=sort:1",{
    // this.server.post("enquiry/listdata", {
      // page: '0',
      // limit: '10',
      // filter: 'sort:1',
      listtype: this.id,
      date: '',
      pendingtype:this.pendingval,
      month: month,
      year: year 
  }).then((data) => {
      if (data.status == 200 && data.result.response != 'failed') {
        this.taskslist = data.result.data;
        this.total_item = Number(data.result.total);
        this.taskslist.forEach((element,index) => {
          let mobileno=element.mobile;
          let mobileno1 = mobileno.substring(0, 2)
          let mobileno2 = mobileno.substring(8, 11)
          this.taskslist[index].custmobile=mobileno1+"XXXXXX"+mobileno2;
        });
     //   console.log("this.taskslist-->",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') {}
    });
  }
  createtestride(){
    
  }
  //===
  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') {}
    });
  }
 //==

  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;
  }

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

