<?php
$pageTitle = 'Gestion des Hôtels';
require_once __DIR__ . '/includes/header.php';
$db = getDB();

$action = $_GET['action'] ?? 'list';
$id = intval($_GET['id'] ?? 0);

if ($action === 'delete' && $id) {
    $db->prepare("UPDATE hotels SET status='inactif' WHERE id=?")->execute([$id]);
    flash('success', 'Hôtel désactivé.'); redirect('/hotels.php');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $editId = intval($_POST['id'] ?? 0);
    $data = [
        $_POST['nom'] ?? '', $_POST['ville'] ?? '', $_POST['pays'] ?? '', $_POST['adresse'] ?? '',
        intval($_POST['etoiles'] ?? 3), $_POST['telephone'] ?? '', $_POST['email'] ?? '',
        $_POST['contact_nom'] ?? '', intval($_POST['chambres_total'] ?? 0),
        floatval($_POST['tarif_double'] ?? 0), floatval($_POST['tarif_triple'] ?? 0),
        floatval($_POST['tarif_quadruple'] ?? 0), floatval($_POST['tarif_quintuple'] ?? 0),
        $_POST['notes'] ?? '',
    ];
    if ($editId) {
        $data[] = $editId;
        $db->prepare("UPDATE hotels SET nom=?,ville=?,pays=?,adresse=?,etoiles=?,telephone=?,email=?,contact_nom=?,chambres_total=?,tarif_double=?,tarif_triple=?,tarif_quadruple=?,tarif_quintuple=?,notes=?,updated_at=datetime('now') WHERE id=?")->execute($data);
        flash('success', 'Hôtel modifié.');
    } else {
        $db->prepare("INSERT INTO hotels (nom,ville,pays,adresse,etoiles,telephone,email,contact_nom,chambres_total,tarif_double,tarif_triple,tarif_quadruple,tarif_quintuple,notes) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)")->execute($data);
        flash('success', 'Hôtel ajouté.');
    }
    redirect('/hotels.php');
}

if ($action === 'add' || $action === 'edit') {
    $hotel = null;
    if ($action === 'edit' && $id) {
        $stmt = $db->prepare("SELECT * FROM hotels WHERE id=?"); $stmt->execute([$id]); $hotel = $stmt->fetch();
    }
?>
    <div class="d-flex justify-content-between mb-4">
        <a href="<?= APP_URL ?>/hotels.php" class="btn btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i>Retour</a>
    </div>
    <div class="card table-card">
        <div class="card-header"><i class="bi bi-building me-2"></i><?= $hotel ? 'Modifier l\'hôtel' : 'Nouvel hôtel' ?></div>
        <div class="card-body p-4">
            <form method="POST">
                <input type="hidden" name="id" value="<?= $hotel['id'] ?? '' ?>">
                <div class="row g-3">
                    <div class="col-md-5"><label class="form-label">Nom *</label><input type="text" name="nom" class="form-control" value="<?= sanitize($hotel['nom'] ?? '') ?>" required></div>
                    <div class="col-md-3"><label class="form-label">Ville *</label><input type="text" name="ville" class="form-control" value="<?= sanitize($hotel['ville'] ?? '') ?>" required></div>
                    <div class="col-md-2"><label class="form-label">Pays</label><input type="text" name="pays" class="form-control" value="<?= sanitize($hotel['pays'] ?? 'Arabie Saoudite') ?>"></div>
                    <div class="col-md-2"><label class="form-label">Étoiles</label><select name="etoiles" class="form-select">
                        <?php for($i=1;$i<=5;$i++): ?><option value="<?= $i ?>" <?= ($hotel['etoiles']??3)==$i?'selected':'' ?>><?= str_repeat('⭐',$i) ?></option><?php endfor; ?>
                    </select></div>
                    <div class="col-md-6"><label class="form-label">Adresse</label><input type="text" name="adresse" class="form-control" value="<?= sanitize($hotel['adresse'] ?? '') ?>"></div>
                    <div class="col-md-3"><label class="form-label">Téléphone</label><input type="text" name="telephone" class="form-control" value="<?= sanitize($hotel['telephone'] ?? '') ?>"></div>
                    <div class="col-md-3"><label class="form-label">Email</label><input type="email" name="email" class="form-control" value="<?= sanitize($hotel['email'] ?? '') ?>"></div>
                    <div class="col-md-3"><label class="form-label">Contact</label><input type="text" name="contact_nom" class="form-control" value="<?= sanitize($hotel['contact_nom'] ?? '') ?>"></div>
                    <div class="col-md-3"><label class="form-label">Chambres total</label><input type="number" name="chambres_total" class="form-control" value="<?= $hotel['chambres_total'] ?? '' ?>"></div>
                    <div class="col-12"><h6 class="text-primary mt-2 border-bottom pb-2">Tarifs par nuit (MAD)</h6></div>
                    <div class="col-md-3"><label class="form-label">Double</label><input type="number" step="0.01" name="tarif_double" class="form-control" value="<?= $hotel['tarif_double'] ?? '0' ?>"></div>
                    <div class="col-md-3"><label class="form-label">Triple</label><input type="number" step="0.01" name="tarif_triple" class="form-control" value="<?= $hotel['tarif_triple'] ?? '0' ?>"></div>
                    <div class="col-md-3"><label class="form-label">Quadruple</label><input type="number" step="0.01" name="tarif_quadruple" class="form-control" value="<?= $hotel['tarif_quadruple'] ?? '0' ?>"></div>
                    <div class="col-md-3"><label class="form-label">Quintuple</label><input type="number" step="0.01" name="tarif_quintuple" class="form-control" value="<?= $hotel['tarif_quintuple'] ?? '0' ?>"></div>
                    <div class="col-12"><label class="form-label">Notes</label><textarea name="notes" class="form-control" rows="2"><?= sanitize($hotel['notes'] ?? '') ?></textarea></div>
                </div>
                <div class="d-flex gap-2 mt-4">
                    <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg me-1"></i>Enregistrer</button>
                    <a href="<?= APP_URL ?>/hotels.php" class="btn btn-outline-secondary">Annuler</a>
                </div>
            </form>
        </div>
    </div>
<?php require_once __DIR__ . '/includes/footer.php'; exit; }

