34788-vm/dashboard.php
2025-10-08 11:45:34 +00:00

233 lines
9.1 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
// Protect this page - redirect to login if user is not logged in
if (!isset($_SESSION['user'])) {
header('Location: login.php');
exit;
}
$user = $_SESSION['user'];
// Fetch prices
$petrol_price = 0.00;
$diesel_price = 0.00;
try {
$pdoconn = db();
$stmt = $pdoconn->query("SELECT * FROM prices ORDER BY updated_at DESC LIMIT 1");
$latest_prices = $stmt->fetch();
if ($latest_prices) {
$petrol_price = $latest_prices['petrol_price'];
$diesel_price = $latest_prices['diesel_price'];
}
} catch (PDOException $e) {
error_log("Could not fetch prices: " . $e->getMessage());
}
// Fetch user's orders
$orders = [];
try {
$stmt = $pdoconn->prepare("SELECT * FROM orders WHERE user_id = :user_id ORDER BY order_date DESC");
$stmt->execute([':user_id' => $user['id']]);
$orders = $stmt->fetchAll();
} catch (PDOException $e) {
error_log("Could not fetch orders: " . $e->getMessage());
}
require_once 'includes/pexels.php';
$bg_image_data = pexels_get('https://api.pexels.com/v1/search?query=abstract+background&orientation=landscape&per_page=1&page=1');
$bg_image = ''; // Default empty
if ($bg_image_data && !empty($bg_image_data['photos'])) {
$photo = $bg_image_data['photos'][0];
$src = $photo['src']['large2x'] ?? ($photo['src']['large'] ?? $photo['src']['original']);
$target = __DIR__ . '/assets/images/pexels/' . $photo['id'] . '.jpg';
download_to($src, $target);
$bg_image = 'assets/images/pexels/' . $photo['id'] . '.jpg';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Customer Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="assets/css/custom.css">
<style>
body {
display: flex;
min-height: 100vh;
flex-direction: column;
}
.main-container {
display: flex;
flex: 1;
}
.sidebar {
width: 280px;
background-color: var(--brand-navy-blue);
color: white;
}
.sidebar .nav-link {
color: rgba(255, 255, 255, 0.7);
padding: 1rem;
}
.sidebar .nav-link:hover, .sidebar .nav-link.active {
color: white;
background-color: var(--brand-orange);
}
.sidebar .nav-link .bi {
margin-right: 10px;
}
.content {
flex: 1;
padding: 2rem;
background-image: url('<?php echo $bg_image; ?>');
background-size: cover;
background-position: center;
}
.top-bar {
background-color: rgba(255, 255, 255, 0.9);
border-bottom: 1px solid #dee2e6;
padding: 1rem;
display: flex;
justify-content: flex-end;
align-items: center;
}
.card {
background-color: rgba(255, 255, 255, 0.9);
}
</style>
</head>
<body>
<div class="main-container">
<div class="sidebar d-flex flex-column p-3">
<a href="dashboard.php" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-white text-decoration-none">
<span class="fs-4">Petrol Price Co.</span>
</a>
<hr>
<ul class="nav nav-pills flex-column mb-auto">
<li class="nav-item">
<a href="dashboard.php" class="nav-link active" aria-current="page">
<i class="bi bi-speedometer2"></i>
Dashboard
</a>
</li>
<li>
<a href="#order-form" class="nav-link">
<i class="bi bi-fuel-pump"></i>
Place Order
</a>
</li>
<li>
<a href="account_statement.php" class="nav-link">
<i class="bi bi-list-ul"></i>
Account Statement
</a>
</li>
<li>
<a href="profile.php" class="nav-link">
<i class="bi bi-person"></i>
Profile
</a>
</li>
</ul>
<hr>
<div class="dropdown">
<a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" id="dropdownUser1" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle fs-4 me-2"></i>
<strong><?php echo htmlspecialchars($user['name']); ?></strong>
</a>
<ul class="dropdown-menu dropdown-menu-dark text-small shadow" aria-labelledby="dropdownUser1">
<li><a class="dropdown-item" href="profile.php">Profile</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="logout.php">Sign out</a></li>
</ul>
</div>
</div>
<div class="d-flex flex-column flex-fill">
<div class="top-bar">
<span class="text-muted">Welcome, <?php echo htmlspecialchars($user['name']); ?>!</span>
</div>
<div class="content">
<?php if (isset($_SESSION['order_success'])) { ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo $_SESSION['order_success']; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php unset($_SESSION['order_success']); ?>
<?php } ?>
<?php if (isset($_SESSION['order_error'])) { ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<?php echo $_SESSION['order_error']; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php unset($_SESSION['order_error']); ?>
<?php } ?>
<h1 class="h2">Dashboard</h1>
<p>Welcome to your customer portal. Here you can view prices, place orders, and see your account history.</p>
<div class="row mt-4">
<div class="col-md-6">
<div class="card">
<div class="card-header">Today's Prices</div>
<div class="card-body">
<p>Petrol: $<?php echo htmlspecialchars(number_format($petrol_price, 2)); ?> / litre</p>
<p>Diesel: $<?php echo htmlspecialchars(number_format($diesel_price, 2)); ?> / litre</p>
<small class="text-muted">Prices are updated daily.</small>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">Banking Details</div>
<div class="card-body">
<p>Please use the following details for payments:</p>
<ul>
<li><strong>Bank A:</strong> 123-456-7890</li>
<li><strong>Bank B:</strong> 098-765-4321</li>
</ul>
</div>
</div>
</div>
</div>
<div class="row mt-4" id="order-form">
<div class="col-12">
<div class="card">
<div class="card-header">Place a New Order</div>
<div class="card-body">
<form action="order_handler.php" method="POST">
<div class="mb-3">
<label for="fuel_type" class="form-label">Fuel Type</label>
<select class="form-select" id="fuel_type" name="fuel_type" required>
<option value="petrol">Petrol</option>
<option value="diesel">Diesel</option>
</select>
</div>
<div class="mb-3">
<label for="quantity" class="form-label">Quantity (litres)</label>
<input type="number" class="form-control" id="quantity" name="quantity" min="1" step="0.01" required>
</div>
<button type="submit" class="btn btn-primary" style="background-color: #fd7e14; border-color: #fd7e14;">Place Order</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>