<?php
  set_time_limit ( 300 );
  $pas=500;
  if(!isset($pos))
  {
    $pos=0;
  }
  $postdata = file_get_contents("php://input");
  $pos1 = strpos($postdata, "NoClient%5B%5D=");
  if ($pos1 !== FALSE)
  {
	$NoClient = array();
  }
  while ($pos1 !== FALSE)
  {
    $pos2 = strpos($postdata, "&", $pos1);
	if ($pos2 !== FALSE)
	{
		$tmp_no = substr($postdata, $pos1 + 15, $pos2 - $pos1 - 15);
	}
	else
	{
		$tmp_no = substr($postdata, $pos1 + 15);
	}
	array_push($NoClient, $tmp_no);
	$pos1 = strpos($postdata, "NoClient%5B%5D=", $pos1 + 15);
  }
  if (isset($NoClient))
  {
    foreach ($NoClient as $val)
    {
      $requette="UPDATE clients SET TypeRabais=".${"Rab".$val}." WHERE NoClient=$val";
      Connection::executeRawQuery($requette);
    }
  }
  $res=TypeRabais::getActiveTypeRabais();
  $nb=count($res);
  $resultat=array_slice(Client::getClient(),$pos, $pas);
  echo "<FORM method='POST'><TABLE border=1>\n";
  echo "   <input type='hidden' name='pos' value=".($pos+$pas).">\n";
  echo "<TABLE border=1>\n";
  foreach($resultat as $ligne)
  {
    echo "<TR><TD><input type='hidden' name='NoClient[]' value='".$ligne['NoClient']."'><SELECT name='Rab".$ligne['NoClient']."'>";
    for($cpt=0;$cpt<$nb;$cpt++)
    {
      echo "<option value='".$res[$cpt][0]."'";
      if($res[$cpt][0]==$ligne['TypeRabais'])
      {
        echo " SELECTED";
      }
      echo ">".htmlspecialchars($res[$cpt][1])."</option>";
    }
    echo "</SELECT></TD><TD>".$ligne['Nom']."</TD><TD>".$ligne['Prenom']."</TD><TD>".$ligne['NPA']."</TD><TD>".$ligne['ChiffreAffaires']."</TD></TR>\n";
  }
  echo "</TABLE><input type='submit' value='valide'></FORM>\n";
?>
