var DashboardSetting = {};
DashboardSetting.displayItems = [
    { id: 'newReport', name: 'newReport', enabled: true, title: '新規報告', msg: 'dashboardSettingNewRegistrationTitle' },
    { id: 'continousWork', name: 'continousWork', enabled: true, title: '工程作業', msg: 'dashboardSettingContinousWorkTitle' },
    { id: 'reportWarning', name: 'reportWarning', enabled: true, title: '警告を含む作業', msg: 'dashboardSettingReportWarningTitle' },
    { id: 'dashboardHome', name: 'dashboardHome', enabled: false, title: 'ダッシュボードをホーム画面にする', msg: 'dashboardSettingDashboardHomeTitle' },
];

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');
    });
};

//CHK_DashboardSetting.displayItemsに設定されている情報から✓されている項目をhtml上に設定する
DashboardSetting.loadSettings = function (settings) {
    DashboardSetting.displayItems.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.displayItems.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) {
        
    });
};

//CHK_DashboardSetting.displayItemsからhtmlに設定画面を表示する
DashboardSetting.initDisplays = function () {
    $('#displayItems tbody').empty();
    DashboardSetting.displayItems.forEach(function (item) {
        var html = DashboardSetting.initHtmlItem(item);
        $('#displayItems tbody').append(html);
    });
};

//設定画面に表示するhtmlを生成する
DashboardSetting.initHtmlItem = function (item) {
    var titleDiv = '<div class="font-weight-normal text-dark multi-lang" data-msg="' + item.msg + '">' + item.title + '</div>';
    var toggleClass = 'toggle';
    if (item.enabled) {
        toggleClass += ' checked';
    }
    var toggleDiv = '<div id="' + item.id + '" class="' + toggleClass + '"><input type="checkbox" name="' + item.name + '" /></div>';
    var html = $('<tr>' + '<th>' + titleDiv + '</th>' + '<td>' + toggleDiv + '</td>' + '</tr>');
    return html;
};

//設定画面でクリックされてた時の処理
//
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 () {
    FOOTER.initFooter();
    DashboardSetting.getSettingData(function (settings) {
        DashboardSetting.loadSettings(settings);
        DashboardSetting.initDisplays();
        DashboardSetting.bindToggleClick();
    });
};

//戻るボタン
DashboardSetting.goBack = function () {
    FOOTER.goDashboard();
};
