<?php
session_start();
error_reporting(0);
include('../../includes/config.php');
require_once("../../includes/dbcontroller.php");
$db_handle = new DBController();

$role= $_SESSION['role'];
if($role!='Admin'){
	header("location:$website_url/");
	exit();
}
$truck_id=$_GET['id'];
if((!isset($_GET['id']))){
	header("location:$website_url$trucks_url");
	exit();
}
$truck_query="SELECT * FROM trucks where Truck_id='$truck_id'";
$truck_data = current($db_handle->runQuery($truck_query));

if(isset($_POST['delete'])){
	$del_id=$_POST['id'];
	$exist_truck_sql="select * from trucks where Truck_id='$del_id'";
	$count = $db_handle->numRows($exist_truck_sql);
	if($count>0){
		$sql = "DELETE FROM trucks WHERE Truck_id='$del_id'";
		$result = $db_handle->executeQuery($sql);
		if ($result) {
			echo "<script>alert('Truck Deleted Succesfully')</script>";
			echo "<script>location.href = '$website_url$trucks_url';</script>";
		}
	}
	else{
		echo "<script>alert('Truck does Not Exist.')</script>";
		echo "<script>location.href = '$website_url$trucks_url';</script>";
	}
}
include('../../includes/menu.php');
?>


<div class="container main-cont single-delete-truck">
	<div class="delete-container">
		<fieldset>
			<div class="page-title">
				<h3> Are you sure you would like to delete the Truck record(s) below?</h3>
			</div>
			<div class="table-responsive">
				<table class="table table-striped table-hover table-reflow">
					<thead>
						<tr>
							<th>#Truck</th><th>Truck Name</th><th></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td><?php echo $truck_data['TruckNumber']; ?></td>
							<td><?php echo $truck_data['TruckName']; ?></td>
						</tr>
					</tbody>
				</table>
			</div>
			<form  enctype="multipart/form-data" method="post" accept-charset="utf-8">
				<input type="hidden" name="id" value="<?php echo $truck_data['Truck_id']; ?>">
				<a class="btn btn-primary" href="<?php echo $website_url.$trucks_url ?>">Cancel</a>
				<input type="submit" value="Yes" name="delete" class="btn btn-danger">
			</form>    
		</fieldset>
	</div>
</div>



<?php
include('../includes/footer.php');
include('../includes/webfooter.php');
?>