<?php 
	include("includes/config.php");
	include("includes/func.php");
	
	if (($_SESSION['id'] == ""))
	{
		redirect("login");
	}
	
	include("templates/header.php");

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function get_category($cat_id)
{
	$Sql_Cat	=	"SELECT * FROM `category` where id = '$cat_id'";
	$Exe_Sql_Query	=	mysql_query($Sql_Cat);
	$Cat_Row	=	mysql_fetch_array($Exe_Sql_Query);
	return ($Cat_Row['desc']);	
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
$_SESSION['PartNumber'] = $_POST['PartNumber'];

$partSearch = $_SESSION['PartNumber'];

// sets the amount of rows to display
$rowsPerPage = 25;

// sets the page number. ( 1 because by default first page is shown )
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
	if($pageNum == "0" || $pageNum == "" || !is_numeric($pageNum)){ $pageNum = 1; }
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

// creates a mysql query that selects all rows from the table
$queryTotalRows = mysql_query("SELECT * FROM `product` where `delete` != '1'") or die ('Error: '.mysql_error ());

$num_rows = mysql_num_rows($queryTotalRows);

// how many pages we have
$maxPage = ceil($num_rows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];


// creating previous and next link plus the link to go straight to the first and last page
if ($pageNum > 1) {
  $page  = $pageNum - 1;
  $prev  = "<a href=\"$self?page=$page\" title='Previous'>&lt;Previous</a>";
   $first = "<a href=\"$self?page=1\" title='First'>&lt;&lt;</a> ";
} else {
  $prev  = "&lt;Previous"; // we're on page one, don't print previous link
  $first = "&lt;&lt;&nbsp;&nbsp;"; // nor the first page link
}

if ($pageNum < $maxPage){
  $page = $pageNum + 1;
  $next = "<a href=\"$self?page=$page\" title='Next'>Next&gt;</a> ";
  $last = "<a href=\"$self?page=$maxPage\" title='Last'>&gt;&gt;</a> ";
} else {
  $next = "&nbsp;Next&gt;"; // we're on the last page, don't print next link
  $last = "&nbsp;&nbsp;&gt;&gt;"; // nor the last page link
}

/*id 	producttitle 	product 	catid 	currentstocklevel 	alertcount 	product_status_id 	delete*/
$sql = "SELECT * FROM `product` where `delete` != '1' ORDER BY `producttitle` ASC LIMIT $offset, $rowsPerPage";
$res = mysql_query($sql) or die ('Error: '.mysql_error ());
if (mysql_num_rows($res) > 0)
	{
		$i=1;
		while($row = mysql_fetch_array($res)){
			
			$User_Type	=	'';
			
			$UsersHTML .= "<tr class='odd'>";
			
			$UsersHTML .= "<td>$i</td>";
			$UsersHTML .= "<td>" . $row['producttitle'] . "</td>";
			$UsersHTML .= "<td>" . $row['product_code'] . "</td>";
			$UsersHTML .= "<td>" . get_category($row['catid']) . "</td>";			
			$UsersHTML .= "<td>{$row['currentstocklevel']}</td>";
			$UsersHTML .= "<td>{$row['alertcount']}</td>";
			$UsersHTML .= "<td align='center'>";
			
			$UsersHTML = $UsersHTML . "<a href='product_add_edit_delete.php?act=edit&uid=".$row['id']."'><img src='images/edit.png' alt='Edit' border='0'></a>&nbsp;";

			$UsersHTML = $UsersHTML .  "<a href='product_add_edit_delete.php?act=delete&uid=".$row['id']."' onclick='return confirm(\"Are you sure want to delete this product?\");'><img src='images/delete.png' alt='Delete' /></a>";
							  
			$UsersHTML = $UsersHTML .  "</td>";
			$UsersHTML .= "</tr>";
			$i ++;
		}
	}
else
	{
		$UsersHTML = "<tr align='center'><td colspan='7'>No Records found.</td></tr>";
		$TableSortHeader = '';
	}


// print the navigation link
if($UsersHTML != "") {
	$pagingTxt = $first . $prev ." $pageNum of $maxPage" . $next . $last ;
} else {
	$pagingTxt = '';
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
?>
<!-- Start main box area in which grid view display-->

<div id="mainPanel">
  <div id="datagrid">
    <h2><?php echo $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Product_Management'); ?></h2>
    <a href="product_add_edit_delete.php" class="form_button"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Add_New_Product'); ?></a>
    <div style="margin-bottom:5px;"></div>
    
    <br clear="all" />
    
    <div style="text-align:left; padding-bottom:5px; color:green;">
    	<?php
        if ($_SESSION['msg'] != '') { 
			    print $_SESSION['msg'];
                $_SESSION['msg'] = '';
        }
		?>
    </div>
    <?php
    
      $Header	=	'<table id="tabling1" width="100%" border="0" cellspacing="0" cellpadding="0">';
      $Header	= 	$Header . '<tr class="header">';
      $Header	= 	$Header . '<td class="col1" width="5%"><span class="blue">#</span></td>';
      $Header	= 	$Header . '<td class="col1" width="250px"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Title').'</span></td>';
      $Header	= 	$Header . '<td class="col1" width="20%"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Product').'</span></td>';
	  $Header	= 	$Header . '<td class="col1" width="15%"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Category').'</span></td>';
      $Header	= 	$Header . '<td class="col1" width="18%"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Current_Stock_Level').'</span></td>';
	  $Header	= 	$Header . '<td class="col2"><span class="blue">encomendado</span></td>  '; 
      $Header	= 	$Header . '<td class="col1" width="12%"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Alert_Count').'</span></td>';
      $Header	= 	$Header . '<td class="col1" style="text-align:center"><span class="blue">'.$Get_XML_Responce->readXmlArrtibut('xml_product_managment.xml', 'Action').'</span></td>';
              
      $Header	= 	$Header . '</tr>';
	  
	  $Footer	=	'</table>';
      
		
        	$Cat_Query	=	"SELECT * FROM `category` where `delete` = '0'";
			$Exe_Cat_Query	=	mysql_query($Cat_Query);
			while($Cat_Row	=	mysql_fetch_array($Exe_Cat_Query))
			{
				$Cat_No	=	$Cat_Row['id'];
				
				$Category_title	= '<h3 style="padding-left:10px">'.$Cat_Row['desc'].'</h3>';
				
				$Html	=	'';
				$k		=	1;
				
				$Product_Query	=	"SELECT * FROM `product` where `catid` = '{$Cat_No}' and `delete` = '0' order by producttitle";
				$Exe_Product_Query	=	mysql_query($Product_Query);
				while($Porduct_Row	=	mysql_fetch_array($Exe_Product_Query))
				{
					
			
					$Intransit			=	$Porduct_Row["in_transit"];
					if($Intransit < 0 || $Intransit == '')
					{
						$Intransit	=	0;
					}	
					
               $Html	=	$Html .'<tr class=odd>';
			
								$Html	=	$Html . '<td>'.$k.'</td>';
			$Html	=	$Html .  '<td>' . $Porduct_Row['producttitle'] . '</td>';
			$Html	=	$Html .  '<td>' . $Porduct_Row['product_code'] . '</td>';
			$Html	=	$Html .  '<td>' . get_category($Porduct_Row['catid']) . '</td>';			
			$Html	=	$Html .  "<td>{$Porduct_Row['currentstocklevel']}</td>";
			$Html	=	$Html .  "<td>$Intransit</td>";
			$Html	=	$Html .  "<td>{$Porduct_Row['alertcount']}</td>";
			$Html	=	$Html .  '<td align=center>';
			
			$Html	=	$Html .  "<a href='product_add_edit_delete.php?act=edit&uid=".$Porduct_Row['id']."'><img src='images/edit.png' alt='Edit' border='0'></a>&nbsp;";

			$Html	=	$Html .  "<a href='product_add_edit_delete.php?act=delete&uid=".$Porduct_Row['id']."' onclick='return confirm(\"Are you sure want to delete this product?\");'><img src='images/delete.png' alt='Delete' /></a>";
							  
			$Html	=	$Html .  '</td>';
			$Html	=	$Html .  '</tr>';
			
					$k++;
													
				}
				
				if($k > 1)
				{
					echo $Category_title;
					echo $Header;
					echo $Html;
					echo $Footer;	
				}
					
				echo "<br />";	
			}
		?>
  
        
  </div>
  <!-- Grid ends --> 
  
</div>
<!-- Maindata box ends -->
<?php include("templates/footer.php"); ?>