<% settings = Setting.plugin_redmine_ux_toolkit || {} %>
<% global_landing_page_type = settings['global_landing_page_type'].to_s %>
<% allow_user_override = RedmineUxToolkit::Services::LandingPageResolver.user_override_allowed? %>

<p>
  <strong><%= l(:label_ux_toolkit_global_landing_page_after_login) %></strong>
</p>

<p>
  <%= hidden_field_tag 'settings[allow_user_landing_page_override]', '0' %>
  <%= check_box_tag 'settings[allow_user_landing_page_override]',
                    '1',
                    allow_user_override,
                    id: 'settings_allow_user_landing_page_override' %>
  <%= label_tag 'settings_allow_user_landing_page_override',
                l(:label_ux_toolkit_allow_user_landing_page_override) %>
  <em class="info"><%= l(:text_ux_toolkit_allow_user_landing_page_override_help) %></em>
</p>

<p>
  <%= label_tag 'settings_global_landing_page_type', l(:label_ux_toolkit_global_landing_page_type) %>
  <%= select_tag 'settings[global_landing_page_type]',
                 options_for_select([
                   [l(:label_ux_toolkit_landing_page_default), ''],
                   [l(:label_ux_toolkit_landing_page_my_page), UxToolkitUserPreference::LANDING_PAGE_TYPE_MY_PAGE],
                   [l(:label_ux_toolkit_landing_page_my_work), UxToolkitUserPreference::LANDING_PAGE_TYPE_MY_WORK],
                   [l(:label_ux_toolkit_landing_page_custom_url), UxToolkitUserPreference::LANDING_PAGE_TYPE_CUSTOM_URL]
                 ], global_landing_page_type),
                 id: 'settings_global_landing_page_type' %>
  <em class="info"><%= l(:text_ux_toolkit_global_landing_page_fallback_help) %></em>
</p>

<p id="ux-toolkit-global-custom-url-row"
   style="<%= global_landing_page_type == UxToolkitUserPreference::LANDING_PAGE_TYPE_CUSTOM_URL ? '' : 'display: none;' %>">
  <%= label_tag 'settings_global_landing_page_value', l(:label_ux_toolkit_global_landing_page_value) %>
  <%= text_field_tag 'settings[global_landing_page_value]',
                     settings['global_landing_page_value'],
                     id: 'settings_global_landing_page_value',
                     disabled: global_landing_page_type != UxToolkitUserPreference::LANDING_PAGE_TYPE_CUSTOM_URL,
                     size: 60 %>
  <em class="info"><%= l(:text_ux_toolkit_global_landing_page_help) %></em>
</p>

<%= javascript_tag do %>
  document.addEventListener('DOMContentLoaded', function() {
    var typeField = document.getElementById('settings_global_landing_page_type');
    var customUrlRow = document.getElementById('ux-toolkit-global-custom-url-row');
    var customUrlField = document.getElementById('settings_global_landing_page_value');

    function updateCustomUrlVisibility() {
      var showCustomUrl = typeField && typeField.value === '<%= UxToolkitUserPreference::LANDING_PAGE_TYPE_CUSTOM_URL %>';

      if (customUrlRow) {
        customUrlRow.style.display = showCustomUrl ? '' : 'none';
      }

      if (customUrlField) {
        customUrlField.disabled = !showCustomUrl;
      }
    }

    if (typeField) {
      typeField.addEventListener('change', updateCustomUrlVisibility);
      updateCustomUrlVisibility();
    }
  });
<% end %>
