{% extends "admin_base.html" %}
{% block title %}Game Cards | Nexora Admin{% endblock %}
{% block content %}
<div class="admin-head">
  <p class="eyebrow">Games</p>
  <h1>Game Cards</h1>
  <p>Game cards appear on the homepage and registration page. Add original prices here; discounted prices are calculated
    automatically from the discount percentage.</p>
</div>

{% if can('games_create') %}
<section class="admin-panel">
  <h2><i class="fas fa-plus"></i> Add New Game</h2>
  <form class="admin-form" method="post" enctype="multipart/form-data" data-game-admin-form>
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
    <label>Game Title<input name="title" placeholder="e.g. PUBG Mobile" required></label>
    <label>Platform<input name="platform" placeholder="e.g. Mobile / PC / PS5" required></label>

    <div class="field-group-header"><i class="fas fa-users"></i> Team Size</div>
    <div class="form-two-col">
      <label>Min Members<input type="number" name="min_members" min="1" max="4" value="1" required
          data-min-members></label>
      <label>Max Members<input type="number" name="max_members" min="1" max="4" value="1" required
          data-max-members></label>
    </div>

    <div class="field-group-header"><i class="fas fa-tag"></i> Original Per-Size Pricing</div>
    <label data-price-row="1">Original Price — 1 Player (Solo) <span class="field-hint">per person</span><input
        name="individual_price" placeholder="e.g. Rs. 600" data-price="1"></label>
    <label data-price-row="2">Original Price — 2 Players <span class="field-hint">(auto-filled: 2× solo)</span><input
        name="price_for_2" placeholder="e.g. Rs. 1,200" data-price="2"></label>
    <label data-price-row="3">Original Price — 3 Players <span class="field-hint">(auto-filled: 3× solo)</span><input
        name="price_for_3" placeholder="e.g. Rs. 1,800" data-price="3"></label>
    <label data-price-row="4">Original Price — 4 Players <span class="field-hint">(auto-filled: 4× solo)</span><input
        name="price_for_4" placeholder="e.g. Rs. 2,400" data-price="4"></label>

    <div class="field-group-header"><i class="fas fa-percent"></i> Auto Discount <span
        class="field-hint">(optional)</span></div>
    <label>Discount Label <span class="field-hint">(e.g. Early Bird, Launch Offer)</span><input name="discount_label"
        placeholder="e.g. Early Bird 20% Off"></label>
    <label>Discount Percentage <span class="field-hint">(discounted price auto-calculates)</span><input type="number"
        name="discount_pct" min="0" max="100" step="0.01" placeholder="e.g. 20"></label>
    <div class="form-two-col">
      <label>Discount From <span class="field-hint">(blank = starts now)</span><input type="datetime-local"
          name="discount_from"></label>
      <label>Discount To <span class="field-hint">(blank = no end date)</span><input type="datetime-local"
          name="discount_to"></label>
    </div>

    <div class="field-group-header"><i class="fas fa-calendar"></i> Registration Window</div>
    <div class="form-two-col">
      <label>Registration Opens <span class="field-hint">(blank = open now)</span>
        <input type="datetime-local" name="reg_from"></label>
      <label>Registration Closes <span class="field-hint">(blank = no limit)</span>
        <input type="datetime-local" name="reg_to"></label>
    </div>

    <label>Game Image <span class="field-hint">(16:10 best)</span><input type="file" name="image"
        accept="image/*"></label>
    <label class="check"><input type="checkbox" name="active" checked> Show on website</label>
    <button class="btn primary" type="submit"><i class="fas fa-plus"></i> Create Card</button>
  </form>
</section>
{% endif %}

