<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "campaign_management";
$id	 = "";
$vat = "";
$query_string = "";
$page_name      = "Add Campaign";
$from = $_REQUEST['from'];
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
    $campaign_name			= $_REQUEST['campaign_name'];
	$mail_subject			= $_REQUEST['mail_subject'];
	$mail_body           	 = $_REQUEST['mail_body'];
	$start_date 			= utility::setFormatedDate($_REQUEST['start_date']);
	$end_date				 = utility::setFormatedDate($_REQUEST['end_date']);
	$is_archive 			= (isset($_REQUEST['is_archive']) && $_REQUEST['is_archive'] !="") ? 1 : 0;
	
	$created_by		= $_SESSION['user_id'];
	$created_on		= date("Y-m-d H:i:s");	
	$updated_by		= $_SESSION['user_id'];

	$sql = "campaign_name = '".$campaign_name."', mail_body = '".$mail_body."',mail_subject = '".$mail_subject."', start_date = '".$start_date."', end_date = '".$end_date."', is_archive = '".$is_archive."', created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";
	$sql= "INSERT INTO $table_name SET ".$sql;
	mysqli_query($conn,$sql);
	$campaign_id = mysqli_insert_id($conn);
	
	
	$_SESSION['info_message'] = "Campaign added sucessfully";
	header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];
	
	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Campaign";
	$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);
	$row_edit = utility::validate_crypt_data($row_edit);

    $campaign_name			= $row_edit['campaign_name'];
	$mail_body		    	= $row_edit['mail_body'];
	$mail_subject			= $row_edit['mail_subject'];
	$start_date 			= $row_edit['start_date'];
	$end_date 				= $row_edit['end_date'];
	$is_archive             = $row_edit['is_archive'];
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string","admin_id","sector"));
	$id = $_REQUEST['id'];
	$campaign_name			= $_REQUEST['campaign_name'];
	$mail_body           	= $_REQUEST['mail_body'];
	$mail_subject           = $_REQUEST['mail_subject'];
	$start_date				= utility::setFormatedDate($_REQUEST['start_date']);
	$end_date 				= utility::setFormatedDate($_REQUEST['end_date']);
	$is_archive				= (isset($_REQUEST['is_archive']) && $_REQUEST['is_archive'] !="") ? 1 : 0;
	
	$updated_by		    	= $_SESSION['user_id'];
	
	$sql = "campaign_name = '".$campaign_name."',mail_body = '".$mail_body."',mail_subject = '".$mail_subject."',start_date = '".$start_date."',end_date = '".$end_date."',is_archive = '".$is_archive."',updated_by = '".$updated_by."'";
	$sql= "UPDATE $table_name SET ".$sql." WHERE id = '".$id."'";
    mysqli_query($conn,$sql);
	
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	
  	$query_string = str_ireplace("page=add", "page=view", $query_string);
	$query_string = "&".$query_string;
	
	$campaign_id = urlencode(utility::app_crypt($id,'e'));
	
	$_SESSION['info_message'] = "Campaign updated sucessfully";
	if(isset($from) && $from == "view_campaign_management_details"){
		header("Location: index?page=view_campaign_management_details&from=view_campaign&campaign_id=$campaign_id&id=$id");
		exit();
	}
	
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<style>
em{color:red;font-size: 20px;position: absolute;}
</style>
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1>
					<?=$page_name;?>
				</h1>
			</div>
			<div class="col-sm-6">
			<?
			$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
			if(isset($from) && $from == "view_campaign_management_details"){
				$query_string = str_ireplace("page=add", "page=view_campaign_management_details", $query_string);
			}else {
				$query_string = str_ireplace("page=add", "page=view", $query_string);
			}
				
			?>
			<button type="button" id="back" name="back" value="back"  style="float:right;margin-right:5px;margin-bottom:8px;" class="btn btn-success" onclick="window.location.href = 'index?<?=$query_string;?>'">Back</button>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="row">
		<div class="col-12">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
			  </div>
			  <? } ?>
			<!----------------------- End Message ------------------------------->
			<!-- general form elements -->
              <div class="card">
                <form class="" method="post" name="form1" id="form1" action="index?page=add&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
					  <div class="row">
						  <div class="col-lg-12 col-sm-12">
							<div class="form-group"> 
								<label class="control-label" for="Campaign Name">Campaign Name<em>*</em></label> 
								<div class=""> 
									<input type="text" id="campaign_name" name="campaign_name" class="form-control" value="<?php echo $campaign_name;?>" placeholder="Campaign Name" required>
								</div> 
							</div>
							<div class="form-group">
								<label class="control-label" for="Start Date">Start Date</label>
								<div>
								  <input type="text" class="form-control datepicker" id="start_date"
										name="start_date" value="<?php echo (isset($start_date) && $start_date != "") ? utility::getFormatedDate($start_date) : ""; ?>" placeholder="DD-MM-YYYY" autocomplete="off">
								</div>
							</div>
							<div class="form-group">
								<label class="control-label" for="End Date">End Date</label>
								<div>
								  <input type="text" class="form-control datepicker" id="end_date"
										name="end_date" value="<?php echo (isset($end_date) && $end_date != "") ? utility::getFormatedDate($end_date) : ""; ?>" placeholder="DD-MM-YYYY" autocomplete="off">
								</div>
							</div>
							<div class="form-group">
								<label for="Mail subject" class="control-label">Mail Subject</label>
								<div class="">
								   <input type="text" id="mail_subject" name="mail_subject" value="<?php echo $mail_subject;?>" class="form-control" placeholder="Mail subject">
								 </div>
							</div>
							<div class="form-group"> 
								<label class="control-label" for="Mail body">Mail Body</label> 
								<div class=""> 
									<textarea class="form-control" rows="5" id="mail_body" name="mail_body"><?php echo $mail_body;?></textarea>
								</div> 
							</div>
						 </div>
					  </div><!-- /.row -->
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="from" name="from" value="<?php echo $from;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">

$(function(){
	$('#form1').submit(function(){
		//$(this).addClass('disabled');
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
	});	
	CKEDITOR.replace('mail_body', {
            height: 300
        });
    
});
</script>