{% from "components/_tabs.html" import tabs_intro %}
<header class="page-heading">
  <p class="eyebrow">VISIÓN MULTIPLANETARIA</p>
  <h1>Panel imperial y personalización</h1>
  <p>Compara todos tus mundos, decide qué datos aparecen y organiza el selector sin perder la sección que estás consultando.</p>
</header>
<div data-tab-scope="empire" data-tab-auto>
{{ tabs_intro('empire', 'Apartados del panel imperial', 'Consulta columnas, mundos, personalización y recuperación como apartados independientes.') }}

<section class="panel" aria-labelledby="dashboard-columns-title">
  <h2 id="dashboard-columns-title">Columnas visibles</h2>
  <form method="post" action="{{ url_for('dashboard_columns_action') }}" class="stacked-form">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
    <fieldset><legend>Elige al menos un grupo de datos</legend><div class="form-grid">{% for key, label in IMPERIAL_COLUMNS.items() %}<label class="check-row"><input name="columns" type="checkbox" value="{{ key }}" {% if key in dashboard_columns %}checked{% endif %}> {{ label }}</label>{% endfor %}</div></fieldset>
    <button class="button button-primary" type="submit">Guardar columnas</button>
  </form>
  <div class="button-row"><a class="button" href="{{ url_for('dashboard_export_action', format_name='csv') }}">Exportar CSV</a><a class="button" href="{{ url_for('dashboard_export_action', format_name='json') }}">Exportar JSON</a></div>
</section>

<section aria-labelledby="imperial-dashboard-title">
  <h2 id="imperial-dashboard-title" class="section-title">Mundos bajo tu mando</h2>
  <div class="table-wrap"><table>
    <caption>Comparación de {{ imperial_dashboard|length }} planetas en el orden configurado.</caption>
    <thead><tr><th scope="col">Planeta</th>{% if 'resources' in dashboard_columns %}<th scope="col">Reservas</th>{% endif %}{% if 'production' in dashboard_columns %}<th scope="col">Producción por hora</th>{% endif %}{% if 'energy' in dashboard_columns %}<th scope="col">Energía</th>{% endif %}{% if 'food' in dashboard_columns %}<th scope="col">Alimento</th>{% endif %}{% if 'population' in dashboard_columns %}<th scope="col">Población</th>{% endif %}{% if 'fields' in dashboard_columns %}<th scope="col">Campos</th>{% endif %}{% if 'queues' in dashboard_columns %}<th scope="col">Colas</th>{% endif %}{% if 'defense' in dashboard_columns %}<th scope="col">Defensas</th>{% endif %}{% if 'fleet' in dashboard_columns %}<th scope="col">Naves</th>{% endif %}</tr></thead>
    <tbody>{% for item in imperial_dashboard %}<tr>
      <th scope="row"><span class="faction-dot" style="--faction-color:{{ item.planet.color_tag or '#8ad5ff' }}" aria-hidden="true"></span> {{ item.planet.alias or item.planet.name }}<br><small>{{ item.planet.name }} [{{ item.planet.galaxy }}:{{ item.planet.system }}:{{ item.planet.position }}] · {{ PLANET_GROUPS.get(item.planet.group_key,'Sin grupo') }}{% if item.planet.label %} · {{ item.planet.label }}{% endif %}</small></th>
      {% if 'resources' in dashboard_columns %}<td>{{ item.snap.resources.metal|number }} M; {{ item.snap.resources.crystal|number }} C; {{ item.snap.resources.deuterium|number }} D</td>{% endif %}
      {% if 'production' in dashboard_columns %}<td>+{{ item.snap.rates.metal|number }} M; +{{ item.snap.rates.crystal|number }} C; +{{ item.snap.rates.deuterium|number }} D</td>{% endif %}
      {% if 'energy' in dashboard_columns %}<td>{{ (item.snap.rates.energy_ratio*100)|round }} %; {{ item.snap.rates.energy_total|number }} / {{ item.snap.rates.energy_used|number }}</td>{% endif %}
      {% if 'food' in dashboard_columns %}<td>{{ item.planet.food|number }}</td>{% endif %}
      {% if 'population' in dashboard_columns %}<td>{{ item.planet.population|number }}; moral {{ item.planet.morale|round }} %</td>{% endif %}
      {% if 'fields' in dashboard_columns %}<td>{{ item.snap.fields_used }} / {{ item.snap.fields_max }}</td>{% endif %}
      {% if 'queues' in dashboard_columns %}<td>{{ item.queues|length }}</td>{% endif %}
      {% if 'defense' in dashboard_columns %}<td>{{ item.defense_units|number }}</td>{% endif %}
      {% if 'fleet' in dashboard_columns %}<td>{{ item.fleet_units|number }}</td>{% endif %}
    </tr>{% endfor %}</tbody>
  </table></div>
