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

/**
 * Html element array, 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/';

/**
 * default setting JSON
 */
DashboardSetting.defaultSettingJson = {
    newReport: 1,
    continousWork: 1,
    reportWarning: 1,
    dashboardHome: 0,
}

/**
 * get setting data from cms
 * @param {function} callback 
 */
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);
        }
    });
};

/**
 * apply settings to screen
 * @param {JSON} settings 
 */
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');
                }
            }
        }
    }
};

/**
 * post request to save setting to cms
 */
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 on/off setting
 */
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;
        }
    });
};

/**
 * init data, action when screen onload
 */
DashboardSetting.init = function () {
    DashboardSetting.getSettingData(function (settings) {
        DashboardSetting.applySettings(settings);
    });
    DashboardSetting.bindToggleClick();
};

/**
 * handle click event of save button
 */
DashboardSetting.onClickSave = function () {
    DashboardSetting.saveSetting();
};

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