{% extends 'base.html' %}
{% block content %}
<div class="page-toolbar">
  <div><h1 class="h3 mb-1">Especialidades</h1><p class="text-muted mb-0">Mantené el catálogo local de especialidades cuando SISA no está activo.</p></div>
  <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#specialtyModal" {% if sisa_enabled %}disabled{% endif %}><i class="bi bi-plus-circle"></i> {{ 'Editar especialidad' if edit_specialty else 'Nueva especialidad' }}</button>
</div>
{% if sisa_enabled %}<div class="alert alert-warning shadow-sm border-0">Config. SISA está activo. La herramienta queda en modo consulta para evitar inconsistencias.</div>{% endif %}
<div class="card table-panel">
  <div class="card-header"><div class="filter-toolbar row g-3 align-items-end"><div class="col-lg-8"><label class="form-label">Filtro rápido</label><input class="form-control" data-table-filter-target="#specialtiesTable" placeholder="Buscar especialidad"></div><div class="col-lg-4 text-muted small">Usá el modal para altas y ediciones.</div></div></div>
  <div class="table-responsive wide-table">
    <table class="table align-middle mb-0" id="specialtiesTable"><thead><tr><th>Nombre</th><th>Estado</th><th class="text-end">Acciones</th></tr></thead><tbody>{% for item in specialties %}<tr><td>{{ item.name }}</td><td>{{ 'Activa' if item.active else 'Desactivada' }}</td><td class="text-end"><div class="d-flex gap-2 justify-content-end"><a class="btn btn-sm btn-outline-primary" href="{{ url_for('admin_specialties', edit=item.id) }}">Editar</a><form method="post" action="{{ url_for('admin_specialty_toggle', specialty_id=item.id) }}"><button class="btn btn-sm btn-outline-warning" {% if sisa_enabled %}disabled{% endif %}>Desactivar</button></form><form method="post" action="{{ url_for('admin_specialty_delete', specialty_id=item.id) }}" onsubmit="return confirm('¿Eliminar?')"><button class="btn btn-sm btn-outline-danger" {% if sisa_enabled %}disabled{% endif %}>Eliminar</button></form></div></td></tr>{% else %}<tr><td colspan="3" class="text-center text-muted py-4">No hay especialidades cargadas.</td></tr>{% endfor %}</tbody></table>
  </div>
</div>
<div class="modal fade" id="specialtyModal" tabindex="-1" aria-hidden="true"><div class="modal-dialog modal-lg modal-dialog-scrollable"><div class="modal-content"><div class="modal-header"><h5 class="modal-title">{{ 'Editar especialidad' if edit_specialty else 'Nueva especialidad' }}</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div><form method="post"><div class="modal-body"><div class="row g-4 modal-form-grid">{% if edit_specialty %}<input type="hidden" name="specialty_id" value="{{ edit_specialty.id }}">{% endif %}<div class="col-12"><div class="section-card"><div class="section-title">Datos de la especialidad</div><div class="row g-3"><div class="col-12"><label class="form-label">Nombre</label><input class="form-control field-placeholder" name="name" value="{{ edit_specialty.name if edit_specialty else '' }}" placeholder="Ej.: Cardiología" {% if sisa_enabled %}disabled{% endif %}></div><div class="col-12"><div class="form-check"><input class="form-check-input" type="checkbox" name="active" {% if not edit_specialty or edit_specialty.active %}checked{% endif %} {% if sisa_enabled %}disabled{% endif %}><label class="form-check-label">Activa</label></div></div></div></div></div></div></div><div class="modal-footer"><button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancelar</button><button class="btn btn-primary" {% if sisa_enabled %}disabled{% endif %}>Guardar especialidad</button></div></form></div></div></div>
{% endblock %}
{% block scripts %}{{ super() }}{% if edit_specialty %}<script>document.addEventListener('DOMContentLoaded',function(){ new bootstrap.Modal(document.getElementById('specialtyModal')).show(); });</script>{% endif %}{% endblock %}
