<?php
include("include/data.php");
Init_boutique();
global $base, $MySQL_Host, $MySQL_User, $MySQL_Passw, $myart, $A_NumArt,$i, $result, 
$NbLigne, $mygroupeart, $mysousgroupeart, $semaine, $limitesuivante, 
$limiteprecedente, $limite, $total, $page, $Nom_Client, $semaine ;
// entete de la page

include 'menu/header.php';

if (!isset($_GET['SelAnne_y']))
{
    $SelAnne_y=date("Y");;
    $SelAnne_y_sel=date("Y");
} else {
    $SelAnne_y=htmlentities($_GET['SelAnne_y']);
    $SelAnne_y_sel=htmlentities($_GET['SelAnne_y']);
}

if (!isset($_GET['SelSem_w']))
{
    $SelSem_w=0;
    $SelSem_w_sel=0;
} else {
    $SelSem_w=htmlentities($_GET['SelSem_w']);
    $SelSem_w_sel=htmlentities($_GET['SelSem_w']);
}

if (!isset($_GET['num_cate_dechets']))
{
    $num_cate_dechets=0;
    $num_cate_dechets_sel=0;
} else {
    $num_cate_dechets=($_GET['num_cate_dechets']);
    $num_cate_dechets_sel=($_GET['num_cate_dechets']);
}

if (!isset($_GET['pages']))
{ $NombrePages=10;} else { $NombrePages=($_GET['pages']);}

$limite=$_GET['limite'];
if (!isset($limite)) $limite = 0;
$nombre = $NombrePages ;
$limitePage = 0 ;
$PageC = ($limite/$NombrePages) ;


$v='select * FROM table_dechets ';
$selectc = 'SELECT count(num_ligne_dechets) FROM table_dechets';
if (($num_cate_dechets || $SelAnne_y || $SelSem_w)>0) {
$v.=' WHERE' ;
$selectc.=' WHERE' ;
} else { 
    $v.=''; 
    $selectc.='';
    }

if ($SelSem_w==0) {
$v.='';
$selectc.='';
} else { 
    $v.=' sem_dechets='.$SelSem_w;
    $selectc.=' sem_dechets='.$SelSem_w; 
    }

if ($num_cate_dechets==0) {
$v.='';
$selectc.='';
 } else { 
   if ($SelSem_w!=0) {
    $v.=' AND num_cate_dechets='.$num_cate_dechets; 
    $selectc.=' AND num_cate_dechets='.$num_cate_dechets; 
     } else {
    $v.=' num_cate_dechets='.$num_cate_dechets; 
    $selectc.=' num_cate_dechets='.$num_cate_dechets; 
    }
}


$SelDeb = date("Y-m-d", mktime(0,0,0,01,01,$SelAnne_y));
$SelFin = date("Y-m-d", mktime(0,0,0,12,31,$SelAnne_y));

if (($num_cate_dechets || $SelSem_w)>0) {
$v.=" AND date_enleve_dechets BETWEEN '".$SelDeb."' AND '".$SelFin."'";
$selectc.=" AND date_enleve_dechets BETWEEN '".$SelDeb."' AND '".$SelFin."'";
} else { 
    $v.=" date_enleve_dechets BETWEEN '".$SelDeb."' AND '".$SelFin."'";
    $selectc.=" date_enleve_dechets BETWEEN '".$SelDeb."' AND '".$SelFin."'";
}

$v.=' ORDER BY date_enleve_dechets DESC limit '.$limite.','.$nombre;

$SelectEA = $v ;
$date = date("d-m-Y");

// on va afficher 10 résultats par page.
$limitesuivante = $limite + $NombrePages;
$limiteprecedente = $limite - $NombrePages;

$req = mysqli_query($link, $selectc); 
$row = mysqli_fetch_row($req);
$total= $row[0];
$NbPage =floor($total/$NombrePages);

/*
echo "<center><HR size='2' width='500'>";
echo $v."<br>".$SelectEA;
echo "<HR size='2' width='500'>";
echo " $total | $SelSem_w | $semaine | $Num_CatPlu | $Num_Client | $SelAnne_y | $SelDeb | $SelFin<br></center>" ;
*/


// titre page
echo "<b>Saisie Dechets</b><br>" ;
echo "<center>";

// FILTRES
// menu haut new-supp-modif
echo "<form id='form1' name='form1' method='get' action='dechets.php'>";
echo "<input type='hidden' name='limite' value='$limite'/>";
echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
echo "<tr><td colspan=5><span class='t-text'><center>FILTRES<br><HR size='1' width='500'></center></td></tr>";
echo "<tr class='text'>";
echo "<td>Par ann&eacute;e :";

  $class = '';
  $fyear='2011';
  $lyear='2050';
  $name = 'SelAnne';

  $yr=date("Y");
