<?php
// PHP 5.3 compatible
header('Content-Type: text/html; charset=UTF-8');
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Include menu
$menuPath = __DIR__ . '/../../menu.php';
if (file_exists($menuPath)) {
    include $menuPath;
} else {
    echo "<hr>Menu file not found: $menuPath<hr>";
}

// Load formations JSON
$jsonFile = __DIR__ . '/formation.json';
if (!file_exists($jsonFile)) {
    die("JSON file not found: $jsonFile");
}

$jsonString = file_get_contents($jsonFile);
$formations = json_decode($jsonString, true);
if ($formations === null) {
    die("JSON ERROR: " . json_last_error_msg());
}

// Get selected formation from POST
$selectedFormation = isset($_POST['Formation']) ? $_POST['Formation'] : '';
?>
<!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" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Erasmus helper: leaving Bordeaux</title>
<link rel="stylesheet" media="all" type="text/css" href="../../layout.css" />
<style>
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #999; padding: 5px; text-align: left; }
.select { padding: 4px; }
.button { padding: 5px 10px; margin-top: 5px; }
</style>
</head>
<body>

<div id="menu"></div>

<div id="content">

<h1><center>Students Leaving Bordeaux</center></h1>

<h2>Préparer la mobilité</h2>

<table>
<tr>
<td>
<a href="flowchart.png"><img src="flowchart-preview.png" alt="Flowchart"/></a>
</td>
<td>
<p>La procédure complète est expliquée dans le diagramme à gauche (cliquer !).</p>
<p>Pour chaque mobilité vous allez vous inscrire à Bordeaux dans une formation, mais suivre les modules dans l'université partenaire étrangère. Pour cadrer cela, on établit un "contrat pédagogique" ("Learning Agreement", L.A. pour les intimes).</p>
<p>Pour préparer les L.A., il faut mettre dans un tableau en face les modules de votre formation bordelaise du semestre / année de mobilité (typiquement: de L3) avec celles proposées par une université partenaire. Ceci est <b>considérablement plus difficile</b> sur un semestre que sur une année: mon conseil est de systématiquement partir <b>une année</b>.</p>
<p>Pour une mobilité en Licence 3 (le standard), ces pages vous guident à travers la première partie: établir une proposition de contrat d'études, en 3 étapes. Pour des mobilités en Master, la procédure est la même, mais je n'ai pas programmé cette éventualité.</p>
</td>
<td>
<img src="../outgoing.png" alt="Outgoing"/>
</td>
</tr>
</table>

<h2>Etape 1: comprendre la formation que je suivrais à Bordeaux <b>sans mobilité</b>:</h2>

<form name="formation" action="" method="POST">
<label for="formation" color="red">Je choisis ma formation pour l'année prochaine:</label>
<select name="Formation" id="formation" class="select">
    <option value="">Choisir</option>
    <option value="L3MathsMEEF" <?php if ($selectedFormation=='L3MathsMEEF') echo 'selected'; ?>>L3 Maths MEEF</option>
    <option value="L3MathsFonda" <?php if ($selectedFormation=='L3MathsFonda') echo 'selected'; ?>>L3 Maths Fonda</option>
    <option value="L3MathsInfo" <?php if ($selectedFormation=='L3MathsInfo') echo 'selected'; ?>>L3 Maths-Info</option>
    <option value="L3IGM" <?php if ($selectedFormation=='L3IGM') echo 'selected'; ?>>L3 Ingé Maths</option>
    <option value="L3MIASHSEG" <?php if ($selectedFormation=='L3MIASHSEG') echo 'selected'; ?>>L3 MIASHS Economie Gestion</option>
    <option value="L3MIASHSSC" <?php if ($selectedFormation=='L3MIASHSSC') echo 'selected'; ?>>L3 MIASHS Science Cognitives</option>
</select>
<input type="submit" value="Envoyer" class="button">
</form>

<?php
if ($selectedFormation != '') {

    // === Display formation courses table ===
    $found = false;
    echo '<h3>Contenu de formation</h3>';
    echo '<p><a href="https://www.math.u-bordeaux.fr/~ajehanne/MentionMath/DescriptionParcours.pdf">Source et plus de détails ici</a></p>';
    echo '<table><thead><tr><th>Cours</th><th>ECTS</th><th>Semestre</th></tr></thead><tbody>';
    for ($i=0; $i<count($formations); $i++) {
        if ($formations[$i]['formation'] == $selectedFormation) {
            $found = true;
            echo '<tr>';
            echo '<td><a href="'.htmlspecialchars($formations[$i]['lien']).'">'.htmlspecialchars($formations[$i]['cours']).'</a></td>';
            echo '<td>'.htmlspecialchars($formations[$i]['ects']).'</td>';
            echo '<td>'.htmlspecialchars($formations[$i]['sem']).'</td>';
            echo '</tr>';
        }
    }
    echo '</tbody></table>';

    // === Display only the responsible person ===
    $responsables = array(
        "L3MathsMEEF" => "Jasmin Raissy",
        "L3MathsFonda" => "Jasmin Raissy",
        "L3MathsInfo" => "Eric Balandraud",
        "L3IGM" => "Lisl Weynans",
        "L3CMIOPTIM" => "Aurélien Froger",
        "L3CMIISI" => "Jérémie Bigot",
        "L3MIASHSEG" => "Emmanuelle Gagnou",
        "L3MIASHSSC" => "Cécile Mazon"
    );
    if (isset($responsables[$selectedFormation])) {
        echo '<p><b>Responsable de la formation:</b> '.htmlspecialchars($responsables[$selectedFormation]).'</p>';
    }

    // === Display steps 2–6 only when formation selected ===
    ?>
    <h2>Etape 2: visiter les universités partenaires en ligne</h2>
    <p>C'est le plus délicat, car deux formations identiques n'existent pas. Cette étape se fait en ligne mais aussi sur papier chez vous, elle prendra du temps!</p>

    <h2>Etape 3:</h2>
    <center>
    <?php
    echo '<a href="submit.php?formation='.urlencode($selectedFormation).'" class="button">Etablir ma proposition de L.A.</a>';
    ?>
    </center>

    <h2>Etape 4:</h2>
    <p>Le responsable de formation accepte votre proposition et me communique le résultat.</p>

    <h2>Etape 5:</h2>
    <p>Vous préparez les documents pour la DRI.</p>

    <h2>Etape 6:</h2>
    <p>Je signe le contrat final et le transmets via la DRI à l'université partenaire.</p>
<?php
} // end if formation selected
?>

</div>
</body>
</html>
