<?php
session_start();
require 'db_con.php'; // Connexion à la base de données

// Vérifiez si l'utilisateur est connecté
if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit;
}

// Vérifiez le rôle de l'utilisateur
$role = $_SESSION['role'];
if ($role !== 'admin' && $role !== 'chef') {
    header("Location: main.php");
    exit;
}

// Gérer l'insertion d'un nouvel OS
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_os'])) {
    $operating_system = $_POST['operating_system'];
    $version = $_POST['version'];
    $architecture = $_POST['architecture'];

    $stmt = $pdo->prepare("INSERT INTO os (operating_system, version, architecture) VALUES (?, ?, ?)");
    $stmt->execute([$operating_system, $version, $architecture]);
}

// Récupérer tous les systèmes d'exploitation
$stmt = $pdo->query("SELECT * FROM os");
$os_list = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestion des Systèmes d'Exploitation</title>
</head>
<body>
    <h1>Gestion des Systèmes d'Exploitation</h1>

    <h2>Ajouter un Système d'Exploitation</h2>
    <form action="os_management.php" method="POST">
        <label for="operating_system">Système d'Exploitation :</label>
        <input type="text" id="operating_system" name="operating_system" required>
        <br>
        <label for="version">Version :</label>
        <input type="text" id="version" name="version" required>
        <br>
        <label for="architecture">Architecture :</label>
        <input type="text" id="architecture" name="architecture" required>
        <br>
        <button type="submit" name="add_os">Ajouter OS</button>
    </form>

    <h2>Liste des Systèmes d'Exploitation</h2>
    <table border="1">
        <tr>
            <th>ID</th>
            <th>Système d'Exploitation</th>
            <th>Version</th>
            <th>Architecture</th>
            <th>Actions</th>
        </tr>
        <?php foreach ($os_list as $os): ?>
            <tr>
                <td><?php echo $os['id']; ?></td>
                <td><?php echo htmlspecialchars($os['operating_system']); ?></td>
                <td><?php echo htmlspecialchars($os['version']); ?></td>
                <td><?php echo htmlspecialchars($os['architecture']); ?></td>
                <td>
                    <a href="edit_os.php?id=<?php echo $os['id']; ?>">Modifier</a>
                    <a href="delete_os.php?id=<?php echo $os['id']; ?>" onclick="return confirm('Êtes-vous sûr de vouloir supprimer cet OS ?');">Supprimer</a>
                </td>
            </tr>
        <?php endforeach; ?>
    </table>

    <br>
    <a href="main.php">Retour à la page principale</a>
</body>
</html>