<?php
session_start();
require_once __DIR__ . '/db_config.php';

$msg = "";

// Image Upload Handler
if (isset($_POST['upload_banner'])) {
    $package_id = intval($_POST['package_id']);
    
    if (isset($_FILES['banner']) && $_FILES['banner']['error'] == 0) {
        $target_dir = "uploads/";
        if (!is_dir($target_dir)) {
            mkdir($target_dir, 0777, true);
        }
        
        $file_name = time() . '_' . basename($_FILES["banner"]["name"]);
        $target_file = $target_dir . $file_name;

        if (move_uploaded_file($_FILES["banner"]["tmp_name"], $target_file)) {
            // Update image path in database
            $stmt = $conn->prepare("UPDATE packages SET banner_image = ? WHERE id = ?");
            $stmt->bind_param("si", $target_file, $package_id);
            $stmt->execute();
            $msg = "Image updated successfully!";
        } else {
            $msg = "Error uploading image.";
        }
    }
}

// Image Delete Handler
if (isset($_POST['delete_banner'])) {
    $package_id = intval($_POST['package_id']);
    
    // Get existing image path
    $result = $conn->query("SELECT banner_image FROM packages WHERE id = $package_id");
    if ($row = $result->fetch_assoc()) {
        if (!empty($row['banner_image']) && file_exists($row['banner_image'])) {
            unlink($row['banner_image']); // Delete physical file
        }
    }

    // Reset database record
    $stmt = $conn->prepare("UPDATE packages SET banner_image = 'uploads/default.jpg' WHERE id = ?");
    $stmt->bind_param("i", $package_id);
    $stmt->execute();
    $msg = "Image deleted successfully!";
}

// Fetch all packages
$packages = $conn->query("SELECT * FROM packages");

// Fetch Realtime Customer Inquiries
$inquiries = $conn->query("SELECT * FROM inquiries ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Admin Dashboard</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; background: #f4f6f9; }
        .card { background: #fff; padding: 20px; border-radius: 8px; margin-bottom: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; }
        table, th, td { border: 1px solid #ddd; padding: 10px; text-align: left; }
        th { background: #0b3c5d; color: #fff; }
        .btn-del { background: #dc3545; color: #fff; border: none; padding: 5px 10px; cursor: pointer; border-radius: 4px; }
        .btn-up { background: #28a745; color: #fff; border: none; padding: 5px 10px; cursor: pointer; border-radius: 4px; }
        .img-preview { width: 100px; height: 60px; object-fit: cover; border-radius: 4px; }
    </style>
</head>
<body>

    <h1>Admin Control Panel</h1>
    <?php if($msg): ?><p style="color: green; font-weight: bold;"><?php echo $msg; ?></p><?php endif; ?>

    <!-- Image Management -->
    <div class="card">
        <h2>Manage Tour Banner Images</h2>
        <table>
            <thead>
                <tr>
                    <th>Package</th>
                    <th>Current Image</th>
                    <th>Upload New Image</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php while($pkg = $packages->fetch_assoc()): ?>
                <tr>
                    <td><strong><?php echo htmlspecialchars($pkg['title']); ?></strong></td>
                    <td>
                        <?php if(!empty($pkg['banner_image']) && file_exists($pkg['banner_image'])): ?>
                            <img src="<?php echo $pkg['banner_image']; ?>" class="img-preview"><br>
                        <?php else: ?>
                            <span>No Image</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <form method="POST" enctype="multipart/form-data" style="display:flex; gap: 5px;">
                            <input type="hidden" name="package_id" value="<?php echo $pkg['id']; ?>">
                            <input type="file" name="banner" required>
                            <button type="submit" name="upload_banner" class="btn-up">Upload</button>
                        </form>
                    </td>
                    <td>
                        <form method="POST">
                            <input type="hidden" name="package_id" value="<?php echo $pkg['id']; ?>">
                            <button type="submit" name="delete_banner" class="btn-del" onclick="return confirm('Delete this image?')">Delete Image</button>
                        </form>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>

    <!-- Customer Inquiries Table -->
    <div class="card">
        <h2>Realtime Inquiries</h2>
        <table>
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Package</th>
                    <th>Name</th>
                    <th>Contact</th>
                    <th>Passengers</th>
                    <th>Travel Date</th>
                    <th>Message</th>
                </tr>
            </thead>
            <tbody>
                <?php while($inq = $inquiries->fetch_assoc()): ?>
                <tr>
                    <td><?php echo $inq['created_at']; ?></td>
                    <td><?php echo htmlspecialchars($inq['package_name']); ?></td>
                    <td><?php echo htmlspecialchars($inq['name']); ?></td>
                    <td>
                        <?php echo htmlspecialchars($inq['email']); ?><br>
                        <?php echo htmlspecialchars($inq['phone']); ?>
                    </td>
                    <td><?php echo $inq['passengers']; ?></td>
                    <td><?php echo $inq['travel_date']; ?></td>
                    <td><?php echo htmlspecialchars($inq['message']); ?></td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>

</body>
</html>