/**
 * Common js for footer.
 * ※Code is written mainly for dashboard and operationList.
 * 
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */

var CHK_Footer = {};

/**
 * Change specific footer bottom to active.
 * 
 * @param {String} bottomNavId 
 */
CHK_Footer.activeBottomNav = function(bottomNavId) {
    let elm = $("#" + bottomNavId);
    if (typeof elm !== 'object') {
        console.log('CHK_Footer.activeBottomNav:elm !== object:' + bottomNavId);
        return;
    }
    elm.removeClass('text-secondary');
    elm.addClass("text-primary");
    let img = elm.find("img")[0];
    if (img && img.getAttribute("data-src")) {
        img.src = img.getAttribute("data-src");
    }
}
/**
 * change inactive all footer bottom
 */
CHK_Footer.inactiveAllBottomNav = function() {
    let navs = document.getElementsByClassName("bottom-nav");
    for (i = 0; i < navs.length; i++) {
        navs[i].className = navs[i].className.replace("text-primary", "text-secondary");
        let img = navs[i].querySelector("img");
        if (img && img.getAttribute('data-inactive-src')) {
            img.src = img.getAttribute('data-inactive-src');
        } 
    }
}

/**
 * Go page of index.html.
 * Store tab id in session for want to show display.
 * 
 * @param {String} tabId 
 */
CHK_Footer.goIndexPage = function(tabId) {
    sessionStorage.activeTab = tabId;
    CHK_Common.goUrlWithCurrentParams(DAFAULT_PAGE);
}

/**
 * is the url index.html?
 */
CHK_Footer.isIndexPage = function() {
    if (typeof location === 'object' && typeof location.pathname === 'string') {
        if (location.pathname.includes(CHK_CONSTANT.PAGE_NAME.DEFAULT) == true) {
            return true;
        }
    }
    return false;
}

/**
 * Initialize with any navigation
 * 
 * @param {String} bottomNav 
 */
CHK_Footer.activeInitBottomNav = function(bottomNav) {
    CHK_Footer.inactiveAllBottomNav();
    CHK_Footer.activeBottomNav(bottomNav);
}

/**
 * Go page of dashboard
 */
CHK_Footer.goDashboard = function() {
    sessionStorage.activeTab = CHK_CONSTANT.PAGE_TAB.DASHBOARD;
    CHK_Footer.activeInitBottomNav('dashboardBottomNav');
    if (CHK_Footer.isIndexPage()) {
        CHK_TOP.showPage(CHK_CONSTANT.PAGE_NAME.DASHBOARD);
        return;
    }
    CHK_Footer.goIndexPage(CHK_CONSTANT.PAGE_NAME.DASHBOARD);
}

/**
 * Go page of operationList
 */
CHK_Footer.goOperationList = function() {
    sessionStorage.activeTab = CHK_CONSTANT.PAGE_TAB.OPERATION_LIST;
    CHK_Footer.activeInitBottomNav('operationListBottomNav');
    if (CHK_Footer.isIndexPage()) {
        CHK_TOP.showPage(CHK_CONSTANT.PAGE_NAME.OPERATION_LIST);
        return;
    }
    CHK_Footer.goIndexPage(CHK_CONSTANT.PAGE_NAME.OPERATION_LIST);
}

/**
 * Go to active tab when footer was loaded.
 * If it is no active tab, go to page in settings.
 */
CHK_Footer.initFooter = function() {
	$("#footer").load(CHK_CONSTANT.PAGE_NAME.FOOTER, function() {
        
		if (typeof sessionStorage != 'undefined' && typeof sessionStorage.activeTab != 'undefined') {
			if (sessionStorage.activeTab == CHK_CONSTANT.PAGE_TAB.DASHBOARD) {
				CHK_Footer.goDashboard();
			} else {
				CHK_Footer.goOperationList();
			}
			return;
		}
		
        CHK_Footer.goOperationList();
		// CHK.loadDashboardSetting(function() {
		// 	if (CHK.dashboardSetting.dashboardHome) {
		// 		CHK_Footer.goDashboard();
		// 	} else {
		// 		CHK_Footer.goOperationList();
		// 	}
		// });
	});
}