{% extends 'base.html' %}
{% block content %}
<div class="d-flex justify-content-between align-items-center flex-wrap gap-3 mb-4">
  <div>
    <h1 class="h3 mb-1">Calendario semanal</h1>
    <p class="text-muted mb-0">Vista operativa por semana para recepción, administración o cada profesional.</p>
  </div>
  <div class="d-flex gap-2 flex-wrap">
    <a class="btn btn-outline-secondary" href="{{ url_for('admin_calendar', week=prev_week, professional_id=selected_professional_id) }}">Semana anterior</a>
    <a class="btn btn-outline-primary" href="{{ url_for('admin_calendar') }}">Semana actual</a>
    <a class="btn btn-primary" href="{{ url_for('admin_calendar', week=next_week, professional_id=selected_professional_id) }}">Semana siguiente</a>
  </div>
</div>

<div class="card mb-4">
  <div class="card-body">
    <form class="row g-3" method="get">
      <div class="col-md-4">
        <label class="form-label">Semana de referencia</label>
        <input class="form-control" type="date" name="week" value="{{ week_dates[0].isoformat() }}">
      </div>
      <div class="col-md-5">
        <label class="form-label">Profesional</label>
        <select class="form-select" name="professional_id">
          <option value="">Todo el equipo</option>
          {% for p in professionals %}
            <option value="{{ p.id }}" {% if selected_professional_id == p.id %}selected{% endif %}>{{ p.display_name }} · {{ p.specialty }}</option>
          {% endfor %}
        </select>
      </div>
      <div class="col-md-3 d-grid align-items-end">
        <button class="btn btn-outline-primary mt-md-4">Aplicar vista</button>
      </div>
    </form>
  </div>
</div>

<div class="calendar-grid">
  {% for day in week_dates %}
    <section class="calendar-column">
      <header class="calendar-column-header">
        <div>{{ weekday_labels[day.weekday()] }}</div>
        <small>{{ day.strftime('%d/%m/%Y') }}</small>
      </header>
      <div class="calendar-column-body">
        {% for item in calendar_map[day.isoformat()] %}
          <article class="appointment-card" style="border-left-color: {{ item.professional.color or '#0d6efd' }};">
            <div class="d-flex justify-content-between gap-2 align-items-start">
              <div>
                <div class="fw-bold">{{ item.start_time.strftime('%H:%M') }} - {{ item.end_time.strftime('%H:%M') }}</div>
                <div>{{ item.client_name }}</div>
              </div>
              <span class="badge rounded-pill text-bg-light border">{{ item.status }}</span>
            </div>
            <div class="small text-muted mt-2">{{ item.service.name }}</div>
            <div class="small text-muted">{{ item.professional.display_name }}</div>
            {% if item.client_phone %}<div class="small text-muted">{{ item.client_phone }}</div>{% endif %}
          </article>
        {% else %}
          <div class="empty-state">Sin turnos cargados</div>
        {% endfor %}
      </div>
    </section>
  {% endfor %}
</div>
{% endblock %}
