<?php
$submit_url = $file_name."?page=view_appointment_for_admin&action=$action"; 
$status_submit_url = $file_name."?page=view_appointment_for_admin&name=$name";

$search_url = $file_name."?page=view_appointment_for_admin"; 
$show_all = "page=view_appointment_for_admin";  

?>
<style>
label {
    margin-top: 5px;
}
.hideshowonmob_btn{
	display:none;
	margin-bottom: 5px;
}
.hideshowonmob_hide_btn{
	display:none;
	margin-bottom: 5px;
}
.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{
		display:none;
	}
	.hideshowonmob_btn{
		display:block;
	}
	.hideshowonmob_2{
		/* display:none; */
	}
	.hideshowonmob_btn_2{
		/* display:block; */
	}
	.document_delete_list{
		font-size:18px;
	}
}
</style>
<button data-toggle="collapse" data-target="#booke_appointment_area" type="button" name="" class="btn btn-primary" style="margin:0 0 5px 10px;float:left;clear:right;">Add Appointment</button>

<div style="clear:both;"></div>
<div class="panel box box-primary <?=$collapse_booke_appointment_area;?>" 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="Notes" class="control-label">Notes</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">Enquiry/Quote/Job</label>
				<? $krscrm->getEnqQuoteJobList("job_id",$job_id,"100%",false,"form-control select2","required"); ?>
				<?if(!$mobiledetect->isMobile() && !$mobiledetect->isTablet()) {?>
					<label for="Document" class="control-label">Document</label>
					<i class="fa fa-upload" style="font-size: 17px;cursor:pointer;" id="document"></i>
					<i style="display:block;color:red;">(pdf/doc/docx/xls/xlsx)</i><br>
				<?}?>
				<i class="fa fa-check pull-right" id="upload_complete" style="color:#bad23a;font-size: 20px;display:none;"></i>
				<i class="fa fa-refresh fa-spin pull-right" style="font-size: 20px;display:none;" id="page_load_div"></i>
				<? if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){ ?>
				<div style="overflow:hidden;">
					<?
						$appointment_id = (isset($_REQUEST['id']) && $_REQUEST['id'] != "" && $_REQUEST['id'] != 0) ? $_REQUEST['id'] : "1111111111111111";
						$appointment_doc_admin = "";
						$sql_for_doc_admin = "SELECT * FROM documents WHERE appointment_id = '".$appointment_id."' ";
						$rs_for_doc_admin = mysql_query($sql_for_doc_admin);
						if(mysql_num_rows($rs_for_doc_admin) > 0){
							if($mobiledetect->isMobile() || $mobiledetect->isTablet()) {								
								?><label for="Document" class="control-label">Document</label><?
							}
							while($data_admin = mysql_fetch_assoc($rs_for_doc_admin)){								
								$appointment_doc_admin .= "<p style='clear:both;width:100%;margin: 0;' id='doc_div".$data_admin["id"]."'>".$data_admin["document_name"]." <i class='fa fa-close pull-right document_delete_list' alt='".$data_admin["id"]."' style='margin-top:3px;color:#3C8DBC;cursor:pointer;'></i></p>";
							}
							$appointment_doc_admin = substr($appointment_doc_admin,0,-4);
						}
						echo $appointment_doc_admin;
					?>
				</div>
				<? } ?>
				<div id="document_div">
					<?
						//unset($_SESSION['uploadfileArray']);
						if(isset($_SESSION['uploadfileArray'])){
							//print_r($_SESSION['uploadfileArray']);
							foreach($_SESSION['uploadfileArray'] as $key => $uploadfile){
								$doc_no = $key + 1;
								echo "<p id='document_p".$doc_no."'>".$uploadfile."<i class='fa fa-close pull-right document_delete' alt='".$uploadfile."' style='margin-top:3px;color:#3C8DBC;cursor:pointer;'></i></p>";
							}
						}
					?>
				</div><br/>
				<!--<div class="hide">
					<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>
			<?/* if($mobiledetect->isMobile()) {
				$class_hide = "hide";
			} */?>
			<div class="hideshowonmob_2" style="margin: 15px 0;">
			
			<div class="col-sm-3">
				
				<label for="Document" class="control-label">Image</label>
				<input type="file" id="fileinput" />
				<img id="source_image"  style="width:100%" class="hide" >
				<input type="button" id="compress" value="Compress" class="btn-primary" style="display: none;">
				<img id="compressed_image"  style="width:100%" class="hide" >
				<input type="button" id="upload" class="btn-success" value="Upload" style="display: none;">
				<i style="display:block;color:red;">(doc type jpg/jpeg/png)</i><br>
				<i class="fa fa-check pull-right" id="upload_complete-123" style="color:#bad23a;font-size: 20px;display:none;"></i>
				<i class="fa fa-refresh fa-spin pull-right" style="font-size: 20px;display:none;" id="page_load_div-123"></i>
				
				<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 = mysql_query($sql_for_admin);
					if(mysql_num_rows($rs_for_admin) > 0){
						while($row_for_admin = mysql_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 <? if(!(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit')){ ?>From<? } ?></label>
				<input type="text" class="form-control datepicker" id="appointment_date" name="appointment_date" value="<?php (isset($appointment_date) && $appointment_date !="") ? utility::getFormatedDate($appointment_date) : utility::getFormatedDate(date($config->dateFormat));?>" 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;?>" autocomplete="off">-->
					<? 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;?>" autocomplete="off">-->
					<? 
					
					utility::getTimeList("appointment_time_to", $appointment_time_to, "Time To", "100%");?>
				</div>
				
				<? if(!(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit')){ ?>
					<label for="Appointment" class="control-label">Date To</label>
					<input type="text" class="form-control datepicker" id="appointment_date_to" name="appointment_date_to" value="<?php utility::getFormatedDate(date($config->dateFormat));?>" required>
				<? } ?>
				
				
				
			</div>
		
			</div>
			<?
				/* if($mobiledetect->isMobile()) {
					$display_on_mobile = 'block';				
					$hide_on_mobile = 'none';				
				} */
			
			?>
			
			<a style="margin-left: 15px;" class="hideshowonmob_btn_2">Show Details</a>
			<a style="margin-left: 15px;" class="hideshowonmob_hide_btn_2">Hide Details</a>
			
			
			
			<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;?>">-->
				
				<!--<div style="width:80px;float:left;clear:right;border:0px solid;">
					<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>-->
						
				<label for="Addess" class="control-label">Address</label>
				<input type="text" class="form-control" id="addess" name="addess" value="<?php echo $addess;?>"><br/>
				
				<div style="display:<?=$quotation_area_hide;?>;">
					<div class="form-group">
						<input class="flat-red" type="checkbox" id="attended" name="attended" <? echo ($attended) ? "checked" : ""; ?>>&nbsp;&nbsp;<b>Attended</b>
					</div>
					<div class="form-group">
						<input class="flat-red" type="checkbox" id="client_not_in" name="client_not_in" <? echo ($client_not_in) ? "checked" : ""; ?>>&nbsp;&nbsp;<b>Client not in</b>
					</div>
				
				
					<hr />
					<div class="form-group">
						<input type="checkbox" class="flat-red quotation_status" id="quotation_accepted" name="quotation_accepted" value="1"  <? echo ($quotation_accepted ==1) ? 'checked':''; ?>>&nbsp;&nbsp;<b>Quotation Accepted</b>						
						<i class="fa fa-check pull-right" id="quotation_accepted_check" style="color:#bad23a;font-size: 20px;display:none;"></i>
						<i class="fa fa-refresh fa-spin pull-right" style="font-size: 20px;display:none;" id="quotation_accepted_refresh"></i>
					</div>
					<div class="form-group">	
						<input type="checkbox" class="flat-red quotation_status" id="quotation_declined" name="quotation_declined" value="1"  <? echo ($quotation_declined ==1) ? 'checked':''; ?>>&nbsp;&nbsp;<b>Quotation Declined</b>
						<i class="fa fa-check pull-right" id="quotation_declined_check" style="color:#bad23a;font-size: 20px;display:none;"></i>
						<i class="fa fa-refresh fa-spin pull-right" style="font-size: 20px;display:none;" id="quotation_declined_refresh"></i>
					</div>
				</div>
			</div>
			<div style="clear:both;"></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="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?></button>
			</div>
		</form>
	</div>
</div>

<div class="box-header" >
<div class="row" >
	<div class="hideshowonmob" style="margin: 15px 0;">
	<form action="<?php echo $search_url;?>" method="post" name="search_form" id="search_form">		
		<div class="col-sm-3">
			<label for="Job" class="control-label">Enquiry/Quote/Job</label>
			<? $krscrm->getEnqQuoteJobList("job_id_search",$job_id_search,"100%",false,"form-control select2"); ?>
		</div>
		<div class="col-sm-2">
			<label for="Consultant" class="control-label">Consultant</label>
			<? Admin::getAdminList("admin_id_search",$admin_id_search,"100%",false,"form-control"); ?>
		</div>
		<div class="col-sm-2">
			<label for="From Appointment Date" class="control-label">From  Date</label>
			<input type="text" class="form-control datepicker" id="from_appointment_date" name="from_appointment_date" value="<?php (isset($from_appointment_date) && $from_appointment_date !="") ? utility::getFormatedDate($from_appointment_date) : "";?>">
		</div>
		<div class="col-sm-2">
			<label for="Appointment" class="control-label">To Date</label>
			<input type="text" class="form-control datepicker" id="to_appointment_date" name="to_appointment_date" value="<?php (isset($to_appointment_date) && $to_appointment_date !="") ? utility::getFormatedDate($to_appointment_date) : "";?>">
		</div>
		<!--<div class="col-sm-1">
			<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-2">
			<div style="clear:both;margin-top:28px;"></div>			
			<button type="submit" name="search" class="btn btn-primary">Search</button>
			<button type="button" name="search" class="btn btn-primary" onclick="javascript:window.location='?<?php echo $show_all ?>'">Show All</button>
		</div>
	</form>
	</div>
	<button type="button" id="" name="" value=""  style="margin-left: 15px;" class="btn btn-success hideshowonmob_btn">Show Filter</button>
	<button type="button" id="" name="" value=""  style="margin-left: 15px;" class="btn btn-warning hideshowonmob_hide_btn">Hide Filter</button>
</div>
</div>

<script type="text/javascript">
$(function(){
	
	
	$(".hideshowonmob_btn").on("click",function() {
		$(".hideshowonmob").show('slow');
		$(".hideshowonmob_hide_btn").show();
		$(".hideshowonmob_btn").hide();
	});
	
	$(".hideshowonmob_hide_btn").on("click",function() {
		$(".hideshowonmob").hide('slow');
		$(".hideshowonmob_hide_btn").hide();
		$(".hideshowonmob_btn").show();
	});
	
	<? if($_REQUEST['action'] == 'edit' ){ ?>
	if(window.matchMedia("(max-width: 640px)").matches){
		$(".hideshowonmob_2").hide('slow');
		$(".hideshowonmob_hide_btn_2").hide();
		$(".hideshowonmob_btn_2").show();
		
		$(".hideshowonmob_btn_2").on("click",function() {
			$(".hideshowonmob_2").show('slow');
			$(".hideshowonmob_hide_btn_2").show();
			$(".hideshowonmob_btn_2").hide();
		});
		
		$(".hideshowonmob_hide_btn_2").on("click",function() {
			$(".hideshowonmob_2").hide('slow');
			$(".hideshowonmob_hide_btn_2").hide();
			$(".hideshowonmob_btn_2").show();
		});
	}
	<?}?>
	
	
	var output_format = null;
	var file_name = null;
	function readFile(evt) {
		$("#page_load_div").show();
		$("#upload_complete").hide();
		var file = evt.target.files[0];
		var reader = new FileReader();
	    reader.onload = function(event) {
			var i = document.getElementById("source_image");
			console.log(i);
	            i.src = event.target.result;
	            i.onload = function(){
	                
	                console.log("Image loaded");
	            }
	    };
		output_format = file.name.split(".").pop();
		file_name = file.name;
	    console.log("Filename:" + file.name);
	    console.log("Fileformat:" + output_format);
	    console.log("Filesize:" + (parseInt(file.size) / 1024) + " Kb");
	    console.log("Type:" + file.type);
	    reader.readAsDataURL(file);
		
		//$("#compress").show();
		setTimeout(function(){
			$("#compress").trigger('click');
		},2000);
		
	    return false;
	}
 // compress image
 	$( "#compress" ).click(function() {
        var source_image = document.getElementById("source_image");
        if (source_image.src == "") {
            alert("You must load an image first!");
            return false;
        }

        var quality = 30;
       	
        console.log("process start...");
        console.log("process start compress ...");
	    compressed_image.src = jic.compress(source_image,quality,output_format).src;
		//$("#upload").trigger('click');
		//$("#upload").show();
		$("#upload").trigger('click');
    	
	});
	// upload imange
	$( "#upload" ).click(function() {
        var compressed_image = document.getElementById("compressed_image");
        if (compressed_image.src == "") {
            alert("You must compress image first!");
            return false;
        }

	    var successCallback= function(response){
			var resultObj = jQuery.parseJSON(response);
		    $.each(resultObj.uploadfileArray, function(key,value) {
				
				id = key+1;
				$("<p id='document_p"+id+"'>"+value+"<i class='fa fa-close pull-right document_delete' alt='"+value+"' style='margin-top:3px;color:#3C8DBC;cursor:pointer;'></i></p>").appendTo("#document_div");
			});
			$("#page_load_div").hide();
			$("#upload_complete").show();
			setTimeout(function(){
				$("#upload_complete").hide();
			},2000);	
			console.log("image uploaded successfully! :)");
            console.log(response);       
        }

        var errorCallback= function(response){
            console.log("image Filed to upload! :)");
            console.log(response);
			$("#page_load_div").hide();
			$("#upload_complete").hide();	
        }
    	
    	console.log("process start upload ...");
    	jic.upload(compressed_image, "<?php echo $siteURL?>/appointment/index?page=ajax_actions&action=upload_image", "file", file_name,successCallback,errorCallback);
    	
    });

	document.getElementById("fileinput").addEventListener("change", readFile, false);
	
	$("#add_form").on("click",".select_admin_id",function(){
		if($("#job_id").val() == ""){
			alert("First select Enquiry/Quote/Job");
			$(this).val("");
			return false;
		}
	});
	<? if($_REQUEST['action'] != 'edit'){ ?>
	$("#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_descrip_by_job",
				data: form_data,
				success: function(result){  
					if(result){
						$("#appointment_text").val(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;
		}
	});
	
	
	$('.quotation_status').on('ifClicked', function(){
		
		$('.quotation_status').iCheck('uncheck');
		$(this).iCheck('check');
		
		if($(this).attr("name") == "quotation_accepted"){
			$("#quotation_accepted_refresh").show();
		}else{
			$("#quotation_declined_refresh").show();
		}
		
		var quotation_status = $(this).attr("name");
		var job_id = $("#job_id").val();
		//alert('aaaaaaaaaaa'+job_id);
		var form_data   = {'job_id':job_id,'quotation_status':quotation_status};       
		$.ajax({
			async: true,
			type: "POST",
			url: site_url+"/modules/appointment/index.php?page=ajax_actions&action=update_quotation",
			data: form_data,
			success: function(r){    
				$("#quotation_accepted_refresh").hide();
				$("#quotation_declined_refresh").hide();
				
				if(r == "quotation_accepted"){
					$("#quotation_accepted_check").show();
					setTimeout(function(){
						$("#quotation_accepted_check").hide();
					},2000);
				}else{
					$("#quotation_declined_check").show();
					setTimeout(function(){
						$("#quotation_declined_check").hide();
					},2000);
				}
			}
		});
	});
	
	
	////////////////// DOCUMENT DELETE //////////////////////////////////////
	$("#add_form").on("click",".document_delete",function(){
		if(confirm("Do you want to delete the document?")){
			var document_p_id = $(this).parent().attr("id");
			//alert(document_p_id);
			var document_name = $(this).attr("alt");
			if(document_name != ""){
				var form_data   = {'document_name':document_name};  
				$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/appointment/index.php?page=ajax_actions&action=delete_document",
					data: form_data,
					success: function(result){
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$("#"+document_p_id).hide();
						}
					}
				});	
			}
		}
	});
	////////////////// AJAX UPLOAD //////////////////////////////////////
	var btnUpload=$('#document');
	//$("#document").click(function(){
		//var identification_span_id = $(this).attr("alt");
		var form_data   = {'job_id':'1'};    
		new AjaxUpload(btnUpload, {
			action: '<?php echo $siteURL?>/appointment/index?page=ajax_actions&action=upload_document',
			name: 'uploadfile[]',
			data: form_data,
			onSubmit: function(file, ext){
				if (! (ext && /^(jpg|png|jpeg|pdf|doc|docx)$/.test(ext))){ 
					// extension is not allowed 
					alert('Only JPG, PNG, PDF, DOC or DOCX files are allowed');
					return false;
				}
				$("#page_load_div").show();
				$("#upload_complete").hide();
				/* if(confirm("Do you want to upload")){
					$("#page_load_div").show();
					$("#upload_complete").hide();
				}else{
					return false;
				} */
			},
			onComplete: function(file, response){
				var resultObj = jQuery.parseJSON(response); 
				if(resultObj.status == "success"){
					console.log(response);
					$("#page_load_div").hide();
					$("#upload_complete").show();
					setTimeout(function(){
						$("#upload_complete").hide();
					},2000);
					
					$.each(resultObj.uploadfileArray, function(key,value) {
						//alert(key+" "+value);
						id = key+1;
						$("<p id='document_p"+id+"'>"+value+"<i class='fa fa-close pull-right document_delete' alt='"+value+"' style='margin-top:3px;color:#3C8DBC;cursor:pointer;'></i></p>").appendTo("#document_div");
					});
				} else{
					$("#page_load_div").hide();
					$("#upload_complete").hide();
					alert("There is some error to upload file.");
				}
				$("input[name='uploadfile[]']").attr("multiple","multiple");
			}
		});
	//});
	
	$("input[name='uploadfile[]']").attr("multiple","multiple");
	
});
</script>
<script type="text/javascript" src="<?php echo $siteURL?>/modules/appointment/js/ajaxupload.3.5.js" ></script>
<script type="text/javascript" src="<?php echo $siteURL?>/modules/appointment/js/jic.js" ></script>