<!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>Nouvelle Famille</title>
</head>
<style type="text/css">
<!--
@media print  { .noprint  { display: none; }
.page-break  { display:block; page-break-after:always; }
@page  
{ 
   size: 210mm 297mm; 
  /* Chrome sets own margins, we change these printer settings */
  margin: 2mm 2mm 2mm 2mm;
        }

}
body,td,th {
	color: #000033;
}
body {
	background-color: #FFF;
}

table {
  width: 100%;
  margin-top: 15px;
  border-spacing: 10px; /* Ajouté pour espacement horizontal/vertical entre cellules */
}

th, td {
  padding: 10px; /* Ajouté pour espacement interne */
}

tr {
  line-height: 1.8; /* Ajouté pour espacement vertical entre lignes */
}
table tr:nth-child(even) {
  background-color: #f2f2f2; /* Ligne paire */
}

table tr:nth-child(odd) {
  background-color: #ffffff; /* Ligne impaire */
}
-->

</style>
<body>

<?php
include '_serveur.php';
$sql = "SELECT MAX(CAST(SUBSTRING(id, 3) AS UNSIGNED)) AS last_number FROM users";
$result = mysqli_query($link, $sql);

$row = mysqli_fetch_assoc($result);
$lastNumber = isset($row['last_number']) ? (int)$row['last_number'] : 0;

// Incrémenter le numéro
$newNumber = $lastNumber + 1;

// Créer le numéro d'inscription avec "26" + numéro à 4 chiffres
$numeroInscription = '26' . str_pad($newNumber, 4, '0', STR_PAD_LEFT);

// Affichage ou insertion
//echo "Numéro d'inscription généré : $numeroInscription";
?>

<div class="noprint">
<form method="post" action="inscription_Fam.php">
  
  <?php include 'header.php'; ?>
 <button type="button" onclick="window.print()"><img src="imprimante.png" height="32" width="40" /></button>
  
  <fieldset >
    <legend>Inscription Famille</legend>
    <input type="hidden" name="userID" value="<?= $numeroInscription ?>">

    <table>
           
        <th>Nom du Père</th>
        <th>Famille</th>
      </tr>
      <tr>
      <td><input type="text" name="father" placeholder="Taper nom du père" />
</td>
        <td>     <input type="text" name="last" placeholder="Taper famille" /></td>

      </tr>
        
    </table>
       <div class="center">
      <input type="reset" value="Effacer">
      <input type="submit" name="envoyer" value="Envoyer">
    </div>
  </fieldset>
</form></div>
 
   

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['envoyer'])) {
  $father   = htmlspecialchars($_POST['father']);
  $userID   = (int)$_POST['userID'];
  $lastname = htmlspecialchars($_POST['last']);
  $username = $father . $lastname;
  $salt     = "MySecretKey";

  $hash     = hash('sha256', $userID . $username . $salt);
  $password = substr($hash, 0, 8);
  $type     = 3;
  $active   = 1;

  echo '<table>
          <tr><th>Nom du Père</th><th>Famille</th></tr>
          <tr><td>' . htmlspecialchars($father) . '</td><td>' . htmlspecialchars($lastname) . '</td></tr>
          <tr><th>Nom de l\'utilisateur</th><th>Password</th></tr>
          <tr><td>' . htmlspecialchars($username) . '</td><td>' . htmlspecialchars($password) . '</td></tr>
        </table>';

  $stmt = mysqli_prepare($link, 'INSERT INTO users(id, username, firstName, lastName, password, type, active) VALUES (?, ?, ?, ?, ?, ?, ?)');
  if (!$stmt) {
    die('Erreur de préparation : ' . mysqli_error($link));
  }

  mysqli_stmt_bind_param($stmt, 'issssii', $userID, $username, $father, $lastname, $password, $type, $active);

  if (mysqli_stmt_execute($stmt)) {
  echo '<p style="text-align:center; margin-top:20px;">';
echo '<a href="_continuerinsfam.php?userID=' . urlencode($userID) . '&father=' . urlencode($father) . '&password=' . urlencode($password) . '&username=' . urlencode($username) . '&lastname=' . urlencode($lastname) . '">';
echo '<button>Continuer l\'inscription des enfants</button>';
echo '</a></p>';

  } else {
    echo "<p style='text-align:center; color:red;'>Erreur lors de l’enregistrement : " . mysqli_error($link) . "</p>";
  }

  mysqli_stmt_close($stmt);
  mysqli_close($link);
}
?>
</body>
</html>