{% extends "admin/base.html" %}
{% load i18n %}
{% block content %}
<div class="portal-v4-page">
  <section class="portal-v4-hero portal-v4-hero-blue">
    <div><span class="portal-eyebrow">OPERAÇÃO DIÁRIA · {{ today|date:"d/m/Y" }}</span><h1>Centro de operações</h1><p>Prioridades de hoje, próximos vencimentos, cobranças e tarefas em atraso numa única página.</p></div>
    <div class="portal-v4-actions">
      <a href="{% url 'admin:billing_renewal_add' %}"><span class="material-symbols-outlined">event_repeat</span>Nova renovação</a>
      <a href="{% url 'admin:billing_payment_add' %}"><span class="material-symbols-outlined">payments</span>Registar pagamento</a>
      <a href="{% url 'admin:operations_internaltask_add' %}"><span class="material-symbols-outlined">add_task</span>Nova tarefa</a>
    </div>
  </section>
  <div class="portal-v4-kpis">
    <div><span class="material-symbols-outlined">today</span><small>Para hoje</small><strong>{{ counts.today }}</strong></div>
    <div><span class="material-symbols-outlined">date_range</span><small>Próximos 7 dias</small><strong>{{ counts.next_7 }}</strong></div>
    <div class="is-danger"><span class="material-symbols-outlined">running_with_errors</span><small>Em atraso</small><strong>{{ counts.overdue }}</strong></div>
    <div class="is-warning"><span class="material-symbols-outlined">account_balance_wallet</span><small>Pagamentos pendentes</small><strong>{{ counts.payments }}</strong></div>
  </div>

  <div class="portal-v4-grid portal-v4-grid-2">
    <section class="portal-v4-panel"><header><div><span>HOJE</span><h2>Tarefas e renovações</h2></div></header>
      <div class="portal-v4-list">
      {% for task in tasks_today %}<a href="{% url 'admin:operations_internaltask_change' task.pk %}"><i class="material-symbols-outlined">task_alt</i><div><strong>{{ task.title }}</strong><small>{{ task.client|default:"Sem cliente" }}</small></div><b>{{ task.get_priority_display }}</b></a>{% endfor %}
      {% for renewal in renewals_today %}<a href="{% url 'admin:billing_renewal_change' renewal.pk %}"><i class="material-symbols-outlined">event_repeat</i><div><strong>{{ renewal.service.name }}</strong><small>{{ renewal.client }}</small></div><b>{{ renewal.sale_amount|floatformat:2 }} €</b></a>{% endfor %}
      {% if not tasks_today and not renewals_today %}<p class="portal-v4-empty">Sem tarefas ou renovações para hoje.</p>{% endif %}
      </div>
    </section>
    <section class="portal-v4-panel"><header><div><span>VALIDAÇÃO</span><h2>Pagamentos e comunicações</h2></div></header>
      <div class="portal-v4-list">
      {% for payment in payments_validating %}<a href="{% url 'admin:billing_payment_change' payment.pk %}"><i class="material-symbols-outlined">fact_check</i><div><strong>{{ payment.client }}</strong><small>{{ payment.renewal.service.name }} · Em validação</small></div><b>{{ payment.amount|floatformat:2 }} €</b></a>{% endfor %}
      {% for notification in notifications_pending %}<a href="{% url 'admin:notifications_notificationlog_change' notification.pk %}"><i class="material-symbols-outlined">notifications</i><div><strong>{{ notification.subject|default:"Comunicação sem assunto" }}</strong><small>{{ notification.client }} · {{ notification.get_status_display }}</small></div><b>→</b></a>{% endfor %}
      {% if not payments_validating and not notifications_pending %}<p class="portal-v4-empty">Sem validações ou comunicações pendentes.</p>{% endif %}
      </div>
    </section>
  </div>

  <div class="portal-v4-grid portal-v4-grid-3">
    <section class="portal-v4-panel"><header><div><span>PRÓXIMOS 7 DIAS</span><h2>Agenda</h2></div></header><div class="portal-v4-list compact">
      {% for renewal in renewals_next %}<a href="{% url 'admin:billing_renewal_change' renewal.pk %}"><div><strong>{{ renewal.service.name }}</strong><small>{{ renewal.client }}</small></div><b>{{ renewal.due_date|date:"d/m" }}</b></a>{% endfor %}
      {% for task in tasks_next %}<a href="{% url 'admin:operations_internaltask_change' task.pk %}"><div><strong>{{ task.title }}</strong><small>{{ task.client|default:"Sem cliente" }}</small></div><b>{{ task.due_date|date:"d/m" }}</b></a>{% endfor %}
      {% if not renewals_next and not tasks_next %}<p class="portal-v4-empty">Agenda livre nos próximos 7 dias.</p>{% endif %}
    </div></section>
    <section class="portal-v4-panel portal-v4-panel-danger"><header><div><span>EM ATRASO</span><h2>Ações urgentes</h2></div></header><div class="portal-v4-list compact">
      {% for renewal in overdue_renewals %}<a href="{% url 'admin:billing_renewal_change' renewal.pk %}"><div><strong>{{ renewal.service.name }}</strong><small>{{ renewal.client }}</small></div><b>{{ renewal.due_date|date:"d/m" }}</b></a>{% endfor %}
      {% for task in overdue_tasks %}<a href="{% url 'admin:operations_internaltask_change' task.pk %}"><div><strong>{{ task.title }}</strong><small>{{ task.client|default:"Sem cliente" }}</small></div><b>{{ task.due_date|date:"d/m" }}</b></a>{% endfor %}
      {% if not overdue_renewals and not overdue_tasks %}<p class="portal-v4-empty">Sem ações em atraso.</p>{% endif %}
    </div></section>
    <section class="portal-v4-panel"><header><div><span>COBRANÇAS</span><h2>Pagamentos pendentes</h2></div><a href="{% url 'admin:billing_payment_changelist' %}">Ver todos</a></header><div class="portal-v4-list compact">
      {% for payment in pending_payments %}<a href="{% url 'admin:billing_payment_change' payment.pk %}"><div><strong>{{ payment.client }}</strong><small>{{ payment.get_status_display }}</small></div><b>{{ payment.amount|floatformat:2 }} €</b></a>{% empty %}<p class="portal-v4-empty">Sem pagamentos pendentes.</p>{% endfor %}
    </div></section>
  </div>
</div>
{% endblock %}
