/**
 * 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: 'reportWarning', 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: 'messageListTitle' },
    {id: 'sendMessage', href: "javascript:DASHBOARD.goSendMessage();", count: 0, enabled: true, img: {src: '../common/img/dahboard_icon_send_message.svg'}, msg: 'sendMessageTitle' },
    {id: 'distanceSupport', href: "javascript:DASHBOARD.goDistanceSupport();", count: 0, enabled: true, img: {src: '../common/img/dahboard_icon_remote.svg'}, msg: 'distanceSupportTitle' },
    {id: 'chat', href: "javascript:DASHBOARD.goChat();", count: 0, enabled: true, img: {src: '../common/img/dahboard_icon_chat.svg'}, msg: 'chatTitle' },
];

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

DASHBOARD.baseApiUrl = CONSTANT.URL.CMS.BASE + ClientData.userInfo_accountPath() + CONSTANT.URL.CMS.API.BASE + 'topScreen/';
DASHBOARD.getDataApiUrl = DashboardSetting.baseApiUrl + 'getAllData';

/**
 * Call api get data 
 */
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  */
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() {
    DASHBOARD.loadCommon();
    DashboardSetting.getSettingData(function (settings) {
        DASHBOARD.getDashboardData(function (dataDashboard) {
            DASHBOARD.updateDataPickups(settings, dataDashboard);
            DASHBOARD.initPickups();
            DASHBOARD.updateDataCommunications(dataDashboard);
            DASHBOARD.initCommunications(); 
            
        });
    }); 
}
/** load common data */
DASHBOARD.loadCommon = function() {
    $("#includedHeader").load("../common/header.html" , function() {
        I18N.initi18n();
    }); 
    $("#includedDashboardSetting").load("dashboard-setting.html", function() {
        DashboardSetting.init();
        I18N.initi18n();
    }); 
    $("#includedConfirmModal").load("../common/confirm-modal.html", function() {
        I18N.initi18n();
    }); 
}

/** Update pickup config from setting dashboard data */
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[item.id]) {
            item.count = dataDashboard[item.id];
        }
    });
}

/** Update communication setting from dashboard data */
DASHBOARD.updateDataCommunications = function(dataDashboard) {
    DASHBOARD.communicationItems.forEach(function(item) {
        if (item.id == 'messageList') {
            item.count = 0;
            if (dataDashboard['pushMesage']) {
                item.count = dataDashboard['pushMesage'];
            } 
        } 
        // else if (item.id == 'chat') {
        //     item.enabled = CHK.isChat;
        // }
    });
}

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

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