<?php session_start();?>
<?php include("../includes/connection.php"); 
include("../includes/func.php");?>
<?php if (empty($_SESSION["admin"])) redirectS("index.php?e=1");

$pg = new paging;
$pg->setpages("SELECT count(*) FROM product_master");
if ($_GET["sort"]!='')
	$_SESSION["sortme"]=$_GET["sort"];

if ($_SESSION["sortme"]==1)
	$sort=" order by " . $_GET["sortby"] ." asc";
else
	$sort=" order by " .$_GET["sortby"] ." desc";
	
if ($_GET["sortby"]=='')
	$sort=" order by product_name asc";


$sql="SELECT PM.*,CM.category_name as category_name from product_master PM,category_master CM where PM.product_category_id=CM.category_id";

//if ($_SESSION["sortme"]!='')
	$sql.=$sort;

$sql .=$pg->getlimit();

if ($_GET["what"]!='')
{
	$sql="SELECT PM.*,CM.category_name as category_name from product_master PM,category_master CM where PM.product_category_id=CM.category_id and  " . $_GET["criteria"] . " like '%" . $_GET["what"] . "%'  ".$sort." ".$sortorder;

	$_SESSION["errorMessage"]="Filtered Results";
}
$cmd = mysql_query($sql);

//Single Delete
if (!empty($_GET["delid"])) {
	mysql_query("delete from product_master where product_id=" . $_GET["delid"]);
	redirectS($_SERVER["HTTP_REFERER"]);
}

?>
<?php include("includes/header.php"); ?>
<??>

<table width="98%" height="100" border="0" align="center" cellpadding="5" cellspacing="5">
	<tr>
		<td height="50"><span style="font-weight: bold">Product Management </span> [<a href="#" onClick="openWin('ProductPopup.php', 'width=750, height=630');" class="BlackAri12B">New Product</a>]&nbsp; [<a href="#search" class="BlackAri12B">Search</a>]
	      <hr size="1">  </td>
	</tr>
	
	<?php
	//if any records retrieved
	if (mysql_num_rows($cmd) != 0) {
	?>
	
	<tr>
		<td class="BlackAri12B" colspan=2><table width="100%" border="0" cellpadding="2" cellspacing="1" class="bgDarkGray">
            <tr class="bgLightGray">
                <td width="5%">&nbsp;</td>
				<td width="60%" class="BlackAri12B">Name &nbsp;<a href="product_master.php?<?if ($_SESSION["sortme"]==1 		) {?>sort=2 <?} else { ?>sort=1<?}?>&sortby=product_name"><img src="../images/icons/Button.gif" border=0></td>
                <td width="30%" class="BlackAri12B">Category &nbsp;<a href="product_master.php?<?if ($_SESSION["sortme"]==1 ) {?>sort=2 <?} else { ?>sort=1<?}?>&sortby=category_name"><img src="../images/icons/Button.gif" border=0></td>
				<td width="30%" class="BlackAri12B">No Of Rings &nbsp;<a href="product_master.php?<?if ($_SESSION["sortme"]==1 		) {?>sort=2 <?} else { ?>sort=1<?}?>&sortby=ring_count"><img src="../images/icons/Button.gif" border=0></td>
                <td width="10%" class="BlackAri12B">Status &nbsp;<a href="product_master.php?<?if ($_SESSION["sortme"]==1 		) {?>sort=2 <?} else { ?>sort=1<?}?>&sortby=product_status"><img src="../images/icons/Button.gif" border=0></td>
				
				
			</tr>
			<?php getErr("5"); ?>
            <tr>
                <td colspan="7" height="10" class="bgWhite"></td>
            </tr>
			<?php
			//loop thru records and display category listing
			while ($rs = mysql_fetch_array($cmd)) {
				if ($bgclass == "bgExtraLightGray")
					$bgclass = "bgWhite";
				else
					$bgclass = "bgExtraLightGray";
					
				
			?>
			
            <tr class="<?php echo($bgclass); ?>">
                <td><table border="0" align="center">
                    <tr>
                        <td>
						
							<a href="#" onClick="openWin('ProductPopup.php?product_id=<?php echo($rs["product_id"]); ?>', 'width=650, height=630');"><img src="../images/icons/view.gif" width="16" height="16" border="0" alt="Edit Record"></a>
				
					
						
						</td>
                        <td><a href="product_master.php?delid=<?php echo($rs["product_id"]); ?>" onClick="return window.confirm('Are you sure you want to delete this record.');"><img 
						src="../images/icons/delete.gif" width="16" height="16" border="0" alt="Delete Record"></a></td>
					 </tr>
                </table>
				<?if ($rs["merchant_image"]=='') {?>
					 
					 <?}else {?>
				<img src=../files/<?=$rs["merchant_image"];?> >
				<?}?>
				</td>
				<td class="BlackAri12" valign=top width="20%"><small>&nbsp;<?php echo($rs["product_name"]); ?><br>
				<font class=Blue><?php echo(GetRecortarTexto($rs["merchant_url"],40)); ?></font> 
				</td>
				<td class="BlackAri12" width="50%" valign=top>&nbsp;<small><?php echo($rs["category_name"]); ?> 
				</small></td>
				<td class="BlackAri12" valign=top width="20%"><small>&nbsp;
				
				<?if ($rs["ring_count"]!=0) { echo $rs["ring_count"]; } else {echo "NA";}?> 
				</td>
				<td align="center"><?php stsfx($rs["product_status"]); ?></td>
				
            </tr>
			<?php } ?>
            <?if ($_GET["what"]=='')
				{?>
					<tr class="bgGray">
					   <td colspan="9" align="center" class="BlackAri12">
							<?php $pg->showPaging("", "&ps=".$_GET["ps"]."&sort=".$_GET["sort"]."&sortorder=".$_GET["sortorder"]."&criteria=".$_GET["criteria"]."&what=".$_GET["what"]); ?>
					  </td>
					</tr>
			<?}?>
        </table></td>
	</tr>
	
	<?php } else { ?>
	<tr>
		<td align="center" class="err">No Product found.</td>
	</tr>
	<?php } ?>
	<tr>
		<td height="50" align="center">&nbsp;</td>
	</tr>
	 <tr class="bgLightGray"><form name=search action="" method=get>
     <td width="100%" colspan=7 class="BlackAri12B">Search Merchants <a name=search></a>:
	
<input type=text name=what size=15 value="<?=$_GET["what"];?>">
<select name=criteria>
<option value=product_name <?if ($_GET["criteria"]=='product_name') echo "selected";?>>Product Name</option>
<option value=product_description <?if ($_GET["criteria"]=='product_description') echo "selected";?>>Description </option>
<option value="CM.category_name" <?if ($_GET["criteria"]=='CM.category_name') echo "selected";?>>Category</option>
<option value="product_status" <?if ($_GET["criteria"]=='product_status') echo "selected";?>>Status(0,1)</option>

</select>
<input type=submit name=search value=Search>
</form>
</td></tr>


</table>



<?php include("includes/footer.php"); ?>
