import { Component, OnInit, ViewChild } from '@angular/core';
import { LocalStorage } from '../../core/services/local_storage.service';
import { CONFIG } from '../../config';
import { HyperService } from '../../core/services/http.service';
import { Router } from "@angular/router";
import { ConfirmPopupComponent } from '../../sales_executive/confirm-popup/confirm-popup.component';

@Component({
    selector: 'app-main-header',
    templateUrl: './main-header.component.html',
    styleUrls: ['./main-header.component.scss']
})
export class MainHeaderComponent implements OnInit {
    is_modal_visible: boolean = false;
    logged_in: boolean;
    userData: any;
    menu_item: number;
    is_firsttime: boolean;
    notification_data: any;
    isCollapsed = true;
    badge: number;
    notification_total: number;
    user_type: any;
    user_role: number;
    access_token: any;
    @ViewChild(ConfirmPopupComponent) confirm_modal: ConfirmPopupComponent;
    show_emi_calculator: boolean;
    constructor(private server: HyperService, private router: Router) {
        if (LocalStorage.getValue('user_data') != undefined) {
            this.user_role = LocalStorage.getValue('user_data').role;
            // alert(this.user_role)
        }
        //  console.log(this.user_role)
        if (LocalStorage.getValue('user_data') != undefined && (LocalStorage.getValue('user_data').role <= 7 || LocalStorage.getValue('user_data').role == 14)) {
            this.user_type = 1;//Manager
        //     console.log(this.user_role)
        }
        else if (LocalStorage.getValue('user_data') != undefined && LocalStorage.getValue('user_data').role >= 7) {
            this.user_type = 0;//Sales Ex
        }
    }