// List
$hotels = $db->query("SELECT * FROM hotels WHERE status='actif' ORDER BY ville, nom")->fetchAll();
?>

<div class="d-flex justify-content-between mb-4">
    <h6 class="text-muted mb-0"><?= count($hotels) ?> hôtel(s)</h6>
    <a href="<?= APP_URL ?>/hotels.php?action=add" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Nouvel hôtel</a>
</div>

<div class="row g-3">
<?php foreach ($hotels as $h): ?>
    <div class="col-md-6 col-lg-4">
        <div class="card stat-card h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-2">
                    <div>
                        <h6 class="fw-bold mb-1"><?= sanitize($h['nom']) ?></h6>
                        <small class="text-muted"><i class="bi bi-geo-alt me-1"></i><?= sanitize($h['ville'].', '.$h['pays']) ?></small>
                    </div>
                    <span class="small"><?= str_repeat('⭐', $h['etoiles']) ?></span>
                </div>
                <div class="row g-1 my-2 small">
                    <div class="col-6"><strong>Chambres:</strong> <?= $h['chambres_total'] ?></div>
                    <div class="col-6"><strong>Contact:</strong> <?= sanitize($h['contact_nom'] ?: '-') ?></div>
                </div>
                <div class="small mb-2">
                    <span class="me-2">Dbl: <strong><?= formatMoney($h['tarif_double']) ?></strong></span>
                    <span class="me-2">Trp: <strong><?= formatMoney($h['tarif_triple']) ?></strong></span>
                    <span>Qd: <strong><?= formatMoney($h['tarif_quadruple']) ?></strong></span>
                </div>
                <div class="d-flex gap-1">
                    <a href="<?= APP_URL ?>/hotels.php?action=edit&id=<?= $h['id'] ?>" class="btn btn-sm btn-outline-warning flex-fill"><i class="bi bi-pencil me-1"></i>Modifier</a>
                    <a href="<?= APP_URL ?>/hotels.php?action=delete&id=<?= $h['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirmDelete()"><i class="bi bi-trash"></i></a>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>
<?php if (empty($hotels)): ?><div class="col-12 text-center text-muted py-5">Aucun hôtel</div><?php endif; ?>
</div>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
