<% preference = @ux_toolkit_user_preference || UxToolkitUserPreference.find_or_initialize_by(user_id: User.current.id) %>
<% user_override_allowed = defined?(@ux_toolkit_user_override_allowed) ? @ux_toolkit_user_override_allowed : RedmineUxToolkit::Services::LandingPageResolver.user_override_allowed? %>

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

<p>
  <%= label_tag 'ux_toolkit_user_preference_landing_page_type', l(:label_ux_toolkit_landing_page_type) %>
  <%= select_tag 'ux_toolkit_user_preference[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]
                 ], preference.landing_page_type),
                 id: 'ux_toolkit_user_preference_landing_page_type',
                 disabled: !user_override_allowed %>
  <% unless user_override_allowed %>
    <em class="info"><%= l(:text_ux_toolkit_user_landing_page_disabled_by_admin) %></em>
  <% end %>
</p>

<p id="ux-toolkit-custom-url-row"
   style="<%= preference.landing_page_type == UxToolkitUserPreference::LANDING_PAGE_TYPE_CUSTOM_URL ? '' : 'display: none;' %>">
  <%= label_tag 'ux_toolkit_user_preference_landing_page_value', l(:label_ux_toolkit_custom_url) %>
  <%= text_field_tag 'ux_toolkit_user_preference[landing_page_value]',
                     preference.landing_page_value,
                     id: 'ux_toolkit_user_preference_landing_page_value',
                     disabled: !user_override_allowed || preference.landing_page_type != UxToolkitUserPreference::LANDING_PAGE_TYPE_CUSTOM_URL,
                     size: 60 %>
  <em class="info"><%= l(:text_ux_toolkit_landing_page_help) %></em>
</p>

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

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

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

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

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