<?php
  //ob_end_flush();
  set_time_limit ( 300 );
  //print_r($_POST);
  //phpinfo();
  $postdata = file_get_contents("php://input");
  $pos = strpos($postdata, "NoDelete%5B%5D=");
  if ($pos !== FALSE)
  {
	$NoDelete = array();
  }
  while ($pos !== FALSE)
  {
    $pos2 = strpos($postdata, "&", $pos);
	if ($pos2 !== FALSE)
	{
		$tmp_no = substr($postdata, $pos + 15, $pos2 - $pos - 15);
	}
	else
	{
		$tmp_no = substr($postdata, $pos + 15);
	}
	array_push($NoDelete, $tmp_no);
	$pos = strpos($postdata, "NoDelete%5B%5D=", $pos + 15);
  }
  //print_r($NoDelete2);
  if (isset($NoDelete))
  {
    //echo "ici 1<BR>\n";
    foreach ($NoDelete as $NoClient)
    {
      //echo "ici 2<BR>\n";
      if(isset(${"NoRemplace".$NoClient}))
      {
		//echo "ici 3<BR>\n";
        $NoRemplace=${"NoRemplace".$NoClient};
        if($NoRemplace!=0)
        {
          if ($ligne_client = Client::getClientByNoClient($NoClient))
          {
            $requette="UPDATE commande".ID_GARAGE." SET NoClient=$NoRemplace WHERE NoClient=$NoClient";
            Connection::executeRawQuery($requette);
            echo "$requette<BR>\n";
            $requette="UPDATE vehicules SET Client=$NoRemplace WHERE Client=$NoClient";
            Connection::executeRawQuery($requette);
            echo "$requette<BR>\n";
            $requette="UPDATE atelier".ID_GARAGE." SET NoClient=$NoRemplace, Nom='" . Connection::getConnection()->quote($ligne_client['Nom']) . "', Prenom='" . Connection::getConnection()->quote($ligne_client['Prenom']) . "' WHERE NoClient=$NoClient";
            Connection::executeRawQuery($requette);
            echo "$requette<BR>\n";
            $requette="UPDATE location".ID_GARAGE." SET NoClient=$NoRemplace, Nom='" . Connection::getConnection()->quote($ligne_client['Nom']) . "', Prenom='" . Connection::getConnection()->quote($ligne_client['Prenom']) . "' WHERE NoClient=$NoClient";
            Connection::executeRawQuery($requette);
            echo "$requette<BR>\n";
            $requette="DELETE FROM clients WHERE NoClient=$NoClient";
            Connection::executeRawQuery($requette);
            echo "$requette<BR>\n";
          }
        }
      }
      else
      {
        $requette="DELETE FROM clients WHERE NoClient=$NoClient";
        Connection::executeRawQuery($requette);
        echo "$requette<BR>\n";
      }
    }
  }
  $requette="SELECT COUNT(*), Nom, Prenom FROM clients GROUP BY Nom, Prenom ORDER BY Nom";
  $resultat=Connection::executeRawQueryWithfetchAll($requette);
  echo "<FORM method='POST' accept-charset='UTF-8'><TABLE border=1><TR><TH>Suppression + No Client remplacement</TH><TH>Nb</TH><TH>No client</TH><TH>Nom</TH><TH>Prénom</TH><TH>Adresse</TH><TH>Email</TH><TH>Portable</TH><TH>Nb commandes</TH><TH>Nb vehicules</TH><TH>Nb ateliers</TH><TH>Nb locations</TH></TR>\n";
  echo "   <input type='hidden' name='Type' value=$Type>\n";
  echo "   <input type='hidden' name='Sous_Type' value=$Sous_Type>\n";
  foreach($resultat as $ligne)
  {
    if($ligne[0]>1)
    {
      echo "<TR><TD>&nbsp;</TD><TD>".htmlspecialchars($ligne[0])."</TD><TD>&nbsp;</TD><TD>".htmlspecialchars($ligne[1])."</TD><TD>".htmlspecialchars($ligne[2])."</TD><TD>&nbsp;</TD><TD>&nbsp;</TD><TD>&nbsp;</TD></TR>\n";
      //$requette2="SELECT NoClient,Nom,Prenom,Adresse,NPA,Localite FROM clients WHERE Nom LIKE '".addslashes($ligne[1])."' ORDER BY Prenom,NoClient";
      $requette2="SELECT NoClient,Nom,Prenom,Adresse,NPA,Localite,Email,Natel FROM clients WHERE Nom = '".addslashes($ligne[1])."' AND Prenom = '".addslashes($ligne[2])."' ORDER BY Prenom,NoClient";
      $resultat2=Client::getClientByNomPrenom(addslashes($ligne["Nom"]), addslashes($ligne["Prenom"]));
      foreach($resultat2 as $ligne2)
      {
        $requette3="SELECT COUNT(*) FROM commande".ID_GARAGE." WHERE NoClient=".$ligne2[0];
        $resultat3 = Commande::getCommandeByNoClient($ligne2["NoClient"]);
        $ligne3=count($resultat3);
        $requette4="SELECT COUNT(*) FROM vehicules WHERE Client=".$ligne2[0];
        $resultat4=Vehicule::getVehiculeByClient($ligne2["NoClient"]);
        $ligne4=count($resultat4);
        $requette5="SELECT COUNT(*) FROM atelier".ID_GARAGE." WHERE NoClient=".$ligne2[0];
        $resultat5=Atelier::getAtelierByNoClient($ligne2["NoClient"]);
        $ligne5=count($resultat5);
        $requette6="SELECT COUNT(*) FROM location".ID_GARAGE." WHERE NoClient=".$ligne2[0];
        $resultat6=Location::getByNoClient($ligne2["NoClient"]);
        $ligne6=count($resultat6);
        if(($ligne3+$ligne4+$ligne5+$ligne6)!=0)
        {
          echo "<TR><TD><INPUT type='checkbox' name='NoDelete[]' value='".$ligne2[0]."'><INPUT name='NoRemplace".$ligne2[0]."'></TD>";
        }
        else
        {
          echo "<TR><TD><INPUT type='checkbox' name='NoDelete[]' value='".$ligne2[0]."'></TD>";
        }
        echo "<TD>&nbsp;</TD><TD>".htmlspecialchars($ligne2[0])."</TD><TD>".htmlspecialchars($ligne2[1])."</TD><TD>".htmlspecialchars($ligne2[2])."</TD><TD>".htmlspecialchars($ligne2[3])." ".htmlspecialchars($ligne2[4])." ".htmlspecialchars($ligne2[5])."</TD><TD>".htmlspecialchars($ligne2[6])."</TD><TD>".htmlspecialchars($ligne2[7])."</TD><TD>".htmlspecialchars($ligne3[0])."</TD><TD>".htmlspecialchars($ligne4[0])."</TD><TD>".htmlspecialchars($ligne5[0])."</TD><TD>".htmlspecialchars($ligne6[0])."</TD></TR>\n";
      }
    }
  }
  echo "</TABLE><input type='submit' value='valide'></FORM>\n";
?>
