<?php

declare(strict_types=1);

require_once '../../private/db.php';
require_once '../../private/auth.php';

require_login();

$message = '';

$stmt = $pdo->query("SELECT * FROM categories ORDER BY name ASC");
$categories = $stmt->fetchAll();

if($_SERVER['REQUEST_METHOD'] === 'POST'){

$imageName = '';

if(isset($_FILES['product_image']) && $_FILES['product_image']['error'] === 0){

$allowed = [
'image/jpeg' => 'jpg',
'image/png' => 'png'
];

$fileType = mime_content_type($_FILES['product_image']['tmp_name']);

if(isset($allowed[$fileType])){

$extension = $allowed[$fileType];

$imageName =
time().'_'.
bin2hex(random_bytes(5)).
'.'.$extension;

$uploadPath =
__DIR__.'/images/'.$imageName;

move_uploaded_file(
$_FILES['product_image']['tmp_name'],
$uploadPath
);

}
}

$stmt = $pdo->prepare("
INSERT INTO products(
sku,
category_id,
name,
image,
price,
quantity,
active
)
VALUES(?,?,?,?,?,?,1)
");

$stmt->execute([
$_POST['sku'],
$_POST['category_id'],
$_POST['name'],
$imageName,
$_POST['price'],
$_POST['quantity']
]);

$message = 'Product added successfully.';
}

$stmt = $pdo->query("
SELECT
products.*,
categories.name AS category_name
FROM products
LEFT JOIN categories
ON products.category_id = categories.id
ORDER BY products.id DESC
");

$products = $stmt->fetchAll();

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Inventory Management</title>

<style>
body{
font-family:Arial;
background:#111827;
color:#fff;
margin:0;
padding:20px;
}
.form-box{
background:#1f2937;
padding:20px;
border-radius:10px;
margin-bottom:30px;
}
input,select{
width:100%;
padding:12px;
margin-bottom:12px;
border:none;
border-radius:6px;
}
button{
padding:12px 20px;
background:#2563eb;
border:none;
border-radius:6px;
color:#fff;
cursor:pointer;
}
table{
width:100%;
border-collapse:collapse;
background:#1f2937;
}
th,td{
padding:12px;
border:1px solid #374151;
}
img{
width:60px;
height:60px;
object-fit:cover;
border-radius:6px;
}
a{
color:#fff;
}
</style>

</head>
<body>

<h1>Inventory Management</h1>

<p><a href="dashboard.php">Dashboard</a></p>

<?php if($message): ?>
<p><?= htmlspecialchars($message) ?></p>
<?php endif; ?>

<div class="form-box">

<form method="POST" enctype="multipart/form-data">

<input type="text" name="sku" placeholder="SKU" required>

<input type="text" name="name" placeholder="Product Name" required>

<select name="category_id" required>

<option value="">Select Category</option>

<?php foreach($categories as $category): ?>

<option value="<?= (int)$category['id'] ?>">
<?= htmlspecialchars($category['name']) ?>
</option>

<?php endforeach; ?>

</select>

<input type="number" step="0.01" name="price" placeholder="Price" required>

<input type="number" name="quantity" placeholder="Quantity" required>

<label>Product Image</label>

<input
type="file"
name="product_image"
accept=".jpg,.jpeg,.png,image/jpeg,image/png"
required
>

<button type="submit">Add Product</button>

</form>

</div>

<table>

<tr>
<th>ID</th>
<th>Image</th>
<th>SKU</th>
<th>Category</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
</tr>

<?php foreach($products as $product): ?>

<tr>

<td><?= (int)$product['id'] ?></td>

<td>

<?php if($product['image']): ?>

<img
src="images/<?= htmlspecialchars($product['image']) ?>"
alt=""
>

<?php endif; ?>

</td>

<td><?= htmlspecialchars($product['sku']) ?></td>

<td><?= htmlspecialchars($product['category_name']) ?></td>

<td><?= htmlspecialchars($product['name']) ?></td>

<td>$<?= number_format((float)$product['price'],2) ?></td>

<td><?= (int)$product['quantity'] ?></td>

</tr>

<?php endforeach; ?>

</table>

</body>
</html>
