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

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

// Fetch products (all roles can select all products)
$products_res = $conn->query("SELECT id, product_name FROM products ORDER BY product_name");

// Fetch stores differently based on role
$role_id = $_SESSION['role_id'];
$user_id = $_SESSION['user_id'];

if ($role_id == 1) {
    // Admin: all stores
    $stores_res = $conn->query("SELECT id, store_name FROM stores ORDER BY store_name");
    $manager_store_id = null;
} else {
    // Manager: only their store
    $stmt = $conn->prepare("SELECT store_id FROM users WHERE id = ?");
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $stmt->bind_result($manager_store_id);
    $stmt->fetch();
    $stmt->close();

    // Get store info for display
    $store_stmt = $conn->prepare("SELECT id, store_name FROM stores WHERE id = ?");
    $store_stmt->bind_param("i", $manager_store_id);
    $store_stmt->execute();
    $stores_res = $store_stmt->get_result();
    // Only one store for manager
}

?>

<!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: 10;
    font-family: 'Segoe UI', sans-serif;
    background: url('../../images/profile.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: black;
    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>
<?php if (!empty($_SESSION['error'])): ?>
    <p class="error"><?= htmlspecialchars($_SESSION['error']) ?></p>
    <?php unset($_SESSION['error']); ?>
<?php endif; ?>

<?php if (!empty($_SESSION['success'])): ?>
    <p class="success"><?= htmlspecialchars($_SESSION['success']) ?></p>
    <?php unset($_SESSION['success']); ?>
<?php endif; ?>

<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>
        <?php if ($role_id == 1): ?>
            <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>
        <?php else: ?>
            <?php 
                // For manager, only one store
                $store = $stores_res->fetch_assoc(); 
            ?>
            <input type="hidden" name="store_id" value="<?= $store['id'] ?>">
            <input type="text" value="<?= htmlspecialchars($store['store_name']) ?>" disabled style="background: rgba(255,255,255,0.3); padding: 10px; border-radius: 8px; border:none; color:black;">
        <?php endif; ?>

        <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>
