<?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;
	$edit_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	$edit_title = "";
	$edit_link = "";
	$edit_image = "";
	$edit_status = 1;

	if ($edit_id > 0) {
		$q_edit = mysql_query("SELECT * FROM tbllogo WHERE id=$edit_id");
		if ($q_edit && $r_edit = mysql_fetch_array($q_edit)) {
			$edit_title = $r_edit['title'];
			$edit_link = isset($r_edit['link']) ? $r_edit['link'] : '';
			$edit_image = $r_edit['image'];
			$edit_status = isset($r_edit['status']) ? intval($r_edit['status']) : 1;
		}
	}

	if (isset($_POST['btn_submit'])) {
		$title = mysql_real_escape_string($_POST['txt_title']);
		$link = mysql_real_escape_string($_POST['txt_link']);
		$status = isset($_POST['cbo_status']) ? intval($_POST['cbo_status']) : 1;
		$edit_id = intval($_POST['txt_id']);

		if (isset($_FILES['file_logo']) && $_FILES['file_logo']['error'] == 0) {
			$fileName = $_FILES['file_logo']['name'];
			$fileTmp = $_FILES['file_logo']['tmp_name'];
			$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
			$allowed = array('jpg', 'jpeg', 'png', 'gif', 'webp');

			if (in_array($ext, $allowed)) {
				$new_image = "logo_" . time() . "." . $ext;
				if (move_uploaded_file($fileTmp, $uploadDir . $new_image)) {
					if ($edit_id > 0) {
						// Delete old image if not default logo.png
						$res_old = mysql_query("SELECT image FROM tbllogo WHERE id=$edit_id");
						if ($res_old && $row_old = mysql_fetch_array($res_old)) {
							if ($row_old['image'] != 'logo.png' && file_exists($uploadDir . $row_old['image'])) {
								@unlink($uploadDir . $row_old['image']);
							}
						}
						$sql = "UPDATE tbllogo SET image='$new_image', title='$title', link='$link', status=$status WHERE id=$edit_id";
					} else {
						$sql = "INSERT INTO tbllogo (image, title, link, status) VALUES ('$new_image', '$title', '$link', $status)";
					}
					if (mysql_query($sql)) {
						header("location:logo.php?p=logo&msg=0");
						exit();
					} else {
						$masg = 0; // Error
					}
				} else {
					$masg = 0; // Failed upload
				}
			} else {
				$masg = 2; // Invalid ext
			}
		} else {
			if ($edit_id > 0) {
				$sql = "UPDATE tbllogo SET title='$title', link='$link', status=$status WHERE id=$edit_id";
				if (mysql_query($sql)) {
					header("location:logo.php?p=logo&msg=0");
					exit();
				} else {
					$masg = 0;
				}
			} else {
				$masg = 0; // Requires image upload for new logo
			}
		}
	}

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

	include('templates/header.php'); 
	include('templates/menu.php'); 
?>

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

<div id="page-heading">
  <h1><?php echo ($edit_id > 0) ? 'Edit / Change Logo' : 'Upload New Logo'; ?></h1>
</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>
		<div id="content-table-inner">
		<?php
		if ($masg == 0) {
			echo '<div id="message-red">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="red-left">Operation failed. Please select an image file and 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 image format! Please upload JPG, PNG, GIF, or WEBP file.</td>
					<td class="yellow-right"><a class="close-yellow"><img src="images/table/icon_close_red.gif" alt="" /></a></td>
				</tr>
				</table>
			</div>';
		}
		?>

		<form action="alogo.php?p=logo<?php echo ($edit_id > 0) ? '&id='.$edit_id : ''; ?>" method="post" enctype="multipart/form-data" id="formID">
			<input type="hidden" name="txt_id" value="<?php echo $edit_id; ?>" />

			<table border="0" cellpadding="0" cellspacing="0" id="id-form">
			<?php if (!empty($edit_image)) { ?>
			<tr>
				<th valign="top">Current Logo:</th>
				<td>
					<div style="background:#f0f0f0; border:1px solid #ccc; padding:10px; display:inline-block; border-radius:4px;">
						<img src="../Site/images/<?php echo $edit_image; ?>" style="max-height:100px; max-width:400px; display:block;" />
					</div>
				</td>
				<td></td>
			</tr>
			<?php } ?>

			<tr>
				<th valign="top">Logo Title / Alt:</th>
				<td><input type="text" name="txt_title" class="inp-form" value="<?php echo htmlspecialchars($edit_title); ?>" placeholder="Enter logo title or description" /></td>
				<td></td>
			</tr>

			<tr>
				<th valign="top">Logo Link URL:</th>
				<td><input type="text" name="txt_link" class="inp-form" value="<?php echo htmlspecialchars($edit_link); ?>" placeholder="e.g. index.php or http://example.com" /></td>
				<td></td>
			</tr>

			<tr>
				<th valign="top">Status:</th>
				<td>
					<select name="cbo_status" class="styled-select" style="padding: 6px 10px; width: 200px; border: 1px solid #ccc; border-radius: 3px;">
						<option value="1" <?php echo ($edit_status == 1) ? 'selected' : ''; ?>>Active</option>
						<option value="0" <?php echo ($edit_status == 0) ? 'selected' : ''; ?>>Inactive</option>
					</select>
				</td>
				<td></td>
			</tr>

			<tr>
				<th>Select Logo Image:</th>
				<td>
					<input type="file" name="file_logo" class="file_1" <?php echo ($edit_id == 0) ? 'required' : ''; ?> />
					<br /><small style="color:#666;">Recommended dimensions: 1298px width x 520px height (Header Banner Image).</small>
				</td>
				<td></td>
			</tr>

			<tr>
				<th>&nbsp;</th>
				<td valign="top">
					<input type="submit" name="btn_submit" value="Save Logo" class="form-submit" />
					<a href="logo.php?p=logo" class="form-reset" style="display:inline-block; padding: 6px 14px; margin-left:10px; background:#999; color:#fff; text-decoration:none; border-radius:3px;">Cancel</a>
				</td>
				<td></td>
			</tr>
			</table>
		</form>

		<div class="clear"></div>
		</div>
	</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'); ?>
