<?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';
/*
http://192.168.1.200/horius/plu.php?ordre=1&limite=0&Num_TypePlu=1&Num_CatPlu=1&search=pale&pages=100#
*/

if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (isset($_POST['Num_CatPlu'])) { $Num_CatPlu=$_POST['Num_CatPlu']; }  else {$Num_CatPlu=''; }
		if (isset($_POST['Num_TypePlu'])) { $Num_TypePlu=$_POST['Num_TypePlu']; } else {$Num_TypePlu=''; }
		if (isset($_POST['limite'])) { $limite=$_POST['limite']; } else {$limite=''; }
		if (isset($_POST['search'])) { $search=$_POST['search']; } else {$search=''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (isset($_GET['Num_CatPlu'])) { $Num_CatPlu=$_GET['Num_CatPlu']; }  else {$Num_CatPlu='0'; }
		if (isset($_GET['Num_TypePlu'])) { $Num_TypePlu=$_GET['Num_TypePlu']; } else {$Num_TypePlu='0'; }
		if (isset($_GET['limite'])) { $limite=$_GET['limite']; } else {$limite='0'; }
		if (isset($_GET['search'])) { $search=$_GET['search']; } else {$search=''; }
		$valForm="GET" ;
}

//echo "Cate : ".$Num_CatPlu." search : ".$search." NumType : ".$Num_TypePlu."Limite : $limite | ordre : $num_ordre";

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

$v="select * FROM plu WHERE IdNumClient='".$IdNumClient."'";
$selectc = "SELECT count(Num_PLU) FROM plu WHERE IdNumClient='".$IdNumClient."'";

if ($Num_TypePlu==0) {
$v.='';
$selectc.='';
 } else { 
    $v.=' AND Num_TypePlu='.$Num_TypePlu; 
    $selectc.=' AND Num_TypePlu='.$Num_TypePlu; 
     }
	
if ($Num_CatPlu==0) {
$v.='';
$selectc.='';
 } else { 
 if ($Num_TypePlu==0) {
    $v.=' AND Num_CatPlu='.$Num_CatPlu; 
    $selectc.=' AND Num_CatPlu='.$Num_CatPlu; 
	} else { 
	$v.=' AND Num_CatPlu='.$Num_CatPlu; 
    $selectc.=' AND Num_CatPlu='.$Num_CatPlu; 
     }
 }
 
if (!empty($search)) {	
		if ($Num_CatPlu==0 AND $Num_TypePlu==0 ) {
        $v.=" AND LCASE (PLU) LIKE LCASE ('%".$search."%')";
 	    $selectc.=" AND LCASE (PLU) LIKE LCASE ('%".$search."%')";
 } else { 
        $v.=" AND LCASE (PLU) LIKE LCASE ('%".$search."%')";
 	    $selectc.=" AND LCASE (PLU) LIKE LCASE ('%".$search."%')";
     }
	$v.='';
	$selectc.='';
}

$v.=' ORDER BY Num_PLU ASC limit '.$limite.','.$NombrePages;

$SelectEA = $v ;

// comptage du nombre de lignes de la base
 

/* test
echo "<center><HR size='2' width='500'>";
echo "v : ".$v."<br> selectC : ".$selectc;
echo "<HR size='2' width='500'>";
echo " $total | $semaine | $Num_CatPlu | $search <br></center>" ;
*/

echo "<center>";


// 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);


// Titre

echo "<div class='titre-text'><center>Fiches Articles</div></center><br>" ;

// Filtres 

echo "<form id='form1' name='form1' method='get' action='plu.php'>";
echo "<input type='hidden' name='limite' value='$limite'/>";
echo "<input type='hidden' name='ordre' value='$num_ordre'/>";
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 Cat&eacute;gorie : <select style=' width: 150px;' name='Num_CatPlu' id='Num_CatPlu'>";
echo "<option value='0'>Toutes</option>";
$myart =new categorieplu;
$select = 'select * FROM categorieplu';
$result = mysqli_query($link, $select);
$myart->getnum($result);
while ($myart->z < $myart->num) {
  $myart->readrow($result);
  $num_cate_plu_intran=$myart->Num_CatPlu ;
  $categorie_plu_intran=$myart->CategoriePlu ;
  echo "<option value='$num_cate_plu_intran'>$num_cate_plu_intran | $categorie_plu_intran</option>";
}
echo "</select>";

