{% extends 'adminapp/master.html' %}
{% load static %}
{% block content %}
<div class="page-content">
    <div class="container-fluid">
        <!-- Page Title -->
        <div class="row">
            <div class="col-12">
                <div class="page-title-box">
                    <h4 class="mb-0 fw-semibold">Manage Feedback</h4>
                    <ol class="breadcrumb mb-0">
                        <li class="breadcrumb-item"><a href="{% url 'index' %}"><i class="bi bi-house-door-fill me-1"></i>Dashboard</a></li>
                        <li class="breadcrumb-item active text-primary">Feedback</li>
                    </ol>
                </div>
            </div>
        </div>
        <!-- Flash Messages -->
        {% if messages %}
            {% for message in messages %}
                <div class="alert {% if message.tags %}alert-{{ message.tags }}{% endif %} alert-dismissible fade show" role="alert">
                    {{ message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            {% endfor %}
        {% endif %}
        <!-- Feedback List Table -->
        <div class="row">
            <div class="col-xl-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center border-bottom">
                        <h4 class="card-title mb-0"><i class="bx bx-comment-dots me-2"></i>Feedback List</h4>
                    </div>
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table align-middle text-nowrap table-hover table-centered mb-0">
                                <thead class="table-dark">
                                    <tr>
                                        <th>ID</th>
                                        <th>User</th>
                                        <th>Message</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for feedback in feedbacks %}
                                        <tr>
                                            <td>{{ feedback.id }}</td>
                                            <td>{{ feedback.user.username }}</td>
                                            <td data-bs-toggle="tooltip" data-bs-placement="top" title="{{ feedback.message|escape }}">
                                                {{ feedback.message|truncatewords:20 }}
                                            </td>
                                            <td>
                                                <!-- Delete Button triggers Modal -->
                                                <button type="button" class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ feedback.id }}">
                                                    <i class="bi bi-trash3 me-1"></i> Delete
                                                </button>
                                            </td>
                                        </tr>
                                        <!-- Delete Confirmation Modal -->
                                        <div class="modal fade justify-content-between" id="deleteModal{{ feedback.id }}" tabindex="-1" aria-labelledby="deleteModalLabel{{ feedback.id }}" aria-hidden="true">
                                            <div class="modal-dialog modal-dialog-centered">
                                                <div class="modal-content">
                                                    <form method="POST" action="{% url 'delete_feedback' feedback.id %}">
                                                        {% csrf_token %}
                                                        <div class="modal-header">
                                                            <h5 class="modal-title" id="deleteModalLabel{{ feedback.id }}">Confirm Deletion</h5>
                                                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                        </div>
                                                        <div class="modal-body">
                                                            Are you sure you want to delete this feedback?
                                                        </div>
                                                        <div class="modal-footer  border-0 justify-content-center">
                                                            <button type="submit" class="btn btn-danger">Yes, Delete</button>
                                                            <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </div>
                                    {% empty %}
                                        <tr>
                                            <td colspan="4" class="text-center">No feedback found.</td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>  
            </div>
        </div>

    </div>
</div>

<!-- Include Bootstrap JS if not already in master.html -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        tooltipTriggerList.forEach(function (tooltipTriggerEl) {
            new bootstrap.Tooltip(tooltipTriggerEl);
        });
    });
</script>
{% endblock %}
