<?php

require_once __DIR__ . '/../lib/init.php';

$appName       = Settings::get('app_name', 'DHCPMan');
$publicSubnets = Subnet::getPublic();
$errors        = [];
$success       = false;

$data = [
    'requester_name'  => '',
    'requester_email' => '',
    'mac_address'     => '',
    'subnet_id'       => '',
    'description'     => '',
];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = [
        'requester_name'  => trim($_POST['requester_name']  ?? ''),
        'requester_email' => trim($_POST['requester_email'] ?? ''),
        'mac_address'     => trim($_POST['mac_address']     ?? ''),
        'subnet_id'       => trim($_POST['subnet_id']       ?? ''),
        'description'     => trim($_POST['description']     ?? ''),
    ];

    $errors = ReservationRequest::validate($data);

    if (!$errors) {
        ReservationRequest::add($data);
        $success = true;
        $data    = ['requester_name' => '', 'requester_email' => '', 'mac_address' => '', 'subnet_id' => '', 'description' => ''];
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= h($appName) ?> — Demande de réservation</title>
    <link rel="stylesheet" href="assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/css/bootstrap-icons.min.css">
    <link rel="stylesheet" href="assets/css/app.css">
</head>
<body>
<div class="login-wrapper">
    <div class="login-card card p-4" style="max-width:480px">
        <div class="login-logo">&#127760;</div>
        <h4 class="text-center mb-1 fw-bold"><?= h($appName) ?></h4>
        <p class="text-center text-muted small mb-4">Demande de réservation DHCP</p>

        <?php if ($success): ?>
            <div class="alert alert-success">
                <i class="bi bi-check-circle me-2"></i>
                <strong>Demande envoyée.</strong><br>
                <span class="text-muted small">Votre demande a été transmise à l'équipe réseau. Elle vous contactera si nécessaire.</span>
            </div>
            <a href="index.php" class="btn btn-outline-secondary w-100">
                <i class="bi bi-arrow-left me-1"></i>Retour à la connexion
            </a>
        <?php else: ?>

            <?php if (!$publicSubnets): ?>
                <div class="alert alert-warning py-2">
                    <i class="bi bi-exclamation-triangle me-1"></i>
                    Aucun réseau disponible pour les demandes pour le moment.
                </div>
                <a href="index.php" class="btn btn-outline-secondary w-100">
                    <i class="bi bi-arrow-left me-1"></i>Retour
                </a>
            <?php else: ?>

                <?php foreach ($errors as $e): ?>
                    <div class="alert alert-danger py-2">
                        <i class="bi bi-exclamation-triangle me-1"></i><?= h($e) ?>
                    </div>
                <?php endforeach; ?>

                <form method="post" autocomplete="off">
                    <div class="mb-3">
                        <label class="form-label fw-semibold">Nom complet <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-person"></i></span>
                            <input type="text" name="requester_name" class="form-control"
                                   value="<?= h($data['requester_name']) ?>"
                                   placeholder="Jean Dupont" required autofocus>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold">Email <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-envelope"></i></span>
                            <input type="email" name="requester_email" class="form-control"
                                   value="<?= h($data['requester_email']) ?>"
                                   placeholder="jean.dupont@example.com" required>
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold">Adresse MAC <span class="text-danger">*</span></label>
                        <div class="input-group">
                            <span class="input-group-text"><i class="bi bi-hdd-network"></i></span>
                            <input type="text" name="mac_address" class="form-control font-monospace"
                                   value="<?= h($data['mac_address']) ?>"
                                   placeholder="aa:bb:cc:dd:ee:ff" required>
                        </div>
                        <div class="form-text">Format : xx:xx:xx:xx:xx:xx</div>
                    </div>

                    <div class="mb-3">
                        <label class="form-label fw-semibold">Réseau <span class="text-danger">*</span></label>
                        <select name="subnet_id" class="form-select" required>
                            <option value="">— Sélectionner un réseau —</option>
                            <?php foreach ($publicSubnets as $sub): ?>
                                <option value="<?= $sub['id'] ?>"
                                    <?= $data['subnet_id'] == $sub['id'] ? 'selected' : '' ?>>
                                    <?= h($sub['name']) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>

                    <div class="mb-4">
                        <label class="form-label fw-semibold">Motif / Description</label>
                        <textarea name="description" class="form-control" rows="3"
                                  placeholder="Décrivez brièvement le besoin (optionnel)"><?= h($data['description']) ?></textarea>
                    </div>

                    <button type="submit" class="btn btn-primary w-100 fw-semibold">
                        <i class="bi bi-send me-1"></i>Envoyer la demande
                    </button>
                </form>

                <div class="text-center mt-3">
                    <a href="index.php" class="text-muted small text-decoration-none">
                        <i class="bi bi-arrow-left me-1"></i>Retour à la connexion
                    </a>
                </div>

            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
</body>
</html>