echo "</td>";

echo "<td>Par Type : <select style=' width: 150px;' name='Num_TypePlu' id='Num_TypePlu'>";
echo "<option value='0'>Toutes</option>";
$sql = "SELECT * FROM type_plu";  
$req = mysqli_query($link, $sql);  
while ($row = mysqli_fetch_assoc($req)) {
        $Num_TypePlu_sel = $row["Num_TypePlu"];
        $TypePlu = $row["TypePlu"];
     
  echo "<option value='$Num_TypePlu_sel'>$Num_TypePlu_sel | $TypePlu</option>";
}
echo "</select>";
echo "</td>";

echo "<td>Mot cle : <input type='text' name='search' placeholder='Ex : sel' autofocus >  </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>";
*/

// sous Menu 1

echo "<center><HR size='1' width='500'>";
echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
echo "<td>";
echo "<a href='ajout_plu.php?ordre=1'><b>Nouveau</b></a> <b> | </b>";
echo "<a href=''><b>Consulter</b></a> <b> | </b>";
echo "<a href=''><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'>";

// Corps de page

//echo "<br>" ;
echo "<table border='1' cellpadding='1' cellspacing='1' width='750' height='22'><tr>";
echo "<td width='30' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>N&#176;</div></td></span>";
echo "<td width='150' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Nom PLU</td></span>";
echo "<td width='100' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Cat. PLU</td></span>";
echo "<td width='100' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Type PLU</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Lettrage</td></span>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>DLC</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 plu;
$result = mysqli_query($link, $SelectEA);
while ($row = mysqli_fetch_assoc($result)) {
        $num_plu_intant = $row["Num_PLU"]; 
			  $nom_plu_intrant = $row["PLU"];
			  $nom_plu_intrant_compl = $row["Plu_complement"];  
			  $num_cate_intrant = $row["Num_CatPlu"];
			  $num_type_intrant = $row["Num_TypePlu"]; 
			  $attribu_plu = $row["attribu_plu"]; 
			  $dlc_plu_intrants = $row["dlc_plu"];

  

//$date_e=date("d/m/Y", strtotime($Date_Entre));
//echo  date("d/m/Y", strtotime($Date_Entre))." ".$date_e;

$sql = "SELECT CategoriePlu FROM categorieplu WHERE Num_CatPlu='$num_cate_intrant'";  
$req =   mysqli_query($link, $sql) ;
 while ($row = mysqli_fetch_assoc($req)) {
        $categorie_plu_intran = $row["CategoriePlu"];
    }

$sql = "SELECT TypePlu FROM type_plu WHERE Num_TypePlu='$num_type_intrant'";  
$req =   mysqli_query($link, $sql) ;
 while ($row = mysqli_fetch_assoc($req)) {
        $type_plu_intran = $row["TypePlu"];
    }
  
    echo "<tr><td width='30' height='20' valign='center'>
	<div align='center'><span class='t-text'>$num_plu_intant</div></td></span>";
	echo "<td width='150' height='20' valign='center'>";
	
		if (!empty($nom_plu_intrant_compl))	{
	echo "<div align='left'><span class='t-text'>$nom_plu_intrant - $nom_plu_intrant_compl</a></div></td></span>";
	} else {
		echo "<div align='left'><span class='t-text'>$nom_plu_intrant</a></div></td></span>";
	}
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'>$categorie_plu_intran</div></a></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><span class='t-text'>$type_plu_intran</div></a></td></span>";
	echo "<td width='50' height='20' valign='center'>	
	<div align='center'><span class='t-text'>$attribu_plu</div></td></span>";
	echo "<td width='50' height='20' valign='center'>	
	<div align='center'><span class='t-text'>$dlc_plu_intrants jours</div></td></span>";
	echo "<td width='20' height='20' valign='center'>
	<div align='center'><a class='' href='plu_detail.php?ordre=2&num_plu=$num_plu_intant'><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_plu.php?num_plu_intant=$num_plu_intant&conf=2'><img src='images/pblle.jpg' width='20' height='20'></a></div></td></tr>";

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

echo "<center>";

echo "<table border='1' cellpadding='0' cellspacing='0' width='500' height='22'><tr>";
echo "<td width='450' height='20' valign='center'><div align='center'><span class='t-text'>";
/*
echo "<br> total enrg = ".$total.
" | page = ".$PageCours.
" | Nbpages = ".$NbPage.
" | limite suivante= ".$limitesuivante.
" | limite encours= ".$limite.
" | limite precedente= ".$limiteprecedente.
" | Num_TypePlu=".$Num_TypePlu.
" | Num_CatPlu=".$Num_CatPlu.
" | Nombre=".$nombre.
" | search=".$search."<br>" ;	 
*/

if($limite != 0) {
    echo '<a href="'.$page.'?limite='.$limiteprecedente
        .'&Num_TypePlu='.$Num_TypePlu
				.'&Num_CatPlu='.$Num_CatPlu
    		.'&search='.$search
    		.'&ordre='.$num_ordre
    		.'&pages='.$NombrePages
    		.'">Page precedente  </a>';
    
}

for ($a = 1 ; $a <= 10 ; $a++) {
    $limitePage = $NombrePages + $limitePage;
	if (($limite/10)==$a) {
    echo '<a href="'.$page.'?limite='.$limitePage
        .'&Num_TypePlu='.$Num_TypePlu
				.'&Num_CatPlu='.$Num_CatPlu
    		.'&search='.$search
    		.'&ordre='.$num_ordre
    		.'&pages='.$NombrePages
    .'"><FONT color="red">'.$a.'</FONT></a> | ';
	} else {
	echo '<a href="'.$page.'?limite='.$limitePage
        .'&Num_TypePlu='.$Num_TypePlu
				.'&Num_CatPlu='.$Num_CatPlu
    		.'&search='.$search
    		.'&ordre='.$num_ordre
    		.'&pages='.$NombrePages
    .'">'.$a.'</a> | ';
	}
}

if($limitesuivante < $total) 
{
echo '<a href="'.$page.'?limite='.$limitesuivante
        .'&Num_TypePlu='.$Num_TypePlu
				.'&Num_CatPlu='.$Num_CatPlu
    		.'&search='.$search
    		.'&pages='.$NombrePages
    		.'&ordre='.$num_ordre
.'">Page Suivante </a>';
}
echo "</div></td></span>";
echo "<td>";
echo "<form id='form1' name='form1' method='get' action='#'>";
echo "<input type='hidden' name='ordre' value='$num_ordre'/>";
echo "<input type='hidden' name='limite' value='$limite'/>";
echo "<input type='hidden' name='Num_TypePlu' value='$Num_TypePlu'/>";
echo "<input type='hidden' name='Num_CatPlu' value='$Num_CatPlu'/>";
echo "<input type='hidden' name='search' value='$search'/>";

echo "<SELECT style=' width: 65px;' NAME='pages' onchange='submit()'>";
echo "<OPTION VALUE='0'>*-*</OPTION>\n";

echo "<OPTION VALUE='5'>5</OPTION>\n";
echo "<OPTION VALUE='10'>10</OPTION>\n";
echo "<OPTION VALUE='25'>25</OPTION>\n";
echo "<OPTION VALUE='50'>50</OPTION>\n";
echo "<OPTION VALUE='100'>100</OPTION>\n";
echo "</SELECT></form>";
echo "</td></tr></table>";

include 'menu/footer.php';
?>
