var DashboardSetting = {};
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.saveSettingApiUrl = DashboardSetting.baseApiUrl + 'saveSetting';
DashboardSetting.getSettingApiUrl = DashboardSetting.baseApiUrl + 'getAllSettingData';

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

DashboardSetting.getSettingData = function (callback) {
    let param = {
        sid: COMMON.getSid(),
    };
    COMMON.cmsAjax(DashboardSetting.getSettingApiUrl, 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)) {
            const 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 (key, enabled) {
    if (DashboardSetting.elementItems.hasOwnProperty(key)) {
        const elementItem = DashboardSetting.elementItems[key];
        elementItem.enabled = enabled;
    }
    let param = {};
    param.sid = COMMON.getSid();
    param[key] = enabled;
    COMMON.cmsAjax(DashboardSetting.saveSettingApiUrl, param, false, function (json) {
        
    });
};

//設定画面でクリックされてた時の処理
DashboardSetting.bindToggleClick = function () {
    $('.toggle').on('click', function () {
        $(this).toggleClass('checked');
        const key = $(this).attr('data-key');
        var enabled = false;
        if ($(this).hasClass('checked')) {
            enabled = true;
        }
        DashboardSetting.saveSetting(key, enabled);
    });
};

//設定画面の初期処理
DashboardSetting.init = function () {
    DashboardSetting.getSettingData(function (settings) {
        DashboardSetting.applySettings(settings);
    });
    DashboardSetting.bindToggleClick();
};

DashboardSetting.onClickSave = function () {
    console.log("DashboardSetting.save");
};
