<?php
$message = (isset($_SESSION['message'])) ? $_SESSION['message'] :"";
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : "insert";

$collapse_add_new_mileage = "collapse";
$add_mileage_btn_hide = "";
$cancel_mileage_btn_hide = "";
$mileage_search_list = "block";
$mileage_listing = "block";

$where 	= " WHERE 1=1 ";


$query_string = "";

$button_value 	= "Add";
$table_name		= "mileage";

$next_page_condition 	= "";

if($_REQUEST['hide_top']){
	$next_page_condition.="&hide_top=1";
}
if($_REQUEST['hide_left']){
	$next_page_condition.="&hide_left=1";
}


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


$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
	//$next_page_condition.="&start=$start";
}
$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){
	$query_string =  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	$collapse_add_new_mileage = "";
	$add_mileage_btn_hide = "hide";
	$cancel_mileage_btn_hide = "hide";
	$mileage_search_list = "none";
	$mileage_listing = "none";
	
	$action 	= "update";
	$button_value 	= "Update";
	$page_name		= "Edit Mileage";	
		
    $id = $_REQUEST['id'];
    $sql= "SELECT * FROM mileage WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

    $id= $row_edit['id'];
    $mileage= $row_edit['mileage'];
    $job_id= $row_edit['job_id'];
    $admin_id= $row_edit['admin_id'];
    $mileage_rate= $row_edit['mileage_rate'];
	$mileage_text= $row_edit['mileage_text'];
	$mileage_date= $row_edit['mileage_date'];
}
################################################# END EDIT ################################################# 


################################################## START update ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string","admin_id"));
	$id= $_REQUEST['id'];
    $mileage= $_REQUEST['mileage'];
    $job_id= $_REQUEST['job_id'];
    $admin_id= $_REQUEST['admin_id'];
    $mileage_rate= $config->mileage_rate;
	$mileage_text= $_REQUEST['mileage_text'];
	$mileage_date	 = utility::setFormatedDate($_REQUEST['mileage_date']);

    $sql= "UPDATE mileage SET mileage = '$mileage',job_id = '$job_id',admin_id = '$admin_id',mileage_rate = '$mileage_rate',mileage_text = '$mileage_text',mileage_date = '$mileage_date',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("mileage updated sucessfully");
	$redirect = "index?page=list$query_string";
	header("Location: $redirect");
	exit();
	
}
################################################# END update ################################################# 
    
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	    
	utility::addslashesOnAllInput(array("query_string","admin_id"));
    $mileage = $_REQUEST['mileage'];	
    $job_id = $_REQUEST['job_id'];	
    $admin_id = $_REQUEST['admin_id'];	
	$mileage_rate= $config->mileage_rate;
	$mileage_text = $_REQUEST['mileage_text'];
	$created_on = date("Y-m-d");
	$source	 = $_REQUEST['source'];
	$mileage_date	 = utility::setFormatedDate($_REQUEST['mileage_date']);
		
    $sql= "INSERT INTO $table_name (job_id,admin_id,mileage,mileage_rate,mileage_text,updated_by,created_on,source,mileage_date) VALUES('$job_id','$admin_id','$mileage','$mileage_rate','$mileage_text','".$_SESSION['user_id']."','$created_on','$source','$mileage_date')";
    mysqli_query($conn,$sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add", "page=list", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	
	$_SESSION['message']	= ucfirst("mileage added sucessfully");
	$redirect = "index?page=list&name=$name$query_string";
	header("Location: $redirect");
	exit();
}
################################################# END INSERT #################################################
###########################################  STRAT FILTER CONITIONS    #####################################
$description 	= isset($_REQUEST['description']) ? $_REQUEST['description'] : "";
if($description) {
    $where	.= " AND description LIKE '%$description%' ";
    $next_page_condition.="&description=$description";
}

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

if($_REQUEST['admin_id_search']){
	$admin_id_search = $_REQUEST['admin_id_search'];
	$where 	.= " AND admin_id LIKE '%$admin_id_search%' ";
	$next_page_condition.="&admin_id_search=".$_REQUEST['admin_id_search'];
}
$from_mileage_date = isset($_REQUEST['from_mileage_date']) ? utility::setFormatedDate($_REQUEST['from_mileage_date']) : "";
$to_mileage_date = isset($_REQUEST['to_mileage_date']) ? utility::setFormatedDate($_REQUEST['to_mileage_date']) : "";
if($from_mileage_date && $to_mileage_date){
	$where .= " AND mileage_date BETWEEN '$from_mileage_date 00:00:00' and '$to_mileage_date 23:59:59' ";
    $next_page_condition.="&from_mileage_date=".$_REQUEST['from_mileage_date']."&to_mileage_date=".$_REQUEST['to_mileage_date']." ";
}else if($from_mileage_date){
	$where.= " AND mileage_date >= '$from_mileage_date' ";
	$next_page_condition.="&from_mileage_date=".$_REQUEST['from_mileage_date'];
}else if($to_mileage_date){
	$where.= " AND mileage_date <= '$to_mileage_date' ";
	$next_page_condition.="&to_mileage_date=".$_REQUEST['to_mileage_date'];
}
###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Mileage Management ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ORDER BY mileage_date DESC LIMIT $eu, $limit ";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$total_no = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php mileage will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete'){
	$id	= $_REQUEST['id'];
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = $id ");
	
	$_SESSION['message']	= ucfirst("mileage deleted sucessfully");
	$redirect = "index?page=list";
	header("Location: $redirect");
	exit();
}
#################################  END DELETE    #########################################