    ngOnInit() {
       // console.log("this.router.url---->",this.router.url);
        if (LocalStorage.getValue('notificationBadge') != undefined) {
            this.badge = LocalStorage.getValue('notificationBadge');
        }
        this.is_firsttime = LocalStorage.getValue('is_firsttime');
        // console.log(CONFIG._active_url)
        ///// Page refresh menu active function ////////////////////////////
        if (CONFIG._active_url.indexOf("mycontent") >= 0 || CONFIG._active_url.indexOf("/mycontent") >= 0 || CONFIG._active_url.indexOf("/mycontent") >= 0 || CONFIG._active_url.indexOf("/analyticalreport") >= 0) {
            this.menu_item = 1;
        } else if (CONFIG._active_url.indexOf("assessment") >= 0 || CONFIG._active_url.indexOf("/assessment") >= 0 || CONFIG._active_url.indexOf("/assessment") >= 0) {
       //   console.log('innnn')
            this.menu_item = 2;
        } else if (CONFIG._active_url.indexOf("leaderboard") >= 0) {
            this.menu_item = 3;
        } else if (CONFIG._active_url.indexOf("feedback") >= 0) {
            this.menu_item = 4;
        } else if (CONFIG._active_url.indexOf("bulletin") >= 0) {
            this.menu_item = 5;
        } else if (CONFIG._active_url.indexOf("/analyticalreport") >= 0) {
            this.menu_item = 6;
        } else if (CONFIG._active_url.indexOf("training") >= 0) {
            this.menu_item = 11;
        }else if (CONFIG._active_url.indexOf("trainer") >= 0) {
            this.menu_item = 13;
        } else if (CONFIG._active_url.indexOf("threesixty-dashboard") >= 0) {
            this.menu_item = 12;
        }else if (CONFIG._active_url.indexOf("myschedules") >= 0) {
            this.menu_item = 14;
        }
        this.logged_in = LocalStorage.getValue('loggedIn');
        if (this.logged_in == true) {
            this.userData = LocalStorage.getValue('user_data');
            this.getNotification();
        }
        this.access_token = LocalStorage.getValue('token')
    //    console.log(CONFIG._active_url)
        if(this.user_type==1){
            if(this.router.url=='/assessment'){
                this.menu_item = 2;
            }else{

                CONFIG._active_url=''
            }
        }
        this.checkemirole();
        //||(this.router.url=="/enquiry-report")   
        
        var final = this.router.url.substr(this.router.url.lastIndexOf('/') + 1);
           // alert(final)
        if((this.router.url=="/myenquiries")||(this.router.url=="/task/today")||(this.router.url=="/create-enquiry")||(this.router.url=="/add-plan")||(this.router.url=="/nonsyncenquiry")||(this.router.url=="/task/today-visit")||(this.router.url=="/task/today-call")||(this.router.url=="/task/today-testride")||(this.router.url=="/task/pending-visit")||(this.router.url=="/task/pending-call")||(this.router.url=="/task/pending-testride")||(this.router.url=="/enquiry-status/hot")||(this.router.url=="/enquiry-status/warm")||(this.router.url=="/enquiry-status/cold")||(this.router.url=="/quotation/back")||(this.router.url=="/enquiry-status/lost")||(this.router.url=="/enquiry-stage/booked")||(this.router.url=="/enquiry-stage/invoiced")||(this.router.url=="/enquiry-stage/lost")||(this.router.url=="/enquiry-stage/postponed")||(this.router.url=="/emi-calculator/web")||(this.router.url=="/activityplanner")||(this.router.url=="/testride")||(this.router.url=="/action-history")||(this.router.url=="/price-list")||(this.router.url=="/enquiry-profile")||(this.router.url=="/quotation")||(this.router.url=="/dealer-quotation")||(this.router.url=="/dealer-nonsyncenquiry")||(this.router.url=="/dealer-quotation/data")||(this.router.url=="/dealer-emi-calculator")||(this.router.url=="/dealer-emi-calculator/quote-emi-quote")||(this.router.url=="/dealer-create-enquiry/back")||(this.router.url=="/activity-today")||(this.router.url=="/quotation/data")||(this.router.url=="/create-enquiry/back")||(this.router.url=="/activity-list/"+final+"")||(this.router.url=="/nonsyncenquiry")||(this.router.url=="/create-enquiry/non-sync")||(this.router.url=="/emi-calculator")||(this.router.url=="/emi-calculator/quote-emi") || (this.router.url=="emi-calculator/quote-emi-quote")|| (this.router.url=="/sales-dse")|| (this.router.url=="/enquiry-dse")|| (this.router.url=="/dealer-enquiry")|| (this.router.url=="/dealer-task/tomorrow-testride")|| (this.router.url=="/dealer-task/tomorrow-visit")||(this.router.url=="/dealer-task/today-call")||(this.router.url=="/dealer-create-enquiry")||(this.router.url=="/dealer-task/today-testride")|| (this.router.url=="/dealer-task/tomorrow-call")|| (this.router.url=="/dealer-sales")||(this.router.url=="/dealer-myenquiries")||(this.router.url=="/dealer-task/today-visit")||(this.router.url=="/dealer-task/pending-visit")||(this.router.url=="/dealer-task/pending-testride")||(this.router.url=="/dealer-task/pending-call")||(this.router.url=="/dealer-testride")||(this.router.url=="/dealer-action-history")||(this.router.url=="/dealer-activity-planner")||(this.router.url=="/dealer-task/datevalentered")||(this.router.url=="/dealer-enquiry-profile")||(this.router.url=="/dealer-enquiry-status/hot")||(this.router.url=="/dealer-enquiry-status/warm")||(this.router.url=="/dealer-activityday-link/"+final+"")||(this.router.url=="/dealer-enquiry-status/cold")||(this.router.url=="/dealer-enquiry-stage/invoiced")||(this.router.url=="/dealer-enquiry-stage/postponed")||(this.router.url=="/dealer-enquiry-stage/lost")||(this.router.url=="/dealer-enquiry-stage/booked")||(this.router.url=="/dealer-pricelist")||(this.router.url=="/dse-user-list")||(this.router.url=="/task/tomorrow-visit")||(this.router.url=="/task/tomorrow-testride")||(this.router.url=="/task/tomorrow-call")||(this.router.url=="/task/datevalentered")||(this.router.url=="/manager-enquiry")||(this.router.url=="/manager-task")||(this.router.url=="/regional-enquiry")||(this.router.url=="/zonal-enquiry")||(this.router.url=="/dealer-enquiry-report"))
        {
            
        }else{
            LocalStorage.setValue("pinvalue",'')
        }
       // console.log("pinpopup- headervalue-->",LocalStorage.getValue("pinvalue"))
    }

    checkemirole(){
     
        if(this.user_role>=7 && this.user_role!=14 && this.user_role!=16){
            this.show_emi_calculator = true;
        }else{
            this.show_emi_calculator = false; 
        }
    }
    gotoThreesixty() {
      LocalStorage.setValue("from_menu", true);
      // LocalStorage.setValue("set_model_id",   "42");
      // setTimeout(() => {
      //   // LocalStorage.setValue("set_model_id",  LocalStorage.getValue("temp_set_model_id"));
      // }, 200);

    }
    onLogOut() {
        // this.is_modal_visible = false;
        // let msg = "Are you sure want to logout?"
        this.confirm_modal.showconfirmModalLogout('confirm', {}, (data)=> {
            // this.is_modal_visible = true;
          })
        // LocalStorage.createJWT();
        // this.router.navigateByUrl('/');
    }

