var DashboardSetting = {};
DashboardSetting.elementItems = [
    { id: 'newReport', name: 'chk-new', enabled: true },
    { id: 'continousWork', name: 'chk-proccess', enabled: true },
    { id: 'reportWarning', name: 'chk-alert', enabled: true },
    { id: 'dashboardHome', 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.dummySettingJson = {
    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');
    });
};

DashboardSetting.applySettings = function (settings) {
    DashboardSetting.elementItems.forEach(function (item) {
        let enabled = settings[item.id];
        if (typeof enabled !== 'undefined') {
            item.enabled = enabled;
            let itemElement = $('#' + item.id);
            if (item.enabled) {
                itemElement.addClass('checked');
            } else {
                itemElement.removeClass('checked');
            }
        }
    });
};

//CHK_DashboardSetting.displayItemsに設定を書き込む
DashboardSetting.saveSetting = function (key, enabled) {
    DashboardSetting.elementItems.forEach(function (item) {
        if (item.id == key) {
            item.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');
        // var id = $(this).attr('id');
        // var enabled = false;
        // if ($(this).hasClass('checked')) {
        //     enabled = true;
        // }
        // DashboardSetting.saveSetting(id, enabled);
    });
};

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

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