<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Liste des Familles</title>
</head>

<body>
<?php
include("_serveur.php");
mysqli_set_charset($link, "utf8");

// --- Handle delete request (fathers without children only) ---
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
    $deleteID = intval($_GET['delete']);
    $link->query("DELETE FROM users WHERE id = $deleteID AND type = 3");
    echo "<p style='color:red;font-weight:bold;'>❌ Famille ID $deleteID supprimée</p>";
}

// --- Query all families (father + all children having same userID) ---
$sql = "
    SELECT 
        u.id AS fatherID, 
        u.firstName AS fatherFirst, 
        u.lastName AS fatherLast,
        s.id AS studentID, 
        s.firstName AS studentFirst, 
        s.lastName AS studentLast, 
        c.name AS className
    FROM users u
    LEFT JOIN students s ON u.id = s.userID
    LEFT JOIN classes c ON s.classID = c.id
    WHERE u.type = 3
    ORDER BY u.lastName, u.firstName, s.lastName, s.firstName
";

$result = $link->query($sql);

echo "<h1>Liste des Familles</h1>";

if ($result && $result->num_rows > 0) {

    echo "<table border='1' cellpadding='6' cellspacing='0' style='border-collapse:collapse;width:100%;text-align:left;'>";
    echo "<tr style='background:#f2f2f2;'>
            <th>#</th>
            <th>Famille</th>
            <th>Père</th>
            <th>Élèves</th>
          </tr>";

    $families = [];
    while ($row = $result->fetch_assoc()) {
        $fid = $row['fatherID'];
        if (!isset($families[$fid])) {
            $families[$fid] = [
                'fatherFirst' => $row['fatherFirst'],
                'fatherLast'  => $row['fatherLast'],
                'children'    => []
            ];
        }
        if (!empty($row['studentID'])) {
            $families[$fid]['children'][] = [
                'first' => $row['studentFirst'],
                'last'  => $row['studentLast'],
                'class' => $row['className']
            ];
        }
    }

    $count = 0;
    foreach ($families as $fid => $fam) {
        $count++;
        echo "<tr>";
        echo "<td style='text-align:center;'>$count</td>";
        echo "<td style='font-weight:bold;'>" . htmlspecialchars($fam['fatherLast']) . "</td>";
        echo "<td>" . htmlspecialchars($fam['fatherFirst'] . " " . $fam['fatherLast']) . " (ID: $fid)";

        // Delete icon only if no children
        if (count($fam['children']) === 0) {
            echo " <a href='?delete=$fid' onclick=\"return confirm('Supprimer ce père ?');\">
                    <img src='delete.png' alt='Delete' style='width:20px;height:20px;cursor:pointer;vertical-align:middle;'>
                  </a>";
        }
        echo "</td><td>";

        if (count($fam['children']) > 0) {
            foreach ($fam['children'] as $ch) {
                echo htmlspecialchars($ch['first'] . " " . $ch['last']) . " (" . htmlspecialchars($ch['class']) . ")<br>";
            }
        } else {
            echo "<i>— Aucun élève —</i>";
        }

        echo "</td></tr>";
    }

    echo "</table>";
} else {
    echo "<p>Aucune famille trouvée.</p>";
}

$link->close();
?>
</body>
</html>
