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

//CODE FOR ADD ANNOUNCEMENT
if(isset($_POST['add']))
{
    $expdate = explode("/",$_POST['ann_date']);
    $ann_date = $expdate[2]."-".$expdate[0]."-".$expdate[1];
    
	$sql_insert = "INSERT INTO `pr_announcements` SET `announcement` = '".addslashes($_POST['announcement'])."',`ann_date` = '".$ann_date."',`ann_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Announcement added successfully.';
	header("location: ann_list.php");
	exit;
}

//CODE FOR UPDATE ANNOUNCEMENT
if(isset($_POST['edit']))
{
	$expdate = explode("/",$_POST['ann_date']);
    $ann_date = $expdate[2]."-".$expdate[0]."-".$expdate[1];
	
	$sql_update = "UPDATE `pr_announcements` SET `announcement` = '".addslashes($_POST['announcement'])."',`ann_date` = '".$ann_date."' WHERE `ann_id` = '".$_REQUEST['ann_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Announcement updated successfully.';
	header("location: ann_list.php");
	exit;
}

//FETCH DATA FROM DATABASE
if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") 
{
	$fetch_record = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `pr_announcements` WHERE `ann_id` = '".$_REQUEST['ann_id']."'"));
	$announcement = stripslashes($fetch_record['announcement']);
	$exp_ann_date = explode("-",$fetch_record['ann_date']);
	$ann_date = $exp_ann_date[1]."/".$exp_ann_date[2]."/".$exp_ann_date[0];
}
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('announcement').value == '')
	{
		alert("Please enter announcement");
		document.getElementById('announcement').focus();
		return false;
	}
	
	if(document.getElementById('datepicker').value == '')
	{
		alert("Please select date");
		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 Announcement</h1>
					<?php } else { ?>
					<h1>Add Announcement</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="ann_id" id="ann_id" value="<?php echo $fetch_record['ann_id'];?>" />
				<div class="block-fluid">
				    <div class="row-form clearfix">
						<div class="span3">Announcement <font color="#FF0000">*</font></div>
						<div class="span9"><textarea name="announcement" id="announcement"><?php echo $announcement;?></textarea></div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Date <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="ann_date" id="datepicker" value="<?php echo $ann_date;?>" /></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='ann_list.php'">Back To Announcements List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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