<?php
  if ($_POST["FormName"] == "new")
  {
    $requette = "INSERT INTO opportunite" . ID_GARAGE . " (Type, Statut, NoClient, Date, PersonneContact, Description, DateProchainSuivit) VALUES ('$TypeOpportunite', '$StatutOpportunite', $NoClient, '" . Decode_Entree_Type($Date,2) . "', '" . addslashes($PersonneContact) . "', '" . addslashes($Description) . "', '" . Decode_Entree_Type($DateSuivi,2) . "')";
	//echo "$requette<br/>";
	Connection::executeRawQuery($requette);
  }
  if ($_POST["FormName"] == "edit")
  {
    $requette = "UPDATE opportunite" . ID_GARAGE . " SET Type = '$TypeOpportunite', Statut = '$StatutOpportunite', NoClient = $NoClient, Date = '" . Decode_Entree_Type($Date,2) . "', PersonneContact = '$PersonneContact', Description = '" . addslashes($Description) . "', DateProchainSuivit = '" . Decode_Entree_Type($DateSuivi,2) . "' WHERE NoOpportunite = $NoOpportunite";
	//echo "$requette<br/>";
	Connection::executeRawQuery($requette);
	$requette = "DELETE FROM opportunitesuivi" . ID_GARAGE . " WHERE NoOpportunite = $NoOpportunite";
	Connection::executeRawQuery($requette);
	for ($cpt = 0; $cpt < $_POST["NbSuivis"]; $cpt++)
	{
	  if (isset($_POST["suivi_date_".$cpt]) && $_POST["suivi_date_".$cpt] != "")
	  {
	    $requette = "INSERT INTO opportunitesuivi" . ID_GARAGE . " (NoOpportunite, Date, Commentaire, PersonneContact) VALUES ($NoOpportunite, '" . Decode_Entree_Type($_POST["suivi_date_".$cpt],2) . "', '" . addslashes($_POST["suivi_description_".$cpt]) . "', '" . $_POST["suivi_personne_".$cpt] . "')";
	    Connection::executeRawQuery($requette);
	  }
	}
    if (isset($_POST["suivi_date_new"]))
	{
	  $requette = "INSERT INTO opportunitesuivi" . ID_GARAGE . " (NoOpportunite, Date, Commentaire, PersonneContact) VALUES ($NoOpportunite, '" . Decode_Entree_Type($suivi_date_new,2) . "', '" . addslashes($suivi_description_new) . "', '$suivi_personne_new')";
	  //echo "$requette<br/>";
	  Connection::executeRawQuery($requette);
	}
  }
  if ($_POST["FormName"] == "delete")
  {
    $requette = "DELETE FROM opportunite" . ID_GARAGE . " WHERE NoOpportunite = $NoOpportunite";
	//echo "$requette<br/>";
	Connection::executeRawQuery($requette);
    $requette = "DELETE FROM opportunitesuivi" . ID_GARAGE . " WHERE NoOpportunite = $NoOpportunite";
	//echo "$requette<br/>";
	Connection::executeRawQuery($requette);
  }
  $types_opportunite = get_config("TYPES_OPPORTUNITE");
  $status_opportunite = get_config("STATUS_OPPORTUNITE");
  //echo "$status_opportunite<br/>";
  $type_opportunite_array = explode(";", $types_opportunite);
  $statut_opportunite_array = explode(";", $status_opportunite);
  echo "<script src=\"js/opportunite.js\"></script>\n";
  echo "<form name=\"filter\" method=\"get\">\n";
  echo "<input type='hidden' name='Type' value=$Type>\n";
  echo "<input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
  echo "Type <select name=\"type_opportunite\" onchange=\"document.filter.submit()\">";
  echo "<option value=\"\"></option>";
  foreach ($type_opportunite_array as $type_opportunite)
  {
    echo "<option value=\"$type_opportunite\"";
	if ($_GET["type_opportunite"] == $type_opportunite)
	{
	  echo " selected=\"selected\"";
	}
	echo ">$type_opportunite</option>";
  }
  echo "</select>";
  echo "Statut <select name=\"statut_opportunite\" onchange=\"document.filter.submit()\">";
  echo "<option value=\"\"></option>";
  echo $statut_opportunite_array;
  foreach ($statut_opportunite_array as $statut_opportunite)
  {
    $len = strlen($statut_opportunite);
    $element = substr($statut_opportunite, 0, $len - 3);
	$mod = substr($statut_opportunite, $len - 3);
	if ($mod == "(1)")
	{
	  $statut_defaut = $element;
	}
    echo "<option value=\"$element\"";
	if ($_GET["statut_opportunite"] == $element)
	{
	  echo " selected=\"selected\"";
	}
	else if ($mod == "(1)" && (!isset($_GET["statut_opportunite"])))
	{
	  echo " selected=\"selected\"";
	}
	echo ">$element</option>";
  }
  echo "</select>";
  echo "Client <input type=\"text\" name=\"nom\" value=\"" . $_GET["nom"] . "\"/> Texte <input type=\"text\" name=\"texte\" value=\"" . $_GET["texte"] . "\"/><button>Rechercher</button>";
  echo "</form>";
  $req_first_element = true;
  $req_complement = "";
  if ($_GET["statut_opportunite"] != "")
  {
    if ($req_first_element)
	{
	  $req_first_element = false;
	}
    $req_complement .= "o.Statut = '" . $_GET["statut_opportunite"] . "'";
  }
  else
  {
    //$req_complement .= " Statut = '" . $statut_defaut . "'";
  }
  if ($_GET["type_opportunite"] != "")
  {
    if ($req_first_element)
	{
	  $req_first_element = false;
	}
	else
	{
	  $req_complement .= " AND ";
	}
    $req_complement .= " o.Type = '" . $_GET["type_opportunite"] . "'";
  }
  if ($_GET["texte"] != "")
  {
    if ($req_first_element)
	{
	  $req_first_element = false;
	}
	else
	{
	  $req_complement .= " AND ";
	}
    $req_complement .= " o.Description LIKE '%" . $_GET["texte"] . "%'";
  }
  if ($_GET["nom"] != "")
  {
    if ($req_first_element)
	{
      $requette = "SELECT o.* FROM opportunite" . ID_GARAGE . " o, clients c WHERE c.NoClient = o.NoClient AND (c.Nom LIKE '". $_GET["nom"] ."%' OR c.Prenom LIKE '". $_GET["nom"] ."%')";
	}
	else
	{
      $requette = "SELECT o.* FROM opportunite" . ID_GARAGE . " o, clients c WHERE " .$req_complement . " AND c.NoClient = o.NoClient AND (c.Nom LIKE '". $_GET["nom"] ."%' OR c.Prenom LIKE '". $_GET["nom"] ."%')";
	}
  }
  else
  {
    if ($req_first_element)
	{
      $requette = "SELECT * FROM opportunite" . ID_GARAGE . " o ORDER BY Type";
	}
	else
	{
      $requette = "SELECT * FROM opportunite" . ID_GARAGE . " o WHERE " .$req_complement . " ORDER BY Type";
	}
  }
  //echo "$requette<br/>";
  $resultat = Connection::executeRawQueryWithfetchAll($requette);
  $last_type = "";
  $pos = 0;
  $nb_fiches = 3;
  $nb_collones = $nb_fiches * 3;
  echo "<button class=\"new_opportunite\" type_opportunite=\"\">Ajouter</button><br/><br/>";
  echo "<table cellspacing=\"0\" id=\"tableOpportunites\">";
  foreach ($resultat as $ligne)
  {
    if ($ligne["Type"] != $last_type)
	{
	  $last_type = $ligne["Type"];
	  if ($pos != 0)
	  {
	    while ($pos != 0)
	    {
	      echo "<td colspan=\"3\" class=\"empty_cell\">&nbsp;</td>";
		  $pos = ($pos+1) % $nb_fiches;
	    }
		echo "</tr>";
	  }
	  echo "<tr class=\"ligneType\"><td colspan=\"$nb_collones\">$last_type <button class=\"new_opportunite\" type_opportunite=\"$last_type\">Ajouter</button></td></tr>";
	  echo "<tr class=\"ligneTitres\">";
	  echo str_repeat("<td>Infos</td><td>Demande</td><td>Suivis</td>",$nb_fiches);
	  echo "</tr>";
	}
	if ($pos == 0)
	{
	  echo "<tr class=\"ligneFiches\">";
	}
	//echo "<td>";
	//echo "<table cellspacing=\"0\" class=\"tableFicheOpportunite\">\n";
	$ligne_client = Client::getClientByNoClient($ligne["NoClient"]); 
	$resultat_suivi = OpportuniteSuivi::getByNoOpportunite($ligne["NoOpportunite"]);
	//$nb_suivis = mysql_num_rows($resultat_suivi);
	//echo "<tr>";
	echo "<td class=\"info1\"><a href=\"#\" class=\"edit_opportunite\" noOpportunite=\"" . $ligne["NoOpportunite"] . "\">" . $ligne_client["Nom"] . " " . $ligne_client["Prenom"] . "<br/>" . Cree_Entree_Type("",7,0,$ligne_client["Natel"],AFFICHAGE) . "<br/>" . $ligne_client["Email"] . "<br/>Date " . Cree_Entree_Type("",2,0,$ligne["Date"],AFFICHAGE) . " (".$ligne["PersonneContact"].")<br/>" . $ligne["Statut"] . "<br/>Prochain suivi " . Cree_Entree_Type("",2,0,$ligne["DateProchainSuivit"],AFFICHAGE) . "</a></td>";
	echo "<td class=\"info2\">" . str_replace("\n", "<br>", $ligne["Description"]) . "</td>";
	$date_suivi = mktime(0, 0, 0, substr($ligne["DateProchainSuivit"],5,2), substr($ligne["DateProchainSuivit"],8,2), substr($ligne["DateProchainSuivit"],0,4));
	$now = time();
	$date_jour = mktime(0, 0, 0, date("m", $now)  , date("d", $now), date("Y", $now));
	$diff_date = ($date_suivi - $date_jour) / 3600 / 24;
	echo "<td class=\"info3";
	if ($diff_date < 0)
	{
	  echo " suivi2";
	}
	else if($diff_date < 7)
	{
	  echo " suivi1";
	}
	echo "\">";
	foreach ($resultat_suivi as $ligne_suivi)
	{
	  echo Cree_Entree_Type("",2,0,$ligne_suivi["Date"],AFFICHAGE) . "(" . $ligne_suivi["PersonneContact"] . ") <div>" . str_replace("\n", "<br/>", $ligne_suivi["Commentaire"]) . "</div><br/>";
	}
	echo "</td>";
	//echo "</tr>";
	//echo "</table>\n";
	//echo "</td>";
	$pos = ($pos + 1) % $nb_fiches;
	if ($pos == 0)
	{
	  echo "</tr>";
	}
  }
  if ($pos != 0)
  {
	while ($pos != 0)
	{
	  echo "<td colspan=\"3\" class=\"empty_cell\">&nbsp;</td>";
	  $pos = ($pos+1) % $nb_fiches;
	}
	echo "</tr>";
  }
  
  echo "</table>";
  echo "<div id=\"dialog_new\" title=\"Nouvelle opportunité\">";
  echo "<form name=\"new\" method=\"post\">";
  echo "<input type='hidden' name='FormName' value='new'>\n";
  echo "<table>";
  echo "<tr><th>Type</th><td><select name=\"TypeOpportunite\">";
  foreach ($type_opportunite_array as $type_opportunite)
  {
    echo "<option value=\"$type_opportunite\">$type_opportunite</option>";
  }
  echo "</select></td></tr>";
  echo "<tr><th>Statut</th><td><select name=\"StatutOpportunite\">";
  foreach ($statut_opportunite_array as $statut_opportunite)
  {
    $len = strlen($statut_opportunite);
    $element = substr($statut_opportunite, 0, $len - 3);
    echo "<option value=\"$element\">$element</option>";
  }
  echo "</select></td></tr>";
  echo "<tr><th>Numéro client</th><td><input type=\"text\" name=\"NoClient\" readonly=\"readonly\" /></td></tr>";
  echo "<tr><th>Nom</th><td><input type=\"text\" name=\"Nom\" onkeydown='test_return_client_dialog(6,\"new\");'/></td></tr>";
  echo "<tr><th>Prénom</th><td><input type=\"text\" name=\"Prenom\" onkeydown='test_return_client_dialog(6,\"new\");'/><button type=\"button\" onclick=\"recherche_client_dialog(6,'new');\">Rechercher</button></td></tr>";
  echo "<tr><th>Date</th><td><input type=\"text\" name=\"Date\" value=\"".date("d.m.Y")."\" /></td></tr>";
  echo "<tr><th>Personne contact</th><td><input type=\"text\" name=\"PersonneContact\"/></td></tr>";
  $nextmonth = mktime(0, 0, 0, date("m")+1, date("d"),   date("Y"));
  echo "<tr><th>Date suivi</th><td><input type=\"text\" name=\"DateSuivi\" value=\"".date("d.m.Y",$nextmonth)."\"/></td></tr>";
  echo "<tr><th>Description</th><td><textarea cols=\"40\" rows=\"5\" name=\"Description\"></textarea></td></tr>";
  echo "</table>";
  echo "<input class='button' type='button' onClick='submit_ok(this.form);' value='Créer' />";
  echo "</form>";
  echo "</div>";
  
  echo "<div id=\"dialog_edit\" title=\"Opportunité\">";
  echo "<form name=\"edit\" method=\"post\">";
  echo "<input type='hidden' name='FormName' value='edit'>\n";
  echo "<input type='hidden' name='NoOpportunite'>\n";
  echo "<table>";
  echo "<tr><th>Type</th><td><select name=\"TypeOpportunite\">";
  foreach ($type_opportunite_array as $type_opportunite)
  {
    echo "<option value=\"$type_opportunite\">$type_opportunite</option>";
  }
  echo "</select></td></tr>";
  echo "<tr><th>Statut</th><td><select name=\"StatutOpportunite\">";
  foreach ($statut_opportunite_array as $statut_opportunite)
  {
    $len = strlen($statut_opportunite);
    $element = substr($statut_opportunite, 0, $len - 3);
    echo "<option value=\"$element\">$element</option>";
  }
  echo "</select></td></tr>";
  echo "<tr><th>Numéro client</th><td><input type=\"text\" name=\"NoClient\" readonly=\"readonly\" /></td></tr>";
  echo "<tr><th>Nom</th><td><input type=\"text\" name=\"Nom\" onkeydown='test_return_client_dialog(6,\"new\");'/></td></tr>";
  echo "<tr><th>Prénom</th><td><input type=\"text\" name=\"Prenom\" onkeydown='test_return_client_dialog(6,\"new\");'/><button type=\"button\" onclick=\"recherche_client_dialog(6,'edit');\">Rechercher</button></td></tr>";
  echo "<tr><th>Date</th><td><input type=\"text\" name=\"Date\"/></td></tr>";
  echo "<tr><th>Personne contact</th><td><input type=\"text\" name=\"PersonneContact\"/></td></tr>";
  echo "<tr><th>Date suivi</th><td><input type=\"text\" name=\"DateSuivi\"/></td></tr>";
  echo "<tr><th style=\"vertical-align:top;\">Description</th><td><textarea cols=\"40\" rows=\"5\" name=\"Description\"></textarea></td></tr>";
  echo "<tr><th style=\"vertical-align:top;\">Suivis</th><td><button type=\"button\" id=\"AjouterSuivi\">Ajouter</button></td></tr>";
  echo "<tr><td colspan=\"2\"><table id=\"TableSuivis\"><tr><th>Date / Personne</th><th>Description</th></tr></table></td></tr>";
  echo "</table>";
  echo "<input type='hidden' name='NbSuivis'>\n";
  echo "<input class='button' type='button' onClick='submit_ok(this.form);' value='Sauver' />";
  echo "<input class='button' type='button' onClick='dialog_delete_opportunite();' value='Supprimer' />";
  echo "</form>";
  echo "<form name=\"delete\" method=\"post\">";
  echo "<input type='hidden' name='FormName' value='delete'>\n";
  echo "<input type='hidden' name='NoOpportunite'>\n";
  echo "</form>";
  echo "</div>";
  
?>