    viewNotification(list, index) {
    //     console.log(list)
        this.readNotification(list.id, index);
    //    console.log(this.user_type)
        if (this.user_type == 0) {
            if (list.notificationtype == 1) {
                this.router.navigateByUrl('mytraining');
            } else if (list.notificationtype == 2) {
                this.router.navigateByUrl('myassessment');
            } else if (list.notificationtype == 3) {
                this.router.navigateByUrl('feedback');
            } else if (list.notificationtype == 4) {
                this.router.navigateByUrl('bulletin');
            } else if (list.notificationtype == 7) {
                this.router.navigateByUrl('wallet');
            }else if (list.notificationtype == 9) {
                this.router.navigateByUrl('myschedules');
            }else if (list.notificationtype == 10) {
                this.router.navigateByUrl('notificationtraining/'+list.notificationdata.schedule_id);
            }else if (list.notificationtype == 11) {
                this.router.navigateByUrl('trainer');
                // this.router.navigateByUrl('trainingfeedback/'+list.notificationdata.assessmentid);
            }else if (list.notificationtype == 12) {
                this.router.navigateByUrl('trainer');
            }
            
        } else {
            // alert("else")
            // if (list.notificationtype == 5) {
            //     this.router.navigateByUrl('setsalesreport');
            // }
            if (list.notificationtype == 8) {
                this.router.navigateByUrl('analyticalreport');
            }else if (list.notificationtype == 9) {
                this.router.navigateByUrl('myschedules');
            }else if (list.notificationtype == 10) {
                this.router.navigateByUrl('notificationtraining/'+list.notificationdata.schedule_id);
            }else if (list.notificationtype == 11) {
                this.router.navigateByUrl('trainer');
                // this.router.navigateByUrl('trainingfeedback/'+list.notificationdata.assessmentid);
            }else if (list.notificationtype == 12) {
                this.router.navigateByUrl('trainer');
            }else if (list.notificationtype == 5 && this.user_role==14) {
                this.router.navigateByUrl('dse-user-list');
            }else if (list.notificationtype == 5 && this.user_role==7) {
                this.router.navigateByUrl('manager-dselist');
            }
        }

    }

    readNotification(id, index) {
        if (this.notification_data[index].status != 1) {
            this.server.get("notification/read_notification/" + id + "")
            .then((data) => {
                if (data.status == 200 && data.result.response == "success") {
                    this.badge -= 1;
                    this.notification_data[index].status = 1;
                    LocalStorage.setValue('notificationBadge', this.badge);
                } else if (data.result.status == 401 && data.result.response == 'failed') {
                    this.router.navigateByUrl('');
                } else if (data.result.response == "failed") {

                } else if (data.status == 500) {
                    alert("Please check your internet connection");
                } else {
                    alert("Please check your internet connection");
                }
            })
        }
    }

    deleteNotification(data, index) {
        this.server.get("notification/remove_notification/" + data.id + "")
            .then((data) => {
                if (data.status == 200 && data.result.response == "success") {
                    this.notification_data[index].status = 1;
                    this.notification_data.splice(index, 1);
                    if (data.result.data != undefined && data.result.data != '' && data.result.data.is_first_login == 1) {
                        this.router.navigateByUrl('setpassword');
                    } else if (data.result.data != undefined && data.result.data != '' && data.result.data.is_first_login != 1) {
                        this.router.navigateByUrl('dashboard');
                    }
                } else if (data.result.response == "failed") {

                } else if (data.status == 500) {
                    alert("Please check your internet connection");
                } else {
                    alert("Please check your internet connection");
                }
            })
    }



    getNotification() {
        this.badge = null;
        this.server.get("notification")
            .then((data) => {
                if (data.status == 200 && data.result.response != 'failed') {
                    this.notification_data = data.result.data;
                    this.notification_total = data.result.total;
                    // console.log(this.notification_total)
                    this.badge = data.result.not_read_count;
                    LocalStorage.setValue('notificationBadge', data.result.not_read_count)
                    // console.log('Notification', this.notification_data)
                } else if (data.result.status == 401 && data.result.response == 'failed') {
                    this.router.navigateByUrl('');
                }
            });
    }


    viewAnalyticalReport() {
        // this.router.navigateByUrl('asessmentleaderboardreport/web/'+this.access_token);
        this.router.navigateByUrl('analyticalreport/web/' + this.access_token);
    }
    viewRewardReport() {
        this.router.navigateByUrl('rewardreport/web/' + this.access_token);
    }
    viewCourseReport() {
        this.router.navigateByUrl('coursereport/web/' + this.access_token);
    }
    viewFeedbackReport() {
        this.router.navigateByUrl('feedbackreport/web/' + this.access_token);
    }
    viewSetSalesReport() {
        this.router.navigateByUrl('setsalesreport/web/' + this.access_token);
    }
    open(){
        // const amazingTimePicker = this.atp.open();
        // amazingTimePicker.afterClose().subscribe(time => {
        //   console.log(time);
        // });
    }
    add(){
        this.router.navigateByUrl('addtraining');
    }
}
