<?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/plan_comptable.php?limite=0&ordre=1&Num_class=10&search=

*/


if (!isset($_GET['ordre'])) { 
		// Transmis en POST
		$num_ordre=$_POST['ordre'];
		if (!isset($_POST['pages'])) { $NombrePages=10;} else { $NombrePages=($_POST['pages']);}
		if (isset($_POST['limite'])) { $limite=$_POST['limite']; }  else {$limite=''; }
		if (isset($_POST['search'])) { $search=$_POST['search']; } else {$search=''; }
		if (isset($_POST['Num_class'])) { $Num_class=$_POST['Num_class']; } else {$Num_class=''; }
		$valForm="POST" ;
} else {
		// Transmis en GET
		$num_ordre=$_GET['ordre'];
		if (!isset($_GET['pages'])) { $NombrePages=10;} else { $NombrePages=($_GET['pages']);}
		if (isset($_GET['limite'])) { $limite=$_GET['limite']; }  else {$limite=''; }
		if (isset($_GET['search'])) { $search=$_GET['search']; } else {$search=''; }
		if (isset($_GET['Num_class'])) { $Num_class=$_GET['Num_class']; }  else {$Num_class=''; }
		$valForm="GET" ;
}



$limitePage = 0 ;

$v='select * FROM Plan_Comptable ';
$selectc = 'SELECT count(Code_Plan) FROM Plan_Comptable';

if (!empty($Num_class)) {
			$v.=" WHERE `Compte_Cpta` LIKE '".$Num_class."%'";
			$selectc.=" WHERE `Compte_Cpta` LIKE '".$Num_class."%'";
} else {

if (!empty($search)) {	
	    $v.=" WHERE LCASE (Nom_Compte_Cpta) LIKE LCASE ('%".$search."%')";
 	    $selectc.=" WHERE LCASE (Nom_Compte_Cpta) LIKE LCASE ('%".$search."%')";
 	    $v.=" AND IdNumClient='".$IdNumClient."'";
			$selectc.=" AND IdNumClient='".$IdNumClient."'";

 } else { 
      
      $v.=" WHERE `IdNumClient`='".$IdNumClient."'";
			$selectc.=" WHERE `IdNumClient`='".$IdNumClient."'";
     }
}

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

$SelectEA = $v ;

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);
$nombre = $NombrePages ;
$limitePage = 0 ;
$PageC = ($limite/$NombrePages) ;

//
// test donneee
echo "<center><HR size='2' width='500'>";
echo "v : ".$v."<br> SelectEA : ".$SelectEA;
echo "<HR size='2' width='500'>";
echo " total : $NbPage | $total | $semaine |  $search <br></center>" ;
//
echo "<div class='titre-text'><center>Plan Comptable</div></center><br>" ;

// Filtres 

echo "<form id='form1' name='form1' method='get' action=''>";
echo "<input type='hidden' name='limite' value='0'/>";
echo "<input type='hidden' name='ordre' value='$num_ordre'/>";
Echo "<table cellspacing=0 cellpadding=0 align=center border=0>";
Echo "<tr><td colspan=2><span class='t-text'><center>FILTRES<br><HR size='1' width='500'></center></td></tr>";
echo "<tr class='text'>";
echo "<td> Par Classe : <select style=' width: 60px;' name='Num_class' id='Num_class'>";
echo "<option value='0'>Toutes</option>";

$select = "select * FROM Plan_Comptable where Niveau=3";
$select.=" AND IdNumClient='".$IdNumClient."'";
$result = mysqli_query($link, $select);
while ($row = mysqli_fetch_assoc($result)) {
  $Id_pcg = $row["Id_pcg"];
  $Numero_Classe = $row["Numero_Classe"];
  $Nom_Compte_Cpta = $row["Nom_Compte_Cpta"];
  $Niveau = $row["Niveau"];	
  $Compte_Cpta = $row["Compte_Cpta"];	
  echo "<option value='$Numero_Classe'>$Numero_Classe | $Nom_Compte_Cpta</option>";
}

