<?php 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	    
	utility::addslashesOnAllInput(array("query_string","admin_id"));
    $job_id = $_REQUEST['job_id'];	
    $admin_id = implode(",",$_REQUEST['admin_id']);
	$appointment_text = $_REQUEST['appointment_text'];
	$source	 = $_REQUEST['source'];
	$appointment_date	 = utility::setFormatedDate($_REQUEST['appointment_date']);
	$appointment_time = $_REQUEST['appointment_time'];
	$appointment_time_to = $_REQUEST['appointment_time_to'];
	$is_full_day = (isset($_REQUEST['is_full_day'])) ? 1 : 0;
	$appointment_location= $_REQUEST['appointment_location'];
	$created_on = date("Y-m-d");
		
    $sql= "INSERT INTO appointment (job_id,admin_id,appointment_text,source,appointment_date,appointment_time,appointment_time_to,is_full_day,appointment_location,updated_by,created_on) VALUES('$job_id','$admin_id','$appointment_text','$source','$appointment_date','$appointment_time','$appointment_time_to','$is_full_day','$appointment_location','".$_SESSION['user_id']."','$created_on')";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = "&".$query_string;
	
	$_SESSION['message']	= ucfirst("Appointment added sucessfully");
	$redirect = "index?page=view_calendar_for_appointment$query_string";
	header("Location: $redirect");
	exit();
}
$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
$submit_url = $file_name."?page=add_appointment_for_calender&action=insert";
$app_date = $_REQUEST["app_date"];
$source = $_REQUEST["source"];
?>
<div style="clear:both;"></div>
<!----------------------- Message ------------------------------->
	<? if(isset($_SESSION['message'])){ ?>
	<section class="content-header">
	  <div class="alert alert-info alert-dismissable" style="margin-bottom:10px;">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
		<? echo $_SESSION['message'];
			unset($_SESSION['message']);
		?>
	  </div>
	</section>
	<div style="clear:both;"></div>
	<? } ?>
<!----------------------- End Message ------------------------------->
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="panel box box-primary" id="booke_appointment_area">
	<div class="box-header with-border">
		<form action="<?php echo $submit_url;?>" method="post" name="add_form" id="add_form">
			<div class="col-sm-3">
				<label for="Appointment Text" class="control-label">Description</label>
				<textarea class="form-control" rows="4" id="appointment_text" name="appointment_text" required><?php echo stripslashes($appointment_text);?></textarea>
				
				<label for="Job" class="control-label">Job</label>
				<? $gsi->getEnqQuoteJobList("job_id",$job_id,"100%",false,"form-control select2","required",$source); ?>
			</div>
			<div class="col-sm-3">
				<label for="Consultant" class="control-label">Consultant</label>
				<div id="admin_id_div">
				<? 
					$admin_ids = "";
					$sql_for_admin = "SELECT * FROM job_admin_mapping WHERE job_id = $job_id";
					$rs_for_admin = mysqli_query($conn,$sql_for_admin);
					if(mysqli_num_rows($rs_for_admin) > 0){
						while($row_for_admin = mysqli_fetch_assoc($rs_for_admin)){
							$admin_ids .= $row_for_admin["admin_id"].",";
						}
						$admin_ids = substr($admin_ids,0,-1);
					}
					Admin::getAdminList("admin_id",$admin_id,"100%",true,"form-control select_admin_id","required",$admin_ids); 
				?>
				</div>
				
				<label for="Appointment" class="control-label">Date</label>
				<input type="text" class="form-control datepicker" id="appointment_date" name="appointment_date" value="<?php echo $app_date;?>" required>
			</div>
			<div class="col-sm-3">
				<label for="Appointment Time" class="control-label">Time From</label>
				<div class="bootstrap-timepicker">
					<!--<input type="text" class="form-control timepicker" id="appointment_time" name="appointment_time" value="<?php //echo $appointment_time;?>">-->
					<? utility::getTimeList("appointment_time", $appointment_time, "Time From", "100%");?>
				</div>
				
				<label for="Appointment Time" class="control-label">Time To</label>
				<div class="bootstrap-timepicker">
					<!--<input type="text" class="form-control timepicker" id="appointment_time_to" name="appointment_time_to" value="<?php //echo $appointment_time_to;?>">-->
					<? utility::getTimeList("appointment_time_to", $appointment_time_to, "Time To", "100%");?>
				</div>
				
				<label for="Full Day" class="control-label">Full Day</label><br>
				<input class="flat-red" type="checkbox" id="is_full_day" name="is_full_day" <? echo ($is_full_day) ? "checked" : ""; ?>>
			</div>
			<div class="col-sm-3">
				<label for="Location" class="control-label">Location</label>
				<input type="text" class="form-control" id="appointment_location" name="appointment_location" value="<?php echo $appointment_location;?>">
					
				<label for="Full Day" class="control-label">&nbsp;</label><br>
				<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
				<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
				<input type="hidden" id="source" name="source" value="<?php echo $source;?>">
				<button type="submit" name="add_master_data" id="submit_form" class="btn btn-primary">Submit</button>
			</div>
		</form>
	</div>
</div>
</div>
</div>
</section>
<style>
.wrapper{
	background-color:inherit !important;
}
.content-wrapper{
	min-height: 380px  !important;
}
</style>
<script type="text/javascript">
$(function(){
	$("#add_form").on("click",".select_admin_id",function(){
		if($("#job_id").val() == ""){
			alert("First select Enquiry/Quote/Job");
			$(this).val("");
			return false;
		}
	});
	$("#job_id").change(function(){	
		var job_id = $(this).val();
		if(job_id != ""){
		var form_data   = {'job_id':job_id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/appointment/index.php?page=ajax_actions&action=get_admin_by_job",
				data: form_data,
				success: function(result){  
					if(result){
						$("#admin_id_div").html(result);
					}else{
						alert("No consultant assign to this job. First assign consultant.");
						$("#admin_id_div").html(result);
					}
				}
			});
		}
	});
	$("#submit_form").click(function(){	
		if($("#span").text() == "No consultant assign to this job. First assign consultant."){
			alert("First select Consultant");
			return false;
		}
	});
});
</script>