/**
 * 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());

    $('#btnLogout').click(HEADER.logoutFunction);
};

/**
 * create url tree & show breadcrumbs list
 */
// HEADER.urlTree = function () {
//     const pathUrl = location.pathname;
//     const htmlName = pathUrl.substring(pathUrl.lastIndexOf('/') + 1);
//     let pageName = urlPath.substring(0, urlPath.indexOf(".html"));
//     if (pageName == CONSTANT.URL.WEB.OPERATION_LIST) pageName = CONSTANT.PAGE_NAME.OPERATION_LIST;
    
//     // session reset
//     if ((htmlName == CONSTANT.URL_TREE_NAME.OPERATION_LIST) || (htmlName == CONSTANT.URL_TREE_NAME.DASHBOARD)) {
//         sessionStorage.pickUpParams = null;
//     }

//     let navs;
//     switch (htmlName) {
//         case CONSTANT.URL_TREE_NAME.DASHBOARD:
//         case CONSTANT.URL_TREE_NAME.OPERATION_LIST:
//             if (sessionStorage.urlTree) sessionStorage.urlTree = null;
//             if (sessionStorage.pickUpType) sessionStorage.pickUpType = null;
//             break;
//         case CONSTANT.URL_TREE_NAME.PICKUP :
//             navs = [
//                     {
//                         titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
//                         href: CONSTANT.URL_TREE_NAME.DASHBOARD,
//                     },
//                     {
//                         titleLang: CONSTANT.PAGE_NAME.PICKUP,
//                     },
//                 ];
//             break;
//         case CONSTANT.URL_TREE_NAME.REPORT_LIST:
//             if (sessionStorage.activeHomePage == CONSTANT.PAGE_TAB.DASHBOARD) {
//                 if (sessionStorage.pickUpType) {
//                     if (sessionStorage.pickUpType == CONSTANT.PICK_UP_TYPE.CONTINOUS_WORK) {
//                         navs = [
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
//                                 href: CONSTANT.URL_TREE_NAME.DASHBOARD,
//                             },
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.PICKUP,
//                                 href: CONSTANT.URL_TREE_NAME.PICKUP_CONTINUOS,
//                             },
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.REPORT_LIST,
//                             },
//                         ]
//                     } else if (sessionStorage.pickUpType == CONSTANT.PICK_UP_TYPE.WARNING_REPORT) {
//                         navs = [
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
//                                 href: CONSTANT.URL_TREE_NAME.DASHBOARD,
//                             },
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.PICKUP,
//                                 href: CONSTANT.URL_TREE_NAME.PICKUP_WARNING,
//                             },
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.REPORT_LIST,
//                             },
//                         ]
//                     } else {
//                         navs = [
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
//                                 href: CONSTANT.URL_TREE_NAME.DASHBOARD,
//                             },
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.PICKUP,
//                                 href: CONSTANT.URL_TREE_NAME.PICKUP_NEW,
//                             },
//                             {
//                                 titleLang: CONSTANT.PAGE_NAME.REPORT_LIST,
//                             },
//                         ]
//                     }
//                 } else {
//                     navs = [
//                         {
//                             titleLang: CONSTANT.PAGE_NAME.DASHBOARD,
//                             href: CONSTANT.URL_TREE_NAME.DASHBOARD,
//                         },
//                         {
//                             titleLang: CONSTANT.PAGE_NAME.PICKUP,
//                             href: CONSTANT.URL_TREE_NAME.PICKUP,
//                         },
//                         {
//                             titleLang: CONSTANT.PAGE_NAME.REPORT_LIST,
//                         },
//                     ]
//                 }
//             } else {
//                 if (sessionStorage.pickUpType) sessionStorage.pickUpType = null;
//                 navs = [
//                     {
//                         titleLang: CONSTANT.PAGE_NAME.OPERATION_LIST,
//                         href: CONSTANT.URL_TREE_NAME.OPERATION_LIST,
//                     },
//                     {
//                         titleLang: CONSTANT.PAGE_NAME.REPORT_LIST,
//                     },
//                 ]
//             }
//             break;
//         case CONSTANT.URL_TREE_NAME.REPORT_FORM:

