<% member = local_assigns[:member] %>
<% team = local_assigns[:team] %>
<% form_url = member.persisted? ? public_team_roster_member_path(team, member) : public_team_roster_members_path(team) %>
<% form_method = member.persisted? ? :patch : :post %>
<% is_player = member.category == "player" %>
<% player_role = member.role_label if member.role_label.in?(TeamRosterMember::VOLLEYBALL_PLAYER_ROLES) %>

<%= form_with url: form_url, method: form_method, multipart: true, class: "roster-member-form card roster-sidebar__form", data: { roster_form: true } do %>
  <h3 class="roster-sidebar__title"><%= member.persisted? ? "Modifica scheda" : "Aggiungi persona" %></h3>
  <p class="roster-sidebar__lead">Compila i campi e salva per aggiornare l’organico.</p>

  <%= label_tag "team_roster_member[category]", "Gruppo" %>
  <%= select_tag "team_roster_member[category]", options_for_select(roster_category_options, member.category),
        data: { roster_category: true } %>

  <%= label_tag "team_roster_member[full_name]", "Nome e cognome" %>
  <%= text_field_tag "team_roster_member[full_name]", member.full_name, required: true, placeholder: "es. Mario Rossi" %>

  <div data-roster-role-fields>
    <div data-roster-role-player <%= "hidden" unless is_player %>>
      <%= label_tag "team_roster_member[role_label]", "Ruolo in campo" %>
      <%= select_tag "team_roster_member[role_label]", roster_player_role_options(player_role),
            data: { roster_role_player: true }, disabled: !is_player %>
    </div>
    <div data-roster-role-other <%= "hidden" if is_player %>>
      <%= label_tag "team_roster_member[role_label_other]", "Ruolo (opzionale)" %>
      <%= text_field_tag "team_roster_member[role_label_other]", (member.role_label unless is_player),
            placeholder: "es. Capo allenatore, Presidente, Massaggiatore",
            data: { roster_role_other: true }, disabled: is_player %>
    </div>
  </div>

  <div data-roster-jersey-row <%= "hidden" unless is_player %>>
    <%= label_tag "team_roster_member[jersey_number]", "Numero maglia" %>
    <%= number_field_tag "team_roster_member[jersey_number]", member.jersey_number, min: 1, max: 99,
          placeholder: "es. 7", data: { roster_jersey: true }, disabled: !is_player %>
  </div>

  <%= label_tag "team_roster_member[bio]", "Descrizione breve" %>
  <%= text_area_tag "team_roster_member[bio]", member.bio, rows: 3, placeholder: "Breve presentazione per la scheda…" %>

  <%= label_tag "team_roster_member[photo_file]", "Foto" %>
  <% if member.photo_url.present? %>
    <div class="roster-form-photo-preview"><%= roster_person_avatar(member, team, size: 72) %></div>
  <% end %>
  <%= file_field_tag "team_roster_member[photo_file]", accept: "image/png,image/jpeg,image/webp" %>
  <p class="branding-hint">Senza foto verrà mostrato un avatar con i colori della squadra.</p>

  <%= submit_tag (member.persisted? ? "Salva scheda" : "Aggiungi all'organico"), class: "btn btn-primary" %>
<% end %>
