<% content_for :title, current_user == @user ? "Change your personal details" : "Change #{@user.name.presence || 'this user'}’s personal details" %>

<% content_for :before_content do %>
  <%= govuk_back_link(href: :back) %>
<% end %>

<%= form_for(@user, as: :user, html: { method: :patch }) do |f| %>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      <% remove_attributes_from_error_messages(@user) %>
      <%= f.govuk_error_summary %>

      <h1 class="govuk-heading-l">
        <%= content_for(:title) %>
      </h1>

      <%= f.govuk_text_field :name,
        label: { text: "Name", size: "m" },
        autocomplete: "name" %>

      <%= f.govuk_email_field :email,
        label: { text: "Email address", size: "m" },
        autocomplete: "email",
        spellcheck: "false" %>

      <%= f.govuk_phone_field :phone,
        label: { text: "Telephone number", size: "m" },
        autocomplete: "tel-national",
        spellcheck: "false" %>

      <%= f.govuk_phone_field :phone_extension,
        label: { text: "Extension number (optional)", size: "m" },
        autocomplete: "tel-extension",
        spellcheck: "false" %>

      <% if UserPolicy.new(current_user, @user).edit_organisation? %>
        <% null_option = [OpenStruct.new(id: "", name: "Select an option")] %>
        <% organisations = Organisation.filter_by_active.map { |org| OpenStruct.new(id: org.id, name: org.name) } %>
        <% answer_options = null_option + organisations %>

        <%= f.govuk_select(:organisation_id,
          label: { text: "Organisation", size: "m" },
          "data-controller": "accessible-autocomplete") do %>
            <% answer_options.each do |answer| %>
              <option value="<%= answer.id %>"
                data-synonyms="<%= answer_option_synonyms(answer.resource) %>"
                data-append="<%= answer_option_append(answer.resource) %>"
                data-hint="<%= answer_option_hint(answer.resource) %>"
                <%= @user.organisation_id == answer.id ? "selected" : "" %>><%= answer.name || answer.resource %></option>
            <% end %>
        <% end %>
      <% end %>

      <% if UserPolicy.new(current_user, @user).edit_roles? %>
        <% roles = current_user.assignable_roles.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) } %>

        <%= f.govuk_collection_radio_buttons :role,
          roles,
          :id,
          :name,
          legend: { text: "Role", size: "m" } %>
      <% end %>

      <%= f.govuk_submit "Save changes" %>
    </div>
  </div>
<% end %>