/**
 * Dashboard js in dashboard.html
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */
var DASHBOARD = {};

/** pickup dashboard configuration */
DASHBOARD.pickupItems = [
    { id: 'newReport', href: "javascript:DASHBOARD.goPickup('newReport');", count: 0, enabled: true, img: { src: '../common/img/dahboard_icon_new.svg' }, msg: 'newRegistrationTitle' },
    { id: 'continousWork', href: "javascript:DASHBOARD.goPickup('continousWork');", count: 0, enabled: true, img: { src: '../common/img/dahboard_icon_proccess.svg' }, msg: 'continousWorkTitle' },
    { id: 'warningReport', href: "javascript:DASHBOARD.goPickup('reportWarning');", count: 0, enabled: true, img: { src: '../common/img/dahboard_icon_alert.svg' }, msg: 'reportWarningTitle' },
];

/** comunication dashboard configuration */
DASHBOARD.communicationItems = [
    { id: 'messageList', href: 'javascript:DASHBOARD.goMessageList();', count: 0, enabled: true, img: { src: '../common/img/dahboard_icon_message_list.svg' }, msg: 'pushMessageList' },
    { id: 'sendMessage', href: 'javascript:DASHBOARD.goSendMessage();', count: 0, enabled: true, img: { src: '../common/img/dahboard_icon_send_message.svg' }, msg: 'sendMessage' },
];

/**   Default dashboard setting data */
DASHBOARD.defaultDashboard = {
    newReport: 0,
    continousWork: 0,
    warningReport: 0,
    pushMesage: 0,
};

DASHBOARD.getDataApiUrl = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.DASHBOARD;

/**
 * Call api get data
 * @param callback
 */
DASHBOARD.getDashboardData = function (callback) {
    let param = {
        sid: COMMON.getSid(),
    };
    COMMON.cmsAjax(
        DASHBOARD.getDataApiUrl,
        param,
        false,
        function (json) {
            if (callback) {
                callback(json);
            }
        },
        function () {
            console.log('DASHBOARD.getData error');
            if (callback) {
                callback(DASHBOARD.defaultDashboard);
            }
        },
    );
};

/** Initialization dashboard pickup items html */
DASHBOARD.initPickups = function () {
    $('#pickupItems').empty();
    DASHBOARD.pickupItems.forEach(function (item) {
        if (item.enabled == true) {
            var html = DASHBOARD.initHtmlItem(item);
            $('#pickupItems').append(html);
        }
    });
    //pickup empty
    if ($('#pickupItems').children().length > 0) {
        $('#pickupHeader').removeClass('d-none');
    } else {
        $('#pickupHeader').addClass('d-none');
    }
};

/** Initialization dashboard communication items html */
DASHBOARD.initCommunications = function () {
    $('#communicationItems').empty();
    DASHBOARD.communicationItems.forEach(function (item) {
        if (item.enabled == true) {
            const html = DASHBOARD.initHtmlItem(item);
            $('#communicationItems').append(html);
        }
    });
};

/**
 * Initialization dashboard items html
 * @param {*} item
 * @returns
 */
DASHBOARD.initHtmlItem = function (item) {
    let countText = '' + item.count;
    if (item.count >= 100) {
        countText = '99+';
    }
    let countDClass = '';
    let countSpan = '';
    if (typeof item.count != 'undefined' && item.count > 0) {
        countDClass = ' ';
        countSpan = '<span class="fs-8 circle-tag-danger' + countDClass + '">' + countText + '</span>';
    }
    let html = $(
        '<li class="card mb-2 position-relative" id="' +
            item.id +
            '">' +
            '<a href="' +
            item.href +
            '" class="d-block text-dark text-decoration-none mb-1 p-3 text-center">' +
            '<div class="bg-lightblue p-2 d-inline-block rounded-circle mb-3">' +
            '<img src="' +
            item.img.src +
            '" alt="' +
            I18N.i18nText(item.msg) +
            '" class="img-fluid"> </div>' +
            '<div class="fs-10 lang" lang="' +
            item.msg +
            '">' +
            I18N.i18nText(item.msg) +
            '</div>' +
            countSpan +
            '</a>' +
            '</li>',
    );
    return html;
};

/** Initialization dashboard screen */
DASHBOARD.init = function () {
    //Check if user is logged in
    COMMON.checkAuth(false);

    sessionStorage.activeHomePage = CONSTANT.PAGE_TAB.DASHBOARD;
    sessionStorage.removeItem("operationId");
    DASHBOARD.loadCommon();
    DashboardSetting.getSettingData(function (settings) {
        DASHBOARD.getDashboardData(function (dataDashboard) {
            DASHBOARD.updateDataPickups(settings, dataDashboard);
            DASHBOARD.initPickups();
            DASHBOARD.updateDataCommunications(dataDashboard);
            DASHBOARD.initCommunications();
        });
    });
    COMMON.closeLoading();
};
/** load common data */
DASHBOARD.loadCommon = function () {
    TEMPLATE.loadHeader('#includedHeader');
    TEMPLATE.loadDashboardSetting('#includedDashboardSetting', DASHBOARD.changeSettingCallback);
    TEMPLATE.loadConfirmModal('#includedConfirmModal');
    TEMPLATE.loadMainNavsTitle('#includedMainTitle', 'dashboard', null, DASHBOARD.loadMainTitleCallback);
};

/**
 * Update pickup config from setting dashboard data
 * @param {*} settings
 * @param {*} dataDashboard
 */
DASHBOARD.updateDataPickups = function (settings, dataDashboard) {
    DASHBOARD.pickupItems.forEach(function (item) {
        const enabled = settings[item.id];
        if (enabled == true || enabled == false) {
            item.enabled = enabled;
        }
        item.count = 0;
        if (dataDashboard.dashboard[item.id]) {
            item.count = dataDashboard.dashboard[item.id];
        }
    });
};

/**
 * Update communication setting from dashboard data
 * @param {*} dataDashboard
 */
DASHBOARD.updateDataCommunications = function (dataDashboard) {
    DASHBOARD.communicationItems.forEach(function (item) {
        if (item.id == 'messageList') {
            item.count = 0;

            if (dataDashboard.dashboard['pushMesage']) {
                item.count = dataDashboard.dashboard['pushMesage'];
            }
        }
    });
};

/**
 * Direct to pickup screen
 * @param {*} pickupId
 */
DASHBOARD.goPickup = function (pickupId) {
    COMMON.goUrlWithCurrentParams('pickup.html', { pickupActive: pickupId });
};

/** Direct to Message list screen */
DASHBOARD.goMessageList = function () {
    COMMON.goUrlWithCurrentParams('pushMessageList.html', {});
};

/**
 * Direct to send message screen
 */
DASHBOARD.goSendMessage = function () {
    COMMON.goUrlWithCurrentParams('sendMessage.html', {});
};

/**
 * callback when setting changed
 */
DASHBOARD.changeSettingCallback = function () {
    location.reload();
};

/**
 * callback when complete load main title html
 */
DASHBOARD.loadMainTitleCallback = function () {
    //add dashboard setting item
    var elmA = $('<a href="#" data-toggle="modal" data-target="#dashboard-setting-modal">');
    let elmImg = $('<img src="../common/img/icon_dashboard_setting.svg" alt="" class="p-1 w-40px lang" lang="dashboardSettings" data-toggle="tooltip" data-placement="bottom" title="">');
    elmImg.attr('title', I18N.i18nText('dashboardSettings'));
    elmA.append(elmImg);
    $('#mainTitleHeader').after(elmA);
};
