<?php
session_start();
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 1) {
    header("Location: ../../index.php");
    exit();
}

include('../../includes/config.php');

// Fetch products and stores
$products_res = $conn->query("SELECT id, product_name FROM products ORDER BY product_name");
$stores_res = $conn->query("SELECT id, store_name FROM stores ORDER BY store_name");

?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Adjust Stock - Digital POS</title>
<link rel="stylesheet" href="../../css/dashboard.css" />
<style>
body, html {
    margin: 0; padding: 0;
    font-family: 'Segoe UI', sans-serif;
    background: url('../../images/bg.jpg') no-repeat center center fixed;
    background-size: cover; color: white;
}
.overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.6); z-index: -1;
}
.content {
    margin: 80px auto; max-width: 600px; padding: 30px 40px;
    background: rgba(255,255,255,0.08); border-radius: 20px;
    backdrop-filter: blur(15px); box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}
h2 { text-align: center; margin-bottom: 25px; }
form label {
    display: block; margin: 15px 0 5px; font-weight: 600;
}
form input, form select, form textarea {
    width: 100%; padding: 10px; border-radius: 8px;
    border: none; background: rgba(255,255,255,0.2); color: white;
    font-size: 14px;
}
form button {
    margin-top: 25px; width: 100%; padding: 12px;
    background: #ff6f00; border: none; border-radius: 10px;
    color: white; font-size: 16px; cursor: pointer;
}
.error { color: #ff3c3c; margin-top: 15px; text-align:center; }
.success { color: #4caf50; margin-top: 15px; text-align:center; }
</style>
</head>
<body>
<div class="overlay"></div>
<div class="content">
    <h2>Adjust Stock</h2>
    <form action="process_adjust_stock.php" method="POST" id="adjustForm">
        <label for="product_id">Select Product</label>
        <select name="product_id" id="product_id" required>
            <option value="">-- Select Product --</option>
            <?php while ($p = $products_res->fetch_assoc()): ?>
                <option value="<?= $p['id'] ?>"><?= htmlspecialchars($p['product_name']) ?></option>
            <?php endwhile; ?>
        </select>

        <label for="store_id">Select Store</label>
        <select name="store_id" id="store_id" required>
            <option value="">-- Select Store --</option>
            <?php while ($s = $stores_res->fetch_assoc()): ?>
                <option value="<?= $s['id'] ?>"><?= htmlspecialchars($s['store_name']) ?></option>
            <?php endwhile; ?>
        </select>

        <label for="adjustment_qty">Adjustment Quantity (Use negative number to remove stock)</label>
        <input type="number" name="adjustment_qty" id="adjustment_qty" required min="-100000" max="100000" step="1" placeholder="E.g., 10 or -5">

        <label for="reason">Reason for Adjustment</label>
        <textarea name="reason" id="reason" rows="3" maxlength="255" required placeholder="Reason for stock change"></textarea>

        <button type="submit">Submit Adjustment</button>
    </form>
</div>
</body>
</html>
