<?php
include 'auth.php';
require_once '../config/db.php';

if (isset($_GET['delete'])) {
    $del_id = (int)$_GET['delete'];
    $conn->query("DELETE FROM testimonials WHERE id = $del_id");
    header("Location: testimonials.php");
    exit();
}

$result = $conn->query("SELECT * FROM testimonials ORDER BY id DESC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Testimonials | Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700;800&display=swap" rel="stylesheet">
    <style>
        * { font-family: 'Inter', sans-serif; }
        body { background: #f0f2f5; padding: 30px; }
        .page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30px; }
        .admin-card { background: #fff; border-radius: 20px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); overflow: hidden; }
        .admin-table th { background: #f8f9fa; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: #888; padding: 12px 20px; border: none; }
        .admin-table td { padding: 14px 20px; border-bottom: 1px solid #f5f5f5; vertical-align: middle; }
        .admin-table tr:last-child td { border-bottom: none; }
        .msg-cell { max-width: 400px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
        .badge-rating { background: #fff3e0; color: #ff6900; padding: 4px 12px; border-radius: 50px; font-size: 12px; font-weight: 600; }
        .btn-back { text-decoration: none; color: #666; font-size: 14px; }
        .btn-back:hover { color: #ff6900; }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="page-header">
            <div>
                <a href="index.php" class="btn-back"><i class="fas fa-arrow-left me-2"></i>Back to Dashboard</a>
                <h2 class="fw-bold mt-2">Testimonials / Reviews</h2>
                <p class="text-muted small mb-0">Manage customer reviews shown on the homepage</p>
            </div>
            <a href="add-testimonial.php" class="btn btn-primary rounded-pill px-4 shadow-sm">
                <i class="fas fa-plus me-2"></i> Add New Review
            </a>
        </div>

        <?php if (isset($_SESSION['success_msg'])): ?>
            <div class="alert alert-success alert-dismissible fade show rounded-4 border-0 shadow-sm" role="alert">
                <i class="fas fa-check-circle me-2"></i> <?= $_SESSION['success_msg'] ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php unset($_SESSION['success_msg']); endif; ?>

        <div class="admin-card">
            <table class="table admin-table mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Client Name</th>
                        <th>Review</th>
                        <th>Rating</th>
                        <th style="width:100px;">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if ($result && $result->num_rows > 0): ?>
                        <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td class="fw-bold"><?= $row['id'] ?></td>
                            <td class="fw-semibold"><?= htmlspecialchars($row['client_name']) ?></td>
                            <td class="msg-cell text-muted"><?= htmlspecialchars($row['message']) ?></td>
                            <td>
                                <span class="badge-rating">
                                    <i class="fas fa-star text-warning me-1"></i> <?= $row['rating'] ?>/5
                                </span>
                            </td>
                            <td>
                                <a href="edit-testimonial.php?id=<?= $row['id'] ?>" class="btn btn-light btn-sm rounded-circle me-1" title="Edit">
                                    <i class="fas fa-edit text-primary" style="font-size:12px;"></i>
                                </a>
                                <a href="testimonials.php?delete=<?= $row['id'] ?>" class="btn btn-light btn-sm rounded-circle" title="Delete" onclick="return confirm('Delete this review?')">
                                    <i class="fas fa-trash text-danger" style="font-size:12px;"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr><td colspan="5" class="text-center text-muted py-5">No reviews yet. <a href="add-testimonial.php" class="text-primary">Add your first review</a></td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
