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 { TaskstatusComponent } from "../taskstatus/taskstatus.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 { EnquirypopupComponent } from '../enquirypopup/enquirypopup.component';

@Component({
  selector: 'app-enquiry-dse',
  templateUrl: './enquiry-dse.component.html',
  styleUrls: ['./enquiry-dse.component.scss']
})
export class EnquiryDseComponent 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='';
  constructor(
    private server: HyperService, 
    private router: Router,
    private _eref: ElementRef,
    private route: ActivatedRoute
    ) {
      
        
    }
    @ViewChild(TaskstatusComponent) taskmodal: TaskstatusComponent;
    @ViewChild(EnquirypopupComponent) content_modal: EnquirypopupComponent;
  ngOnInit() {
    this.model_image_url='';
    this.displaylist=false;
    this.displayallmodellist=true;
    this.current_page = 0;
    this.activePage = 0;
    this.total_item=0;   



   // 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() {
    //alert("decreasMonth"+this.displaylist)
    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;
      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() {

   // alert("decreasMonth"+this.displaylist)
    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;

    let y = LocalStorage.getValue('yearvalueinitial');
    let m = LocalStorage.getValue('monthvalueinitial')
    this.getalllists(y,m,0);
  }
  
   salesdetailModal() {
    this.displaylist=false;
    this.model_image_url='';
  //  console.log("inside sales details modal");
    this.is_modal_visible = true;
    this.loader = true;
    // alert("this.yearpresent--->"+this.yearpresent);
   
    this.yearpresent=LocalStorage.getValue('yearvalueinitial');
    this.monthpresent=LocalStorage.getValue('monthvalueinitial');

   // console.log("yearinitialvalue--->",this.yearpresent);
   // console.log("monthpresent--->",this.monthpresent);
    var urlvalue='nonmyenquiries';
    this.taskmodal.getmonthbaseddetails(this.yearpresent,this.monthpresent,urlvalue)

    this.server.get("dashboard/enquiry/" + 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;
    var urlvalue='nonmyenquiries';
    this.taskmodal.getmonthbaseddetails(year,month,urlvalue)

    this.server.post("enquiry/listdata?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);
      } 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('myenquiries');
    }else {
      this.displaylist=false;
      this.displayallmodellist=true;
    }
  }

  testridestatus(param,mobileno){
    LocalStorage.setValue("pagevalue",'enquirystatus');
    LocalStorage.setValue('sessionstatusvalue','enquiry-dse');
    this.content_modal.testridepopup(param,mobileno);
  }
}
 
