import { Component, OnInit , ViewChild} from '@angular/core';
import { CONFIG } from '../../../app/config';
import { HyperService } from '../../core/services/http.service';
import { LocalStorage } from '../../core/services/local_storage.service';
import { ActivatedRoute,Router } from '@angular/router';
import { EnquirypopupManagerComponent } from '../../leadmodulemanager/enquirypopup-manager/enquirypopup-manager.component';
@Component({
  selector: 'app-taskstatus-manager',
  templateUrl: './taskstatus-manager.component.html',
  styleUrls: ['./taskstatus-manager.component.scss']
})
export class TaskstatusManagerComponent implements OnInit {


  @ViewChild(EnquirypopupManagerComponent) content_modal: EnquirypopupManagerComponent;


  today_personal_visit:any;
  today_personal_testride:any;
  today_personal_call:any;
  today_pending_visit:any;
  today_pending_testride:any;
  today_pending_call:any;
  selecteddse:any='';
  selecteddealer:any=''
  tomorrow_personal_visit:any;
  tomorrow_personal_testride:any;
  tomorrow_personal_call:any;

  displaybottom:boolean=false;
  yearpresent:any='';
  monthpresent:any='';
  stage_testride:any;
  stage_booked:any;
  stage_invoiced:any;
  stage_lost:any;
  status_hot:any;
  status_warm:any;
  status_cold:any;
  displayboolean:boolean=false;
  href: string = "";
  smpinselected:any='';
  today_enable:boolean=false;
  tommorrow_enable:boolean=false;
  more_enable:boolean=false;
  setdealerselectednameobj:any='';
  setdseselectednameobj:any='';
  selecteduserrole:any='';
  selectedareamanager:any='';
  selectedregionalmanager:any='';
  constructor( private server: HyperService, private router: Router,private route: ActivatedRoute) { }
 

  
  public getmonthbaseddetails(year,month,urlvalue){
    
    if(urlvalue == 'manager-myenquiries'){
      this.displayboolean=false;
    }else{
      this.displayboolean=true;
    }
  
 
   
  }
  ngOnInit() {



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



    let smpinvalue=LocalStorage.getValue('setdseselected');
    let dealerselected=LocalStorage.getValue('selecteddealervalue');
    let areamanagerselected=LocalStorage.getValue('selectedamvalue');
    let regionalmanagerselected=LocalStorage.getValue('selectedrmvalue');

    if(areamanagerselected != undefined){
      this.selectedareamanager=areamanagerselected;
    }else{
      this.selectedareamanager='';
    }
   
    if(regionalmanagerselected != undefined){
      this.selectedregionalmanager=regionalmanagerselected;
    }else{
      this.selectedregionalmanager='';
    }


    let setdealerselectedname =  LocalStorage.getValue("setdealerselectedname");
   let setdseselectedname =  LocalStorage.getValue("setdseselectedname");

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


    this.smpinselected = smpinvalue

    
    this.today_enable=true;
    this.href = this.router.url;

    this.smpinselected=LocalStorage.getValue('setdseselected');
  //  console.log("setdseselected--->",this.smpinselected);
    //alert("this.smpinselected--->"+this.smpinselected);
    this.href = this.router.url;
    let check=LocalStorage.getValue("activity_check");
    if(check=='activity_check'){
      this.displayboolean=true;
    }else{
      this.displayboolean=false;
    }
  //  console.log(this.router.url);
  //  console.log("displayboolean--->",this.displayboolean);
    // var year = CONFIG.configyearval;
    // var month = CONFIG.configmonthval;

    // console.log("year----->",year);
    // console.log("month----->",month);

  //  this.yearpresent=year;
  //  this.monthpresent=month;
    // this.route.params.subscribe(params => {
    // });
   
    if((this.smpinselected !='')&&(this.smpinselected !=undefined)){
      this.getDashboardstatic();
      let yrval = LocalStorage.getValue('yearvalueinitial');
      let mhval =   LocalStorage.getValue('monthvalueinitial');
      this.getDashboardmonthbased(yrval,mhval);
    }else{

    }
   
    
  }
  //===
  getDashboardstatic() {
    let dealerselected=LocalStorage.getValue("selecteddealervalue");
    let dseselected=LocalStorage.getValue("setdseselected");
    let amselected=LocalStorage.getValue("selectedamvalue");
    let rmselected=LocalStorage.getValue("selectedrmvalue");
    let zmselected=LocalStorage.getValue("selectedzmvalue");

    if(amselected != undefined){
     // this.selectedareamanager=areamanagerselected;
    }else{
      amselected='';
    }
   
    if(rmselected != undefined){
     // this.selectedregionalmanager=regionalmanagerselected;
    }else{
      rmselected='';
    }

    if(zmselected != undefined){
      // this.selectedregionalmanager=regionalmanagerselected;
     }else{
       zmselected='';
     }
 



    let req = {
      zmemail:zmselected,
      rmemail:rmselected,
      amemail:amselected,
      dealer_code:dealerselected,
      smpin:dseselected
    }

    this.server.post("managerenquiry/dashboard",req)
    .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.tomorrow_personal_visit=data.result.data.tomorrow.visit;
           this.tomorrow_personal_testride=data.result.data.tomorrow.testride;
           this.tomorrow_personal_call=data.result.data.tomorrow.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') {}
    });
  }
  //==
  getDashboardmonthbased(year, month){
   
    let dealerselected=LocalStorage.getValue("selecteddealervalue");
    let dseselected=LocalStorage.getValue("setdseselected");
    let amselected=LocalStorage.getValue("selectedamvalue");
    let rmselected=LocalStorage.getValue("selectedrmvalue");
    let zmselected=LocalStorage.getValue("selectedzmvalue");

    if(amselected != undefined){
     // this.selectedareamanager=areamanagerselected;
    }else{
      amselected='';
    }
   
    if(rmselected != undefined){
     // this.selectedregionalmanager=regionalmanagerselected;
    }else{
      rmselected='';
    }

    if(zmselected != undefined){
      // this.selectedregionalmanager=regionalmanagerselected;
     }else{
       zmselected='';
     }
 



    let req = {
      zmemail:zmselected,
      rmemail:rmselected,
      amemail:amselected,
      dealer_code:dealerselected,
      smpin:dseselected
    }


    this.server.post("managerenquiry/enquirysales/" + year + "/" + month + "",req)
    .then((data) => {
        if (data.status == 200 && data.result.response != 'failed') {
          
          //  this.pending_tasks=data.result.data;
          // console.log(this.pending_tasks.enquiry.target);
          //==
          this.stage_testride=data.result.data.stage.testride;
          this.stage_booked=data.result.data.stage.booked;
          this.stage_invoiced=data.result.data.stage.postponed;
          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') {}
    });
  }
  //==
  tasks_select(get_value){
    let dealerselected=LocalStorage.getValue("selecteddealervalue");
    let dseselected=LocalStorage.getValue("setdseselected");
    let amselected=LocalStorage.getValue("selectedamvalue");
    let rmselected=LocalStorage.getValue("selectedrmvalue");
    let zmselected=LocalStorage.getValue("selectedzmvalue");

  //  alert("this.selecteduserrole--->"+this.selecteduserrole);
  //  alert("dealerselected--->"+dealerselected);
  //  alert("dseselected--->"+dseselected);
  //  alert("amselected--->"+amselected);
  //  alert("rmselected--->"+rmselected);

    if(this.selecteduserrole == 7){ //AM
    if((dealerselected !='')&&(dseselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)){
      if(get_value==1){
        this.today_enable=true;
        this.tommorrow_enable=false;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==2){
        this.today_enable=false;
        this.tommorrow_enable=true;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==3){
        this.today_enable=false;
        this.tommorrow_enable=false;
        this.more_enable=true;
        this.router.navigateByUrl('manager-task/datevalentered');
      }
    }else{
      this.content_modal.dealerdseerrorpopup()
      //alert("Please select a dealer and corresponding dse");
    }
  }else if(this.selecteduserrole == 6){ //RM
    if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)){
     // alert("inside RM");
      if(get_value==1){
        this.today_enable=true;
        this.tommorrow_enable=false;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==2){
        this.today_enable=false;
        this.tommorrow_enable=true;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==3){
        this.today_enable=false;
        this.tommorrow_enable=false;
        this.more_enable=true;
        this.router.navigateByUrl('manager-task/datevalentered');
      }
    }else{
      this.content_modal.regionalmanagererrorpopup()
      //alert("Please select a dealer and corresponding dse");
    }
  }else if(this.selecteduserrole == 4){ //ZM
    if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)){
      if(get_value==1){
        this.today_enable=true;
        this.tommorrow_enable=false;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==2){
        this.today_enable=false;
        this.tommorrow_enable=true;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==3){
        this.today_enable=false;
        this.tommorrow_enable=false;
        this.more_enable=true;
        this.router.navigateByUrl('manager-task/datevalentered');
      }
    }else{
      this.content_modal.zonalmanagererrorpopup()
      //alert("Please select a dealer and corresponding dse");
    }
  }else if(this.selecteduserrole == 5){ //TRAINER
    if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)){
      if(get_value==1){
        this.today_enable=true;
        this.tommorrow_enable=false;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==2){
        this.today_enable=false;
        this.tommorrow_enable=true;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==3){
        this.today_enable=false;
        this.tommorrow_enable=false;
        this.more_enable=true;
        this.router.navigateByUrl('manager-task/datevalentered');
      }
    }else{
      this.content_modal.zonalmanagererrorpopup()
      //alert("Please select a dealer and corresponding dse");
    }
  }else if(this.selecteduserrole == 3){ //HO
    if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(zmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)&&(zmselected !=undefined)){
      if(get_value==1){
        this.today_enable=true;
        this.tommorrow_enable=false;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==2){
        this.today_enable=false;
        this.tommorrow_enable=true;
        this.more_enable=false;
        this.getDashboardstatic();
      }else if(get_value==3){
        this.today_enable=false;
        this.tommorrow_enable=false;
        this.more_enable=true;
        this.router.navigateByUrl('manager-task/datevalentered');
      }
    }else{
      this.content_modal.homanagererrorpopup()
      //alert("Please select a dealer and corresponding dse");
    }
  }

  }

  todaysTask(tempvalue){
   
    let dealerselected=LocalStorage.getValue("selecteddealervalue");
    let dseselected=LocalStorage.getValue("setdseselected");
    let amselected=LocalStorage.getValue("selectedamvalue");
    let rmselected=LocalStorage.getValue("selectedrmvalue");
    let zmselected=LocalStorage.getValue("selectedzmvalue");

    if(this.selecteduserrole == 7){ //AM
      if((dealerselected !='')&&(dseselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)){
        this.router.navigateByUrl('manager-task/'+tempvalue);
      }else{
        this.content_modal.dealerdseerrorpopup()
        //alert("Please select a dealer and corresponding dse");
      }
    }else if(this.selecteduserrole == 6){
      if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)){
        this.router.navigateByUrl('manager-task/'+tempvalue);
      }else{
        this.content_modal.regionalmanagererrorpopup()
        //alert("Please select a dealer and corresponding dse");
      }
    }else if(this.selecteduserrole == 4){
      if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)){
        this.router.navigateByUrl('manager-task/'+tempvalue);
      }else{
        this.content_modal.zonalmanagererrorpopup()
        //alert("Please select a dealer and corresponding dse");
      }
    }else if(this.selecteduserrole == 3){
      if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(zmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)&&(zmselected !=undefined)){
        this.router.navigateByUrl('manager-task/'+tempvalue);
      }else{
        this.content_modal.homanagererrorpopup()
        //alert("Please select a dealer and corresponding dse");
      }
    }else if(this.selecteduserrole == 5){
      if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)){
        this.router.navigateByUrl('manager-task/'+tempvalue);
      }else{
        this.content_modal.zonalmanagererrorpopup()
        //alert("Please select a dealer and corresponding dse");
      }
    }
    
  }
  todaysTaskpending(tempvalue){
    let dealerselected=LocalStorage.getValue("selecteddealervalue");
    let dseselected=LocalStorage.getValue("setdseselected");
  
    if((dealerselected !='')&&(dseselected !='')){
      this.router.navigateByUrl('manager-task/'+tempvalue);
    }else{
      alert("Please select a dealer and corresponding dse");
    }
  }
  clicktestride(){
    //alert("33")
    this.router.navigateByUrl('manager-testride');
  }
  clickplanner(){
    this.router.navigateByUrl('manager-activity-planner');
  }
  clickenquirystage(tempvalue){
    this.router.navigateByUrl('manager-enquiry-stage/'+tempvalue);
  }
  //==
  clickenquirystatus(tempvalue){
    this.router.navigateByUrl('manager-enquiry-status/'+tempvalue);
  }
  tomorrowtask(tempvalue){
    ///alert("tempvalue---->"+tempvalue);
    this.router.navigateByUrl('manager-task/'+tempvalue);
  }

  public relaodtaskstatus(){
   // console.log('this.selecteduserrole-->',this.selecteduserrole)
    let dealerselected=LocalStorage.getValue("selecteddealervalue");
    let dseselected=LocalStorage.getValue("setdseselected");
    let amselected=LocalStorage.getValue("selectedamvalue");
    let rmselected=LocalStorage.getValue("selectedrmvalue");
    let zmselected=LocalStorage.getValue("selectedzmvalue");

    if(this.selecteduserrole == 7){ //AM
      if((dealerselected !='')&&(dseselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)){
        let yrval = LocalStorage.getValue('yearvalueinitial');
        let mhval =   LocalStorage.getValue('monthvalueinitial');
        this.getDashboardmonthbased(yrval,mhval);
        this.getDashboardstatic();
      }else{
        this. resetrightpanel()
      }
    }else if(this.selecteduserrole == 6){ //RM
      console.log('dealerselected-->',dealerselected)
      console.log('dseselected-->',dseselected)
      console.log('amselected-->',amselected)
      if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)){
        let yrval = LocalStorage.getValue('yearvalueinitial');
        let mhval =   LocalStorage.getValue('monthvalueinitial');
        this.getDashboardmonthbased(yrval,mhval);
        this.getDashboardstatic();
      }else{
        this. resetrightpanel()
      }
    }else if(this.selecteduserrole == 4){ //ZM
      if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)){
        let yrval = LocalStorage.getValue('yearvalueinitial');
        let mhval =   LocalStorage.getValue('monthvalueinitial');
        this.getDashboardmonthbased(yrval,mhval);
        this.getDashboardstatic();
      }else{
        this. resetrightpanel()
      }
    }else if(this.selecteduserrole == 5){ //TRAINER
      if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)){
        let yrval = LocalStorage.getValue('yearvalueinitial');
        let mhval =   LocalStorage.getValue('monthvalueinitial');
        this.getDashboardmonthbased(yrval,mhval);
        this.getDashboardstatic();
      }else{
        this. resetrightpanel()
      }
    }else if(this.selecteduserrole == 3){ //HO
      // alert("dealerselected-->"+dealerselected);
      // alert("dseselected-->"+dseselected);
      // alert("amselected-->"+amselected);
      // alert("rmselected-->"+rmselected);
      // alert("zmselected-->"+zmselected);
      if((dealerselected !='')&&(dseselected !='')&&(amselected !='')&&(rmselected !='')&&(zmselected !='')&&(dealerselected !=undefined)&&(dseselected !=undefined)&&(amselected !=undefined)&&(rmselected !=undefined)&&(zmselected !=undefined)){
        let yrval = LocalStorage.getValue('yearvalueinitial');
        let mhval =   LocalStorage.getValue('monthvalueinitial');
        this.getDashboardmonthbased(yrval,mhval);
        this.getDashboardstatic();
      }else{
        this. resetrightpanel()
      }
    }
  }

  resetrightpanel(){
    this.today_personal_visit='';
    this.today_personal_testride='';
    this.today_personal_call='';
    this.today_pending_visit='';
    this.today_pending_testride='';
    this.today_pending_call='';
    this.tomorrow_personal_visit='';
    this.tomorrow_personal_testride='';
    this.tomorrow_personal_call='';
    this.stage_testride='';
    this.stage_booked='';
    this.stage_invoiced='';
    this.stage_lost='';
    this.status_hot='';
    this.status_warm='';
    this.status_cold='';
  }
  
}