<div id="ux-toolkit-settings-root"
     data-ux-toolkit-settings-root
     data-config-warning="<%= l(:text_ux_toolkit_enabled_configuration_incomplete) %>"
     data-unsaved-label="<%= l(:text_ux_toolkit_unsaved_changes) %>">
<%= render_tabs [
  {
    name: 'overview',
    partial: 'settings/redmine_ux_toolkit/overview',
    label: :label_ux_toolkit_tab_overview
  },
  {
    name: 'landing_pages_and_links',
    partial: 'settings/redmine_ux_toolkit/landing_pages_and_links',
    label: :label_ux_toolkit_tab_landing_pages_and_links
  },
  {
    name: 'notices',
    partial: 'settings/redmine_ux_toolkit/notices',
    label: :label_ux_toolkit_tab_notices
  },
  {
    name: 'recent_items',
    partial: 'settings/redmine_ux_toolkit/recent_items',
    label: :label_ux_toolkit_tab_recent_items
  },
  {
    name: 'issues',
    partial: 'settings/redmine_ux_toolkit/issues',
    label: :label_ux_toolkit_tab_issues
  },
  {
    name: 'sla_light',
    partial: 'settings/redmine_ux_toolkit/sla_light',
    label: :label_ux_toolkit_tab_sla_light
  },
  {
    name: 'scheduled_tasks',
    partial: 'settings/redmine_ux_toolkit/scheduled_tasks',
    label: :label_ux_toolkit_tab_scheduled_tasks
  },
  {
    name: 'attachments',
    partial: 'settings/redmine_ux_toolkit/attachments',
    label: :label_ux_toolkit_tab_attachments
  },
  {
    name: 'ticket_templates_and_text_snippets',
    partial: 'settings/redmine_ux_toolkit/ticket_templates_and_text_snippets',
    label: :label_ux_toolkit_tab_ticket_templates_and_text_snippets
  },
  {
    name: 'custom_css',
    partial: 'settings/redmine_ux_toolkit/custom_css',
    label: :label_ux_toolkit_tab_custom_css_js
  }
] %>

<template id="ux-toolkit-scheduler-tab-icon">
  <%= ux_toolkit_sprite_icon('time', nil, icon_only: true) %>
</template>

<%= javascript_tag do %>
  document.addEventListener('DOMContentLoaded', function() {
    var storageKey = 'redmine_ux_toolkit_settings_tab';
    var settingsContainer = document.getElementById('settings');
    var form = settingsContainer && settingsContainer.querySelector('form');

    if (form) {
      form.addEventListener('submit', function() {
        var selectedTab = settingsContainer.querySelector('.tabs a.selected');

        if (selectedTab && selectedTab.id && selectedTab.id.indexOf('tab-') === 0) {
          window.sessionStorage.setItem(storageKey, selectedTab.id.replace('tab-', ''));
        }
      });
    }

    var schedulerTab = document.getElementById('tab-scheduled_tasks');
    var schedulerIconTemplate = document.getElementById('ux-toolkit-scheduler-tab-icon');
    if (schedulerTab && schedulerIconTemplate && !schedulerTab.querySelector('svg')) {
      var schedulerIcon = schedulerIconTemplate.content.firstElementChild;
      if (schedulerIcon) {
        schedulerTab.prepend(schedulerIcon.cloneNode(true));
        schedulerTab.classList.add('ux-toolkit-settings-tab-with-icon');
      }
    }

    if (window.location.search.indexOf('tab=') !== -1) {
      return;
    }

    var tabName = window.sessionStorage.getItem(storageKey);
    var tabAliases = {
      landing_pages: 'landing_pages_and_links',
      links: 'landing_pages_and_links',
      my_work: 'landing_pages_and_links',
      modern_ui: 'landing_pages_and_links',
      ticket_templates: 'ticket_templates_and_text_snippets',
      text_snippets: 'ticket_templates_and_text_snippets',
      attachment_storage: 'attachments',
      attachment_previews: 'attachments',
      issue_reminders: 'scheduled_tasks',
      repository_refresh: 'scheduled_tasks',
      system: 'scheduled_tasks'
    };

    if (tabAliases[tabName]) {
      tabName = tabAliases[tabName];
    }

    var tabLink = tabName && document.getElementById('tab-' + tabName);

    if (tabLink) {
      window.sessionStorage.removeItem(storageKey);
      tabLink.click();
    }
  });
<% end %>
</div>
