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


@Component({
  selector: 'app-taskstatus-dealer',
  templateUrl: './taskstatus-dealer.component.html',
  styleUrls: ['./taskstatus-dealer.component.scss']
})
export class TaskstatusDealerComponent implements OnInit {


  @ViewChild(EnquirypopupDealerComponent) content_modal: EnquirypopupDealerComponent;

  today_personal_visit:any;
  today_personal_testride:any;
  today_personal_call:any;
  today_pending_visit:any;
  today_pending_testride:any;
  today_pending_call: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;

  constructor( private server: HyperService, private router: Router,private route: ActivatedRoute) { }
 

  
 
  ngOnInit() {


    let smpinvalue=LocalStorage.getValue('setdseselected')
    this.smpinselected = smpinvalue

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

    this.smpinselected=LocalStorage.getValue('setdseselected');
 
    this.href = this.router.url;
    let check=LocalStorage.getValue("activity_check");
    if(check=='activity_check'){
      this.displayboolean=true;
    }else{
      this.displayboolean=false;
    }
 
    if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
      this.getDashboardstatic();
      let yrval = LocalStorage.getValue('yearvalueinitial');
      let mhval =   LocalStorage.getValue('monthvalueinitial');
      this.getDashboardmonthbased(yrval,mhval);
    }else{
     this.resetrightpanel();
    }
   
    
  }
  public getmonthbaseddetails(year,month,urlvalue){
    if(urlvalue == 'dealer-myenquiries'){
      this.displayboolean=false;
    }else{
      this.displayboolean=true;
    }
  
  }
  //===
  getDashboardstatic() {
    let smpinvalue=LocalStorage.getValue('setdseselected')
    this.smpinselected = smpinvalue
   // alert("this.smpinselected--->"+this.smpinselected)
    
    this.server.get("dealerenquiry/dashboard/"+this.smpinselected)
    .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){
   
   // alert("getDashboardmonthbased ---taskstatus--> ");
    // this.server.get("enquiry/enquirysales/2019/02")
    this.server.get("dealerenquiry/enquirysales/" + this.smpinselected + "/"  + year + "/" + month + "")
    .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;
          //==
        }else if (data.result.status == 406 ) {
              //alert(data.result.message)
        }
         else if (data.result.status == 401 && data.result.response == 'failed') {}
    });
  }
  //==
  tasks_select(get_value){
    this.smpinselected=LocalStorage.getValue('setdseselected');
     if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
    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('dealer-task/datevalentered');
    }
  }else{
    this.content_modal.dealererrorpopup();
  }
  }

  todaysTask(tempvalue){
   this.smpinselected=LocalStorage.getValue('setdseselected');
    if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
     
    this.router.navigateByUrl('dealer-task/'+tempvalue);
    }else{
      this.content_modal.dealererrorpopup();
    }
  }
  todaysTaskpending(tempvalue){
    this.smpinselected=LocalStorage.getValue('setdseselected');
     if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
    this.router.navigateByUrl('dealer-task/'+tempvalue);
     }else{
      this.content_modal.dealererrorpopup();
     }
  }
  clicktestride(){
    this.smpinselected=LocalStorage.getValue('setdseselected');
     if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
    //alert("33")
    this.router.navigateByUrl('dealer-testride');
     }else{
      this.content_modal.dealererrorpopup();
     }
  }
  clickplanner(){
    this.smpinselected=LocalStorage.getValue('setdseselected');
     if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
    this.router.navigateByUrl('dealer-activity-planner');
     }else{
      this.content_modal.dealererrorpopup();
     }
  }
  clickenquirystage(tempvalue){
    this.smpinselected=LocalStorage.getValue('setdseselected');
    // console.log("setdseselected--->",this.smpinselected);
     if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
    this.router.navigateByUrl('dealer-enquiry-stage/'+tempvalue);
     }else{
      this.content_modal.dealererrorpopup();
     }
  }
  //==
  clickenquirystatus(tempvalue){
    this.smpinselected=LocalStorage.getValue('setdseselected');
    // console.log("setdseselected--->",this.smpinselected);
     if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
        this.router.navigateByUrl('dealer-enquiry-status/'+tempvalue);

     }else{
      this.content_modal.dealererrorpopup();
     }
  }
  tomorrowtask(tempvalue){
    this.smpinselected=LocalStorage.getValue('setdseselected');
    // console.log("setdseselected--->",this.smpinselected);
     if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
    this.router.navigateByUrl('dealer-task/'+tempvalue);
     }else{
      this.content_modal.dealererrorpopup();
     }
  }

  public relaodtaskstatus(){
   // alert("relaodtaskstatus---taskstatus>"+this.smpinselected);

    this.smpinselected=LocalStorage.getValue('setdseselected');
    // console.log("setdseselected--->",this.smpinselected);
    if((this.smpinselected !='')&&(this.smpinselected !=undefined)&&(this.smpinselected !='all')){
      
       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='';
  }
  
}
