var CHK_DashboardSetting = {};
CHK_DashboardSetting.displayItems = [
    { id:'dashboardSettingNewReport', name: 'chk-new', enabled: true, title:'新規報告', msg: 'dashboardSettingNewRegistrationTitle'},
    { id:'dashboardSettingContinousWork', name: 'chk-proccess', enabled: true, title:'工程作業', msg: 'dashboardSettingContinousWorkTitle'},
    { id:'dashboardSettingReportWarning', name: 'chk-alert', enabled: true, title:'警告を含む作業', msg: 'dashboardSettingReportWarningTitle'},
    { id:'dashboardSettingDashboardHome', name: 'chk-home', enabled: false, title:'ダッシュボードをホーム画面にする', msg: 'dashboardSettingDashboardHomeTitle'},
];

CHK_DashboardSetting.loadLocalSettings = function() {
    CHK_DashboardSetting.displayItems.forEach(function(item) {
        let enabled = CHK_DashboardSetting.isSettingEnabled(item.id);
        if (enabled == true || enabled == false) {
            item.enabled = enabled;
        }
    });
}

CHK_DashboardSetting.saveLocalSetting = function(key, value) {
    localStorage.setItem(key, value);
}

CHK_DashboardSetting.isSettingEnabled = function(key) {
    var setting = localStorage.getItem(key);
    if (setting == '1') {
        return true;
    } else if (setting == '0') {
        return false;
    }
    return setting;
}

CHK_DashboardSetting.initDisplays = function() {
    $("#displayItems tbody").empty();
    CHK_DashboardSetting.displayItems.forEach(function(item) {
        var html = CHK_DashboardSetting.initHtmlItem(item);
        $("#displayItems tbody").append(html);
    });
}

CHK_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;
}

CHK_DashboardSetting.bindToggleClick = function() {
    $(".toggle").on("click", function() {
        $(this).toggleClass("checked");
        var id = $(this).attr('id');
        if ($(this).hasClass('checked')) {
            CHK_DashboardSetting.saveLocalSetting(id, '1');
        } else {
            CHK_DashboardSetting.saveLocalSetting(id, '0');
        }
    });
}

CHK_DashboardSetting.init = function() {
    CHK_DashboardSetting.loadLocalSettings();
    CHK_DashboardSetting.initDisplays();
    $("#footer").load("main-footer.html", function() {
        CHK_Footer.activeDashboardBottomNav();
    });
    CHK_DashboardSetting.bindToggleClick();
}

CHK_DashboardSetting.goBack = function() {
    history.back();
}
