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

var HEADER = {};

/**
 * init
 */
 HEADER.init = function() {
     $("#dspLoginId").text(ClientData.userInfo_loginId());
     HEADER.initLogout();
 };

 HEADER.initLogout = function() {
    
    $("#btnLogout").click(HEADER.logoutFunction);
 }

 /**
 * create url tree & show breadcrumbs list
 */
HEADER.goToHomePage = function (pageId) {
    DashboardSetting.getSettingData(function (settings) {
        if (settings.dashboardHome == 1) {
            HEADER.goDashboard();
        } else {
            HEADER.goOperationList();
        }
    });
};

/**
 * logout function
 */
HEADER.logoutFunction = function () {
    console.log('clicked logout');
    COMMON.showConfirm(I18N.i18nText('msgLogoutConfirm'), HEADER.processLogout, {title: I18N.i18nText("confirmation")});
};

/**
 * process logout
 */
HEADER.processLogout = function () {
    var params = {
        sid: ClientData.userInfo_sid(),
    };
    const url = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.LOGOUT;
    COMMON.cmsAjax(
        url,
        params,
        false,
        function (data) {
            if (data.httpStatus == CONSTANT.HTTP_STATUS.OK) {
                SessionStorageUtils.clear();
                COMMON.userSetting().remove(CONSTANT.KEYS.userInfo_sid);
                COMMON.userSetting().remove(CONSTANT.KEYS.userInfo_sid_local);
                // Move to login screen
                //window.location = COMMON.ScreenIds.Login;
                COMMON.avwScreenMove(CONSTANT.PAGE_NAME.LOGIN);
            } else {
                if (data.errorMessage) {
                    COMMON.showAlert(null, {message: data.errorMessage});
                } else {
                    COMMON.showAlert('errorOccured');
                }
            }
        },
        function (xmlHttpRequest, txtStatus, errorThrown) {
            if (xmlHttpRequest.errorMessage) {
                COMMON.showAlert(null, {message: xmlHttpRequest.errorMessage});
            } else {
                COMMON.showAlert('errorOccured');
            }
        },
    );
};

/**
 * Go page of operationList
 */
HEADER.goOperationList = function () {
    OL.init();
 };
 
 /**
  * Go page of dashboard
  */
 HEADER.goDashboard = function () {
    COMMON.avwScreenMove("dashboard.html");
 };
 
 
 /**
  * Initialize with any navigation
  *
  * @param {String} bottomNav
  */
 HEADER.activeInitBottomNav = function (bottomNav) {
     HEADER.inactiveAllBottomNav();
     HEADER.activeBottomNav(bottomNav);
 };
 
 /**
  * change inactive all footer bottom
  */
 HEADER.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');
         }
     }
 };
 
 
 /**
  * Change specific footer bottom to active.
  *
  * @param {String} bottomNavId
  */
 HEADER.activeBottomNav = function (bottomNavId) {
     let elm = $('#' + bottomNavId);
     if (typeof elm !== 'object') {
         console.log('HEADER.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');
     }
 };
 
