{% extends "base.html" %}

{% block title %}Gestión de FAQs{% endblock %}

{% block content %}
<div class="section">
    <div class="container">
        <div class="row">
            <div class="col-12 mb-4 admin-header">
                <h1>Gestión de Preguntas Frecuentes</h1>
            </div>
        </div>
        
        <div class="row mb-4">
            <div class="col-md-6">
                <p class="mb-0">Administre las preguntas frecuentes que aparecen en la página de inicio.</p>
            </div>
            <div class="col-md-6 text-md-end">
                <a href="{{ url_for('admin.create_faq') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i> Nueva Pregunta
                </a>
            </div>
        </div>
        
        <div class="row">
            <div class="col-12">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Pregunta</th>
                                        <th>Orden</th>
                                        <th>Estado</th>
                                        <th>Acciones</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for faq in faqs %}
                                    <tr>
                                        <td>{{ faq.id }}</td>
                                        <td>{{ faq.question }}</td>
                                        <td>{{ faq.order }}</td>
                                        <td>
                                            {% if faq.is_active %}
                                            <span class="badge bg-success">Activa</span>
                                            {% else %}
                                            <span class="badge bg-secondary">Inactiva</span>
                                            {% endif %}
                                        </td>
                                        <td>
                                            <div class="btn-group">
                                                <a href="{{ url_for('admin.edit_faq', faq_id=faq.id) }}" 
                                                   class="btn btn-sm btn-outline-primary" title="Editar pregunta frecuente">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                                <button type="button" class="btn btn-sm btn-outline-danger" 
                                                        data-faq-id="{{ faq.id }}"
                                                        data-faq-question="{{ faq.question }}"
                                                        onclick="confirmDelete(this.getAttribute('data-faq-id'), this.getAttribute('data-faq-question'))" title="Eliminar pregunta frecuente">
                                                    <i class="fas fa-trash-alt"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                    
                                    {% if not faqs %}
                                    <tr>
                                        <td colspan="5" class="text-center">No hay preguntas frecuentes registradas.</td>
                                    </tr>
                                    {% endif %}
                                </tbody>
                            </table>
                        </div>
                        {% if pagination and pagination.total > 0 %}
                        <nav aria-label="Page navigation">
                            <ul class="pagination justify-content-center mt-3">{% if pagination.has_prev %}<li class="page-item">
                                    <a class="page-link" href="{{ url_for('admin.manage_faqs', page=pagination.prev_num, per_page=per_page) }}" aria-label="Previous">
                                        <span aria-hidden="true">&laquo;</span>
                                    </a>
                                </li>
                                {% endif %}
                                {% for p in pagination.iter_pages() %}
                                {% if p %}
                                    {% if p == pagination.page %}
                                    <li class="page-item active" aria-current="page"><a class="page-link" href="#">{{ p }}</a></li>
                                    {% else %}
                                    <li class="page-item"><a class="page-link" href="{{ url_for('admin.manage_faqs', page=p, per_page=per_page) }}">{{ p }}</a></li>
                                    {% endif %}
                                {% else %}
                                <li class="page-item disabled"><a class="page-link" href="#">...</a></li>
                                {% endif %}
                                {% endfor %}
                                {% if pagination.has_next %}
                                <li class="page-item">
                                    <a class="page-link" href="{{ url_for('admin.manage_faqs', page=pagination.next_num, per_page=per_page) }}" aria-label="Next">
                                        <span aria-hidden="true">&raquo;</span>
                                    </a>
                                </li>
                                {% endif %}
                            </ul>
                        </nav>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal de confirmación de eliminación -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="deleteModalLabel">Confirmar eliminación</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>¿Estás seguro que deseas eliminar la pregunta frecuente <strong id="deleteFaqQuestion"></strong>?</p>
                <p class="text-danger">Esta acción no se puede deshacer.</p>
            </div>
            <div class="modal-footer">
                <form id="deleteForm" action="" method="POST">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
                    <button type="submit" class="btn btn-danger">Eliminar</button>
                </form>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block scripts %}
<script>
    function confirmDelete(faqId, faqQuestion) {
        // Configurar el modal
        document.getElementById('deleteFaqQuestion').textContent = faqQuestion;
        document.getElementById('deleteForm').action = "{{ url_for('admin.delete_faq', faq_id=0) }}".replace('0', faqId);
        
        // Mostrar el modal
        var deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
        deleteModal.show();
    }
</script>
{% endblock %}
