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 { TaskstatusDealerComponent } from "../taskstatus-dealer/taskstatus-dealer.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;
import { EnquirypopupDealerComponent } from '../enquirypopup-dealer/enquirypopup-dealer.component';

@Component({
  selector: 'app-enquiry-dealer',
  templateUrl: './enquiry-dealer.component.html',
  styleUrls: ['./enquiry-dealer.component.scss']
})
export class EnquiryDealerComponent 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='';
  
  constructor(
    private server: HyperService, 
    private router: Router,
    private _eref: ElementRef,
    private route: ActivatedRoute
    ) {
      
        
    }
    @ViewChild(TaskstatusDealerComponent) taskmodal: TaskstatusDealerComponent;
    @ViewChild(EnquirypopupDealerComponent) content_modal: EnquirypopupDealerComponent;
    
  ngOnInit() {
    this.smpinselected=LocalStorage.getValue('setdseselected');
    this.smpinselectedname=LocalStorage.getValue('setdseselectedname');
    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');
    
  //  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
      //this.date_now = moment(this.sales_date, "MMM YYYY").format("MMM YYYY");
      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;
    }




    if(this.displaylist ==false){
      this.salesdetailModal();
     }else{
     
      this.getalllists(this.yearpresent,this.monthpresent,0);
     }
  }
  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) {
      // let now = moment(this.sales_date, "MMM YYYY").format("YYYY-MM-DD");
      // let tdyDate = moment(new Date()).format('YYYY-MM-DD');
    
      this.sales_date = moment(mhval+"-25-"+yrval).add(1, 'months').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;
      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.getDashboardmonthbased(y, 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;
    }
    if(this.displaylist ==false){
      this.salesdetailModal();
     }else{
     
      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.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.server.post("dealerenquiry/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);
        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 = "XXXXXXXX" + mobileno2;
        });
      } else if (data.result.status == 406) { }
      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;
  }
  backclick(){
    if(this.displayallmodellist==true){
      this.router.navigateByUrl('dealer-myenquiries');
    }else {
      this.displaylist=false;
      this.displayallmodellist=true;
    }
  }
  testridestatus(param, mobileno) {
    //    LocalStorage.setValue("pagevalue",'enquiry-stage/');
    LocalStorage.setValue('sessionstatusvalue', 'dealer-enquiry');
    this.content_modal.testridepopup(param, mobileno);
  }

}
 