echo "</select>";
echo "</td>";
echo "<td><center>Mot cle : <input type='text' name='search' placeholder='Ex : sel' autofocus size='25'><input type='submit' value='ok'></center></td>";
Echo "</tr>";
Echo "</span>";
Echo "</table>";
echo "</form></center>";
/*
echo "total : ".$total."| limite  ".$limite."<br> limite suivante ".$limitesuivante."| limite precedente".$limiteprecedente ."<br>";
echo "selectselect ".$select."<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_bases_clients.php'><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 "<a href=''><b></b></a><b></b>";
echo "</td></tr>";
echo "</tbody>";
echo "</table><br>";

// Corps de page

//echo "<br>" ;
echo "<table border='1' cellpadding='1' cellspacing='1' width='600' height='22'><tr>";
echo "<td width='50' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>N&#176; Compte</div></td></span>";
echo "<td width='100' height='20' valign='center' bgcolor='#CCCCCC'>
	<div align='center'><span class='t-text'>Nom Compte</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>";



$req = mysqli_query($link, $SelectEA);
while ($row = mysqli_fetch_assoc($req)) {
	
	$Compte_Cpta = $row["Compte_Cpta"];  
	$Nom_Compte_Cpta = $row["Nom_Compte_Cpta"];
  

    echo "<tr><td width='50' height='20' valign='center'>
	<div align='center'><span class='t-text'>$Compte_Cpta</div></td></span>";
	echo "<td width='100' height='20' valign='center'>
	<div align='center'><a class='' href='consult_plu_intrants.php?Num_entre=$Num_Client_Base'><span class='t-text'>$Nom_Compte_Cpta</div></a></td></span>";
	echo "<td width='32' height='20' valign='center'>
	<div align='center'><a class='' href='modif_bases_clients.php?Num_entre=$Num_Client_Base'><img src='images/modifie.png' width='20' height='20'></a></div></td>";
	echo "<td width='32' height='20' valign='center'>
	<div align='center'><a class='' href='traitement_delette_clients_base.php?Num_entre=$Num_Client_Base&conf=2'><img src='images/pblle.jpg' width='20' height='20'></a></div></td></tr>";

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

echo "<center>";

// bas de page Navig

echo "<table border='1' cellpadding='0' cellspacing='0' width='600' height='22'><tr>";
echo "<td width='120' height='20' valign='center'><div align='center'><span class='t-text'>";

/*
echo "total enrg = ".$total.
" | Nbpages = ".$NbPage.
" | limite suivante= ".$limitesuivante.
" | limite encours= ".$limite.
" | limite precedente= ".$limiteprecedente.
" <br> Anne =".$SelAnne_y.
" | Semaine =".$SelSem_w.
" | Num_CatPlu=".$Num_CatPlu.
" | Num_Client=".$Num_Client_sel.
" | Nombre=".$NombrePages."<br>" ;	 
*/
if($limite != 0) {
    echo '<a href="'.$page.'?limite='.$limiteprecedente
   .'&search='.$search
    .'">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
    .'&pages='.$NombrePages
		.'&ordre='.$num_ordre
    .'&Num_class='.$Num_class
    .'&search='.$search
    .'"><FONT color="red">'.$pagenum.'</FONT></a> | ';
    } else {
      echo '<a href="'.$page.'?limite='.$limitePage
		.'&pages='.$NombrePages
		.'&ordre='.$num_ordre
    .'&Num_class='.$Num_class
    .'&search='.$search
    .'">'.$pagenuma.'</a> | ';
  	}	
	}	
}

if($limitesuivante < $total) 
{
echo '<a href="'.$page.'?limite='.$limitesuivante
		.'&pages='.$NombrePages
		.'&ordre='.$num_ordre
    .'&Num_class='.$Num_class
    .'&search='.$search
		.'">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_class' value='$Num_class'/>";
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';
?>