#################################  Export CSV    #########################################
if(isset($_REQUEST['action']) && $_REQUEST['action']=='export_mileage'){
  include("export_mileage_to_csv.inc.php");
}
##########################################################################################
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><? echo str_replace(array("_"),array(" "),$page_header); ?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<!----------------------- Message ------------------------------->
	<? if(isset($_SESSION['message'])){ ?>
	<section class="content-header">
	  <div class="alert alert-info alert-dismissable" style="margin-bottom:0px;">
		<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
		<? echo $_SESSION['message'];
			unset($_SESSION['message']);
		?>
	  </div>
	</section>
	<? } ?>
<!----------------------- End Message ------------------------------->
	<section class="content">
		<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">

				<div class="card-body">
				<div class="" style="padding:0px 10px;">
					<?php include_once('add_list_mileage.inc.php');?>
				</div>
				Total : <?=$total_no;?>
				
				<div style="float:right;margin:5px;"  class="hide_search_div">Export Mileage &nbsp;&nbsp;<a href="index?page=list&action=export_mileage<?=$next_page_condition;?>"><i class="fa fa-download" style="font-size:20px;"></i></a></div>
				<!--<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">-->					
				<form action="?page=list&name=<?=$name?>" method="post" name="form1" id="form1">
				<? if($result>0){
				?>
					<!-- Start  records header -->
				<div id="mileage_listing" style="display:<?=$mileage_listing;?>;">
				<table id="example1" class="table table-bordered table-striped">
					<thead>
						<tr>
							<th>Id</th>
							<th>Company Name</th>
							<th>1st Contact Name</th>
							<th>Description</th>
							<th>Consultant</th>
							<th>Mileage Rate</th>
							<th>Mileage (In miles)</th>
							<th>Cost (&pound;)</th>
							<th>Date</th>
							<th class="hide_search_div">Edit</th>						
							<th class="hide_search_div">Delete</th>
						</tr>
					</thead>
					<tbody>
					<!-- End records header -->
					<?
					$total_mileage = 0;
					$mileage = 0;
					while($row = mysqli_fetch_array($sql)){
						$company_name = utility::getTableFieldById("job","company_name","id",$row['job_id']);
						$first_name = utility::getTableFieldById("job","first_name","id",$row['job_id']);
						$last_name = utility::getTableFieldById("job","last_name","id",$row['job_id']);
						$contact_name = ucfirst($first_name)." ".ucfirst($last_name);
					?>
					<!-- Start data -->
					<tr>
						<td><?php echo $row['job_id']; ?></td>
						<td><?php echo $company_name; ?></td>
						<td><?php echo $contact_name; ?></td>
						<td><?php echo $row['mileage_text']; ?></td>
						<td><?php echo Admin::getAdminById($row['admin_id']); ?></td>
						<td><?php echo number_format($row['mileage_rate'],2); ?></td>
						<td><?php echo $row['mileage']; ?></td>
						<td>
							<?php 
								$vat = utility::getTableFieldById('keycode','code','name','vat');
								$mileage_cost = $row['mileage']*$vat*$row['mileage_rate'];
								$total_mileage = $total_mileage + $mileage_cost;
								$mileage = $mileage + $row['mileage'];
								echo number_format($mileage_cost,2);
							?>
						</td>
						<td><?php echo utility::getFormatedDateReturn($row['mileage_date']); ?></td>
						<!--<td class="hide_search_div"><a href="index?page=list&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"> <img src="../images/edit.gif" border="0" /> </a></td>-->
						<td class="hide_search_div"><a href="" class="edit_mileage" data-toggle="modal" data-target="#myModal" alt="<?=$row['id'].$next_page_condition.$paging;?>"> <i class="fa fa-fw fa-edit"></i> </a></td>
						<td class="hide_search_div"><a
							href="index?page=list&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>"
							onclick=" javascript:return confirm('Are you sure! You want to delete');"><i class="fa fa-fw fa-trash"></i> </a></td>
					</tr>
					<!-- End data -->
					<? 
					}?>
					<tr>
						<td colspan="6" style="text-align:right;"><b>Total</b></td>
						<td><b><?=$mileage;?></b></td>
						<td colspan="4"><b><?=number_format($total_mileage,2);?></b></td>
					</tr>
					<?}else{?>
					<tr>
						<td colspan="11">No mileage found...</td>
					</tr>
					<?php }?>
					</tbody>
					
				</table>
				</div>
					<!-- Paging starts here  -->
					<? 
					$next_page_condition .="&page=list";
					include("paging/paging_row.inc.php") ?>
					<!-- Paging ends starts here  -->
						
					<!--</table>-->
					</form>
					</div>
				</div>
			</div>
		</div>
		</div>
	</section>
<style>
@media print
{
	.hide_search_div{
		display:none;
	}
}
</style>	
<script type="text/javascript">
$(function(){
	$(".edit_mileage").click(function(){
		$("#myModalLabel").text("Mileage Management");
		var hide_top_left = "&hide_top=1&hide_left=1";
		//var job_id = "<?=$row['id'];?>";	
		var mileage_id = $(this).attr("alt");
		var view_mileage_for_admin = "&view_mileage_for_admin=show_contact_list";		
		$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/mileage/index?page=view&action=edit&id="+mileage_id+view_mileage_for_admin+hide_top_left);
	});
});
</script>