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

	$table = "tbl_service_banner";
	$uploadDir = "../Site/images/ads/";
	$masg = -1;
	$edit_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	$edit_title = "";
	$edit_link = "";
	$edit_image = "";

	if ($edit_id > 0) {
		$q_edit = mysql_query("SELECT * FROM tbl_service_banner 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'];
		}
	}

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

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

			if (in_array($ext, $allowed)) {
				$new_image = "srv_banner_" . time() . "." . $ext;
				if (move_uploaded_file($fileTmp, $uploadDir . $new_image)) {
					if ($edit_id > 0) {
						$res_old = mysql_query("SELECT image FROM tbl_service_banner WHERE id=$edit_id");
						if ($res_old && $row_old = mysql_fetch_array($res_old)) {
							if (!empty($row_old['image']) && file_exists($uploadDir . $row_old['image'])) {
								@unlink($uploadDir . $row_old['image']);
							}
						}
						$sql = "UPDATE tbl_service_banner SET image='$new_image', title='$title', link='$link' WHERE id=$edit_id";
					} else {
						$sql = "INSERT INTO tbl_service_banner (image, title, link, status) VALUES ('$new_image', '$title', '$link', 1)";
					}
					if (mysql_query($sql)) {
						header("location:servicebanner.php?p=servicebanner&msg=1");
						exit();
					} else {
						$masg = 0;
					}
				} else {
					$masg = 0;
				}
			} else {
				$masg = 2;
			}
		} else {
			if ($edit_id > 0) {
				$sql = "UPDATE tbl_service_banner SET title='$title', link='$link' WHERE id=$edit_id";
				if (mysql_query($sql)) {
					header("location:servicebanner.php?p=servicebanner&msg=1");
					exit();
				} else {
					$masg = 0;
				}
			} else {
				$masg = 0;
			}
		}
	}

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

	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 Service Banner' : 'Add New Service Banner'; ?></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="aservicebanner.php?p=aservicebanner<?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 Banner:</th>
				<td>
					<div style="background:#f0f0f0; border:1px solid #ccc; padding:10px; display:inline-block; border-radius:4px;">
						<img src="../Site/images/ads/<?php echo $edit_image; ?>" style="max-height:120px; max-width:500px; display:block;" />
					</div>
				</td>
				<td></td>
			</tr>
			<?php } ?>

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

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

			<tr>
				<th>Select Banner Image:</th>
				<td>
					<input type="file" name="file_banner" class="file_1" <?php echo ($edit_id == 0) ? 'required' : ''; ?> />
					<br /><small style="color:#666;">Recommended dimensions: 934px width x 220px height (JPG or PNG format).</small>
				</td>
				<td></td>
			</tr>

			<tr>
				<th>&nbsp;</th>
				<td valign="top">
					<input type="submit" name="btn_submit" value="Save Banner" class="form-submit" />
					<a href="servicebanner.php?p=servicebanner" 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'); ?>