<section class="admin-panel">
  <h2><i class="fas fa-layer-group"></i> Existing Games</h2>
  {% if games %}
  <div class="admin-card-grid compact-admin-grid">
    {% for game in games %}
    {% set open_now, state_label = game_status(game, settings.registration_open) %}
    {% set gm_min = game.min_members|int if game.min_members is defined else 1 %}
    {% set gm_max = game.max_members|int if game.max_members is defined else 1 %}
    {% set has_discount = discount_active(game) %}
    <article class="mini-admin-card compact-manage-card">
      {% set game_img = img(game.image) if game.image else '' %}
      {% if game_img %}
      <img class="thumb game-admin-thumb" src="{{ url_for('serve_media', filename=game_img) }}"
        alt="{{ game.title }}">
      {% endif %}
      <div class="compact-card-head">
        <div>
          <h3>{{ game.title }}</h3>
          <p>{{ game.platform }}</p>
        </div>
      </div>
      <div class="compact-badges">
        <span class="badge {{ 'paid' if open_now else 'unpaid' }}">{{ state_label }}</span>
        <span class="badge neutral">{{ 'Visible' if game.active else 'Hidden' }}</span>
        {% if gm_min != gm_max %}
        <span class="badge neutral"><i class="fas fa-users"></i> {{ gm_min }}–{{ gm_max }} Players</span>
        {% else %}
        <span class="badge neutral"><i class="fas fa-user"></i> {{ gm_min }} Player</span>
        {% endif %}
        {% if has_discount %}
        <span class="badge amber"><i class="fas fa-percent"></i> {{ discount_label_text(game) }}</span>
        {% endif %}
      </div>
      <div class="compact-prices">
        {% if game.individual_price and gm_min <= 1 %} {% set
          p=game.individual_price|disc_price(discount_pct_value(game) if has_discount else 0) %} <p><small>Solo
            (1)</small>{% if p.active %}<strong>{{ p.discounted }}</strong><del>{{ p.original }}</del>{% else
          %}<strong>{{ p.original }}</strong>{% endif %}</p>
          {% endif %}
          {% if game.price_for_2 and gm_max >= 2 %}
          {% set p = game.price_for_2|disc_price(discount_pct_value(game) if has_discount else 0) %}
          <p><small>Team (2)</small>{% if p.active %}<strong>{{ p.discounted }}</strong><del>{{ p.original }}</del>{%
            else %}<strong>{{ p.original }}</strong>{% endif %}</p>
          {% endif %}
          {% if game.price_for_3 and gm_max >= 3 %}
          {% set p = game.price_for_3|disc_price(discount_pct_value(game) if has_discount else 0) %}
          <p><small>Team (3)</small>{% if p.active %}<strong>{{ p.discounted }}</strong><del>{{ p.original }}</del>{%
            else %}<strong>{{ p.original }}</strong>{% endif %}</p>
          {% endif %}
          {% if game.price_for_4 and gm_max >= 4 %}
          {% set p = game.price_for_4|disc_price(discount_pct_value(game) if has_discount else 0) %}
          <p><small>Team (4)</small>{% if p.active %}<strong>{{ p.discounted }}</strong><del>{{ p.original }}</del>{%
            else %}<strong>{{ p.original }}</strong>{% endif %}</p>
          {% endif %}
          {% if game.group_price and not game.price_for_2 and not game.price_for_3 and not game.price_for_4 %}
          <p><small>Group</small><strong>{{ game.group_price }}</strong></p>
          {% endif %}
      </div>
      {% if has_discount %}
      <p style="font-size:11px;color:var(--muted);margin:0 14px;"><i class="fas fa-clock"></i> Discount: {{
        discount_pct_value(game) }}%{% if game.discount_from or game.discount_to %} · {{ game.discount_from or 'Now' }}
        → {{ game.discount_to or 'No end' }}{% endif %}</p>
      {% endif %}

      {% if can('games_update') or can('games_delete') %}
      <div class="compact-actions">
        <button class="btn outline small" type="button" data-toggle-edit><i class="fas fa-pen"></i> Edit</button>
        <form method="post" action="{{ url_for('toggle_game', game_id=game.id) }}">
          <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
          <button class="btn outline small" type="submit"><i class="fas fa-eye"></i> {{ 'Hide' if game.active else
            'Show' }}</button>
        </form>
        <form method="post" action="{{ url_for('delete_game', game_id=game.id) }}"
          data-confirm="Delete '{{ game.title }}'?">
          <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
          <button class="danger-link small" type="submit"><i class="fas fa-trash"></i> Delete</button>
        </form>
      </div>
      {% endif %}

      <form method="post" action="{{ url_for('update_game', game_id=game.id) }}" enctype="multipart/form-data"
        class="admin-inline-edit" hidden data-game-admin-form>
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <label>Title<input name="title" value="{{ game.title }}" required></label>
        <label>Platform<input name="platform" value="{{ game.platform }}" required></label>

        <div class="field-group-header"><i class="fas fa-users"></i> Team Size</div>
        <div class="form-two-col">
          <label>Min Members<input type="number" name="min_members" min="1" max="4" value="{{ gm_min }}" required
              data-min-members></label>
          <label>Max Members<input type="number" name="max_members" min="1" max="4" value="{{ gm_max }}" required
              data-max-members></label>
        </div>

        <div class="field-group-header"><i class="fas fa-tag"></i> Original Per-Size Pricing</div>
        <label data-price-row="1">Original Price — 1 Player (Solo)<input name="individual_price"
            value="{{ game.individual_price }}" data-price="1"></label>
        <label data-price-row="2">Original Price — 2 Players <span class="field-hint">(auto-filled)</span><input
            name="price_for_2" value="{{ game.price_for_2 }}" data-price="2"></label>
        <label data-price-row="3">Original Price — 3 Players <span class="field-hint">(auto-filled)</span><input
            name="price_for_3" value="{{ game.price_for_3 }}" data-price="3"></label>
        <label data-price-row="4">Original Price — 4 Players <span class="field-hint">(auto-filled)</span><input
            name="price_for_4" value="{{ game.price_for_4 }}" data-price="4"></label>

        <div class="field-group-header"><i class="fas fa-percent"></i> Auto Discount</div>
        <label>Discount Label<input name="discount_label" value="{{ game.discount_label }}"></label>
        <label>Discount Percentage<input type="number" name="discount_pct" min="0" max="100" step="0.01"
            value="{{ game.discount_pct or discount_pct_value(game) }}"></label>
        <div class="form-two-col">
          <label>Discount From<input type="datetime-local" name="discount_from"
              value="{{ game.discount_from }}"></label>
          <label>Discount To<input type="datetime-local" name="discount_to" value="{{ game.discount_to }}"></label>
        </div>

        <div class="field-group-header"><i class="fas fa-calendar"></i> Registration Window</div>
        <div class="form-two-col">
          <label>Registration Opens<input type="datetime-local" name="reg_from" value="{{ game.reg_from }}"></label>
          <label>Registration Closes<input type="datetime-local" name="reg_to" value="{{ game.reg_to }}"></label>
        </div>
        <label>Replace Image<input type="file" name="image" accept="image/*"></label>
        <label class="check"><input type="checkbox" name="active" {% if game.active %}checked{% endif %}> Show on
          website</label>
        {% if can('games_update') %}<button class="btn primary small" type="submit"><i class="fas fa-floppy-disk"></i>
          Save Changes</button>{% endif %}
      </form>
    </article>
    {% endfor %}
  </div>
  {% else %}
  <div class="empty"><i class="fas fa-gamepad"></i>No game cards yet. Create your first card above.</div>
  {% endif %}
</section>
{% endblock %}