{% comment %}
This fragment renders a friend code selector used for filtering or form submissions.
Expected variables:
  - friend_codes: A list or QuerySet of FriendCode objects.
  - selected_friend_code (optional): The currently selected FriendCode. If not provided, the user's default friend code is used.
  - field_name (optional): The name/id for the input element (default "friend_code").
  - label (optional): The label text (default None).
{% endcomment %}

{% with field_name=field_name|default:"friend_code" label=label|default:"" %}
  {% with effective_friend_code=selected_friend_code|default:request.user.default_friend_code %}
    {% if friend_codes|length > 1 %}
      <div class="form-control">
        {% if label and label != "" %}
          <label for="{{ field_name }}" class="label">
            <span class="label-text p-2 rounded">{{ label }}</span>
          </label>
        {% endif %}
        <select id="{{ field_name }}" name="{{ field_name }}" class="select select-bordered w-full" @change="$el.form.submit()">
          {% for code in friend_codes %}
            <option value="{{ code.pk }}" {% if effective_friend_code and code.pk|stringformat:"s" == effective_friend_code.pk|stringformat:"s" %}selected{% endif %}>
              {{ code.in_game_name }}
            </option>
          {% endfor %}
        </select>
      </div>
    {% else %}
      <input type="hidden" id="{{ field_name }}" name="{{ field_name }}" value="{{ friend_codes.0.pk }}">
    {% endif %}
  {% endwith %}
{% endwith %} 