<link rel="stylesheet" href="css.css">
<link rel="icon" href="test.ico" />
<meta charset="UTF-8">


<form>
    <button type="submit"><a href="DB.php?action=Creer">Creer Partie</a></button>
</form>
<form>
    <input type="" name="placer" id="MettreID" placeholder="Mettre ID partie">
    <button name="action" type="submit"><a href="DB.php?action=Rejoindre">Creer Partie</a></button>
</form>
<h1>Morpion</h1>

<form method="post">
    <input  type="text" name="forme" required="required" placeholder="Mettre X ou O" maxlength="1">
    <input type="number" name="case" required="required" placeholder="Emplacement (1-9)" maxlength="1">
    <button type="submit" name="action">Inserer</button>
</form>


</p>

<?php
require("MyDB.php");
$db = new MyDB();


$action = $_REQUEST['action'] ?? NULL;
echo $action;

if($action=="Creer"){
    $partiecreer=genererChaineAleatoire();
    $db->query('INSERT INTO partie(Id) VALUES("'.$partiecreer.'")');
    $idpartie=$partiecreer;
    require("DB.php");
}
if($action=="Rejoindre"){
    $idpartie=$_POST['placer'];
    $idpartie=$partiecreer;
    require("DB.php");
}

echo "<h2>Id : $idpartie</h2>" ;
$colonne="";

if(isset($_POST['case'])){

    if($_POST['case']=='1'){
    $colonne='un';
}
elseif ($_POST['case']=='2'){
    $colonne='deux';
}
elseif ($_POST['case']=='3'){
    $colonne='trois';
}
elseif ($_POST['case']=='4'){
    $colonne='quatre';
}
elseif ($_POST['case']=='5'){
    $colonne='cinq';
}
elseif ($_POST['case']=='6'){
    $colonne='six';
}
elseif ($_POST['case']=='7'){
    $colonne='sept';
}
elseif ($_POST['case']=='8'){
    $colonne='huit';
}
elseif ($_POST['case']=='9'){
    $colonne='neuf';
}
    else{
        $colonne='null';
    }
}
$valeur="";
if(isset($_POST['forme'])){
    $valeur=$_POST['forme'];
}


header("refresh: 2");
//$db->exec('CREATE TABLE partie(Id STRING,un STRING,deux STRING,trois STRING, quatre STRING,cinq STRING,six STRING,sept STRING,huit STRING,neuf STRING)');
if(isset($colonne)and $valeur!="" and $colonne!=''){

    $aff=$db->query('SELECT "'.$colonne.'" FROM partie where Id="'.$idpartie.'" ');
    $res = $aff->fetchArray();

    if(strlen($res[$colonne])==1){
        echo " <p> Insertion impossible une valeur est déjà à l'intérieur</p> <br>";
    }
    else{
        $update = $db->query('UPDATE partie SET "'.$colonne.'" = "'.$valeur.'" where Id="'.$idpartie.'" ');
    }
}

$afficher = $db->query('SELECT * FROM partie where Id="'.$idpartie.'" ');
while ($row = $afficher->fetchArray()) {

$un=$row['un'];
$deux=$row['deux'];
$trois=$row['trois'];
$quatre=$row['quatre'];
$cinq=$row['cinq'];
$six=$row['six'];
$sept=$row['sept'];
$huit=$row['huit'];
$neuf=$row['neuf'];
}
echo "
    <table>
    <tr>
        <td>$un</td>
        <td>$deux</td>
        <td>$trois</td>
    </tr>
    <tr>
        <td>$quatre</td>
        <td>$cinq</td>
        <td>$six</td>
    </tr>
    <tr>
        <td>$sept</td>
        <td>$huit</td>
        <td>$neuf</td>
    </tr>
</table>
    ";

?>
