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

// Self-heal: create table if missing
$conn->query("CREATE TABLE IF NOT EXISTS `google_reviews` (
    `id` INT AUTO_INCREMENT PRIMARY KEY,
    `reviewer_name` VARCHAR(200) NOT NULL,
    `reviewer_photo` VARCHAR(500) DEFAULT NULL,
    `rating` TINYINT(1) DEFAULT 5,
    `review_text` TEXT NOT NULL,
    `review_date` VARCHAR(100) DEFAULT NULL,
    `is_active` TINYINT(1) DEFAULT 1,
    `sort_order` INT DEFAULT 0,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");

// Handle delete
if (isset($_GET['delete'])) {
    $del_id = (int)$_GET['delete'];
    $rev = $conn->query("SELECT reviewer_photo FROM google_reviews WHERE id=$del_id")->fetch_assoc();
    if ($rev && $rev['reviewer_photo'] && file_exists("../uploads/" . $rev['reviewer_photo'])) {
        unlink("../uploads/" . $rev['reviewer_photo']);
    }
    $conn->query("DELETE FROM google_reviews WHERE id=$del_id");
    $_SESSION['success_msg'] = "Review deleted.";
    header("Location: google-reviews.php"); exit();
}

// Handle toggle active
if (isset($_GET['toggle'])) {
    $tog_id = (int)$_GET['toggle'];
    $conn->query("UPDATE google_reviews SET is_active = 1 - is_active WHERE id=$tog_id");
    header("Location: google-reviews.php"); exit();
}

$reviews = $conn->query("SELECT * FROM google_reviews ORDER BY sort_order DESC, id DESC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Google Reviews | 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; margin: 0; }
        .admin-sidebar { position: fixed; top: 0; left: 0; width: 260px; height: 100vh; background: linear-gradient(180deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%); z-index: 100; padding: 30px 0; display: flex; flex-direction: column; box-shadow: 4px 0 20px rgba(0,0,0,0.3); }
        .sidebar-logo { text-align: center; padding: 0 20px 30px; border-bottom: 1px solid rgba(255,255,255,0.1); margin-bottom: 20px; }
        .sidebar-logo img { height: 70px; filter: brightness(0) invert(1); }
        .sidebar-logo h6 { color: #aaa; font-size: 11px; text-transform: uppercase; letter-spacing: 3px; margin-top: 8px; }
        .sidebar-nav a { display: flex; align-items: center; padding: 12px 25px; color: rgba(255,255,255,0.65); text-decoration: none; font-size: 14px; font-weight: 500; transition: 0.3s; gap: 12px; border-left: 3px solid transparent; }
        .sidebar-nav a:hover, .sidebar-nav a.active { color: #fff; background: rgba(255,255,255,0.08); border-left-color: #ff6900; }
        .sidebar-nav a i { width: 20px; text-align: center; font-size: 15px; }
        .sidebar-section { padding: 15px 25px 5px; color: rgba(255,255,255,0.3); font-size: 10px; text-transform: uppercase; letter-spacing: 2px; }
        .sidebar-bottom { margin-top: auto; padding: 20px 25px; border-top: 1px solid rgba(255,255,255,0.1); }
        .sidebar-bottom a { display: flex; align-items: center; gap: 10px; color: rgba(255,255,255,0.6); text-decoration: none; font-size: 14px; padding: 10px; border-radius: 10px; transition: 0.3s; }
        .sidebar-bottom a:hover { color: #fff; background: rgba(255,0,0,0.2); }
        .admin-main { margin-left: 260px; min-height: 100vh; padding: 30px; }
        .admin-topbar { display: flex; justify-content: space-between; align-items: center; background: #fff; border-radius: 16px; padding: 15px 25px; margin-bottom: 30px; box-shadow: 0 2px 20px rgba(0,0,0,0.06); }
        .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; }
        .reviewer-avatar { width: 44px; height: 44px; border-radius: 50%; object-fit: cover; border: 2px solid #eee; }
        .reviewer-initials { width: 44px; height: 44px; border-radius: 50%; background: linear-gradient(135deg, #4285F4, #34A853); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 16px; }
        .google-badge { display: inline-flex; align-items: center; gap: 5px; background: #fff; border: 1px solid #e0e0e0; border-radius: 50px; padding: 3px 10px; font-size: 11px; font-weight: 600; color: #555; }
        .star-display { color: #ffc107; font-size: 13px; letter-spacing: 1px; }
        .review-snippet { max-width: 350px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #666; font-size: 13px; }
        .status-pill { display: inline-flex; align-items: center; gap: 5px; padding: 4px 12px; border-radius: 50px; font-size: 11px; font-weight: 600; }
        .status-active { background: #d1fae5; color: #065f46; }
        .status-inactive { background: #fee2e2; color: #991b1b; }
    </style>
</head>
<body>

<div class="admin-sidebar">
    <div class="sidebar-logo">
        <img src="../assets/img/logo.png" alt="Logo" onerror="this.style.display='none'">
        <h6>Admin Panel</h6>
    </div>
    <div class="sidebar-nav">
        <a href="index.php"><i class="fas fa-home"></i> Dashboard</a>
        <div class="sidebar-section" style="padding-top:20px;">Tour Management</div>
        <a href="packages.php"><i class="fas fa-globe"></i> International Tours</a>
        <a href="add-package.php"><i class="fas fa-plus-circle"></i> Add Tour</a>
        <a href="local-tours.php"><i class="fas fa-map-marked-alt"></i> Local Tours</a>
        <a href="add-local-tour.php"><i class="fas fa-plus"></i> Add Local Tour</a>
        <a href="categories.php"><i class="fas fa-tags"></i> Categories</a>
        <div class="sidebar-section" style="padding-top:20px;">Homepage</div>
        <a href="destinations.php"><i class="fas fa-map-pin"></i> Destinations</a>
        <a href="add-destination.php"><i class="fas fa-plus-circle"></i> Add Destination</a>
        <a href="testimonials.php"><i class="fas fa-star"></i> Testimonials</a>
        <a href="add-testimonial.php"><i class="fas fa-plus-circle"></i> Add Testimonial</a>
        <a href="google-reviews.php" class="active"><i class="fab fa-google"></i> Google Reviews</a>
        <div class="sidebar-section" style="padding-top:20px;">Media</div>
        <a href="manage-gallery.php"><i class="fas fa-images"></i> Gallery Manager</a>
        <div class="sidebar-section" style="padding-top:20px;">Site</div>
        <a href="../index.php" target="_blank"><i class="fas fa-external-link-alt"></i> View Website</a>
    </div>
    <div class="sidebar-bottom">
        <a href="logout.php"><i class="fas fa-sign-out-alt text-danger"></i> Logout</a>
    </div>
</div>

<div class="admin-main">
    <div class="admin-topbar">
        <div>
            <h5 class="fw-bold mb-0"><i class="fab fa-google me-2" style="color:#4285F4;"></i>Google Reviews</h5>
            <p class="text-muted small mb-0">Manage reviews displayed on the homepage</p>
        </div>
        <a href="add-google-review.php" class="btn btn-primary rounded-pill px-4 shadow-sm">
            <i class="fas fa-plus me-2"></i>Add Google Review
        </a>
    </div>

    <?php if (isset($_SESSION['success_msg'])): ?>
        <div class="alert alert-success border-0 rounded-4 shadow-sm mb-4">
            <i class="fas fa-check-circle me-2"></i><?= $_SESSION['success_msg'] ?>
        </div>
        <?php unset($_SESSION['success_msg']); ?>
    <?php endif; ?>

    <!-- Info Banner -->
    <div class="alert border-0 mb-4" style="background: linear-gradient(135deg, #e8f0fe, #e6f4ea); border-radius: 16px;">
        <div class="d-flex align-items-center gap-3">
            <div style="font-size: 28px;">
                <img src="https://www.google.com/favicon.ico" width="28" height="28" style="border-radius:4px;" onerror="this.style.display='none'">
            </div>
            <div>
                <p class="fw-bold mb-0" style="color: #1a73e8;">Google Reviews Manager</p>
                <p class="mb-0 small text-muted">Add authentic Google reviews here to display them on your website's homepage. Toggle active/inactive to show or hide individual reviews.</p>
            </div>
        </div>
    </div>

    <div class="admin-card">
        <table class="table admin-table mb-0">
            <thead>
                <tr>
                    <th>Reviewer</th>
                    <th>Rating</th>
                    <th>Review</th>
                    <th>Date</th>
                    <th>Status</th>
                    <th>Order</th>
                    <th style="width:130px;">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($reviews && $reviews->num_rows > 0): ?>
                    <?php while ($rev = $reviews->fetch_assoc()): ?>
                    <tr>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <?php if (!empty($rev['reviewer_photo'])): ?>
                                    <img src="../uploads/<?= htmlspecialchars($rev['reviewer_photo']) ?>"
                                         class="reviewer-avatar"
                                         onerror="this.style.display='none'; this.nextElementSibling.style.display='flex';">
                                    <div class="reviewer-initials" style="display:none;">
                                        <?= strtoupper(substr($rev['reviewer_name'], 0, 1)) ?>
                                    </div>
                                <?php else: ?>
                                    <div class="reviewer-initials">
                                        <?= strtoupper(substr($rev['reviewer_name'], 0, 1)) ?>
                                    </div>
                                <?php endif; ?>
                                <div>
                                    <div class="fw-semibold small"><?= htmlspecialchars($rev['reviewer_name']) ?></div>
                                    <span class="google-badge"><i class="fab fa-google" style="color:#4285F4;"></i> Google</span>
                                </div>
                            </div>
                        </td>
                        <td>
                            <div class="star-display">
                                <?= str_repeat('★', $rev['rating']) ?><?= str_repeat('☆', 5 - $rev['rating']) ?>
                            </div>
                            <small class="text-muted"><?= $rev['rating'] ?>/5</small>
                        </td>
                        <td class="review-snippet"><?= htmlspecialchars($rev['review_text']) ?></td>
                        <td class="small text-muted"><?= htmlspecialchars($rev['review_date'] ?? '—') ?></td>
                        <td>
                            <a href="google-reviews.php?toggle=<?= $rev['id'] ?>" class="text-decoration-none">
                                <span class="status-pill <?= $rev['is_active'] ? 'status-active' : 'status-inactive' ?>">
                                    <i class="fas <?= $rev['is_active'] ? 'fa-eye' : 'fa-eye-slash' ?>"></i>
                                    <?= $rev['is_active'] ? 'Active' : 'Hidden' ?>
                                </span>
                            </a>
                        </td>
                        <td class="text-muted small"><?= $rev['sort_order'] ?></td>
                        <td>
                            <a href="edit-google-review.php?id=<?= $rev['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="google-reviews.php?delete=<?= $rev['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="7" class="text-center text-muted py-5">
                            <i class="fab fa-google fa-3x mb-3 d-block" style="color:#4285F4; opacity:0.3;"></i>
                            No Google reviews yet. <a href="add-google-review.php" class="text-primary fw-bold">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>