//             break;
//     }
//     CONSTANT.URL_TREE_PASS = {
//         1: [CONSTANT.URL_TREE_NAME.DASHBOARD, CONSTANT.URL_TREE_NAME.PICKUP, CONSTANT.URL_TREE_NAME.REPORT_LIST, CONSTANT.URL_TREE_NAME.REPORT_FORM],
//         2: [CONSTANT.URL_TREE_NAME.DASHBOARD, CONSTANT.URL_TREE_NAME.PICKUP, CONSTANT.URL_TREE_NAME.REPORT_FORM],
//         3: [CONSTANT.URL_TREE_NAME.DASHBOARD, CONSTANT.URL_TREE_NAME.MESSAGE_LIST, CONSTANT.URL_TREE_NAME.MESSAGE_DETAIL],
//         4: [CONSTANT.URL_TREE_NAME.DASHBOARD, CONSTANT.URL_TREE_NAME.SEND_MESSAGE],
//         5: [CONSTANT.URL_TREE_NAME.OPERATION_LIST, CONSTANT.URL_TREE_NAME.REPORT_LIST, CONSTANT.URL_TREE_NAME.REPORT_FORM],
//         6: [CONSTANT.URL_TREE_NAME.OPERATION_LIST, CONSTANT.URL_TREE_NAME.REPORT_LIST, CONSTANT.URL_TREE_NAME.PDF_PRINT],
//     };
//     //now url tree
//     // const urlTree = sessionStorage.urlTree ? JSON.parse(sessionStorage.urlTree) : sessionStorage.urlTree;
//     // console.log("kdh check urlTree : " + urlTree);
//     // // return;
//     // if (urlTree) {
//     //     urlList: for (var i = 0; i < urlTree.length; i++) {
//     //         const urlHtmlUrl = urlTree[i].href;
//     //         const urlHtmlHref = urlHtmlUrl.includes('?') ? urlHtmlUrl.substring(0, urlHtmlUrl.indexOf('?')) : urlHtmlUrl;
//     //         for (var v in CONSTANT.URL_TREE_PASS) {
//     //             findUrl:  for (var k in CONSTANT.URL_TREE_PASS[v]) {
//     //                 if (urlHtmlHref == CONSTANT.URL_TREE_PASS[v][k]) {
//     //                     if (htmlName == CONSTANT.URL_TREE_PASS[v][k]) {
//     //                         authFlg = true;
//     //                         break urlList;
//     //                     }
//     //                     continue;
//     //                 } else if ((urlHtmlHref != CONSTANT.URL_TREE_PASS[v][k]) && (htmlName == CONSTANT.URL_TREE_PASS[v][k])) {
//     //                     authFlg = true;
//     //                     break urlList;
//     //                 } else {
//     //                     break findUrl;
//     //                 }
//     //             }
//     //         }
//     //     }
//     // }
                
//     // if (authFlg) {
//     //     const urlPath = htmlName + location.search;
//     //     let pageName = urlPath.substring(0, urlPath.indexOf(".html"));
//     //     if (pageName == CONSTANT.URL.WEB.OPERATION_LIST) pageName = CONSTANT.PAGE_NAME.OPERATION_LIST;
//     //     let addReportFlg = "0"
//     //     if (urlPath.includes(CONSTANT.URL_TREE_NAME.REPORT_FORM) && urlPath.includes("addReport")) {
//     //         addReportFlg = urlPath.substring(urlPath.indexOf("addReport=") + 10);
//     //     }
//     //     let newUrlTree = [];
//     //     if (urlTree) {
//     //         let isAdd = false;
//     //         for (var i = 0; i < urlTree.length; i++) {
//     //             const sessionUrl = urlTree[i].href.includes('?') ? urlTree[i].href.substring(0, urlTree[i].href.indexOf('?')) : urlTree[i].href;
//     //             const urlHtmlName = urlPath.includes('?') ? urlPath.substring(0, urlPath.indexOf('?')) : urlPath;
//     //             if (sessionUrl == urlHtmlName) {
//     //                 if (addReportFlg == "1") {
//     //                     var nav = {titleLang : CONSTANT.PAGE_NAME.REPORT_LIST , href : CONSTANT.URL_TREE_NAME.REPORT_LIST};
//     //                     newUrlTree.push(nav);
//     //                 };
//     //                 var nav = {titleLang : pageName , href : urlPath};
//     //                 newUrlTree.push(nav);
//     //                 isAdd = true;
//     //                 break;
//     //             } else {
//     //                 newUrlTree.push(urlTree[i]);
//     //             };
//     //         };
//     //         if (!isAdd) {
//     //             const nav = {titleLang : pageName , href : urlPath};
//     //             newUrlTree.push(nav);
//     //         }
//     //     } else {
//     //         const nav = {titleLang : pageName , href : urlPath};
//     //         newUrlTree.push(nav);
//     //     }
//     //     sessionStorage.urlTree = JSON.stringify(newUrlTree);
        

//     // } else {
//     //     console.log("kdh check go index");
//     //     // COMMON.avwScreenMove('index.html');
//     // }
// };

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

 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');
    $('#msgModel').text(I18N.i18nText('msgLogoutConfirm'));

    $('#confirmYes').click(HEADER.processLogout);
};

/**
 * 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.goUrlWithCurrentParams(CONSTANT.PAGE_NAME.LOGIN);
            } else {
                if (data.errorMessage) {
                    COMMON.displayAlert(data.errorMessage);
                } else {
                    COMMON.displayAlert('errorOccured');
                }
            }
        },
        function (xmlHttpRequest, txtStatus, errorThrown) {
            if (xmlHttpRequest.errorMessage) {
                COMMON.displayAlert(xmlHttpRequest.errorMessage);
            } else {
                COMMON.displayAlert('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');
     }
 };
 
