<?php 
require_once './includes/nav.php'; 
require_once 'config.php';

// Vérifier l'authentification
if (!isAuthenticated()) {
    header("Location: ?page=login");
    exit;
}
$message = "";
$firstname = "";
$lastname = "";
$email = "";
$gsm = "";
$linkedin = "";
$twitter = "";
$github = "";

if($_POST){
    if (!isset($_POST['csrf_token']) || !verifyCSRFToken($_POST['csrf_token'])) {
		echo "<div class='alert alert-error'>Token de sécurité invalide.</div>";
	} else {
        $firstname = sanitizeInput($_POST['firstname']);
        $lastname = sanitizeInput($_POST['lastname']);
        $email = sanitizeInput($_POST['email']);
        $gsm = sanitizeInput($_POST['gsm']);
        $linkedin = sanitizeInput($_POST['linkedin']);
        $twitter = sanitizeInput($_POST['twitter']);
        $github = sanitizeInput($_POST['github']);
        $message = '<div class="alert alert-success">Formulaire soumis</div>';
        
        $jsonFile = '../data/contacts.json';
        if(file_exists($jsonFile)) {
			$content = file_get_contents($jsonFile);
			$contact = $content;

            if($contact) {
                $contact = json_decode($content, true);
            }

            $updatedContact = [
                'firstname' => $firstname,
                'lastname' => $lastname,
                'email' => $email,
                'gsm' => $gsm,
                'linkedin' => $linkedin,
                'twitter' => $twitter,
                'github' => $github
            ];

            $contact = array_merge($contact, $updatedContact);
            if (file_put_contents($jsonFile, json_encode($contact, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE))) {
                $message = '<div class="alert alert-success">Données de contact mises à jour avec succès !</div>';
            } else {
                $message = '<div class="alert alert-error">Erreur lors de la mise à jour des données de contact.</div>';
            }
		}
    }
} else {
    // Charger les données de contact existantes
    $jsonFile = '../data/contacts.json';
    if(file_exists($jsonFile)) {
        $content = file_get_contents($jsonFile);
        if($content) {
            $contact = json_decode($content, true);
            $firstname = $contact['firstname'] ?? '';
            $lastname = $contact['lastname'] ?? '';
            $email = $contact['email'] ?? '';
            $gsm = $contact['gsm'] ?? '';
            $linkedin = $contact['linkedin'] ?? '';
            $twitter = $contact['twitter'] ?? '';
            $github = $contact['github'] ?? '';
        }
    }
}

?>
<section>
    <div class="dashboard">
        <h1>Données de contacts</h1>
        <p>Les données de contacts affichées ici sont reprise sur le site dans la rubrique contact</p>
        <div class="form-project">
            <form action="" method="post">
                <?= $message; ?>
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars(generateCSRFToken()); ?>">
                <div class="form-group">
                    <input type="text" name="firstname" placeholder="Prénom" value="<?= $firstname; ?>" />
                </div>
                <div class="form-group">
                    <input type="text" name="lastname" placeholder="Nom de famille" value="<?= $lastname; ?>" />
                </div>
                <div class="form-group">
                    <input type="email" name="email" placeholder="Adresse email" value="<?= $email; ?>" />
                </div>
                <div class="form-group">
                    <input type="tel" name="gsm" placeholder="Numéro de téléphone" value="<?= $gsm; ?>" />
                </div>
                <div class="form-group">
                    <input type="text" name="linkedin" placeholder="LinkedIn" value="<?= $linkedin; ?>" />
                </div>
                <div class="form-group">
                    <input type="text" name="twitter" placeholder="X (Twitter)" value="<?= $twitter; ?>" />
                </div>
                <div class="form-group">
                    <input type="text" name="github" placeholder="Github" value="<?= $github; ?>" />
                </div>
                <button type="submit" class="btn-success">Mettre à jour</button>
            </form>
        </div>
    </div>
</section>