<?php 
$button_value 	= "Submit";
$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
$submit_url = $file_name."?page=assign_helper_for_appointment&action=insert";


$type_of_appointment = 1;
$table_name ='appointment';

if(isset($_REQUEST['job_id'])){
	$job_id = $_REQUEST['job_id'];
	$next_page_condition.="&job_id=$job_id";
}

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']);
	$created_on = date("Y-m-d");
	
	$sql= "INSERT INTO $table_name (job_id,admin_id,updated_by,created_on) VALUES('$job_id','$admin_id','".$_SESSION['user_id']."','$created_on')";
	mysqli_query($conn,$sql);
	$appointment_id = mysqli_insert_id();		
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
	$query_string = "&".$query_string;
	
	//$_SESSION['message']	= ucfirst("Task added sucessfully");
	$redirect = "index?page=assign_helper_for_appointment$query_string";
	header("Location: $redirect");
	exit();
}
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
	
	$submit_url = $file_name."?page=assign_helper_for_appointment&action=update";
	$button_value 	= "Update";
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
		
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM appointment WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    $id= $row_edit['id'];
    $job_id= $row_edit['job_id'];
    $admin_id= explode(",",$row_edit['admin_id']);
	
}
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){
 
	utility::addslashesOnAllInput(array("query_string","admin_id"));
	//unset($_SESSION["update_not_done"]);
	$id= $_REQUEST['id'];
    $job_id= $_REQUEST['job_id'];
    $admin_id= implode(",",$_REQUEST['admin_id']);

    $sql= "UPDATE appointment SET job_id = '$job_id', admin_id = '$admin_id', updated_by = '".$_SESSION['user_id']."' WHERE id = $id";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	//$query_string = str_ireplace(array("page=add","action=edit"), array("page=list",""), $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("Task Assigned Updated sucessfully");
	$redirect = "index?page=assign_helper_for_appointment$query_string";
	header("Location: $redirect");
	exit();
	
}
?>
<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="Staff Member" class="control-label">Staff Member</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>
			</div>	
			
			<!--<div class="col-sm-3">
				<label for="Notes" class="control-label">Notes</label>
				<textarea class="form-control" rows="4" id="notes" name="notes"><?php //echo stripslashes($notes);?></textarea>
			</div>
			<div style="clear:both;margin-top:5px;"></div>-->
			<div class="col-sm-3">
				<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="job_id" name="job_id" value="<?php echo $job_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"><?=$button_value;?>
					<i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div"></i>
				</button>
			</div>
		</form>
	</div>
</div>
</div>
</div>
</section>
<style>
label {
    margin-top: 5px;
}
.wrapper{
	background-color:inherit !important;
}
.content-wrapper{
	min-height: 380px  !important;
}
.hideshowonmob_btn_2{
	display:none;
	margin-bottom: 15px;
	text-decoration: underline;
    cursor: pointer;
    font-size: 15px;
}
.hideshowonmob_hide_btn_2{
	display:none;
	margin-bottom: 15px;
    text-decoration: underline;
    cursor: pointer;
    font-size: 15px;
}
@media only screen and (max-width: 640px){
	.hideshowonmob_2{
		/* display:none; */
	}
	.hideshowonmob_btn_2{
		/* display:block; */
	}
	.document_delete_list{
		font-size:18px;
	}
}
</style>
<script type="text/javascript">
$(function(){
	$('#add_form').submit(function(){
		$("#add_master_data").attr("disabled", true);
		$('#page_load_div').show();
	});	
	
	$(".document_delete_list").click(function(){
		if(confirm("Do you want to delete the document?")){
			var document_id = $(this).attr("alt");
			if(document_id != ""){
				var form_data   = {'document_id':document_id};  
				$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/appointment/index.php?page=ajax_actions&action=delete_document_from_list",
					data: form_data,
					success: function(result){
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$("#doc_div"+document_id).hide();
						}
					}
				});	
			}
		}
	});
	
	/* $("#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);
					}
				}
			});
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/appointment/index.php?page=ajax_actions&action=get_address_by_job",
				data: form_data,
				success: function(result){  
					if(result){
						$("#addess").val(result);
					}
				}
			});
		}
	}); 
	$("#submit_form").click(function(){	
		if($("#span").text() == "No consultant assign to this job. First assign consultant."){
			alert("First select Consultant");
			return false;
		}
	});*/
	
	
});
</script>