"use strict";

app.controller("globalCtrl", ["$scope", "authManager", "$location", "vServices", "$state", "jwtHelper", function ($scope, authManager, $location, vServices, $state, jwtHelper) {
    // assign and initialization of the variables
    $scope.$root.count = { Employees: null, Managers: null, Pool: null, Projects: null, Accounts: null };
    $scope.emp_acls = [];
    $scope.$root.constant = {
        googleCaptchaSiteKey: "6LdT07QUAAAAAMCqn1oYHz8BJI1VCTooPoV4FjNq"
    };
    $scope.$root._empAll = [];
    $scope.$root.baseurl = "";
    $scope.$root.loading = true;
    $scope.$root.gUser = {
        emp_code: "",
        emp_department: null,
        emp_designation: null,
        emp_doj: "2015-01-01",
        emp_email: "guest@girikon.com",
        emp_first_name: "Guest",
        emp_gender: "Male",
        emp_last_name: null,
        emp_location: null,
        emp_marital_status: null,
        emp_middle_name: null,
        emp_mobile: null,
        emp_nick_name: "Guest",
        emp_pool: "0",
        emp_skype: null,
        emp_status: "active",
        emp_username: "guest@girikon.com",
        is_admin: null,
        profile_image: null,
        emp_country: null,
        diarytz: 'Asia/Kolkata'
    };
    $scope.$root.page = { title: "Project Management Tool" };
    $scope.$root.menus = [{
        "href": "/login",
        "name": "Login",
        hasIcon: true,
        icon: "fa-sign-in",
        hasBadge: false,
        badgeType: "",
        badgeCount: 0
    }];

    $scope.$root._loginAs = (heroid) => {
        if ($scope.$root.gUser && $scope.$root.gUser.emp_first_name === 'Admin') {
            vServices.callApi("auth/login_as", "POST", { emp_id: heroid }).then(function (res, err) {
                if (res && res.status) {
                    if (localStorage.getItem("id_token")) {
                        localStorage.removeItem("id_token");
                        localStorage.setItem("id_token", res.token);
                        $scope.$root.mainLeft = $scope.$root.isAuthenticated = true;
                        $scope.$root.updateMenu();
                        window.location.href = "/dashboard?token=" + Math.random();
                    }
                }
                else if (err) {
                    alert(err);
                }
            });
        }
    };

    // Dynamic variables
    $scope.$root.isAuthenticated = authManager.isAuthenticated();
    if ($scope.$root.isAuthenticated) {
        $scope.$root.gUser = jwtHelper.decodeToken(localStorage.getItem("id_token"));
        if ($scope.$root.gUser.emp_first_name === 'Admin' && $scope.$root._empAll && $scope.$root._empAll.length === 0) {
            vServices.callApi("employee/all", "GET", {}).then(empAll => {
                try {
                    $scope.$root._empAll = empAll.map(o => ({
                        employeeid: o.employeeid,
                        emp_first_name: o.emp_first_name,
                        emp_middle_name: o.emp_middle_name,
                        emp_last_name: o.emp_last_name,
                        emp_code: o.emp_code,
                        emp_email: o.emp_email
                    }));
                }
                catch (e) {
                    console.error(e);
                }
            });
        }

    }
    else {
        //$location.path("/login");
    }
    $scope.getAllNotfications = [];
    $scope.$root.notificationsToNow = (d) => moment(d).toNow(true);
    $scope.$root.getNotifications = () => {
        // $scope.$root.isAuthenticated && vServices.callApi("employee/notifications", "GET", {}).then(res => {
        //     $scope.$root.notifications = res;
        // });
        // Notifications
        if ($scope.$root.gUser.id != 1) {
            $scope.$root.isAuthenticated && vServices.callApi("notifications/notifyOverallAssessmentManagers?managerId=" + $scope.$root.gUser.id, "GET", {}).then(function (res) {
                if (res) {

                    $scope.getAllNotfications = [
                        // Overall Assessment Notifications
                        {
                            'count': res[0].count,
                            'message': res[0].n_label,
                            'link': res[0].n_url,
                            'icon': res[0].n_icon
                        }
                    ];
                }
            });
            $scope.$root.isAuthenticated && vServices.callApi("notifications/notifyGoals?managerId=" + $scope.$root.gUser.id, "GET", {}).then(function (res) {
                if (res) {

                    $scope.getAllNotfications.push({
                        // Goals Notifications
                        'count': res[0].count,
                        'message': res[0].n_label,
                        'link': res[0].n_url,
                        'icon': res[0].n_icon
                    });
                }
            });
        }
    }

    $scope.$root.getNotifications();
    setInterval($scope.$root.getNotifications, 360000);

    $scope.$root.loading = false;
    $scope.$root.logout = function () {
        if (localStorage.getItem("id_token")) {
            localStorage.removeItem("id_token");
            $location.path("/login");
            $scope.$root.isAuthenticated = false;
            $scope.$root.updateMenu();
        }
    };

    $scope.$root.fullscreen = {
        isEnable: document.fullscreenEnabled,
        fullscreen: () => {
            document.documentElement.requestFullscreen();
            $scope.$root.fullscreen.isFullscreen = true;
        },
        exitFullscreen: () => {
            document.exitFullscreen();
            $scope.$root.fullscreen.isFullscreen = false;
        },
        isFullscreen: false
    };

    $scope.$root.myAccess = {};
    $scope.$root.updateMenu = function () {
        try {
            if (localStorage.getItem("id_token")) {
                $scope.$root.gUser = jwtHelper.decodeToken(localStorage.getItem("id_token"));

                vServices.callApi("auth/count/", "GET", {}).then(function (res) {
                    //Color = "primary", "success", "warning", "danger", "indigo", "navy", "purple", "maroon", "lime", "teal", "olive", "orange", "fuchsia"
                    if (res) {
                        $scope.$root.count = res;
                        $scope.$root.menus = [];
                        let setting = [];
                        let assessment = [];
                        let reports = [];
                        let goals = [];
                        let watchlist = [];
                        let shoutout = [];
                        const acls = "," + $scope.$root.gUser.emp_acls + ",";
                        $scope.emp_acls = acls;
                        let forClearCache = Math.random();

                        const isAdmin = $scope.emp_acls.includes(",27,");
                        const isSystemUser = $scope.emp_acls.includes(",97,");

                        $scope.$root.gUser.emp_acls = "";

                        if (!isSystemUser) {
                            // My Profile Menu Start
                            if (!isAdmin) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/my/skills/add",
                                    type: "bg-gradient-success",
                                    name: "My Skills",
                                    hasIcon: true,
                                    icon: "fa-id-badge",
                                    iconText: "text-success",
                                    hasBadge: false,
                                    badgeType: "badge-success"
                                });
                            }
                            if (!isAdmin) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/my/profile/add",
                                    type: "bg-gradient-success",
                                    name: "My Profile",
                                    hasIcon: true,
                                    icon: "fa-image",
                                    iconText: "text-success",
                                    hasBadge: false,
                                    badgeType: "badge-success"
                                });
                            }
                            // $scope.$root.menus.push({href: $scope.$root.baseurl + "/my/certificate/add", type: "bg-gradient-success", name: "My Certificates", hasIcon: true, icon: "fa-id-badge", iconText: "text-success", hasBadge: false, badgeType: "badge-success"});
                            if (!isAdmin) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/mycertificates/list",
                                    type: "bg-gradient-success",
                                    name: "My Certificates",
                                    hasIcon: true,
                                    icon: "fa-id-badge",
                                    iconText: "text-success",
                                    hasBadge: false,
                                    badgeType: "badge-success"
                                });
                            }
                            if (!isAdmin) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/my/projects/list",
                                    type: "bg-gradient-success",
                                    name: "My Projects",
                                    hasIcon: true,
                                    icon: "fa-list-alt",
                                    iconText: "text-success",
                                    hasBadge: true,
                                    badgeType: "badge-success",
                                    badgeCount: $scope.$root.count.MyProjects
                                });
                            }
                            if (!isAdmin) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/my/teams/list",
                                    type: "bg-gradient-maroon",
                                    name: "My Teams",
                                    hasIcon: true,
                                    icon: "fa-user",
                                    iconText: "text-maroon",
                                    hasBadge: true,
                                    badgeType: "bg-maroon",
                                    badgeCount: $scope.$root.count.MyTeam
                                });
                            }
                            if ($scope.emp_acls.includes(",104,")) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/heroes/my-team-skills",
                                    type: "bg-gradient-maroon",
                                    name: "My Team Skills",
                                    hasIcon: true,
                                    icon: "fa-user",
                                    iconText: "text-maroon",
                                    hasBadge: true,
                                    badgeType: "bg-maroon",
                                    badgeCount: $scope.$root.count.MyTeam
                                });
                            }
                            if (!isAdmin) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/mydocument/list",
                                    type: "bg-gradient-success",
                                    name: "My Documents",
                                    hasIcon: true,
                                    icon: "fa-id-badge",
                                    iconText: "text-success",
                                    hasBadge: false,
                                    badgeType: "badge-success"
                                });
                            }

                            if (!isAdmin) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/my-diary/add-clone",
                                    type: "bg-gradient-olive",
                                    name: "My Diary",
                                    hasIcon: true,
                                    icon: "fa-book",
                                    iconText: "text-olive",
                                    hasBadge: false,
                                    badgeType: "bg-olive",
                                    badgeCount: 0
                                });
                            }
                            // For testing dev URL
                            if (!isAdmin && ($scope.$root.gUser.id == 488 || $scope.$root.gUser.id == 162 || $scope.$root.gUser.id == 111 || $scope.$root.gUser.id == 368)) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/my-diary/add-clone",
                                    type: "bg-gradient-olive",
                                    name: "My Diary (DEV)",
                                    hasIcon: true,
                                    icon: "fa-book",
                                    iconText: "text-olive",
                                    hasBadge: false,
                                    badgeType: "bg-olive",
                                    badgeCount: 0
                                });
                            }
                            /*27 Jan 2023 Created By Suresh Yadav [This is Use for Manage Newsletter] */
                            if (!isAdmin) {
                                $scope.$root.menus.push({
                                    href: $scope.$root.baseurl + "/newsletters/archivelist",
                                    type: "",
                                    name: "Quick Links",
                                    hasIcon: true,
                                    icon: "fa fa-envelope",
                                    hasBadge: false,
                                    badgeType: "bg-indigo",
                                    badgeCount: $scope.$root.count.tip
                                });
                            }
                        }


                        if (isAdmin || acls.includes(",13,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/accounts/list",
                                type: "bg-gradient-primary",
                                name: "Account",
                                hasIcon: true,
                                icon: "fa-user-secret",
                                hasBadge: true,
                                badgeType: "badge-primary",
                                badgeCount: $scope.$root.count.Accounts
                            });
                        }
                        if (isAdmin || acls.includes(",15,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/projects/list",
                                type: "bg-gradient-success",
                                name: "Projects",
                                hasIcon: true,
                                icon: "fa-table",
                                hasBadge: true,
                                badgeType: "badge-success",
                                badgeCount: $scope.$root.count.Projects
                            });
                        }
                        if (isAdmin || acls.includes(",98,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/contacts/list",
                                type: "bg-gradient-indigo",
                                name: "Contact",
                                hasIcon: true,
                                icon: "fa-envelope",
                                hasBadge: true,
                                badgeType: "bg-indigo",
                                badgeCount: $scope.$root.count.Contacts
                            });
                        }
                        if (isAdmin || acls.includes(",14,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/heroes/list",
                                type: "bg-gradient-warning",
                                name: "Heroes",
                                hasIcon: true,
                                icon: "fa-users",
                                hasBadge: true,
                                badgeType: "badge-warning",
                                badgeCount: $scope.$root.count.Employees
                            });
                        }
                        if (isAdmin || acls.includes(",44,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/heroes/project/timeline/0",
                                type: "bg-gradient-warning",
                                name: "Hero's Timeline",
                                hasIcon: true,
                                icon: "fa-chart-line",
                                hasBadge: false,
                                badgeType: "badge-warning",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",45,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/projects/timeline",
                                type: "bg-gradient-warning",
                                name: "Project's Timeline",
                                hasIcon: true,
                                icon: "fa-chart-bar",
                                hasBadge: false,
                                badgeType: "badge-warning",
                                badgeCount: 0
                            });
                        }

                        //Goal Menu Start
                        if (!isAdmin && !isSystemUser) {
                            goals.push({
                                href: $scope.$root.baseurl + "/my/goals",
                                type: "bg-gradient-warning",
                                name: "My Goals",
                                hasIcon: true,
                                icon: "fa-bullseye",
                                iconText: "text-warning",
                                hasBadge: false,
                                badgeType: "badge-warning"
                            });
                        }
                        if (isAdmin || acls.includes(",61,")) {
                            goals.push({ href: $scope.$root.baseurl + "/my/addgoalbytemplate", type: "", name: "Add Goals", hasIcon: true, icon: "fa-cog", hasBadge: false, badgeType: "bg-indigo", badgeCount: 0 });
                            goals.push({
                                href: $scope.$root.baseurl + "/goal-dashboard/0",
                                type: "bg-gradient-warning",
                                name: "Goal Dashboard",
                                hasIcon: true,
                                icon: "fa-bullseye",
                                iconText: "text-warning",
                                hasBadge: false,
                                badgeType: "badge-warning"
                            });
                            goals.push({
                                href: $scope.$root.baseurl + "/goal-dashboard/add/",
                                type: "bg-gradient-warning",
                                name: "Assign Goals",
                                hasIcon: true,
                                icon: "fa-bullseye",
                                iconText: "text-warning",
                                hasBadge: false,
                                badgeType: "badge-warning"
                            });
                            goals.push({
                                href: $scope.$root.baseurl + "/my/goalTemplateDetail",
                                type: "bg-gradient-warning",
                                name: "Goals Report",
                                hasIcon: true,
                                icon: "fa-bullseye",
                                iconText: "text-warning",
                                hasBadge: false,
                                badgeType: "badge-warning"
                            });
                        }
                        if (isAdmin || acls.includes(",61,")) {
                            $scope.$root.menus.push({ href: "#", type: "", name: "Goals", hasIcon: true, icon: "fa-bullseye", hasBadge: false, iconText: "text-warning", badgeType: "", badgeCount: 0, isparent: true, child: goals });
                        }
                        else {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/my/goals",
                                type: "bg-gradient-warning",
                                name: "My Goals",
                                hasIcon: true,
                                icon: "fa-bullseye",
                                iconText: "text-warning",
                                hasBadge: false,
                                badgeType: "badge-warning"
                            });
                        }
                        //Goal Menu End

                        if (isAdmin || acls.includes(",20,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/managers/list",
                                type: "bg-gradient-info",
                                name: "Managers",
                                hasIcon: true,
                                icon: "fa-user-circle",
                                hasBadge: true,
                                badgeType: "badge-info",
                                badgeCount: $scope.$root.count.Managers
                            });
                        }
                        if (isAdmin || acls.includes(",96,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/shoutouts/list",
                                type: "",
                                name: "Shout Out",
                                hasIcon: true,
                                icon: "fa-bullhorn",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: $scope.$root.count.tip
                            });
                        }
                        if (!isAdmin && !isSystemUser) {
                            $scope.$root.menus.push({ href: "#", type: "", name: "Shout Out", hasIcon: true, icon: "fa-bullhorn", hasBadge: false, badgeType: "0", badgeCount: 0, isparent: true, child: shoutout });
                        }
                        if (!isAdmin && !isSystemUser) {
                            shoutout.push({
                                href: $scope.$root.baseurl + "/shoutouts/heroshoutoutlist",
                                type: "",
                                name: "Shout Out List",
                                hasIcon: true,
                                icon: "fa-bullhorn",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: $scope.$root.count.tip
                            });
                        }
                        if (!isAdmin && !isSystemUser) {
                            shoutout.push({
                                href: $scope.$root.baseurl + "/shoutouts/herotoshoutoutlist",
                                type: "",
                                name: "Received Shout Out",
                                hasIcon: true,
                                icon: "fa-bullhorn",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: $scope.$root.count.tip
                            });
                        }

                        // Client Appriciation Menu Start
                        // if (isAdmin || acls.includes(",62,")) $scope.$root.menus.push({href: $scope.$root.baseurl + "/clientappreciation/list", type: "", name: "Client Appreciations", hasIcon: true, icon: "fas fa-thumbs-up", hasBadge: false, badgeType: "bg-indigo", badgeCount: $scope.$root.count.tip});
                        if (!isAdmin && !isSystemUser) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/clientappreciation/appreciationlist",
                                type: "",
                                name: "Log Client Appreciations",
                                hasIcon: true,
                                icon: "fas fa-thumbs-up",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: $scope.$root.count.tip
                            });
                        }
                        // Client Appriciation Menu End

                        if (isAdmin || acls.includes(",36,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/project-diary/add",
                                type: "bg-gradient-olive",
                                name: "Project Diary",
                                hasIcon: true,
                                icon: "fa-book",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }
                        // Assesment Menu Start
                        if (isAdmin || acls.includes(",57,")) {
                            assessment.push({
                                href: $scope.$root.baseurl + "/assessment/schedule",
                                type: "bg-gradient-blue",
                                name: "Create Assessment",
                                hasIcon: true,
                                icon: "fas fa-poll",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",57,")) {
                            assessment.push({
                                href: $scope.$root.baseurl + "/assessment/overall",
                                type: "bg-gradient-blue",
                                name: "Assign Overall Assessment",
                                hasIcon: true,
                                icon: "fas fa-poll",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",58,")) {
                            assessment.push({
                                href: $scope.$root.baseurl + "/assessment/admin",
                                type: "bg-gradient-success",
                                name: "View Assessment Question",
                                hasIcon: true,
                                icon: "fas fa-poll-h",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",56,")) {
                            // assessment.push({href: $scope.$root.baseurl + "/assessment/dashboard", type: "bg-gradient-blue", name: "Project Assessment Dashboard", hasIcon: true, icon: "fas fa-poll", iconText: "text-olive", hasBadge: false, badgeType: "bg-olive", badgeCount: 0});
                            assessment.push({
                                href: $scope.$root.baseurl + "/overallassessment/dashboard?" + forClearCache,
                                type: "bg-gradient-blue",
                                name: "Overall Assessment Dashboard ",
                                hasIcon: true,
                                icon: "fas fa-poll",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",59,")) {
                            assessment.push({
                                href: $scope.$root.baseurl + "/assessment",
                                type: "bg-gradient-success",
                                name: "Project Self Assessment",
                                hasIcon: true,
                                icon: "fas fa-poll-h",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }
                        $scope.$root.menus.push({ href: "#", type: "", name: "Assessment", hasIcon: true, icon: "fa-tasks", hasBadge: false, iconText: "text-primary", badgeType: "", badgeCount: 0, isparent: true, child: assessment });
                        assessment.push({
                            href: $scope.$root.baseurl + "/overallassessment",
                            type: "bg-gradient-success",
                            name: "Overall Self Assessment ",
                            hasIcon: true,
                            icon: "fas fa-poll-h",
                            iconText: "text-olive",
                            hasBadge: false,
                            badgeType: "bg-olive",
                            badgeCount: 0
                        });
                        // Assesment Menu End

                        if (isAdmin || acls.includes(",90,") || acls.includes(",91,") || acls.includes(",92,")) {
                            $scope.$root.menus.push({
                                href: "#",
                                type: "",
                                name: "Watchlist",
                                hasIcon: true,
                                icon: "fa-binoculars",
                                hasBadge: false,
                                badgeType: "0",
                                badgeCount: 0,
                                isparent: true,
                                child: watchlist
                            });
                        }
                        if (isAdmin || acls.includes(",92,")) {
                            watchlist.push({
                                href: $scope.$root.baseurl + "/watchlists/tagmaster",
                                type: "",
                                name: "Watchlist Tag",
                                hasIcon: true,
                                icon: "fa-tag",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",90,")) {
                            watchlist.push({
                                href: $scope.$root.baseurl + "/watchlists/master",
                                type: "",
                                name: "Watchlist",
                                hasIcon: true,
                                icon: "fa-list",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",91,")) {
                            watchlist.push({
                                href: $scope.$root.baseurl + "/watchlists/heroes",
                                type: "",
                                name: "Tagged Hero List",
                                hasIcon: true,
                                icon: "fa-list",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: 0
                            });
                        }

                        // Reports Menu Start
                        // Created By Syed Mohd Bilal
                        // 30 July 2021
                        if (isAdmin || acls.includes(",76,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/resource/resourcepool",
                                type: "bg-gradient-danger",
                                name: "Resource Pool",
                                hasIcon: true,
                                icon: "fa-user-circle",
                                hasBadge: true,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",75,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/resource/allAllocation",
                                type: "bg-gradient-danger",
                                name: "Resource Allocation",
                                hasIcon: true,
                                icon: "fa-user-circle",
                                hasBadge: true,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        // if (isAdmin || acls.includes(",21,")) reports.push({href: $scope.$root.baseurl + "/pools/list", type: "bg-gradient-danger", name: "Pool", hasIcon: true, icon: "fa-circle", hasBadge: true, badgeType: "badge-danger", badgeCount: $scope.$root.count.Pool});
                        if (isAdmin) {
                            reports.push({
                                href: $scope.$root.baseurl + "/allallocation",
                                type: "bg-gradient-danger",
                                name: "All Allocation",
                                hasIcon: true,
                                icon: "fa-circle",
                                hasBadge: true,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",86,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/allallocationreport",
                                type: "bg-gradient-danger",
                                name: "All Allocation Report",
                                hasIcon: true,
                                icon: "fa-circle",
                                hasBadge: true,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",31,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/my-diary/report",
                                type: "bg-gradient-olive",
                                name: "Heroes Diary",
                                hasIcon: true,
                                icon: "fa-book",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",99,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/heroDiary/location/",
                                type: "bg-gradient-olive",
                                name: "Heroes Diary (Location)",
                                hasIcon: true,
                                icon: "fa-book",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",46,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/my-diary/unfilled-report",
                                type: "bg-gradient-olive",
                                name: "Unfilled Diary",
                                hasIcon: true,
                                icon: "fa-book",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",46,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/my-diary/unfilled-report-clone3",
                                type: "bg-gradient-olive",
                                name: "Monthly Billing",
                                hasIcon: true,
                                icon: "fa-book",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin) {
                            reports.push({
                                href: $scope.$root.baseurl + "/my-diary/billing-lock-report",
                                type: "bg-gradient-olive",
                                name: "Billing Lock Report",
                                hasIcon: true,
                                icon: "fa-book",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",103,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/projects/forecastreport",
                                type: "bg-gradient-olive",
                                name: "Forecast Report",
                                hasIcon: true,
                                icon: "fa-book",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",52,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/my-diary/utility-report",
                                type: "bg-gradient-olive",
                                name: "Utility Diary",
                                hasIcon: true,
                                icon: "fa-table",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",53,") || acls.includes(",54,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/my-diary/billing-efficiency-report",
                                type: "bg-gradient-olive",
                                name: "Billing Efficiency Report",
                                hasIcon: true,
                                icon: "fa-table",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }
                        //if (isAdmin || acls.includes(",51,")) reports.push({href: $scope.$root.baseurl + "/heroes/reopeningbookingreport", type: "bg-gradient-olive", name: "Hero Office Visit Booking", hasIcon: true, icon: "fa-calendar-check", iconText: "text-olive", hasBadge: false, badgeType: "bg-olive", badgeCount: 0});
                        if (isAdmin || acls.includes(",36,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/project-diary/report",
                                type: "bg-gradient-olive",
                                name: "Project Diary",
                                hasIcon: true,
                                icon: "fa-book",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",68,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/skillReport",
                                type: "bg-gradient-danger",
                                name: "Skill Update",
                                hasIcon: true,
                                icon: "fa-circle",
                                hasBadge: true,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",69,")) {
                            reports.push({ href: $scope.$root.baseurl + "/heroes/heroesbyskills", type: "bg-gradient-warning", name: "Heroes By Skills", hasIcon: true, icon: "fa-users", });
                        }
                        if (isAdmin || acls.includes(",70,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/mycertificate/report-heroview/0",
                                type: "bg-gradient-olive",
                                name: "Heroes Certificates",
                                hasIcon: true,
                                icon: "fa-certificate",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        if (isAdmin || acls.includes(",87,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/report/BirthdayReport",
                                type: "bg-gradient-danger",
                                name: "Heroes Birthday",
                                hasIcon: true,
                                icon: "fa-circle",
                                hasBadge: true,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",88,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "report/workAnniversaryReport",
                                type: "bg-gradient-danger",
                                name: "Heroes Work Anniversary",
                                hasIcon: true,
                                icon: "fa-circle",
                                hasBadge: true,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",62,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/clientappreciation/list",
                                type: "",
                                name: "Client Appreciations",
                                hasIcon: true,
                                icon: "fas fa-thumbs-up",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: $scope.$root.count.tip
                            });
                        }

                        if (isAdmin || acls.includes(",93,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/tips/report",
                                type: "",
                                name: "Tip Report",
                                hasIcon: true,
                                icon: "fa-file",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: $scope.$root.count.tip
                            });
                        }

                        if (isAdmin || acls.includes(",76,") || acls.includes(",75,") || acls.includes(",68,") || acls.includes(",69,") || acls.includes(",31,") || acls.includes(",46,")
                            || acls.includes(",52,") || acls.includes(",53,") || acls.includes(",51,") || acls.includes(",36,") || acls.includes(",70,") || acls.includes(",86,")
                            || acls.includes(",87,") || acls.includes(",88,") || acls.includes(",93,")) {
                            $scope.$root.menus.push({ href: "#", type: "", name: "Reports", hasIcon: true, icon: "fa-list-alt", hasBadge: false, badgeType: "0", badgeCount: 0, isparent: true, child: reports });
                        }
                        // Reports Menu End

                        // Settings Menu Start
                        if (isAdmin || acls.includes(",16,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/acls/list",
                                type: "bg-gradient-danger",
                                name: "Manage ACL",
                                hasIcon: true,
                                icon: "fa-lock",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",16,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/acls/profile/list",
                                type: "bg-gradient-danger",
                                name: "Manage ACL Profiles",
                                hasIcon: true,
                                icon: "fa-lock",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",37,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/emailtemplates/list",
                                type: "bg-gradient-danger",
                                name: "Manage Email Templates",
                                hasIcon: true,
                                icon: "fa-mail-bulk",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",83,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/emailnotificationconfigurations",
                                type: "bg-gradient-danger",
                                name: "Email Notification Configuration",
                                hasIcon: true,
                                icon: "fa fa-envelope",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",101,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/diaryconfiguration",
                                type: "bg-gradient-danger",
                                name: "Diary Configuration",
                                hasIcon: true,
                                icon: "fa-id-badge",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",77,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/newsletters/list",
                                type: "bg-gradient-danger",
                                name: "Manage Quick Links",
                                hasIcon: true,
                                icon: "fa-mail-bulk",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",29,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/manages/skill",
                                type: "bg-gradient-danger",
                                name: "Manage Skill",
                                hasIcon: true,
                                icon: "fa-id-badge",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",29,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/manages/department",
                                type: "bg-gradient-danger",
                                name: "Manage Department",
                                hasIcon: true,
                                icon: "fa-building",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",29,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/manages/designation",
                                type: "bg-gradient-danger",
                                name: "Manage Designation",
                                hasIcon: true,
                                icon: "fa-graduation-cap",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",29,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/manages/log type",
                                type: "bg-gradient-danger",
                                name: "Manage My Diary Type",
                                hasIcon: true,
                                icon: "fa-th-list",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",29,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/manages/certificatedomains",
                                type: "bg-gradient-danger",
                                name: "Manage Certificate Domains",
                                hasIcon: true,
                                icon: "fa-th-list",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",29,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/manages/revenuecenter",
                                type: "bg-gradient-danger",
                                name: "Manage Revenue Centers",
                                hasIcon: true,
                                icon: "fa-building",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",82,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/managecountries",
                                type: "bg-gradient-danger",
                                name: "Managec Countries",
                                hasIcon: true,
                                icon: "fas fa-flag",
                                hasBadge: false,
                                badgeType: "badge-danger",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",66,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/my/templatemaster",
                                type: "",
                                name: "Goal Template Master",
                                hasIcon: true,
                                icon: "fa-cog",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",84,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "/assessment/master",
                                type: "",
                                name: "Assessment Master",
                                hasIcon: true,
                                icon: "fa-cog",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",85,")) {
                            setting.push({
                                href: $scope.$root.baseurl + "projects/typemaster/add/",
                                type: "",
                                name: "Project Type Master",
                                hasIcon: true,
                                icon: "fa-cog",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin) {
                            setting.push({
                                href: $scope.$root.baseurl + "projects/forecastprofile/add/",
                                type: "",
                                name: "Billing Profile Master",
                                hasIcon: true,
                                icon: "fa-cog",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",95,")) {
                            reports.push({
                                href: $scope.$root.baseurl + "/tips/list",
                                type: "",
                                name: "Tip of the Day",
                                hasIcon: true,
                                icon: "fa-clipboard",
                                hasBadge: false,
                                badgeType: "bg-indigo",
                                badgeCount: $scope.$root.count.tip
                            });
                        }

                        if (isAdmin || acls.includes(",16,") || acls.includes(",37,") || acls.includes(",77,") || acls.includes(",27,") || acls.includes(",82,") ||
                            acls.includes(",83,") || acls.includes(",66,") || acls.includes(",84,") || acls.includes(",85,") || acls.includes(",95,")) {
                            $scope.$root.menus.push({ href: "#", type: "", name: "Settings", hasIcon: true, icon: "fa-cog", hasBadge: false, badgeType: "0", badgeCount: 0, isparent: true, child: setting });
                        }

                        // Settings Menu End
                        if (isAdmin || acls.includes(",102,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/requisitions/dashboard/",
                                type: "bg-gradient-warning",
                                name: "Requisition Dashboard",
                                hasIcon: true,
                                icon: "fa-chart-bar",
                                hasBadge: false,
                                badgeType: "badge-warning",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || acls.includes(",47,")) {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/manpowerplanningdiscussions/add",
                                type: "bg-gradient-olive",
                                name: "Manpower Planning Discussions",
                                hasIcon: true,
                                icon: "fa-handshake",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }
                        if (isAdmin || $scope.$root.gUser.emp_country == '1') {
                            $scope.$root.menus.push({
                                href: $scope.$root.baseurl + "/rms_ai_bot/",
                                type: "bg-gradient-olive",
                                name: "RMS AI Chat",
                                hasIcon: true,
                                icon: "fa-robot",
                                iconText: "text-olive",
                                hasBadge: false,
                                badgeType: "bg-olive",
                                badgeCount: 0
                            });
                        }

                        $scope.$root.myAccess = {
                            accountAdd: isAdmin || $scope.emp_acls.includes(",2,"),
                            accountDelete: isAdmin || $scope.emp_acls.includes(",4,"),
                            accountEdit: isAdmin || $scope.emp_acls.includes(",3,"),
                            accountList: isAdmin || $scope.emp_acls.includes(",13,"),
                            accountView: isAdmin || $scope.emp_acls.includes(",1,"),
                            aclList: isAdmin || $scope.emp_acls.includes(",16,"),
                            allocationAdd: isAdmin || $scope.emp_acls.includes(",23,"),
                            allocationDelete: isAdmin || $scope.emp_acls.includes(",26,"),
                            allocationEdit: isAdmin || $scope.emp_acls.includes(",24,"),
                            allocationList: isAdmin || $scope.emp_acls.includes(",25,"),
                            allocation: isAdmin || $scope.emp_acls.includes(",22,"),
                            heroAdd: isAdmin || $scope.emp_acls.includes(",6,"),
                            heroDelete: isAdmin || $scope.emp_acls.includes(",8,"),
                            heroEdit: isAdmin || $scope.emp_acls.includes(",7,"),
                            heroList: isAdmin || $scope.emp_acls.includes(",14,"),
                            heroView: isAdmin || $scope.emp_acls.includes(",5,"),
                            heroLoginAs: isAdmin || $scope.emp_acls.includes(",33,"),
                            showActAsPM: isAdmin || $scope.emp_acls.includes(",34,"),
                            showActAsLM: isAdmin || $scope.emp_acls.includes(",35,"),
                            managerList: isAdmin || $scope.emp_acls.includes(",20,"),
                            masterAdd: isAdmin || $scope.emp_acls.includes(",19,"),
                            masterDelete: isAdmin || $scope.emp_acls.includes(",17,"),
                            masterEdit: isAdmin || $scope.emp_acls.includes(",18,"),
                            poolList: isAdmin || $scope.emp_acls.includes(",21,"),
                            resourcePoolList: isAdmin || $scope.emp_acls.includes(",76,"),
                            resourceAllocationList: isAdmin || $scope.emp_acls.includes(",75,"),
                            poolListFull: isAdmin || $scope.emp_acls.includes(",38,"),
                            poolListPartial: isAdmin || $scope.emp_acls.includes(",39,"),
                            poolListOverAllocated: isAdmin || $scope.emp_acls.includes(",40,"),
                            poolListShadow: isAdmin || $scope.emp_acls.includes(",41,"),
                            poolReport: isAdmin || $scope.emp_acls.includes(",28,"),
                            projectAdd: isAdmin || $scope.emp_acls.includes(",10,"),
                            projectDelete: isAdmin || $scope.emp_acls.includes(",12,"),
                            projectEdit: isAdmin || $scope.emp_acls.includes(",11,"),
                            projectList: isAdmin || $scope.emp_acls.includes(",15,"),
                            projectView: isAdmin || $scope.emp_acls.includes(",9,"),
                            resetPassword: isAdmin || $scope.emp_acls.includes(",29,"),
                            myDiaryReport: isAdmin || $scope.emp_acls.includes(",31,"),
                            projectDiary: isAdmin || $scope.emp_acls.includes(",32,"),
                            projectDiaryReport: isAdmin || $scope.emp_acls.includes(",36,"),
                            emailTemplate: isAdmin || $scope.emp_acls.includes(",37,"),
                            myDiaryLock: isAdmin || $scope.emp_acls.includes(",42,"),
                            repeatDiary: isAdmin || $scope.emp_acls.includes(",43,"),
                            heroTimeline: isAdmin || $scope.emp_acls.includes(",44,"),
                            projectTimeline: isAdmin || $scope.emp_acls.includes(",45,"),
                            unfilledDiaryReport: isAdmin || $scope.emp_acls.includes(",46,"),
                            manPowerPlanningDiscussionsAdd: isAdmin || $scope.emp_acls.includes(",47,"),
                            masterCertificateDomains: isAdmin || $scope.emp_acls.includes(",48,"),
                            myCertificateReportHeroView: isAdmin || $scope.emp_acls.includes(",49,"),
                            officeVisitBookingReport: isAdmin || $scope.emp_acls.includes(",51,"),
                            utilityDiaryReport: isAdmin || $scope.emp_acls.includes(",52,"),
                            billingEfficiencyReportAus: isAdmin || $scope.emp_acls.includes(",53,"),
                            billingEfficiencyReportGlobal: isAdmin || $scope.emp_acls.includes(",54,"),
                            heroSkills: isAdmin || $scope.emp_acls.includes(",59,") || $scope.emp_acls.includes(",69,"),
                            watchlistHeroes: isAdmin || acls.includes(",91,"),
                            watchlistMaster: isAdmin || acls.includes(",90,"),
                            watchlistTagMaster: isAdmin || acls.includes(",92,"),
                            skillReport: isAdmin || acls.includes(",68,"),
                            heroBySkill: isAdmin || acls.includes(",69,"),
                            myCertificateReport: isAdmin || acls.includes(",70,"),
                            allAllocationReport: isAdmin || acls.includes(",86,"),
                            birthdayReport: isAdmin || acls.includes(",87,"),
                            workAnniversaryReport: isAdmin || acls.includes(",88,"),
                            tipReport: isAdmin || acls.includes(",93,"),
                            tipList: isAdmin || acls.includes(",95,"),
                            manageCountriesMaster: isAdmin || acls.includes(",82,"),
                            emailNotificationConf: isAdmin || acls.includes(",83,"),
                            quickLinks: isAdmin || acls.includes(",77,"),
                            manageMyDiaryType: isAdmin || acls.includes(",81,"),
                            manageCertificateDomain: isAdmin || acls.includes(",48,"),
                            shoutoutList: isAdmin || acls.includes(",96,"),
                            myDiaryLocationReport: isAdmin || $scope.emp_acls.includes(",99,"),


                        };
                    }
                });
            }
            else {
                $scope.$root.gUser = {
                    emp_code: "",
                    emp_department: null,
                    emp_designation: null,
                    emp_doj: "2015-01-01",
                    emp_email: "guest@girikon.com",
                    emp_first_name: "Guest",
                    emp_gender: "Male",
                    emp_last_name: null,
                    emp_location: null,
                    emp_marital_status: null,
                    emp_middle_name: null,
                    emp_mobile: null,
                    emp_nick_name: "Guest",
                    emp_pool: "0",
                    emp_skype: null,
                    emp_status: "active",
                    emp_username: "guest@girikon.com",
                    is_admin: null
                };
                $scope.$root.menus = [{
                    "href": $scope.$root.baseurl + "/login",
                    "name": "Login",
                    hasIcon: true,
                    icon: "fa-sign-in-alt",
                    hasBadge: false,
                    badgeType: "",
                    badgeCount: 0
                }];
            }
        }
        catch (e) {
        }
    };

    $scope.$root.getAccess = () => $scope.$root.myAccess;
    $scope.$root.updateMenu();

    /* Global functions */
    //Quick function for applications.
    $scope.$root.inProject = (d) => {
        let k = 8 - parseFloat(d);
        return ((k * 100) / 8) + "%";
    };
    $scope.$root.hoursToPercent = (d) => {
        /*if (d && d !== '-')
         return ((d * 100) / 8) + "%";
         else "0%";
         */
        return d;
    };
    $scope.$root.percentToHours = (d) => (d * 8) / 100;
    $scope.$root.formatDate = (date) => new Date(date);

    const handleModelPopup = () => {
        if ($(document).find(".modal-backdrop.show").length) {
            $(document).find(".modal-backdrop.show").remove();
            $(document).find("body").removeClass("modal-open");
        }
    };
    // State redirect & can work with data-id
    $scope.$root.goToSelf = (s, j) => {
        handleModelPopup();
        $state.go(s, j, { reload: true });
    };
    $scope.$root.goTo = function (event, state, id = null) {
        try {
            handleModelPopup();
            let currentTarget = event.target;
            if ((currentTarget.dataset && currentTarget.dataset.id) || id) {
                id = id ? id : currentTarget.dataset.id;
                $state.go(state, { id: id }, {});
            }
            else {
                $state.go(state, {}, {});
            }
        }
        catch (e) {
            console.error(e);
        }
    };

    $scope.$root.getActiveClass = function (d, t = "b") {
        if (d && typeof d === "string" && d.toLowerCase() === "inactive") {
            return (t === "b" ? "badge-" : t === "t" ? "text-" : t === "bg" ? "bg-" : "") + "secondary";
        }
        else if (d && typeof d === "string" && d.toLowerCase() === "hold") {
            return (t === "b" ? "badge-" : t === "t" ? "text-" : t === "bg" ? "bg-" : "") + "warning";
        }
        else if (d && typeof d === "string" && d.toLowerCase() === "hold") {
            return (t === "b" ? "badge-" : t === "t" ? "text-" : t === "bg" ? "bg-" : "") + "warning";
        }
        else if (d && typeof d === "string" && d.toLowerCase() === "abcd") {
            return (t === "b" ? "badge-" : t === "t" ? "text-" : t === "bg" ? "bg-" : "") + "danger";
        }
        else {
            return (t === "b" ? "badge-" : t === "t" ? "text-" : t === "bg" ? "bg-" : "") + "success";
        }
    };

    let tableObj = {};
    $scope.$root.callDataTable = (idOrClass, timeoutInSec, cb, pdfOptions = {}, fixedColumns = {}) => {
        //debugger;
        var kk = (t = 100) => {
            setTimeout(() => {
                //                const eles = $(document).find(idOrClass);
                //                console.log(eles.length);
                //                if (eles.length > 0) {
                $(document).find(idOrClass).each(function (i, ele1) {
                    try {
                        const ele = $(this);
                        const uid = ele.attr("data-name") || 'empty';
                        console.log(uid);
                        if (tableObj[uid]) {
                            delete tableObj[uid];
                        }
                        tableObj[uid] = ele.DataTable({
                            columnDefs: [{
                                orderable: false,
                                className: 'select-checkbox',
                                targets: 0
                            }],
                            select: {
                                style: 'os',
                                selector: 'td:first-child'
                            },
                            aLengthMenu: [
                                [25, 50, 100, 200, -1],
                                [25, 50, 100, 200, "All"]
                            ],
                            iDisplayLength: -1,
                            paging: true,
                            ...fixedColumns,
                            lengthChange: true,
                            searching: true,
                            ordering: true,
                            info: true,
                            autoWidth: true,
                            responsive: true,
                            destroy: true,
                            order: [],
                            dom: "lBfrtip",
                            buttons: [
                                {
                                    extend: "copy",
                                    text: "<span class=\"fas fa-copy text-warning\" title=\"Copy to Clipboard\"></span>",
                                    exportOptions: {
                                        modifier: {
                                            page: "all",
                                            search: "none"
                                        }
                                    }
                                }, {
                                    extend: "excel",
                                    text: "<span class=\"fas fa-file-excel text-green\" title=\"Download as Excel\"></span>",
                                    exportOptions: {
                                        modifier: {
                                            page: "all",
                                            search: "none"
                                        }
                                    }
                                }, {
                                    extend: "csv",
                                    text: "<span class=\"fas fa-file-csv text-green\" title=\"Download as CSV\"></span>",
                                    exportOptions: {
                                        modifier: {
                                            page: "all",
                                            search: "none"
                                        }
                                    }
                                }, {
                                    ...pdfOptions,
                                    extend: "pdf",
                                    text: "<span class=\"fas fa-file-pdf text-red\" title=\"Download as PDF\"></span>",
                                    exportOptions: {
                                        modifier: {
                                            page: "all",
                                            search: "none"
                                        }
                                    },
                                    customize: function (doc) {
                                        // Splice the image in after the header, but before the table
                                        doc.content.splice(0, 0, {
                                            margin: [-10, -25, 0, 10],
                                            alignment: "left",
                                            image: "data:image/png;base64,[encoded data, 18020 characters]"
                                        });
                                        // Data URL generated by http://dataurl.net/#dataurlmaker
                                    }
                                }, {
                                    extend: "print",
                                    text: "<span class=\"fas fa-print text-white\" title=\"Print\"></span>",
                                    exportOptions: {
                                        modifier: {
                                            page: "all",
                                            search: "none"
                                        }
                                    }
                                }
                            ]
                        });

                        $(document).find(".table-responsive").overlayScrollbars({});

                        try {
                            const colHide = ele.attr("data-col-hide");
                            const colHideStrArr = colHide ? colHide.indexOf(',') > -1 ? colHide.split(',') : [colHide] : [];
                            const colHideArr = colHideStrArr && colHideStrArr.length > 0 ? colHideStrArr.map(o => parseInt(o)) : [];
                            if (colHideArr.length > 0) {
                                colHideArr.forEach(o => {
                                    console.log(o);
                                    const c = tableObj[uid].column(o);
                                    c.visible(!c.visible());
                                });
                            }
                        }
                        catch (e) {
                            console.log(e);
                        }

                        let buttons = uid !== 'empty' ? $(document).find('button[data-table-name="' + uid + '"]') : ele.closest('.card').find("button.toggle-vis,input.toggle-vis");
                        if (buttons) {
                            buttons.on("click", function (e) {
                                e.preventDefault();
                                try {
                                    // Get the column API object
                                    const tableName = $(this).attr("data-table-name");
                                    const columnPosition = parseInt($(this).attr("data-column"));
                                    let column = tableName && tableObj[tableName] ? tableObj[tableName].column(columnPosition) : tableObj['empty'].column(columnPosition);
                                    // Toggle the visibility
                                    column.visible(!column.visible());
                                }
                                catch (e) {
                                    console.log(e);
                                }
                            });
                        }
                        if (kk) {
                            try {
                                clearTimeout(kk);
                            }
                            catch (e) {
                            }
                            if (cb) {
                                cb(tableObj[uid]);
                            }
                        }
                    }
                    catch (e) {
                        console.error(e);
                    }
                });
            }, t);
        };
        kk(timeoutInSec);
    };
    $(document).find("button.toggle-vis,input.toggle-vis").on("click", function (e) {
        e.preventDefault();
        try {
            // Get the column API object
            const tableName = $(this).attr("data-table-name");
            const columnPosition = $(this).attr("data-column");
            console.log(tableName, columnPosition)
            let column = null;
            if (tableName && tableObj[tableName]) {
                column = tableObj[tableName].column(columnPosition);
            }
            else {
                column = tableObj['empty'].column(columnPosition);
            }
            // Toggle the visibility
            column.visible(!column.visible());
        }
        catch (e) {
        }
    });

    $scope.$root.sort = (data, prop, type, order = 'asc') => {
        //console.log(data);
        try { //return data.sort((a, b) => a[prop] > b[prop] ? 1 : a[prop] < b[prop] ? -1 : 0);
            type = type ? type : 'default';
            if (type === 'date' && order === 'asc') {
                return data.sort((a, b) => moment(a[prop]) < moment(b[prop]) ? 1 : moment(a[prop]) > moment(b[prop]) ? -1 : 0);
            }
            else if (type === 'date' && order === 'desc') {
                return data.sort((a, b) => moment(a[prop]) > moment(b[prop]) ? 1 : moment(a[prop]) < moment(b[prop]) ? -1 : 0);
            }
            else if (type === 'default') {
                return data.sort((a, b) => a[prop] > b[prop] ? 1 : a[prop] < b[prop] ? -1 : 0);
            }
        }
        catch (e) {
            console.error(e);
        }

    };

    $scope.$root.prompt = (html = `Do you real want to continue?`, dis_msg = 'Operation has been canceled!') => {
        return new Promise(resolve => {
            Swal.fire({
                html: html,
                showCancelButton: true,
                confirmButtonColor: '#1e7e34',
                cancelButtonColor: '#bd2130',
                confirmButtonText: '<i class="fa fa-check-circle"></i> Yes',
                confirmButtonAriaLabel: 'Yes',
                cancelButtonText: '<i class="fa fa-times-circle"></i> No',
                cancelButtonAriaLabel: 'No'
            }).then((result) => {
                if (result.dismiss) {
                    toastr.warning(dis_msg);
                }
                resolve(!!result.value);
            });
        });
    };

    /**
     * Convert date to UTC date time format in DD-MM-YYYY HH:mm:ss format from given format or default format
     * @param {string} _date
     * @param {string} _format
     * @return {*}
     */
    $scope.$root.convertDate2DateTimeUTC = (_date = moment().format('DD-MM-YYYY'), _format = 'DD-MM-YYYY', user_time = null) => {
        /**
         * Get user system date in DD-MM-YYYY format
         */
        let user_date, user_date_time, user_date_time_utc;
        user_date = moment(_date, _format).format('DD-MM-YYYY');
        /**
         * Get user system time in 24 hours format HH:mm:ss
         */
        if (!user_time) {
            user_time = moment().format('HH:mm:ss');
        }

        /***
         * Concat date and time in DD-MM-YYYY HH:mm:ss format
         */
        user_date_time = user_date + ' ' + user_time;
        /***
         * Add UTC time zone in DD-MM-YYYY HH:mm:ss ZZ format
         */
        user_date_time_utc = moment(user_date_time, 'DD-MM-YYYY HH:mm:ss').format('YYYY-MM-DD HH:mm:ss ZZ');
        return user_date_time_utc;
    };

}]);
