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 { TaskstatusManagerComponent } from "../taskstatus-manager/taskstatus-manager.component";
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-manager',
  templateUrl: './enquiry-manager.component.html',
  styleUrls: ['./enquiry-manager.component.scss']
})
export class EnquiryManagerComponent implements OnInit {

  loader: boolean=false;
  is_modal_visible:boolean=false;
  yearpresent:any='';
  monthpresent:any='';
  sales_list:any=[];
  displaylist:boolean=false;
  displayallmodellist:boolean=false;
  modelselectedvalue: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;
  taskslist:any=[];
  today: number = Date.now();
  sales_date: any;
  date_now: any;
  model_image_url:any='';
  smpinselected:any='';
  smpinselectedname:any='';
  
  selecteduserrole:any='';
  setamselectednameobj:any='';
  setrmselectednameobj:any='';
  setdealerselectednameobj:any='';
  setdseselectednameobj:any='';
  selectedareamanager:any='';
  selectedregionalmanager:any='';
  setzmselectednameobj:any='';

  constructor(
    private server: HyperService, 
    private router: Router,
    private _eref: ElementRef,
    private route: ActivatedRoute
    ) {
      
        
    }
    @ViewChild(TaskstatusManagerComponent) taskmodal: TaskstatusManagerComponent;

    
  ngOnInit() {
    this.smpinselected=LocalStorage.getValue('setdseselected');
    this.selecteduserrole = LocalStorage.getValue('selecteduserrole');
    this.smpinselected=LocalStorage.getValue('setdseselected');
    this.smpinselectedname=LocalStorage.getValue('setdseselectedname');
    let setdealerselectedname =  LocalStorage.getValue("setdealerselectedname");
    let setdseselectedname =  LocalStorage.getValue("setdseselectedname");
    let setamselectedname =  LocalStorage.getValue("setamselectedname");
    let setrmselectedname =  LocalStorage.getValue("setrmselectedname");

      let smpinvalue=LocalStorage.getValue('setdseselected');
    let dealerselected=LocalStorage.getValue('selecteddealervalue');
    let areamanagerselected=LocalStorage.getValue('selectedamvalue');
    let regionalmanagerselected=LocalStorage.getValue('selectedrmvalue');
    let setzmselectedname =  LocalStorage.getValue("setzmselectedname");
  
  if(setzmselectedname ==undefined){
      this.setzmselectednameobj='';
    }else{
  this.setzmselectednameobj=setzmselectedname;
   }
    if(areamanagerselected != undefined){
      this.selectedareamanager=areamanagerselected;
    }else{
      this.selectedareamanager='';
    }
   
    if(regionalmanagerselected != undefined){
      this.selectedregionalmanager=regionalmanagerselected;
    }else{
      this.selectedregionalmanager='';
    }

  //  console.log("setdseselected--->",this.smpinselected);
    LocalStorage.setValue("activity_check",'activity_check');
    this.model_image_url='';
    this.displaylist=false;
    this.displayallmodellist=true;
    this.current_page = 0;
    this.activePage = 0;
    this.total_item=0;   


    this.smpinselected=LocalStorage.getValue('setdseselected');
    if(setdealerselectedname ==undefined){
      this.setdealerselectednameobj='';
    }else{
      this.setdealerselectednameobj=setdealerselectedname;
    }
  
    if(setdseselectedname ==undefined){
      this.setdseselectednameobj='';
    }else{
      this.setdseselectednameobj=setdseselectedname;
    }
  
    if(setamselectedname ==undefined){
      this.setamselectednameobj='';
    }else{
      this.setamselectednameobj=setamselectedname;
    }
  
    if(setrmselectedname ==undefined){
      this.setrmselectednameobj='';
    }else{
      this.setrmselectednameobj=setrmselectedname;
    }
    
  //  console.log("setdseselected--->",this.smpinselected);

   // this.getDashboardstatic();

    let yrval = LocalStorage.getValue('yearvalueinitial');
    let mhval =   LocalStorage.getValue('monthvalueinitial');
    if((yrval !=''&& mhval!='')&&(yrval !=undefined&& mhval!=undefined)){
      this.sales_date = moment(mhval+"-25-"+yrval).format("MMM YYYY")//current month and year
      let tempcurr = moment().format("MMM YYYY")//current month and year
      this.date_now = moment(tempcurr, "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.yearpresent=y;
      this.monthpresent=m;
    }
    else{
      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.yearpresent=y;
    this.monthpresent=m;
    }



   
    this.salesdetailModal();
  }
  increasMonth() {

    
    let yrval = LocalStorage.getValue('yearvalueinitial');
    let mhval =   LocalStorage.getValue('monthvalueinitial');

    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(mhval+"-25-"+yrval).add(1, 'months').format("MMM YYYY");
     // 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");
      LocalStorage.setValue('yearvalueinitial',y);
      LocalStorage.setValue('monthvalueinitial',m);
      this.yearpresent=y;
      this.monthpresent=m;
      this.taskmodal.relaodtaskstatus();
      if(this.displaylist ==false){
        this.salesdetailModal();
      }else{
      
        this.getalllists(this.yearpresent,this.monthpresent,0);
      }
 //   }
   // else{
    // this.sales_date = moment(this.sales_date, "MMM YYYY").add(1, 'months').format("MMM YYYY"); //- one month
    // 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");
    // LocalStorage.setValue('yearvalueinitial',y);
    // LocalStorage.setValue('monthvalueinitial',m);
    // this.yearpresent=y;
    // this.monthpresent=m;
    //}
    
 //  this.getalllists(this.yearpresent,this.monthpresent,0);
      
  //  }
  }
  decreasMonth() {


    let yrval = LocalStorage.getValue('yearvalueinitial');
    let mhval =   LocalStorage.getValue('monthvalueinitial');
    if((yrval !=''&& mhval!='')&&(yrval !=undefined&& mhval!=undefined)){
      this.sales_date = moment(mhval+"-25-"+yrval).add(-1, 'months').format("MMM YYYY");
     // 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");
      LocalStorage.setValue('yearvalueinitial',y);
      LocalStorage.setValue('monthvalueinitial',m);
      this.yearpresent=y;
      this.monthpresent=m;
    }
    else{
    this.sales_date = moment(this.sales_date, "MMM YYYY").add(-1, 'months').format("MMM YYYY"); //- one month
    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");
    LocalStorage.setValue('yearvalueinitial',y);
    LocalStorage.setValue('monthvalueinitial',m);
    this.yearpresent=y;
    this.monthpresent=m;
    }
    this.taskmodal.relaodtaskstatus();
    if(this.displaylist ==false){
      this.salesdetailModal();
     }else{
     
      this.getalllists(this.yearpresent,this.monthpresent,0);
     }
     // this.getalllists(this.yearpresent,this.monthpresent,0);
  //  }, 1200);
  }
  getselectedmodel(tempvalue){
  //  console.log("tempvalue---->",tempvalue);
    this.modelselectedvalue=tempvalue.modelname;
    this.model_image_url=tempvalue.model_image_url;
    this.getalllists(0,0,0);
  }
  
  salesdetailModal() {
    this.displaylist=false;
    this.model_image_url='';
  //  console.log("inside sales details modal");
    this.is_modal_visible = true;
    this.loader = true;
   
    this.yearpresent=LocalStorage.getValue('yearvalueinitial');
    this.monthpresent=LocalStorage.getValue('monthvalueinitial');
    var urlvalue='nonmyenquiries';
    // this.taskmodal.getmonthbaseddetails(this.yearpresent,this.monthpresent,urlvalue)
    this.taskmodal.relaodtaskstatus();
    this.server.get("dashboard/dealerenquiry/"+this.smpinselected+"/" + this.yearpresent + "/" + this.monthpresent + "")
    .then((data) => {
      this.loader = false;
      if (data.status == 200 && data.result.response != 'failed') {
        this.sales_list = data.result.data;
      } else if (data.result.status == 406) { }
      else if (data.result.status == 401 && data.result.response == 'failed') { }
    });
  }

  getalllists(year, month,pagevalue: number = 0) {
    this.displaylist=true;
    this.displayallmodellist=false;
    this.loader = true;
    this.is_modal_visible = true;
    // this.screen_callback = callBack;
    LocalStorage.setValue("activity_check",'activity_check');
    var urlvalue='nonmyenquiries';
    this.taskmodal.getmonthbaseddetails(year,month,urlvalue)
    this.taskmodal.relaodtaskstatus();
    this.server.post("managerenquiry/listdata/"+this.smpinselected+"?page="+pagevalue+"&limit=10&filter=sort:1",{
    //  this.yearpresent=y;
    // this.monthpresent=m;
      listtype: 'model',
      date: '',
      model:this.modelselectedvalue,
      month: this.monthpresent,
      year: this.yearpresent 
  }).then((data) => {
    this.loader = false;
      if (data.status == 200 && data.result.response != 'failed') {
        this.taskslist = data.result.data;
        this.total_item = Number(data.result.total);
        // alert("this.total_item-->"+this.total_item)
      } else if (data.result.status == 406) { }
      else if (data.result.status == 401 && data.result.response == 'failed') { }
    });
    // this.taskmodal.relaodtaskstatus();
  }
  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;
  }
  backclick(){
    // alert("this.selecteduserrole--->"+this.selecteduserrole);
    if(this.displayallmodellist==true){
      if(this.selecteduserrole == 7){
        this.router.navigateByUrl('manager-enquiry');
      }else if(this.selecteduserrole == 6){
        this.router.navigateByUrl('regional-enquiry');
      }else if(this.selecteduserrole == 4){
        this.router.navigateByUrl('zonal-enquiry');
      }else if(this.selecteduserrole == 3){
        this.router.navigateByUrl('ho-enquiry');
      }else if(this.selecteduserrole == 5){
        this.router.navigateByUrl('trainer-enquiry');
      }else{

      }

    
    }else {
      this.displaylist=false;
      this.displayallmodellist=true;
    }
  }






}
 