// Année 
  
  $s.="<SELECT NAME=\"${name}_y\" class='$class'>\n"; 
  $s.="<OPTION VALUE=$yr>$yr</OPTION>\n";
  $sclt=""; 
  for ($i=$fyear;$i<=$lyear;$i++)
  { 
  $i2="$i"; 
  if ($i2==$yr) 
  $sclt=" SELECTED"; 
  else 
  $sclt=""; 
  $s.="<OPTION VALUE=\"$i\"$sclt>$i</OPTION>\n"; 
  } 
  $s.="</SELECT>\n"; 
    echo"$s";
echo "</td>";
echo "<td>Par semaine :";

  $class = '';
  $fyear='1';
  $lyear='53';
  $name = 'SelSem';

  $yr=date("W");
  
// categorie 
  
  $s="<SELECT style=' width: 65px;' NAME=\"${name}_w\" class='$class'>\n";
  $s.="<OPTION VALUE='0' SELECTED>Toutes</OPTION>\n";
  $s.="<OPTION VALUE=$yr>$yr</OPTION>\n";
  $sclt=""; 
  for ($i=$fyear;$i<=$lyear;$i++)
  { 
  $i2="$i"; 
  if ($i2==$yr) 
  //$sclt=" SELECTED";
  $sclt=""; 
  else 
  $sclt=""; 
  $s.="<OPTION VALUE=\"$i\"$sclt>$i</OPTION>\n"; 
  } 
  $s.="</SELECT>\n"; 
    echo"$s";
echo "</td>";
echo "<td>Par Cat&eacute;gorie : <select style=' width: 60px;' name='num_cate_dechets' id='num_cate_dechets'>";
echo "<option value='0'>Toutes</option>";
$myart =new categorie_dechets;
$select = 'select * FROM categorie_dechets';
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $num_cate_dechets=$myart->num_cate_dechets ;
  $categorie_dechets=$myart->categorie_dechets ;
  echo "<option value='$num_cate_dechets'>$num_cate_dechets | $categorie_dechets</option>";
}

echo "</select>";
echo "</td>";
echo "<td><input type='submit' value='ok'>";
echo "</td>";
echo "</tr>";
echo "</span>";
echo "</table>";
echo "</form></center>";

//
//echo "total : ".$total."| limite  ".$limite."<br> limite suivante ".$limitesuivante."| limite precedente".$limiteprecedente ."<br>";
//

// Menu
echo "<center><HR size='1' width='500'>";
echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
echo "<td>";
echo "<a href='saisie_dechets.php'><b>Nouveau</b></a> <b> | </b>";
echo "<a href=''><b>Consulter</b></a> <b> | </b>";
echo "<a href='delete_dechets.php'><b>Supprimer</b></a> <b> | </b>";
echo "<a href=''><b>Modifier</b></a><b></b>";
echo "</td></tr>";
echo "</tbody>";
echo "</table><HR size='1' width='500'>";

//detail Tableau
//echo "<br>" ;
echo "<table border='1' cellpadding='1' cellspacing='1' width='500' height='22'><tr>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Date</div></td></span>";
echo "<td width='100' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Prestataire</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Num Bon</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Poids</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>cate</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>MRS ?</td></span>";
echo "<td width='32' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>M</td></span>";
echo "<td width='32' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>D</td></span>";
echo "</tr>";

$myart =new table_dechets ;
$result = mysqli_query($link, $SelectEA) ;
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result) ;
  $num_ligne_dechets=$myart->num_ligne_dechets ;
  $Num_bon_dechets=$myart->Num_bon_dechets ;
  $Num_prestataire=$myart->Num_prestataire ;
  $num_cate_dechets=$myart->num_cate_dechets ;
  $mrs=$myart->mrs ;
  $poids_dechets=$myart->poids_dechets ;
  $sem_dechets=$myart->sem_dechets ;
  $date_enleve_dechets=$myart->date_enleve_dechets ;
  $observations=$myart->observations ;
 
$sql = "SELECT Nom_Fournisseur FROM fournisseurs WHERE Num_fournisseur='$Num_prestataire'";  
$req = mysqli_query($link, $sql);  
while ($row = mysqli_fetch_assoc($req)) {
        $nom_prestataire = $row["Nom_Fournisseur"];
    		}   
	  
//echo "<br>".$sql;

$sql = "SELECT categorie_dechets FROM categorie_dechets WHERE num_cate_dechets='$num_cate_dechets'";  
$req = mysqli_query($link, $sql);  
while ($row = mysqli_fetch_assoc($req)) {
        $categorie_dechets = $row["categorie_dechets"];
				}

