<script language="JavaScript" type="text/javascript">
function cree_message(a)
{
  var de = $("#newde").val();
  var message = $("#newmessage").val();
  jQuery.ajax({ 
  url: "creeMessageAjax.php", 
  data: { A: a, De: de, Message: message }, 
  success: function(data) {
    window.location.reload(true);
  } 
  }); 
}
function update_message(id)
{
  var de = $("#de"+id).val();
  var message = $("#message"+id).val();
  var statut = $("#statut"+id).val();
  //alert(statut);
  jQuery.ajax({ 
    url: "updateMessageAjax.php", 
    data: { Id: id, De: de, Message: message, Statut: statut }, 
    success: function(data) {
      window.location.reload(true);
    } 
  });
}
</script>
<?php
  function formatdatetime($timestamp)
  {
    return substr($timestamp, 11, 2).":".substr($timestamp, 14, 2)." ".substr($timestamp, 8, 2).".".substr($timestamp, 5, 2).".".substr($timestamp, 0, 4);
  }
  function ddlde($id, $default)
  {
    $result = "<select name=\"$id\" id=\"$id\">";
	$personnes_notes = get_config("NOTES_PERSO");
    if ($personnes_notes != "")
    {
      echo "<div id=\"notes\">\n";
	  $personnes_notes_a = explode(";", $personnes_notes);
	  foreach ($personnes_notes_a as $personne_notes)
	  {
	    $result .= "<option";
		if ($default == $personne_notes)
		{
		  $result .= " selected=\"selected\"";
		}
		$result .= ">$personne_notes</option>";
	  }
	}
	$result .= "</select>";
	return $result;
  }
  function ddlstatut($id, $default)
  {
    $result = "<select name=\"$id\" id=\"$id\">";
	if ($default == 0)
	{
	  $result .= "<option value=\"0\" selected=\"selected\">Nouveau</option>";
	  $result .= "<option value=\"1\">Actif</option>";
	}
	else
	{
	  $result .= "<option value=\"1\" selected=\"selected\">Actif</option>";
	}
	$result .= "<option value=\"2\">Terminé</option>";
    $result .= "</select>";
    return $result;
  }
  echo "<h1>$Personne</h1>\n";
  echo "<table border=\"1\"><tr><th>De</th><th>Création</th><th>Message</th><th>Statut</th><th>Action</th></tr>\n";
  echo "<tr><td>".ddlde("newde","")."</td><td>&nbsp;</td><td><textarea style=\"width: 500px; height: 50px;\" name=\"newmessage\" id=\"newmessage\"></textarea></td><td>Nouveau</td><td><button onclick=\"cree_message('$Personne');\">Créer</button></td></tr>\n";
  $resultatnew = Message::getByAAndEtat($Personne);
  foreach ($resultatnew as $lignenew)
  {
    echo "<tr data-id=\"".$lignenew["ID"]."\" style=\"background-color: yellow;\"><td>".ddlde("de".$lignenew["ID"], $lignenew["De"])."</td><td>".formatdatetime($lignenew["Date"])."</td><td><textarea style=\"width: 500px; height: 50px;\" name=\"message".$lignenew["ID"]."\" id=\"message".$lignenew["ID"]."\">".$lignenew["Message"]."</textarea></td><td>".ddlstatut("statut".$lignenew["ID"], 0)."</td><td><button onclick=\"update_message(".$lignenew["ID"].");\">Sauver</button></td></tr>\n";
  }
  //$nbnew = mysql_num_rows($resultatnew);
  $resultatactif = Message::getByAAndEtat($Personne, 1);
  foreach ($resultatactif as $ligneactif)
  {
    echo "<tr data-id=\"".$ligneactif["ID"]."\" style=\"background-color: blue;\"><td>".ddlde("de".$ligneactif["ID"], $ligneactif["De"])."</td><td>".formatdatetime($ligneactif["Date"])."</td><td><textarea style=\"width: 500px; height: 50px;\" name=\"message".$ligneactif["ID"]."\" id=\"message".$ligneactif["ID"]."\">".$ligneactif["Message"]."</textarea></td><td>".ddlstatut("statut".$ligneactif["ID"], 1)."</td><td><button onclick=\"update_message(".$ligneactif["ID"].");\">Sauver</button></td></tr>\n";
  }
  $nbactif = count($resultatactif);
  echo "</table>\n";
?>
