/**
 * Dashboard Setting js in dashboard.html
 *
 * @since 1.0 check web
 */
var DashboardSetting = {};

//setting html elements, map with json key from setting data API
DashboardSetting.elementItems = {
    newReport: { id: 'settingNewReport', name: 'chk-new', enabled: true },
    continousWork: { id: 'settingContinousWork', name: 'chk-proccess', enabled: true },
    reportWarning: { id: 'settingReportWarning', name: 'chk-alert', enabled: true },
    dashboardHome: { id: 'settingDashboardHome', name: 'chk-home', enabled: false },
};

DashboardSetting.baseApiUrl = CONSTANT.URL.CMS.BASE + ClientData.userInfo_accountPath() + CONSTANT.URL.CMS.API.BASE + 'dashboardSetting/';

DashboardSetting.defaultSettingJson = {
    newReport: 1,
    continousWork: 1,
    reportWarning: 1,
    dashboardHome: 0,
}

DashboardSetting.getSettingData = function (callback) {
    let param = {
        sid: COMMON.getSid(),
    };
    const url = DashboardSetting.baseApiUrl + 'getAllSettingData';
    COMMON.cmsAjax(url, param, false, function (json) {
        if (callback) {
            callback(json);
        }
    }, function() {
        console.log('DashboardSetting.getSettingData error');
        if (callback) {
            callback(DashboardSetting.defaultSettingJson);
        }
    });
};

DashboardSetting.applySettings = function (settings) {
    for (const key in settings) {
        const enabled = settings[key];
        if (DashboardSetting.elementItems.hasOwnProperty(key)) {
            //apply enabled, disabled setting
            let elementItem = DashboardSetting.elementItems[key];
            elementItem.enabled = enabled;
            let element = $('#' + elementItem.id);
            if (element) {
                if (enabled) {
                    element.addClass('checked');
                } else {
                    element.removeClass('checked');
                }
            }
        }
    }
};

DashboardSetting.saveSetting = function () {
    let param = {};
    param.sid = COMMON.getSid();
    for (const key in DashboardSetting.elementItems) {
        const element = DashboardSetting.elementItems[key];
        param[key] = element.enabled;
    }
    const url = DashboardSetting.baseApiUrl + 'saveSetting';
    COMMON.cmsAjax(url, param, false, function (json) {
        DashboardSetting.closeModal();
    }, function() {
        console.log('DashboardSetting.saveSetting error');
        DashboardSetting.closeModal();
    });
};

//handle click on/off of setting item
DashboardSetting.bindToggleClick = function () {
    $('.toggle').on('click', function () {
        $(this).toggleClass('checked');
        const key = $(this).attr('data-key');
        let enabled = false;
        if ($(this).hasClass('checked')) {
            enabled = true;
        }
        if (DashboardSetting.elementItems.hasOwnProperty(key)) {
            let elementItem = DashboardSetting.elementItems[key];
            elementItem.enabled = enabled;
        }
    });
};

DashboardSetting.init = function () {
    DashboardSetting.getSettingData(function (settings) {
        DashboardSetting.applySettings(settings);
    });
    DashboardSetting.bindToggleClick();
};

DashboardSetting.onClickSave = function () {
    DashboardSetting.saveSetting();
};

DashboardSetting.closeModal = function () {
    $('#dashboard-setting-modal .close').click();
};