//
//echo "<br>".$sql."<br>" ;
//echo "<br>prestataire : ".$nom_prestataire." Categorie dechets :".$categorie_dechets."<br>" ;
//
$date_e=date("d/m/Y", strtotime($date_enleve_dechets));
//
//echo  date("d/m/Y", strtotime($date_enleve_dechets))." ".$date_e;
//
  
    echo "<tr><td width='30' height='20' valign='center'>
	<div align='center'><span class='t-text'>$date_e</div></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'>$nom_prestataire</div></td></span>";
	echo "<td width='50' height='20' valign='center'>
	<div align='center'><a class='' href='consult_dechets.php?Num_entre=$num_ligne_dechets'>
	<span class='t-text'>$Num_bon_dechets</div></a></td></span>";
	echo "<td width='50' height='20' valign='center'>
	<div align='center'><span class='t-text'>$poids_dechets kg</div></td></span>";
	echo "<td width='50' height='20' valign='center'>
	<div align='center'><span class='t-text'>$categorie_dechets</div></td></span>";
	echo "<td width='50' height='20' valign='center'>" ;
		if ($mrs==1) {
		echo "<div align='center'><span class='t-text'>
		      <a class='' href='consult_dechets.php?Num_entre=$num_ligne_dechets'>
		      <img src='images/coche.gif' width='20' height='20'>
		      </a></div></td></span>";
		} else {
		echo "<div align='center'><span class='t-text'>
		      <a class='' href='consult_dechets.php?Num_entre=$num_ligne_dechets'>
		      <img src='images/croix.gif' width='20' height='20'></div></td></span>";
		}
	echo "<td width='20' height='20' valign='center'>
	<div align='center'><a class='' href='modif_dechets.php?Num_entre=$num_ligne_dechets'>
	<img src='images/modifie.png' width='20' height='20'></a></div></td>";
	echo "<td width='20' height='20' valign='center'>
	<div align='center'><a class='' href='delette_dechets.php?Num_entre=$num_ligne_dechets'>
	<img src='images/pblle.jpg' width='20' height='20'></a></div></td></tr>";

} 
echo "</tr></table>";

echo "<br>";
// menu saisie Mrs
echo "<center>";
Echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
Echo "<tbody>";
echo "<span class='t-text'><b>Saisies M.R.S. : </b></span> <b> : </b>";
echo "<a href='saisie_mrs.php?limite=0'><b>Saisie</b></a> <b> | </b>";
echo "<a href='saisie_mrs.php?limite=0'><b>Consulter</b></a> <b> | </b>";
echo "<a href='saisie_mrs.php?limite=0'><b>Modifier</b></a>";
Echo "</td></tr>";
Echo "</tbody>";
Echo "</table><br>";


echo "<table border='0' cellpadding='0' cellspacing='0' width='500' height='22'><tr>";
echo "<td width='120' height='20' valign='center'><div align='center'><span class='t-text'>";
	 
if($limite != 0) {
    echo '<a href="'.$page.'?limite='.$limiteprecedente
   .'&SelSem_w='.$SelSem_w_sel
    .'&Num_CatPlu='.$Num_CatPlu_sel
    .'&Num_Client='.$Num_Client_sel
    .'">Page precedente  </a>';
   // echo '<a href="'.$page.'?limite='.$limiteprecedente.'">Page precedente  </a>';
}

// Nombre dintervale entre deux
$range = 5;
 
// affiche les intervales entre pages
$initial_num = $PageC - $range;
$condition_limit_num = ($PageC + $range)  + 1;

for ($a = $initial_num ; $a <= $condition_limit_num; $a++) {
// 0 devant 123456789   
   $pagenum = $PageC < 10 ? $pagenum = "0".$PageC : $pagenum = $PageC; 
   $pagenuma = $a < 10 ? $pagenuma = "0".$a : $pagenuma = $a;
 
  if ($a > 0 && $a <= $NbPage ) {
  	
  $limitePage = $NombrePages*$a ;
  
    if (($limite/10)==$a) {
    echo '<a href="'.$page.'?limite='.$limitePage
    .'&SelSem_w='.$SelSem_w_sel
.'&Num_CatPlu='.$Num_CatPlu_sel
.'&Num_Client='.$Num_Client_sel
    .'"><FONT color="red">'.$pagenum.'</FONT></a> | ';
    } else {
     echo '<a href="'.$page.'?limite='.$limitePage
    .'&SelSem_w='.$SelSem_w_sel
.'&Num_CatPlu='.$Num_CatPlu_sel
.'&Num_Client='.$Num_Client_sel
    .'">'.$pagenuma.'</a> | ';
  	}	
	}	
}

if($limitesuivante < $total) 
{
echo '<a href="'.$page.'?limite='.$limitesuivante
.'&SelSem_w='.$SelSem_w_sel
.'&Num_CatPlu='.$Num_CatPlu_sel
.'&Num_Client='.$Num_Client_sel
.'">Page Suivante</a>';
}
echo "</div></td></span>";
echo "";
echo "</tr></table>";



include 'menu/footer.php';
?>
