146 lines
6.2 KiB
PHP
146 lines
6.2 KiB
PHP
<?php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
require_once 'db/config.php';
|
|
|
|
// Ensure user is logged in and is a vendor
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'vendor') {
|
|
header("Location: login.php");
|
|
exit;
|
|
}
|
|
|
|
$vendor_id = $_SESSION['user_id'];
|
|
$message = '';
|
|
|
|
// Handle form submission for adding a new product
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_product'])) {
|
|
$name = trim($_POST['name']);
|
|
$description = trim($_POST['description']);
|
|
$price = trim($_POST['price']);
|
|
$image_url = trim($_POST['image_url']);
|
|
|
|
if (!empty($name) && !empty($description) && !empty($price) && !empty($image_url)) {
|
|
try {
|
|
$sql = "INSERT INTO products (vendor_id, name, description, price, image_url) VALUES (:vendor_id, :name, :description, :price, :image_url)";
|
|
$stmt = db()->prepare($sql);
|
|
$stmt->bindParam(':vendor_id', $vendor_id, PDO::PARAM_INT);
|
|
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
|
|
$stmt->bindParam(':description', $description, PDO::PARAM_STR);
|
|
$stmt->bindParam(':price', $price, PDO::PARAM_STR);
|
|
$stmt->bindParam(':image_url', $image_url, PDO::PARAM_STR);
|
|
|
|
if ($stmt->execute()) {
|
|
$message = "<div class='alert alert-success'>Product added successfully!</div>";
|
|
} else {
|
|
$message = "<div class='alert alert-danger'>Error adding product.</div>";
|
|
}
|
|
} catch (PDOException $e) {
|
|
$message = "<div class='alert alert-danger'>Database error: " . $e->getMessage() . "</div>";
|
|
}
|
|
} else {
|
|
$message = "<div class='alert alert-warning'>Please fill in all fields.</div>";
|
|
}
|
|
}
|
|
|
|
// Fetch vendor's products
|
|
$products = [];
|
|
try {
|
|
$sql = "SELECT * FROM products WHERE vendor_id = :vendor_id ORDER BY created_at DESC";
|
|
$stmt = db()->prepare($sql);
|
|
$stmt->bindParam(':vendor_id', $vendor_id, PDO::PARAM_INT);
|
|
$stmt->execute();
|
|
$products = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOException $e) {
|
|
$message .= "<div class='alert alert-danger'>Error fetching products: " . $e->getMessage() . "</div>";
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Vendor Dashboard</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
<?php include 'includes/header.php'; ?>
|
|
|
|
<div class="container mt-5">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h1>Vendor Dashboard</h1>
|
|
<a href="logout.php" class="btn btn-danger">Logout</a>
|
|
</div>
|
|
|
|
<p>Welcome, <strong><?php echo htmlspecialchars($_SESSION['user_name']); ?></strong>!</p>
|
|
|
|
<?php echo $message; ?>
|
|
|
|
<!-- Add New Product Form -->
|
|
<div class="card mb-5">
|
|
<div class="card-header">
|
|
<h3>Add a New Product</h3>
|
|
</div>
|
|
<div class="card-body">
|
|
<form action="vendor_dashboard.php" method="post">
|
|
<div class="mb-3">
|
|
<label for="name" class="form-label">Product Name</label>
|
|
<input type="text" class="form-control" id="name" name="name" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="description" class="form-label">Description</label>
|
|
<textarea class="form-control" id="description" name="description" rows="3" required></textarea>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="price" class="form-label">Price</label>
|
|
<input type="number" class="form-control" id="price" name="price" step="0.01" required>
|
|
</div>
|
|
<div class="mb-3">
|
|
<label for="image_url" class="form-label">Image URL</label>
|
|
<input type="url" class="form-control" id="image_url" name="image_url" required>
|
|
</div>
|
|
<button type="submit" name="add_product" class="btn btn-primary">Add Product</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Vendor's Product List -->
|
|
<div class="card">
|
|
<div class="card-header">
|
|
<h3>Your Products</h3>
|
|
</div>
|
|
<div class="card-body">
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">Image</th>
|
|
<th scope="col">Name</th>
|
|
<th scope="col">Price</th>
|
|
<th scope="col">Date Added</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($products)): ?>
|
|
<tr>
|
|
<td colspan="4" class="text-center">You haven't added any products yet.</td>
|
|
</tr>
|
|
<?php else: ?>
|
|
<?php foreach ($products as $product): ?>
|
|
<tr>
|
|
<td><img src="<?php echo htmlspecialchars($product['image_url']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>" style="width: 50px; height: 50px; object-fit: cover;"></td>
|
|
<td><?php echo htmlspecialchars($product['name']); ?></td>
|
|
<td>$<?php echo htmlspecialchars($product['price']); ?></td>
|
|
<td><?php echo date("Y-m-d", strtotime($product['created_at'])); ?></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|