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

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

?>

<div class="hide_search_div">
<!--<button data-toggle="collapse" data-target="#add_mileage_area" type="button" name="" class="btn btn-primary add_mileage_hide_show <?=$add_mileage_btn_hide;?>" >Add Mileage</button>-->
<a href="" data-toggle="modal" data-target="#myModal" class="btn btn-primary mileage" style="margin:0 0 5px 10px;">Mileage</a>

<div id="add_mileage_area" class="panel <?=$collapse_add_new_mileage;?>">
	<div class="box-header with-border ">
		<div class="col-12">
		<form action="<?php echo $submit_url;?>" method="post" name="search_form" id="search_form">
			<div class="row">
			<div class="col-sm-3">
				<label for="Mileage Text" class="control-label">Description</label>
				<textarea class="form-control" rows="4" id="mileage_text" name="mileage_text" required><?php echo stripslashes($mileage_text);?></textarea>
			</div>
			<div class="col-sm-3">
				<label for="Job" class="control-label">Enquiry/Quote/Job</label>
				<? $gsi->getEnqQuoteJobList("job_id",$job_id,"100%",false,"form-control select2"); ?>
				
				<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%",false,"form-control","required",$admin_ids); 
				?>
				</div>
			</div>
			<div class="col-sm-3">
				<label for="Mileage" class="control-label">Mile(s)</label>
				<input type="text" class="form-control" id="mileage" name="mileage" value="<?php echo $mileage;?>" required>
			</div>
			<div class="col-sm-3">
				<label for="Mileage" class="control-label">Date</label>
				<input type="text" class="form-control datepicker" id="mileage_date" name="mileage_date" value="<?php (isset($mileage_date) && $mileage_date !="") ? utility::getFormatedDate($mileage_date) : utility::getFormatedDate(date($config->dateFormat));?>" required>
			</div>
			<div class="col-sm-2">
				<div style="clear:both;margin-top:23px;"></div>
				<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>
				<button data-toggle="collapse" data-target="#add_mileage_area" type="button" name="" class="btn btn-primary cancel_mileage_btn <?=$cancel_mileage_btn_hide;?>">Cancel</button>
			</div>
			</div>
		</form>
		</div>
	</div>
</div>

<div class="box-header" id="mileage_search_list" style="display:<?=$mileage_search_list;?>;">
	<form action="<?php echo $search_url;?>" method="post" name="search_form1" id="search_form1">		
		<div class="card-body">
		<div class="row">
		<div class="col-sm-3">
			<label for="Job" class="control-label">Enquiry/Quote/Job</label>
			<?// $gsi->getEnqQuoteJobList("job_id_search",$job_id_search,"100%",false,"form-control select2"); ?>
			<? $gsi->getEnqQuoteJobList("job_id_search",$job_id_search,"100%",false,"form-control select2 job_id",""); ?>
		</div>
		<div class="col-sm-3">
			<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 Mileage Date" class="control-label">From Date</label>
			<input type="text" class="form-control datepicker" id="from_mileage_date" name="from_mileage_date" value="<?php (isset($from_mileage_date) && $from_mileage_date !="") ? utility::getFormatedDate($from_mileage_date) : "";?>">
		</div>
		<div class="col-sm-2">
			<label for="Mileage" class="control-label">To Date</label>
			<input type="text" class="form-control datepicker" id="to_mileage_date" name="to_mileage_date" value="<?php (isset($to_mileage_date) && $to_mileage_date !="") ? utility::getFormatedDate($to_mileage_date) : "";?>">
		</div>
		<div class="col-sm-3">
			<div style="clear:both;margin-top:23px;"></div>			
			<button type="submit" name="search" class="btn btn-success" style="float:left;clear:right;margin-right:10px;">Search<i class="fa fa-refresh fa-spin" style="display:none;" id="page_load_div"></i>
			</button>
			<button type="button" name="search" class="btn btn-info" onclick="javascript:window.location='?<?php echo $show_all ?>'" style="float:left;clear:right;margin-right:10px;">Show All</button>
			
			<a href="" onClick="window.print();" class="btn btn-default hide_search_div" style="float:right !important;margin-bottom:4px;"><i class="fa fa-print"></i> Print</a>
		</div>
		</div>
		</div>
	</form>
</div>
</div>
<script type="text/javascript">
$(function(){
	$('.add_mileage_hide_show').click(function(){
		$('.add_mileage_hide_show').hide();
		$('.cancel_mileage_btn').show();
		$('#mileage_search_list').hide();
		$('#mileage_listing').hide();
	});
	$('.cancel_mileage_btn').click(function(){
		$('.add_mileage_hide_show').show();
		$('#mileage_search_list').show();
		$('#mileage_listing').show();
	});
	$('#search_form1').submit(function(){
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
	});
	$("#admin_id").change(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/mileage/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;
		}
	});
	$(".mileage").click(function(){	
		$("#myModalLabel").text("Mileage Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var view_mileage_for_admin = "&view_mileage_for_admin=show_contact_list";
		//var job_id = $(this).attr("alt");		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/mileage/index?page=view"+view_mileage_for_admin+hide_top_left);
	});
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	});
});
</script>