% content_for :title, "Invite somebody to submit CORE data" %>
<% content_for :before_content do %>
  <%= govuk_back_link(href: :back) %>
<% end %>
<%= form_for(@resource, as: :user, html: { method: :post }) do |f| %>
  
    
      <%= f.govuk_error_summary %>
      
        <%= content_for(:title) %>
      
      <%= f.govuk_text_field :name,
        autocomplete: "name",
        label: { text: "Name", size: "m" } %>
      <%= f.govuk_email_field :email,
        label: { text: "Email address", size: "m" },
        autocomplete: "email",
        spellcheck: "false",
        value: @resource.email %>
      <% if current_user.support? %>
        <% null_option = [OpenStruct.new(id: "", name: "Select an option")] %>
        <% organisations = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } %>
        <% answer_options = null_option + organisations %>
        <% if @organisation_id %>
          <% organisation = Organisation.find(@organisation_id) %>
          <% answer_options = [OpenStruct.new(id: organisation.id, name: organisation.name)] %>
        <% end %>
          <%= f.govuk_collection_select :organisation_id,
            answer_options,
            :id,
            :name,
            "data-controller": "accessible-autocomplete",
            label: { text: "Organisation", size: "m" },
            options: { disabled: [""], selected: @organisation_id ? answer_options.first : "" } %>
      <% end %>
      <% hints_for_roles = { data_provider: ["Can view and submit logs for this organisation"], data_coordinator: ["Can view and submit logs for this organisation and any of its managing agents", "Can manage details for this organisation", "Can manage users for this organisation"], support: nil } %>
      <% roles_with_hints = current_user.assignable_roles.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize, description: hints_for_roles[key.to_sym]) } %>
      <%= f.govuk_collection_radio_buttons :role,
                                           roles_with_hints,
                                           :id,
                                           :name,
                                           lambda { |option|
                                             option.description&.map { |hint| content_tag(:li, hint) }&.reduce(:+)
                                           },
                                           legend: { text: "Role (optional)", size: "m" },
                                           hint: { text: "You do not need to select a role if the user is a data protection officer only. You can tell us that this user is a data protection officer after you have invited them." } %>
      <%= f.govuk_submit "Continue" %>
    
   
<% end %>