235 lines
11 KiB
PHP
235 lines
11 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
// Handle Add Product
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
|
|
if ($_POST['action'] === 'add') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
$category = trim($_POST['category'] ?? '');
|
|
$quantity = trim($_POST['quantity'] ?? '');
|
|
$expiration_date = $_POST['expiration_date'] ?: null;
|
|
|
|
if ($name) {
|
|
$stmt = db()->prepare("INSERT INTO products (name, category, quantity, expiration_date) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$name, $category, $quantity, $expiration_date]);
|
|
}
|
|
} elseif ($_POST['action'] === 'delete') {
|
|
$id = (int)($_POST['id'] ?? 0);
|
|
if ($id) {
|
|
$stmt = db()->prepare("DELETE FROM products WHERE id = ?");
|
|
$stmt->execute([$id]);
|
|
}
|
|
}
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
// Fetch Products
|
|
$products = db()->query("SELECT * FROM products ORDER BY expiration_date ASC")->fetchAll();
|
|
|
|
$today = new DateTime();
|
|
$expiring_soon_days = 7;
|
|
?>
|
|
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>Home Pantry Tracker</title>
|
|
<meta name="description" content="<?= htmlspecialchars($_SERVER['PROJECT_DESCRIPTION'] ?? 'Track your home food supplies and expiration dates.') ?>">
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
|
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
|
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time() ?>">
|
|
<script src="https://unpkg.com/html5-qrcode"></script>
|
|
<style>
|
|
#reader { width: 100%; border-radius: 8px; overflow: hidden; margin-bottom: 15px; display: none; }
|
|
.ai-loading { display: none; text-align: center; padding: 20px; }
|
|
.scanner-actions { display: flex; gap: 10px; margin-bottom: 15px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<nav class="navbar navbar-expand-lg mb-4">
|
|
<div class="container">
|
|
<a class="navbar-brand" href="#"><i class="bi bi-house-door-fill me-2"></i>Home Pantry</a>
|
|
<button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addItemModal">
|
|
<i class="bi bi-plus-lg me-1"></i> Add Item
|
|
</button>
|
|
</div>
|
|
</nav>
|
|
|
|
<div class="container">
|
|
<div class="row mb-4">
|
|
<?php
|
|
$total_items = count($products);
|
|
$expired_count = 0;
|
|
$warning_count = 0;
|
|
foreach ($products as $p) {
|
|
if ($p['expiration_date']) {
|
|
$exp_date = new DateTime($p['expiration_date']);
|
|
$diff = $today->diff($exp_date);
|
|
$days = (int)$diff->format("%r%a");
|
|
if ($days < 0) $expired_count++;
|
|
elseif ($days <= $expiring_soon_days) $warning_count++;
|
|
}
|
|
}
|
|
?>
|
|
<div class="col-md-4">
|
|
<div class="card p-3 text-center">
|
|
<div class="text-muted small text-uppercase fw-bold">Total Items</div>
|
|
<div class="h3 mb-0"><?= $total_items ?></div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card p-3 text-center">
|
|
<div class="text-muted small text-uppercase fw-bold text-danger">Expired</div>
|
|
<div class="h3 mb-0 text-danger"><?= $expired_count ?></div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<div class="card p-3 text-center">
|
|
<div class="text-muted small text-uppercase fw-bold text-warning">Expiring Soon</div>
|
|
<div class="h3 mb-0 text-warning"><?= $warning_count ?></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<span>Inventory List</span>
|
|
<div class="text-muted small">Sorted by expiration date</div>
|
|
</div>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>Product Name</th>
|
|
<th>Category</th>
|
|
<th>Quantity</th>
|
|
<th>Expiration</th>
|
|
<th>Status</th>
|
|
<th class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($products)): ?>
|
|
<tr>
|
|
<td colspan="6" class="text-center py-4 text-muted">No items in your pantry yet. Add some!</td>
|
|
</tr>
|
|
<?php endif; ?>
|
|
<?php foreach ($products as $p):
|
|
$status_class = '';
|
|
$badge_class = 'badge-fresh';
|
|
$status_text = 'Fresh';
|
|
|
|
if ($p['expiration_date']) {
|
|
$exp_date = new DateTime($p['expiration_date']);
|
|
$diff = $today->diff($exp_date);
|
|
$days = (int)$diff->format("%r%a");
|
|
|
|
if ($days < 0) {
|
|
$status_class = 'status-expired';
|
|
$badge_class = 'badge-expired';
|
|
$status_text = 'Expired';
|
|
} elseif ($days <= $expiring_soon_days) {
|
|
$status_class = 'status-warning';
|
|
$badge_class = 'badge-warning';
|
|
$status_text = 'Expiring Soon';
|
|
}
|
|
}
|
|
?>
|
|
<tr class="<?= $status_class ?>">
|
|
<td class="fw-bold"><?= htmlspecialchars($p['name']) ?></td>
|
|
<td><span class="badge bg-light text-dark"><?= htmlspecialchars($p['category']) ?></span></td>
|
|
<td><?= htmlspecialchars($p['quantity']) ?></td>
|
|
<td><?= $p['expiration_date'] ? date('M d, Y', strtotime($p['expiration_date'])) : '-' ?></td>
|
|
<td><span class="badge <?= $badge_class ?>"><?= $status_text ?></span></td>
|
|
<td class="text-end">
|
|
<form method="POST" style="display:inline;">
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="id" value="<?= $p['id'] ?>">
|
|
<button type="submit" class="btn btn-link text-danger p-0" onclick="return confirm('Delete this item?')">
|
|
<i class="bi bi-trash"></i>
|
|
</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Add Item Modal -->
|
|
<div class="modal fade" id="addItemModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form method="POST" id="addItemForm">
|
|
<input type="hidden" name="action" value="add">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Add New Pantry Item</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="scanner-actions">
|
|
<button type="button" class="btn btn-outline-secondary btn-sm flex-fill" id="btnScanBarcode">
|
|
<i class="bi bi-upc-scan me-1"></i> Scan Barcode
|
|
</button>
|
|
<button type="button" class="btn btn-outline-secondary btn-sm flex-fill" id="btnTakePhoto">
|
|
<i class="bi bi-camera me-1"></i> Take Photo (AI)
|
|
</button>
|
|
</div>
|
|
|
|
<div id="reader"></div>
|
|
|
|
<div class="ai-loading" id="aiLoading">
|
|
<div class="spinner-border text-primary mb-2" role="status"></div>
|
|
<div>AI is identifying the product...</div>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Product Name</label>
|
|
<input type="text" name="name" id="p_name" class="form-control" required placeholder="e.g. Milk, Eggs, Bread">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Category</label>
|
|
<select name="category" id="p_category" class="form-select">
|
|
<option value="Dairy">Dairy</option>
|
|
<option value="Meat">Meat</option>
|
|
<option value="Bakery">Bakery</option>
|
|
<option value="Produce">Produce</option>
|
|
<option value="Pantry">Pantry</option>
|
|
<option value="Frozen">Frozen</option>
|
|
</select>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Quantity</label>
|
|
<input type="text" name="quantity" id="p_quantity" class="form-control" placeholder="e.g. 2 liters, 1 pack">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label class="form-label">Expiration Date</label>
|
|
<input type="date" name="expiration_date" id="p_expiration" class="form-control">
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
|
|
<button type="submit" class="btn btn-primary">Save Product</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<canvas id="photoCanvas" style="display:none;"></canvas>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?= time() ?>"></script>
|
|
</body>
|
|
</html>
|