<?php
include 'auth.php';
include '../includes/header.php';

// Auto-migrate local_tour_gallery table if missing
$conn->query("CREATE TABLE IF NOT EXISTS local_tour_gallery (
    id INT AUTO_INCREMENT PRIMARY KEY,
    tour_id INT NOT NULL,
    image VARCHAR(255) NOT NULL,
    FOREIGN KEY (tour_id) REFERENCES local_tours(id) ON DELETE CASCADE
)");

// Self-heal new columns (silent)
@$conn->query("ALTER TABLE local_tours ADD COLUMN IF NOT EXISTS category_id INT DEFAULT NULL");
@$conn->query("ALTER TABLE local_tours ADD COLUMN IF NOT EXISTS duration VARCHAR(100) DEFAULT NULL");
@$conn->query("ALTER TABLE local_tours ADD COLUMN IF NOT EXISTS tour_type VARCHAR(50) DEFAULT 'day-tour'");
@$conn->query("ALTER TABLE local_tours ADD COLUMN IF NOT EXISTS sort_order INT DEFAULT 0");

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

$tours = $conn->query("SELECT t.*, c.name as category_name FROM local_tours t LEFT JOIN local_tour_categories c ON t.category_id = c.id ORDER BY t.sort_order DESC, t.id DESC");
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold mb-0">Manage Local Tours</h2>
        <a href="add-local-tour.php" class="btn btn-primary rounded-pill px-4 shadow-sm">
            <i class="fas fa-plus me-2"></i> Add New Tour
        </a>
    </div>

    <div class="card border-0 shadow-sm" style="border-radius: 20px;">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th>Image</th>
                            <th>Title</th>
                            <th>Category</th>
                            <th>Location</th>
                            <th>Duration</th>
                            <th>Type</th>
                            <th>Price</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if($tours && $tours->num_rows > 0): ?>
                            <?php while($t = $tours->fetch_assoc()): ?>
                                <tr>
                                    <td>
                                        <?php if($t['main_image']): ?>
                                            <img src="../uploads/<?= $t['main_image'] ?>" height="50" class="rounded" style="width:65px;object-fit:cover;">
                                        <?php else: ?>
                                            <div class="bg-light rounded text-center d-flex align-items-center justify-content-center" style="width: 50px; height: 50px;">
                                                <i class="fas fa-image text-muted"></i>
                                            </div>
                                        <?php endif; ?>
                                    </td>
                                    <td><span class="fw-bold"><?= htmlspecialchars($t['title']) ?></span></td>
                                    <td>
                                        <?php if (!empty($t['category_name'])): ?>
                                            <span class="badge" style="background:linear-gradient(135deg,#ff6900,#ff8c00);color:#fff;font-size:11px;">
                                                <i class="fas fa-tag me-1"></i><?= htmlspecialchars($t['category_name']) ?>
                                            </span>
                                        <?php else: ?>
                                            <span class="text-muted small">—</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><span class="badge bg-light text-dark"><?= htmlspecialchars($t['location']) ?></span></td>
                                    <td>
                                        <?php $dur = $t['duration'] ?? ''; ?>
                                        <span class="small text-muted"><?= $dur ? htmlspecialchars($dur) : '—' ?></span>
                                    </td>
                                    <td>
                                        <?php
                                        $ttype = $t['tour_type'] ?? 'day-tour';
                                        $type_labels = ['day-tour'=>'Day Tour','overnight'=>'Overnight','multi-day'=>'Multi-Day','private'=>'Private'];
                                        $type_colors = ['day-tour'=>'bg-info','overnight'=>'bg-warning text-dark','multi-day'=>'bg-success','private'=>'bg-secondary'];
                                        ?>
                                        <span class="badge <?= $type_colors[$ttype] ?? 'bg-secondary' ?> small">
                                            <?= $type_labels[$ttype] ?? ucfirst($ttype) ?>
                                        </span>
                                    </td>
                                    <td><span class="text-primary fw-bold">LKR <?= number_format($t['price'], 0) ?></span></td>
                                    <td class="text-end">
                                        <div class="btn-group shadow-sm" style="border-radius: 10px; overflow: hidden;">
                                            <a href="edit-local-tour.php?id=<?= $t['id'] ?>" class="btn btn-light btn-sm px-3">
                                                <i class="fas fa-edit text-primary"></i>
                                            </a>
                                            <a href="local-tours.php?delete=<?= $t['id'] ?>" class="btn btn-light btn-sm px-3" onclick="return confirm('Are you sure?')">
                                                <i class="fas fa-trash text-danger"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="8" class="text-center py-5">
                                    <i class="fas fa-map-marked-alt fa-3x text-muted opacity-25 mb-3 d-block"></i>
                                    <p class="text-muted">No local tours found. <a href="add-local-tour.php" class="text-primary fw-bold">Add your first local tour</a></p>
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?php include '../includes/footer.php'; ?>
