import { Component, OnInit, ViewChild, ElementRef, NgModule } from '@angular/core';
import { HyperService } from '../../core/services/http.service';
import { Router, RouterModule, Routes, ActivatedRoute } from '@angular/router';
import { FormGroup, FormBuilder, Validators, FormControl } 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 { DatepickerOptions } from 'ng2-datepicker';
import * as enLocale from 'date-fns/locale/en';
import { ConfirmPopupComponent } from '../../sales_executive/confirm-popup/confirm-popup.component';
// declare var $: any;
import { Subscription } from "rxjs";
import { Observable } from "rxjs/Observable";
import { Location } from '@angular/common';
import { ToasterService } from '@service/toaster.service';
import { TaskstatusManagerComponent } from "../taskstatus-manager/taskstatus-manager.component";

import $ from 'jquery';
declare var $: $;

@Component({
  selector: 'app-enquiry-profile-manager',
  templateUrl: './enquiry-profile-manager.component.html',
  styleUrls: ['./enquiry-profile-manager.component.scss']
})
export class EnquiryProfileManagerComponent implements OnInit {
  loader: boolean = false;
  @ViewChild(TaskstatusManagerComponent) taskmodal: TaskstatusManagerComponent;
  enquirylist: any = [];
  enquirydata: any = [];
  idvalue: any = [];
  sales_date: any;
  date_now: any;
  yearpresent: any;
  monthpresent: any;
  smpinselected: any = '';
  smpinselectedname: any = '';

  selecteduserrole: any = '';
  setamselectednameobj: any = '';
  setrmselectednameobj: any = '';
  setdealerselectednameobj: any = '';
  setdseselectednameobj: any = '';
  setzmselectednameobj:any='';

  constructor(
    private server: HyperService,
    private router: Router,
    private _eref: ElementRef,
    private route: ActivatedRoute,
    private formBuilder: FormBuilder,
    private toaster: ToasterService
  ) {


  }

  ngOnInit() {
    this.smpinselected = LocalStorage.getValue('setdseselected');
    this.selecteduserrole = LocalStorage.getValue('selecteduserrole');
    let setdealerselectedname = LocalStorage.getValue("setdealerselectedname");
    let setdseselectedname = LocalStorage.getValue("setdseselectedname");
    let setamselectedname = LocalStorage.getValue("setamselectedname");
    let setrmselectedname = LocalStorage.getValue("setrmselectedname");
    let setzmselectedname =  LocalStorage.getValue("setzmselectedname");
  
  if(setzmselectedname ==undefined){
      this.setzmselectednameobj='';
    }else{
  this.setzmselectednameobj=setzmselectedname;
   }
   
    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;
    }

    this.smpinselected = LocalStorage.getValue('setdseselected');
    this.smpinselectedname = LocalStorage.getValue('setdseselectedname');

    this.selecteduserrole = LocalStorage.getValue('selecteduserrole');

    this.setdealerselectednameobj = setdealerselectedname;
    this.setdseselectednameobj = setdseselectedname;


    //  console.log("setdseselected--->",this.smpinselected);

    this.idvalue = LocalStorage.getValue('selectedidvalue');
    //  console.log("this.idvalue--->",this.idvalue);
    this.enquirylist = [];
    this.enquirydata = [];
    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 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.getcommondetails()
    this.getenquirydetails();
    this.taskmodal.relaodtaskstatus();
  }
  getenquirydetails() {
    this.loader = true;
    let tempvalue = this.idvalue;
    var urlvalue = 'nonmyenquiries';
    LocalStorage.setValue("activity_check", 'activity_check');
    this.taskmodal.getmonthbaseddetails(this.yearpresent, this.monthpresent, urlvalue)
    this.server.get("dealerenquiry/details/" + this.smpinselected + "/" + tempvalue)
      .then((data) => {
        this.loader = false;
        if (data.status == 200 && data.result.response != 'failed') {
          this.enquirydata = data.result.data;
          let temp1 = this.enquirydata.newvechicledate;
           let temp2=temp1.split('-');
          let newvechicledate = temp2[2]+"-"+temp2[1]+"-"+temp2[0];
          this.enquirydata.newvechicledate=newvechicledate;
          
          this.enquirylist = this.enquirydata.history;
          let mobileno = this.enquirydata.mobile;
          let mobileno1 = mobileno.substring(0, 2)
          let mobileno2 = mobileno.substring(8, 11)
          this.enquirydata.custmobile = "XXXXXXXX" + mobileno2;
          //  console.log("this.enquirylist--->",this.enquirylist);
        } else if (data.result.status == 406) {
        }
        else if (data.result.status == 401 && data.result.response == 'failed') { }
      });
  }
  //==
  insertfollowup() {
    let req = {

    }
    // console.log("req-->",req);
    // return false;

    this.server.post("enquiry", req).then((data) => {
      if (data.status == 200 && data.result.response != 'failed' && data.result.data != null) {

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

      }
    });

  }
  //==
  getcommondetails() {
    this.server.get("dealerenquiry/common/" + this.smpinselected)
      .then((data) => {
        if (data.status == 200 && data.result.response != 'failed') {
          //console.log("data--->",data);

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

        }
      });
  }
  //==
  createtestride() {
    this.router.navigateByUrl('create-enquiry');
  }
  backactionhistory() {
    let tempvalue = LocalStorage.getValue('sessionstatusvalue');
    //  console.log("tempvalue--->",tempvalue);
    this.router.navigateByUrl(tempvalue);
  }



}
