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

//CODE FOR ADD PINCODE
if(isset($_POST['add']))
{
	$sql_insert = "INSERT INTO `sc_pincodes` SET `pincode` = '".addslashes($_POST['pincode'])."',`pincode_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Pincode added successfully.';
	header("location: zip_list.php");
	exit;
}

//CODE FOR UPDATE PINCODE
if(isset($_POST['edit']))
{
	$sql_update = "UPDATE `sc_pincodes` SET `pincode` = '".addslashes($_POST['pincode'])."' WHERE `pincode_id` = '".$_REQUEST['pincode_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Pincode updated successfully.';
	header("location: zip_list.php");
	exit;
}

//FETCH DATA FROM DATABASE
if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") 
{
	$fetch_record = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `sc_pincodes` WHERE `pincode_id` = '".$_REQUEST['pincode_id']."'"));
	$pincode = stripslashes($fetch_record['pincode']);
}
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('pincode').value == '')
	{
		alert("Please enter pincode");
		document.getElementById('pincode').focus();
		return false;
	}
}
</script>

<?php require_once("left_panel.php");?>
<div class="content">
	<div class="workplace">
		<div class="row-fluid">
			<div class="span12">
				<div class="head clearfix">
					<div class="isw-documents"></div>
					<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
					<h1>Update Pincode</h1>
					<?php } else { ?>
					<h1>Add Pincode</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="pincode_id" id="pincode_id" value="<?php echo $fetch_record['pincode_id'];?>" />
				<div class="block-fluid">
					<div class="row-form clearfix">
						<div class="span3">Pincode <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="pincode" id="pincode" value="<?php echo $pincode;?>" /></div>
					</div>
					<div class="row-form clearfix">
						<div class="span3">&nbsp;</div>
						<div class="span9">
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
							<button class="btn" type="submit" name="edit">Save</button>
						<?php } else { ?>
							<button class="btn" type="submit" name="add">Save</button>
						<?php } ?>
							<button class="btn" type="button" name="back" onclick="window.location.href='zip_list.php'">Back To Pincodes List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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