/**
 * 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 FOOTER = {};

/**
 * Change specific footer bottom to active.
 *
 * @param {String} bottomNavId
 */
FOOTER.activeBottomNav = function (bottomNavId) {
    let elm = $('#' + bottomNavId);
    if (typeof elm !== 'object') {
        console.log('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
 */
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
 */
FOOTER.goIndexPage = function (tabId) {
    sessionStorage.activeTab = tabId;
    COMMON.goUrlWithCurrentParams(DAFAULT_PAGE);
};

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

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

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

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

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

        FOOTER.goOperationList();
        // CHK.loadDashboardSetting(function() {
        // 	if (CHK.dashboardSetting.dashboardHome) {
        // 		FOOTER.goDashboard();
        // 	} else {
        // 		FOOTER.goOperationList();
        // 	}
        // });
    });
};
