<?php 
require_once("header.php");
require_once("session_check.php");

//CODE FOR CHANGE STATUS TO INACTIVE
if(isset($_GET['action']) && $_GET['action'] == "inactive")
{
	$sql_update = "UPDATE `sc_products` SET `product_status` = 'Inactive' WHERE `product_id` = '".$_GET['product_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['change_succ_msg'] = 'Product de-activated successfully.';
	header("location: product_list.php?category_id=".$_GET['category_id']);
	exit;
}

//CODE FOR CHANGE STATUS TO ACTIVE
if(isset($_GET['action']) && $_GET['action'] == "active")
{
	$sql_update = "UPDATE `sc_products` SET `product_status` = 'Active' WHERE `product_id` = '".$_GET['product_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['change_succ_msg'] = 'Product activated successfully.';
	header("location: product_list.php?category_id=".$_GET['category_id']);
	exit;
}

//CODE FOR DELETE PRODUCT
if(isset($_GET['action']) && $_GET['action'] == "delete")
{
    $sql_banner_image = "SELECT * FROM `sc_products` WHERE `product_id` = '".$_GET['product_id']."'";
	$exe_banner_image = mysqli_query($connection,$sql_banner_image);
	while($arr_banner_image = mysqli_fetch_array($exe_banner_image))
	{
		if($arr_banner_image['product_photo']!="")
		{
			unlink("../uploads/product/".$arr_banner_image['product_photo']);
		}
	}
	mysqli_query($connection,"DELETE FROM `sc_products` WHERE `product_id` = '".$_GET['product_id']."'");
	
	$_SESSION['del_succ_msg'] = 'Product deleted successfully.';
	header("location: product_list.php?category_id=".$_GET['category_id']);
	exit;
}
?>

<?php
$ferch_category = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `sc_category` WHERE `category_id` = '".$_GET['category_id']."'"));
?>

<?php require_once("left_panel.php");?>
<div class="content">
	<div class="workplace">
		<?php if(isset($_SESSION['add_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['add_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['add_succ_msg']);
		}
		?>
		
		<?php if(isset($_SESSION['edit_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['edit_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['edit_succ_msg']);
		}
		?>
		
		<?php if(isset($_SESSION['del_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['del_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['del_succ_msg']);
		}
		?>
		
		<?php if(isset($_SESSION['change_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['change_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['change_succ_msg']);
		}
		?>
		<div class="row-fluid">
			<div class="span12">                    
				<div style="text-align:right;"><button class="btn btn-inverse" type="button" name="back" onclick="window.location.href='add_edit_product.php?category_id=<?php echo $_GET['category_id']?>'">Add Product For <?php echo stripslashes($ferch_category['category_name']);?></button></div>
				<div class="head clearfix">
					<div class="isw-grid"></div>
					<h1>Products List For <?php echo stripslashes($ferch_category['category_name']);?></h1>
				</div>
				<div class="block-fluid table-sorting clearfix">
					<table cellpadding="0" cellspacing="0" width="100%" class="table" id="tSortable">
						<thead>
							<tr>
								<th width="10%">Serial No.</th>
								<th width="30%">Name & Price</th>
								<th width="30%">Photo</th>
								<th width="10%">Status</th>
								<th width="20%">Action</th>
							</tr>
						</thead>
						<tbody>
						<?php
						$product_counter = 1;
						$sql_record = "SELECT * FROM `sc_products` WHERE `category_id` = '".$_GET['category_id']."' ORDER BY `product_id` DESC";
						$exe_record = mysqli_query($connection,$sql_record);
						$num_record = mysqli_num_rows($exe_record);
						if($num_record>0)
						{
							while($fetch_record = mysqli_fetch_array($exe_record))
							{
								$product_name = stripslashes($fetch_record['product_name']);
								$product_price = stripslashes($fetch_record['product_price']);
								$product_photo = stripslashes($fetch_record['product_photo']);
								$product_status = $fetch_record['product_status'];
							?>
							<tr>
								<td><?php echo $product_counter;?></td>
								<td><?php echo $product_name;?><br><b>Rs.<?php echo $product_price;?></b></td>
								<td><img src="../uploads/product/<?php echo $product_photo;?>" alt="" height="150" width="150"></td>
								<td><?php if($product_status == 'Active') { ?><a href="product_list.php?product_id=<?php echo $fetch_record['product_id']?>&category_id=<?php echo $_GET['category_id']?>&action=inactive" title="Click to inactive" onclick="return confirm('Are you sure you want to inactive this product?');"><ul class="the-icons clearfix"><li><i class="isb-unlock"></i></li></ul></a><?php } else { ?><a href="product_list.php?product_id=<?php echo $fetch_record['product_id']?>&category_id=<?php echo $_GET['category_id']?>&action=active" title="Click to active" onclick="return confirm('Are you sure you want to active this product?');"><ul class="the-icons clearfix"><li><i class="isb-lock"></i></li></ul></a><?php } ?></td>
								<td><a href="add_edit_product.php?category_id=<?php echo $_GET['category_id'];?>&product_id=<?php echo $fetch_record['product_id'];?>&action=edit" title="Edit" style="float:left; display:block; margin:0 5px 0 0;"><ul class="the-icons clearfix"><i class="isb-edit"></i></ul></a><a href="product_list.php?product_id=<?php echo $fetch_record['product_id']?>&category_id=<?php echo $_GET['category_id']?>&action=delete" title="Delete" onclick="return confirm('Are you sure you want to delete this product?');" style="float:left; display:block; margin:0 5px 0 0;"><ul class="the-icons clearfix"><i class="isb-delete"></i></ul></a></td>
							</tr>
							<?php
							$product_counter++;
							}
						}
						else
						{
						?>
						<tr>
							<td align="center"><font color="#FF0000">No product found</font></td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
							<td>&nbsp;</td>
						</tr>
						<?php
						}
						?>
						</tbody>
					</table>
				</div>
			</div>                                
		</div>
	</div>
</div>

<?php require_once("footer.php"); ?>