feat(benefits-reception): add institutional benefits, QR digital card & reception access control (SDD-012 v3.8.39)

This commit is contained in:
2026-10-27 23:34:57 -03:00
parent e45eb972dd
commit e5e6bfa094
30 changed files with 2994 additions and 11 deletions
+206
View File
@@ -0,0 +1,206 @@
{% extends "base.html" %}
{% block title %}Club de Beneficios & Convenios — UniCABA{% endblock %}
{% block content %}
<div class="container-fluid py-3 px-lg-4">
<!-- Hero / Banner Institucional -->
<div class="card border-0 rounded-4 p-4 p-lg-5 mb-4 shadow-sm text-white position-relative overflow-hidden"
style="background: linear-gradient(135deg, #7E22CE 0%, #B43E8E 50%, #EC4899 100%);">
<div class="position-relative z-1" style="max-width: 680px;">
<span class="badge bg-white text-dark rounded-pill px-3 py-1 fw-bold small text-uppercase mb-2 shadow-sm">
<i class="bi bi-stars text-warning me-1"></i> Comunidad UniCABA
</span>
<h1 class="display-6 fw-bold mb-2">Club de Beneficios &amp; Convenios</h1>
<p class="lead fs-6 mb-3 opacity-90">Aprovecha descuentos exclusivos en cafeterías, librerías, tecnología y comercios presentando tu credencial universitaria digital.</p>
<div class="d-flex flex-wrap gap-2">
<a href="/credential/my-card" class="btn btn-light btn-sm rounded-pill px-4 fw-semibold text-primary shadow-sm">
<i class="bi bi-qr-code me-1"></i> Abrir Mi Credencial QR
</a>
{% if canManage %}
<a href="/benefits/manage" class="btn btn-outline-light btn-sm rounded-pill px-3">
<i class="bi bi-gear me-1"></i> Gestionar Convenios (Bedelía)
</a>
{% endif %}
</div>
</div>
<div class="position-absolute end-0 bottom-0 opacity-25 d-none d-md-block pe-4 pb-2">
<i class="bi bi-bag-heart-fill" style="font-size: 14rem;"></i>
</div>
</div>
<!-- Barra de Búsqueda y Filtros de Categoría -->
<div class="card border-0 shadow-sm rounded-4 p-3 mb-4">
<form method="GET" action="/benefits" class="row g-2 align-items-center">
<div class="col-md-5 col-12">
<div class="input-group">
<span class="input-group-text bg-transparent border-end-0 text-muted"><i class="bi bi-search"></i></span>
<input type="text" name="search" class="form-control border-start-0 ps-0" placeholder="Buscar por cafetería, librería o descuento..." value="{{ searchQuery }}">
</div>
</div>
<div class="col-md-7 col-12">
<div class="d-flex flex-wrap gap-1 align-items-center justify-content-md-end">
<a href="/benefits{% if searchQuery %}?search={{ searchQuery }}{% endif %}" class="btn btn-sm rounded-pill px-3 {% if not currentCategory %}btn-primary{% else %}btn-outline-secondary{% endif %}">
Todos
</a>
{% for cat in categories %}
<a href="/benefits?category={{ cat }}{% if searchQuery %}&search={{ searchQuery }}{% endif %}" class="btn btn-sm rounded-pill px-3 {% if currentCategory == cat %}btn-primary{% else %}btn-outline-secondary{% endif %}">
{% if cat == 'GASTRONOMIA' %}☕ Cafetería
{% elif cat == 'LIBRERIA' %}📚 Librería
{% elif cat == 'TECNOLOGIA' %}💻 Tecnología
{% elif cat == 'SALUD_DEPORTE' %}🏋️ Salud &amp; Deportes
{% elif cat == 'IDIOMAS' %}🌍 Idiomas
{% elif cat == 'CULTURA' %}🎭 Cultura
{% else %}🏷️ {{ cat }}
{% endif %}
</a>
{% endfor %}
</div>
</div>
</form>
</div>
<!-- Grilla de Tarjetas de Beneficios -->
<div class="row g-4" id="benefitsGrid">
{% if benefits and benefits | length > 0 %}
{% for b in benefits %}
<div class="col-12 col-md-6 col-xl-4">
<div class="card border-0 shadow-sm rounded-4 h-100 overflow-hidden benefit-card transition-all" style="transition: transform 0.2s ease, box-shadow 0.2s ease;">
<div class="card-body p-4 d-flex flex-column justify-content-between">
<div>
<!-- Header de Tarjeta -->
<div class="d-flex align-items-start justify-content-between gap-2 mb-3">
<div class="d-flex align-items-center gap-3">
<div class="rounded-4 p-2 bg-primary-subtle text-primary d-flex align-items-center justify-content-center shadow-sm" style="width: 48px; height: 48px;">
{% if b.category == 'GASTRONOMIA' %}
<i class="bi bi-cup-hot fs-4"></i>
{% elif b.category == 'LIBRERIA' %}
<i class="bi bi-book fs-4"></i>
{% elif b.category == 'TECNOLOGIA' %}
<i class="bi bi-laptop fs-4"></i>
{% elif b.category == 'SALUD_DEPORTE' %}
<i class="bi bi-heart-pulse fs-4"></i>
{% else %}
<i class="bi bi-tag fs-4"></i>
{% endif %}
</div>
<div>
<span class="badge bg-secondary-subtle text-secondary-emphasis rounded-pill px-2 py-0 small mb-1">
{{ b.category }}
</span>
<h5 class="fw-bold mb-0 text-dark">{{ b.partner_name }}</h5>
</div>
</div>
<span class="badge bg-danger text-white rounded-pill px-3 py-2 fw-bold fs-6 shadow-sm">
{{ b.discount_label }}
</span>
</div>
<h6 class="fw-semibold text-primary mb-2">{{ b.title }}</h6>
<p class="text-muted small mb-3" style="min-height: 40px;">
{{ b.description or 'Presenta tu credencial universitaria en caja para aplicar el beneficio acordado.' }}
</p>
</div>
<!-- Footer de Tarjeta -->
<div class="pt-3 border-top border-light-subtle d-flex align-items-center justify-content-between">
<span class="text-muted small">
<i class="bi bi-geo-alt-fill text-danger me-1"></i>{{ b.building_name or 'Todas las Sedes' }}
</span>
<button type="button" class="btn btn-primary btn-sm rounded-pill px-3 shadow-sm"
onclick='openRedeemModal({{ b | tojson | safe }})'>
<i class="bi bi-ticket-perforated me-1"></i> Ver Beneficio
</button>
</div>
</div>
</div>
</div>
{% endfor %}
{% else %}
<div class="col-12 py-5 text-center text-muted">
<i class="bi bi-tags display-3 text-secondary opacity-50 mb-3 d-block"></i>
<h5>No se encontraron promociones en esta categoría</h5>
<p class="small text-muted mb-3">Intenta cambiar los filtros de búsqueda o consulta a UdeCi para conocer próximos convenios.</p>
<a href="/benefits" class="btn btn-outline-primary btn-sm rounded-pill px-4">Ver Todos los Beneficios</a>
</div>
{% endif %}
</div>
</div>
<!-- Modal de Detalle y Canje -->
<div class="modal fade" id="redeemModal" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content border-0 shadow-lg rounded-4 overflow-hidden">
<div class="modal-header bg-primary text-white py-3 px-4">
<h5 class="modal-title fw-bold" id="redeemTitle">Detalle del Beneficio</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body p-4 text-center">
<div class="rounded-circle bg-danger-subtle text-danger mx-auto d-flex align-items-center justify-content-center mb-3" style="width: 72px; height: 72px;">
<i class="bi bi-gift-fill fs-2"></i>
</div>
<h4 class="fw-bold mb-1" id="redeemPartner">Comercio</h4>
<div class="badge bg-danger fs-5 px-3 py-1 rounded-pill mb-3" id="redeemDiscount">20% OFF</div>
<p class="text-muted small px-3 mb-3" id="redeemDesc"></p>
<div class="p-3 bg-light rounded-4 border mb-3 text-start" id="couponBox">
<div class="small text-muted text-uppercase fw-bold mb-1">Cupón de Descuento</div>
<div class="d-flex align-items-center justify-content-between">
<code class="fs-5 fw-bold text-primary" id="redeemCoupon">UNICABA-20</code>
<button type="button" class="btn btn-sm btn-outline-primary rounded-pill px-3" onclick="copyCoupon()">
<i class="bi bi-copy me-1"></i> Copiar
</button>
</div>
</div>
<div class="alert alert-info rounded-4 small text-start mb-3">
<i class="bi bi-info-circle-fill me-1"></i> <strong>¿Cómo usar este beneficio?</strong>
<div class="mt-1" id="redeemTerms">Presenta tu Credencial Universitaria Digital UniCABA al momento de solicitar la cuenta o pagar en caja.</div>
</div>
<a href="/credential/my-card" class="btn btn-outline-primary rounded-pill w-100 py-2 fw-semibold">
<i class="bi bi-qr-code me-1"></i> Mostrar Mi Credencial QR al Comercio
</a>
</div>
</div>
</div>
</div>
<script>
function openRedeemModal(b) {
document.getElementById('redeemTitle').innerText = b.title;
document.getElementById('redeemPartner').innerText = b.partner_name;
document.getElementById('redeemDiscount').innerText = b.discount_label;
document.getElementById('redeemDesc').innerText = b.description || 'Promoción exclusiva para la comunidad universitaria de UniCABA.';
document.getElementById('redeemTerms').innerText = b.terms_conditions || 'Válido presentando tu credencial digital con código QR.';
const couponBox = document.getElementById('couponBox');
if (b.coupon_code) {
couponBox.style.display = 'block';
document.getElementById('redeemCoupon').innerText = b.coupon_code;
} else {
couponBox.style.display = 'none';
}
const modal = new bootstrap.Modal(document.getElementById('redeemModal'));
modal.show();
}
function copyCoupon() {
const code = document.getElementById('redeemCoupon').innerText;
if (navigator.clipboard) {
navigator.clipboard.writeText(code).then(() => {
alert('¡Código de cupón copiado al portapapeles!');
});
}
}
</script>
<style>
.benefit-card:hover {
transform: translateY(-4px);
box-shadow: 0 12px 28px rgba(0,0,0,0.08) !important;
}
</style>
{% endblock %}
+403
View File
@@ -0,0 +1,403 @@
{% extends "base.html" %}
{% block title %}Gestión de Convenios & Beneficios — Bedelía{% endblock %}
{% block content %}
<div class="container-fluid py-3 px-lg-4">
<!-- Header de Página -->
<div class="d-flex flex-wrap align-items-center justify-content-between gap-3 mb-4">
<div>
<div class="d-flex align-items-center gap-2">
<span class="badge bg-warning-subtle text-warning-emphasis border border-warning-subtle rounded-pill px-3 py-1 fw-semibold">
<i class="bi bi-tag-fill me-1"></i> Bedelía &amp; Servicios
</span>
<span class="text-muted small">Módulo SDD-012</span>
</div>
<h1 class="h3 fw-bold mt-1 mb-0">Gestión de Convenios &amp; Beneficios</h1>
<p class="text-muted small mb-0">Administra las promociones comerciales, descuentos en cafeterías, librerías y beneficios para la comunidad UniCABA.</p>
</div>
<div class="d-flex align-items-center gap-2">
<a href="/benefits" class="btn btn-outline-secondary btn-sm rounded-pill px-3">
<i class="bi bi-eye me-1"></i> Ver Catálogo Público
</a>
<button type="button" class="btn btn-primary btn-sm rounded-pill px-3 shadow-sm" data-bs-toggle="modal" data-bs-target="#benefitModal" onclick="openCreateModal()">
<i class="bi bi-plus-lg me-1"></i> Nuevo Convenio
</button>
</div>
</div>
<!-- Alertas de Estado -->
<div id="alertContainer"></div>
<!-- Métricas Rápidas -->
<div class="row g-3 mb-4">
<div class="col-6 col-md-3">
<div class="card border-0 shadow-sm rounded-4 h-100 p-3">
<div class="d-flex align-items-center justify-content-between">
<div>
<span class="text-muted small text-uppercase fw-bold">Total Convenios</span>
<h3 class="fw-bold mb-0 text-primary mt-1">{{ benefits | length }}</h3>
</div>
<div class="p-3 bg-primary-subtle text-primary rounded-circle">
<i class="bi bi-tags fs-4"></i>
</div>
</div>
</div>
</div>
<div class="col-6 col-md-3">
<div class="card border-0 shadow-sm rounded-4 h-100 p-3">
<div class="d-flex align-items-center justify-content-between">
<div>
<span class="text-muted small text-uppercase fw-bold">Activos</span>
<h3 class="fw-bold mb-0 text-success mt-1" id="activeCount">
{% set active_count = 0 %}
{% for b in benefits %}
{% if b.is_active %}{% set active_count = active_count + 1 %}{% endif %}
{% endfor %}
{{ active_count }}
</h3>
</div>
<div class="p-3 bg-success-subtle text-success rounded-circle">
<i class="bi bi-check-circle fs-4"></i>
</div>
</div>
</div>
</div>
<div class="col-6 col-md-3">
<div class="card border-0 shadow-sm rounded-4 h-100 p-3">
<div class="d-flex align-items-center justify-content-between">
<div>
<span class="text-muted small text-uppercase fw-bold">Categorías</span>
<h3 class="fw-bold mb-0 text-info mt-1">{{ categories | length }}</h3>
</div>
<div class="p-3 bg-info-subtle text-info rounded-circle">
<i class="bi bi-grid fs-4"></i>
</div>
</div>
</div>
</div>
<div class="col-6 col-md-3">
<div class="card border-0 shadow-sm rounded-4 h-100 p-3">
<div class="d-flex align-items-center justify-content-between">
<div>
<span class="text-muted small text-uppercase fw-bold">Sedes</span>
<h3 class="fw-bold mb-0 text-warning mt-1">{{ buildings | length }}</h3>
</div>
<div class="p-3 bg-warning-subtle text-warning rounded-circle">
<i class="bi bi-buildings fs-4"></i>
</div>
</div>
</div>
</div>
</div>
<!-- Tabla Principal de Convenios -->
<div class="card border-0 shadow-sm rounded-4 overflow-hidden">
<div class="card-header bg-body border-0 py-3 px-4 d-flex flex-wrap align-items-center justify-content-between gap-3">
<h5 class="fw-bold mb-0">Nómina de Convenios Registrados</h5>
<div class="d-flex align-items-center gap-2">
<input type="text" id="tableFilter" class="form-control form-control-sm rounded-pill px-3" placeholder="Filtrar por comercio o título..." style="min-width: 240px;">
</div>
</div>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0" id="benefitsTable">
<thead class="table-light">
<tr>
<th class="ps-4">Convenio &amp; Comercio</th>
<th>Categoría</th>
<th>Descuento</th>
<th>Sede</th>
<th>Destinatarios</th>
<th>Vigencia</th>
<th>Cupón</th>
<th class="text-center">Estado</th>
<th class="text-end pe-4">Acciones</th>
</tr>
</thead>
<tbody>
{% if benefits and benefits | length > 0 %}
{% for b in benefits %}
<tr id="benefit-row-{{ b.id }}" data-search="{{ b.title | lower }} {{ b.partner_name | lower }} {{ b.category | lower }}">
<td class="ps-4">
<div class="d-flex align-items-center gap-3">
<div class="rounded-3 p-2 bg-primary-subtle text-primary d-flex align-items-center justify-content-center" style="width: 42px; height: 42px;">
{% if b.category == 'GASTRONOMIA' %}
<i class="bi bi-cup-hot fs-5"></i>
{% elif b.category == 'LIBRERIA' %}
<i class="bi bi-book fs-5"></i>
{% elif b.category == 'TECNOLOGIA' %}
<i class="bi bi-laptop fs-5"></i>
{% elif b.category == 'SALUD_DEPORTE' %}
<i class="bi bi-heart-pulse fs-5"></i>
{% else %}
<i class="bi bi-tag fs-5"></i>
{% endif %}
</div>
<div>
<div class="fw-bold text-dark">{{ b.title }}</div>
<div class="small text-muted"><i class="bi bi-shop me-1"></i>{{ b.partner_name }}</div>
</div>
</div>
</td>
<td>
<span class="badge bg-secondary-subtle text-secondary-emphasis rounded-pill px-2 py-1 small">
{{ b.category }}
</span>
</td>
<td>
<span class="badge bg-danger-subtle text-danger border border-danger-subtle rounded-pill px-2 py-1 fw-bold">
{{ b.discount_label }}
</span>
</td>
<td>
<span class="small text-muted">
<i class="bi bi-geo-alt me-1"></i>{{ b.building_name or 'Todas las Sedes' }}
</span>
</td>
<td>
{% if b.target_roles == 'ALL' %}
<span class="badge bg-info-subtle text-info rounded-pill px-2">Todos</span>
{% elif 'ALUMNO' in b.target_roles and 'DOCENTE' in b.target_roles %}
<span class="badge bg-primary-subtle text-primary rounded-pill px-2">Alumnos &amp; Docentes</span>
{% elif 'ALUMNO' in b.target_roles %}
<span class="badge bg-success-subtle text-success rounded-pill px-2">Solo Alumnos</span>
{% elif 'DOCENTE' in b.target_roles %}
<span class="badge bg-warning-subtle text-warning-emphasis rounded-pill px-2">Solo Docentes</span>
{% else %}
<span class="badge bg-secondary-subtle text-secondary rounded-pill px-2">{{ b.target_roles }}</span>
{% endif %}
</td>
<td>
<div class="small">
<div><i class="bi bi-calendar-check me-1 text-success"></i>Desde: {{ b.valid_from }}</div>
{% if b.valid_until %}
<div><i class="bi bi-calendar-x me-1 text-danger"></i>Hasta: {{ b.valid_until }}</div>
{% else %}
<div class="text-muted"><i class="bi bi-infinity me-1"></i>Indefinido</div>
{% endif %}
</div>
</td>
<td>
{% if b.coupon_code %}
<code class="px-2 py-1 bg-light rounded text-primary fw-bold small border">{{ b.coupon_code }}</code>
{% else %}
<span class="text-muted small">Credencial</span>
{% endif %}
</td>
<td class="text-center">
<div class="form-check form-switch d-inline-block">
<input class="form-check-input" type="checkbox" role="switch" id="switch-{{ b.id }}" {% if b.is_active %}checked{% endif %} onchange="toggleStatus({{ b.id }})">
</div>
</td>
<td class="text-end pe-4">
<div class="btn-group btn-group-sm">
<button type="button" class="btn btn-outline-secondary rounded-start-pill" title="Editar" onclick='openEditModal({{ b | tojson | safe }})'>
<i class="bi bi-pencil"></i>
</button>
<button type="button" class="btn btn-outline-danger rounded-end-pill" title="Eliminar" onclick="confirmDelete({{ b.id }}, '{{ b.title }}')">
<i class="bi bi-trash"></i>
</button>
</div>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="9" class="text-center py-5 text-muted">
<i class="bi bi-tags display-4 d-block mb-3 text-secondary opacity-50"></i>
<h5>No hay convenios cargados aún</h5>
<p class="small text-muted mb-3">Comienza creando promociones de cafetería, librerías y comercios para los alumnos y docentes.</p>
<button type="button" class="btn btn-primary btn-sm rounded-pill px-4" data-bs-toggle="modal" data-bs-target="#benefitModal" onclick="openCreateModal()">
<i class="bi bi-plus-lg me-1"></i> Crear Primer Convenio
</button>
</td>
</tr>
{% endif %}
</tbody>
</table>
</div>
</div>
</div>
<!-- Modal de Creación / Edición -->
<div class="modal fade" id="benefitModal" tabindex="-1" aria-labelledby="benefitModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content border-0 shadow-lg rounded-4 overflow-hidden">
<form id="benefitForm" method="POST" action="/benefits">
<div class="modal-header bg-primary text-white py-3 px-4">
<h5 class="modal-title fw-bold" id="benefitModalLabel">
<i class="bi bi-tag-fill me-2"></i> Nuevo Convenio Comercial
</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body p-4">
<input type="hidden" id="benefitId" name="benefitId">
<div class="row g-3">
<div class="col-md-7">
<label class="form-label small fw-bold">Título de la Promoción *</label>
<input type="text" class="form-control" id="title" name="title" required placeholder="Ej: 20% en Cafetería La Estación">
</div>
<div class="col-md-5">
<label class="form-label small fw-bold">Comercio / Entidad *</label>
<input type="text" class="form-control" id="partner_name" name="partner_name" required placeholder="Ej: Café Martínez Central">
</div>
<div class="col-md-4">
<label class="form-label small fw-bold">Categoría *</label>
<select class="form-select" id="category" name="category" required>
<option value="GASTRONOMIA">☕ Gastronomía / Cafetería</option>
<option value="LIBRERIA">📚 Librería / Fotocopias</option>
<option value="TECNOLOGIA">💻 Tecnología &amp; Software</option>
<option value="SALUD_DEPORTE">🏋️ Salud &amp; Deportes</option>
<option value="IDIOMAS">🌍 Idiomas &amp; Cursos</option>
<option value="CULTURA">🎭 Cultura &amp; Entretenimiento</option>
<option value="GENERAL" selected>🏷️ General / Otros</option>
</select>
</div>
<div class="col-md-4">
<label class="form-label small fw-bold">Etiqueta de Descuento *</label>
<input type="text" class="form-control" id="discount_label" name="discount_label" required placeholder="Ej: 20% OFF / 2x1">
</div>
<div class="col-md-4">
<label class="form-label small fw-bold">Código de Cupón (Opcional)</label>
<input type="text" class="form-control" id="coupon_code" name="coupon_code" placeholder="Ej: UNICABA-20">
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">Sede Aplicable</label>
<select class="form-select" id="building_id" name="building_id">
<option value="">Todas las Sedes</option>
{% for bld in buildings %}
<option value="{{ bld.id }}">{{ bld.name }}</option>
{% endfor %}
</select>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">Destinatarios *</label>
<select class="form-select" id="target_roles" name="target_roles" required>
<option value="ALL">Todos (Alumnos, Docentes y Personal)</option>
<option value="ALUMNO,DOCENTE">Alumnos y Docentes</option>
<option value="ALUMNO">Solo Estudiantes</option>
<option value="DOCENTE">Solo Docentes</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">Fecha de Inicio *</label>
<input type="date" class="form-control" id="valid_from" name="valid_from" required>
</div>
<div class="col-md-6">
<label class="form-label small fw-bold">Fecha de Vencimiento (Opcional)</label>
<input type="date" class="form-control" id="valid_until" name="valid_until">
</div>
<div class="col-12">
<label class="form-label small fw-bold">Descripción del Beneficio</label>
<textarea class="form-control" id="description" name="description" rows="2" placeholder="Detalles de cómo aprovechar el beneficio en el local..."></textarea>
</div>
<div class="col-12">
<label class="form-label small fw-bold">Términos &amp; Condiciones</label>
<textarea class="form-control" id="terms_conditions" name="terms_conditions" rows="2" placeholder="Válido de lunes a viernes con credencial universitaria vigente..."></textarea>
</div>
<div class="col-12">
<div class="form-check form-switch mt-2">
<input class="form-check-input" type="checkbox" role="switch" id="is_active" name="is_active" checked>
<label class="form-check-label fw-semibold" for="is_active">Publicar inmediatamente en el catálogo de alumnos y docentes</label>
</div>
</div>
</div>
</div>
<div class="modal-footer bg-light py-3 px-4">
<button type="button" class="btn btn-outline-secondary rounded-pill px-4" data-bs-dismiss="modal">Cancelar</button>
<button type="submit" class="btn btn-primary rounded-pill px-4" id="submitBtn">
<i class="bi bi-check-lg me-1"></i> Guardar Convenio
</button>
</div>
</form>
</div>
</div>
</div>
<script>
// Filtro en tiempo real de la tabla
document.getElementById('tableFilter').addEventListener('input', function(e) {
const val = e.target.value.toLowerCase().trim();
const rows = document.querySelectorAll('#benefitsTable tbody tr[data-search]');
rows.forEach(r => {
const txt = r.getAttribute('data-search');
r.style.display = txt.includes(val) ? '' : 'none';
});
});
function openCreateModal() {
document.getElementById('benefitModalLabel').innerHTML = '<i class="bi bi-tag-fill me-2"></i> Nuevo Convenio Comercial';
document.getElementById('benefitForm').action = '/benefits';
document.getElementById('benefitForm').reset();
document.getElementById('benefitId').value = '';
document.getElementById('valid_from').value = new Date().toISOString().split('T')[0];
document.getElementById('is_active').checked = true;
}
function openEditModal(b) {
document.getElementById('benefitModalLabel').innerHTML = '<i class="bi bi-pencil-fill me-2"></i> Editar Convenio Comercial';
document.getElementById('benefitForm').action = `/benefits/${b.id}/edit`;
document.getElementById('benefitId').value = b.id;
document.getElementById('title').value = b.title || '';
document.getElementById('partner_name').value = b.partner_name || '';
document.getElementById('category').value = b.category || 'GENERAL';
document.getElementById('discount_label').value = b.discount_label || '';
document.getElementById('coupon_code').value = b.coupon_code || '';
document.getElementById('building_id').value = b.building_id || '';
document.getElementById('target_roles').value = b.target_roles || 'ALL';
document.getElementById('valid_from').value = b.valid_from ? b.valid_from.split('T')[0] : '';
document.getElementById('valid_until').value = b.valid_until ? b.valid_until.split('T')[0] : '';
document.getElementById('description').value = b.description || '';
document.getElementById('terms_conditions').value = b.terms_conditions || '';
document.getElementById('is_active').checked = b.is_active;
const modal = new bootstrap.Modal(document.getElementById('benefitModal'));
modal.show();
}
async function toggleStatus(id) {
try {
const res = await fetch(`/benefits/${id}/toggle`, { method: 'POST' });
if (!res.ok) throw new Error('Error al cambiar estado');
const data = await res.json();
showAlert('Estado actualizado exitosamente.', 'success');
} catch (err) {
showAlert('No se pudo actualizar el estado: ' + err.message, 'danger');
}
}
async function confirmDelete(id, title) {
if (!confirm(`¿Estás seguro de que deseas eliminar el convenio "${title}"?`)) return;
try {
const res = await fetch(`/benefits/${id}/delete`, { method: 'POST', headers: { 'Accept': 'application/json' } });
if (res.ok) {
const row = document.getElementById(`benefit-row-${id}`);
if (row) row.remove();
showAlert(`Convenio "${title}" eliminado.`, 'info');
} else {
throw new Error('Error al eliminar');
}
} catch (err) {
showAlert('Error: ' + err.message, 'danger');
}
}
function showAlert(msg, type = 'info') {
const container = document.getElementById('alertContainer');
container.innerHTML = `
<div class="alert alert-${type} alert-dismissible fade show rounded-4 shadow-sm" role="alert">
<i class="bi bi-info-circle me-2"></i> ${msg}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
setTimeout(() => { container.innerHTML = ''; }, 4000);
}
</script>
{% endblock %}