<?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 `pr_banners` SET `banner_status` = 'Inactive' WHERE `banner_id` = '".$_GET['banner_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['change_succ_msg'] = 'Banner de-activated successfully.';
	header("location: banner_list.php");
	exit;
}

//CODE FOR CHANGE STATUS TO ACTIVE
if(isset($_GET['action']) && $_GET['action'] == "active")
{
	$sql_update = "UPDATE `pr_banners` SET `banner_status` = 'Active' WHERE `banner_id` = '".$_GET['banner_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['change_succ_msg'] = 'Banner activated successfully.';
	header("location: banner_list.php");
	exit;
}

//CODE FOR DELETE BANNER
if(isset($_GET['action']) && $_GET['action'] == "delete")
{
	$sql_banner_image = "SELECT * FROM `pr_banners` WHERE `banner_id` = '".$_GET['banner_id']."'";
	$exe_banner_image = mysqli_query($connection,$sql_banner_image);
	while($arr_banner_image = mysqli_fetch_array($exe_banner_image))
	{
		if($arr_banner_image['banner_image']!="")
		{
			unlink("../uploads/banner/".$arr_banner_image['banner_image']);
		}
	}
	mysqli_query($connection,"DELETE FROM `pr_banners` WHERE `banner_id` = '".$_GET['banner_id']."'") or die(mysql_error());
	
	$_SESSION['del_succ_msg'] = 'Banner deleted successfully.';
	header("location: banner_list.php");
	exit;
}
?>

<?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['change_succ_msg'])) { ?>
			<div class="alert alert-success">                
			<b><?php echo $_SESSION['change_succ_msg'];?></b>
			</div> 
		<?php
		unset($_SESSION['change_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']);
		}
		?>
		<div class="row-fluid">
			<div class="span12">                    
				<div class="head clearfix">
					<div class="isw-picture"></div>
					<h1>Banners List</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="50%">Banner</th>
								<th width="15%">Status</th>
								<th width="15%">Action</th>
								<th width="10%">&nbsp;</th>
							</tr>
						</thead>
						<tbody>
						<?php
						$banner_counter = 1;
						$sql_record = "SELECT * FROM `pr_banners` ORDER BY `banner_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))
							{
								$banner_image = $fetch_record['banner_image'];
								$banner_status = $fetch_record['banner_status'];
								?>
								<tr>
									<td><?php echo $banner_counter;?></td>
									<td><img src="../uploads/banner/<?php echo $banner_image;?>" width="500" height="200" border="0" alt="" /></td>
									<td><?php if($banner_status == 'Active') { ?><a href="banner_list.php?banner_id=<?php echo $fetch_record['banner_id']?>&action=inactive" title="Click to inactive" onclick="return confirm('Are you sure you want to inactive this banner?');"><ul class="the-icons clearfix"><li><i class="isb-unlock"></i></li></ul></a><?php } else { ?><a href="banner_list.php?banner_id=<?php echo $fetch_record['banner_id']?>&action=active" title="Click to active" onclick="return confirm('Are you sure you want to active this banner?');"><ul class="the-icons clearfix"><li><i class="isb-lock"></i></li></ul></a><?php } ?></td>
									<td><a href="add_edit_banner.php?banner_id=<?php echo $fetch_record['banner_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="banner_list.php?banner_id=<?php echo $fetch_record['banner_id']?>&action=delete" title="Delete" onclick="return confirm('Are you sure you want to delete this banner?');" style="float:left; display:block; margin:0 5px 0 0;"><ul class="the-icons clearfix"><i class="isb-delete"></i></ul></a></td>
									<td>&nbsp;</td>
								</tr>
							<?php
							$banner_counter++;
							}
						}
						else
						{
						?>
						<tr>
							<td><font color="#FF0000">No banner 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"); ?>