</section>

<section class="panel" aria-labelledby="planet-customization-title">
  <p class="eyebrow">ORDEN Y ETIQUETAS</p>
  <h2 id="planet-customization-title">Personalizar planetas</h2>
  <div class="card-grid">{% for own_planet in planets %}<article class="tech-card">
    <div class="card-copy"><h3>{{ own_planet.name }}</h3><p>[{{ own_planet.galaxy }}:{{ own_planet.system }}:{{ own_planet.position }}]</p>
      <form method="post" action="{{ url_for('planet_preferences_action') }}" class="stacked-form">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="planet_id" value="{{ own_planet.id }}">
        <label>Alias<input name="alias" maxlength="40" value="{{ own_planet.alias }}"></label>
        <label>Grupo<select name="group_key">{% for key, label in PLANET_GROUPS.items() %}<option value="{{ key }}" {% if own_planet.group_key == key %}selected{% endif %}>{{ label }}</option>{% endfor %}</select></label>
        <label>Color textual y visual<input name="color_tag" type="color" value="{{ own_planet.color_tag or '#8ad5ff' }}"></label>
        <label>Etiqueta descriptiva<input name="label" maxlength="60" value="{{ own_planet.label }}"></label>
        <button class="button" type="submit">Guardar personalización</button>
      </form>
      <form method="post" action="{{ url_for('planet_move_action') }}" class="button-row">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="planet_id" value="{{ own_planet.id }}">
        <button class="button" name="direction" value="up" type="submit" {% if loop.first %}disabled{% endif %}>Mover arriba</button>
        <button class="button" name="direction" value="down" type="submit" {% if loop.last %}disabled{% endif %}>Mover abajo</button>
      </form>
    </div>
  </article>{% endfor %}</div>
</section>

{% if recovery_status.protected_until > now or recovery_missions %}
<section class="panel notice notice-warning" aria-labelledby="recovery-title">
  <h2 id="recovery-title">Recuperación tras una derrota grave</h2>
  {% if recovery_status.protected_until > now %}<p>Protección dinámica activa durante {{ (recovery_status.protected_until-now)|duration }}. Intensidad actual: {{ recovery_status.recovery_strength|round }} %. Se reduce de forma automática y queda desactivada en dificultad alta.</p>{% else %}<p>No hay protección temporal activa.</p>{% endif %}
  {% for mission in recovery_missions %}{% if mission.status == 'available' %}<form method="post" action="{{ url_for('recovery_claim_action') }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><input type="hidden" name="mission_id" value="{{ mission.id }}"><button class="button button-primary" type="submit">Recibir ayuda de reconstrucción</button></form>{% endif %}{% endfor %}
  {% if recovery_status.protected_until > now %}<form method="post" action="{{ url_for('recovery_decline_action') }}"><input type="hidden" name="csrf_token" value="{{ csrf_token() }}"><button class="button button-danger" type="submit">Rechazar protección y ayudas</button></form>{% endif %}
</section>
{% endif %}
</div>
