<?php
    session_start();    
    if(empty($_SESSION['p0B_c0ff33'])) header('location:login.php?goto=index');
	include "../db_function.php";  

	$table = "tbllogo";
	$uploadDir = "../Site/images/";
	$masg = -1;

	if (isset($_GET['msg'])) {
		$masg = intval($_GET['msg']);
	}

	// Handle Logo Delete Action
	if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) {
		$del_id = intval($_GET['id']);
		$res = mysql_query("SELECT image FROM tbllogo WHERE id=$del_id");
		if ($res && $r = mysql_fetch_array($res)) {
			if ($r['image'] != 'logo.png' && file_exists($uploadDir . $r['image'])) {
				@unlink($uploadDir . $r['image']);
			}
		}
		mysql_query("DELETE FROM tbllogo WHERE id=$del_id");
		header("location:logo.php?p=logo&msg=3");
		exit();
	}

	if(empty ($_GET['p'])){
		$_GET['p']="logo";
	}

	include('templates/header.php'); 
?>
<body> 
<!-- Start: page-top-outer -->
<?php 
	include('templates/menu.php'); 
?>

<!-- start content-outer -->
<div id="content-outer">
<!-- start content -->
<div id="content">

	<!--  start page-heading -->
	<div id="page-heading">
		<h1>Logo Management Module</h1>
	</div>
	<div class="clear"></div>

	<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
	<tr>
		<th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
		<th class="topleft"></th>
		<td id="tbl-border-top">&nbsp;</td>
		<th class="topright"></th>
		<th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
	</tr>
	<tr>
		<td id="tbl-border-left"></td>
		<td>
		<!--  start content-table-inner -->
		<div id="content-table-inner">
		<?php
		if ($masg == 0) {
			echo '<div id="message-green">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="green-left">Logo updated successfully!</td>
					<td class="green-right"><a class="close-green"><img src="images/table/icon_close_green.gif" alt="" /></a></td>
				</tr>
				</table>
			</div>';
		} else if ($masg == 1) {
			echo '<div id="message-red">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="red-left">Logo update failed. Please try again!</td>
					<td class="red-right"><a class="close-red"><img src="images/table/icon_close_red.gif" alt="" /></a></td>
				</tr>
				</table>
			</div>';
		} else if ($masg == 2) {
			echo '<div id="message-yellow">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="yellow-left">Invalid file format. Please upload JPG, PNG, GIF, or WEBP image.</td>
					<td class="yellow-right"><a class="close-yellow"><img src="images/table/icon_close_red.gif" alt="" /></a></td>
				</tr>
				</table>
			</div>';
		} else if ($masg == 3) {
			echo '<div id="message-green">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="green-left">Logo deleted successfully. Default logo restored.</td>
					<td class="green-right"><a class="close-green"><img src="images/table/icon_close_green.gif" alt="" /></a></td>
				</tr>
				</table>
			</div>';
		}
		?>

		<div id="table-content">
			<div style="margin-bottom: 20px;">
				<a href="alogo.php?p=logo" class="form-submit" style="display:inline-block; padding: 6px 16px; background:#00adef; color:#fff; text-decoration:none; font-weight:bold; border-radius:4px;">+ Upload New Header Banner</a>
			</div>

			<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
				<tr>
					<th class="table-header-repeat line-left"><a href="#">ID</a></th>
					<th class="table-header-repeat line-left"><a href="#">Banner Preview</a></th>
					<th class="table-header-repeat line-left"><a href="#">Title / Description</a></th>
					<th class="table-header-repeat line-left"><a href="#">Link URL</a></th>
					<th class="table-header-repeat line-left"><a href="#">Status</a></th>
					<th class="table-header-options line-left"><a href="#">Options</a></th>
				</tr>
				<?php 
				$all_logos = mysql_query("SELECT * FROM tbllogo ORDER BY id DESC");
				if ($all_logos && mysql_num_rows($all_logos) > 0) {
					while ($r = mysql_fetch_array($all_logos)) {
						$link_display = !empty($r['link']) ? htmlspecialchars($r['link']) : '-';
						$status_display = (isset($r['status']) && $r['status'] == 1) ? '<span style="color:#27ae60; font-weight:bold;">Active</span>' : '<span style="color:#e74c3c; font-weight:bold;">Inactive</span>';
						echo '<tr>
							<td>'.$r['id'].'</td>
							<td style="padding:10px; background:#f9f9f9; text-align:center;">
								<img src="../Site/images/'.$r['image'].'" style="max-height:80px; max-width:350px; border:1px solid #ccc; padding:2px; background:#fff; border-radius:3px;" />
							</td>
							<td>'.htmlspecialchars($r['title']).'</td>
							<td>'.$link_display.'</td>
							<td align="center">'.$status_display.'</td>
							<td class="options-width">
								<a href="alogo.php?p=logo&id='.$r['id'].'" title="Edit Banner" class="icon-1 info-tooltip"></a>
								<a href="logo.php?p=logo&action=delete&id='.$r['id'].'" title="Delete Banner" onclick="return confirm(\'Are you sure you want to delete this header banner?\')" class="icon-2 info-tooltip"></a>
							</td>
						</tr>';
					}
				} else {
					echo '<tr><td colspan="6" style="text-align:center; padding:20px;">No custom logo banner uploaded yet. Default logo (logo.png) is active.</td></tr>';
				}
				?>
			</table>
		</div>
		<div class="clear"></div>
		</div>
		<!--  end content-table-inner -->
		</td>
		<td id="tbl-border-right"></td>
	</tr>
	<tr>
		<th class="sized bottomleft"></th>
		<td id="tbl-border-bottom">&nbsp;</td>
		<th class="sized bottomright"></th>
	</tr>
	</table>
	<div class="clear">&nbsp;</div>
</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer -->

<div class="clear">&nbsp;</div>
<?php include('templates/footer.php'); ?>
