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

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

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// Fetch entry details
$stmt = $conn->prepare("
    SELECT * FROM stock_entries WHERE id = ?
");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$entry = $result->fetch_assoc();
$stmt->close();

if (!$entry) {
    echo "Stock entry not found.";
    exit;
}

// Fetch products and stores for dropdowns
$products = $conn->query("SELECT id, product_name FROM products");
$stores = $conn->query("SELECT id, store_name FROM stores");

// Handle update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $product_id = $_POST['product_id'];
    $store_id = $_POST['store_id'];
    $quantity = $_POST['quantity'];
    $cost_price = $_POST['cost_price'];
    $purchase_date = $_POST['purchase_date'];
    $remarks = $_POST['remarks'];
    $updated_by = $_SESSION['user_id'];

    $update_stmt = $conn->prepare("
        UPDATE stock_entries 
        SET product_id = ?, store_id = ?, quantity = ?, cost_price = ?, purchase_date = ?, remarks = ?, updated_by = ?, updated_at = NOW()
        WHERE id = ?
    ");
    $update_stmt->bind_param("iiidssii", $product_id, $store_id, $quantity, $cost_price, $purchase_date, $remarks, $updated_by, $id);

    if ($update_stmt->execute()) {
        header("Location: stock_entries.php?msg=Stock+entry+updated+successfully");
        exit();
    } else {
        $error = "Error updating stock entry: " . $update_stmt->error;
    }
    $update_stmt->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Stock Entry</title>
    <link rel="stylesheet" href="../../css/dashboard.css">
    <style>
        body, html {
            font-family: 'Segoe UI', sans-serif;
            background: url('../../images/bg.jpg') no-repeat center center fixed;
            background-size: cover;
            margin: 0; padding: 0;
            color: white;
        }

        .overlay {
            position: fixed; top: 0; left: 0; width: 100%; height: 100%;
            background: rgba(0,0,0,0.6); z-index: -1;
        }

        .container {
            max-width: 600px;
            margin: 100px auto;
            padding: 30px;
            background: rgba(255,255,255,0.08);
            border-radius: 16px;
            backdrop-filter: blur(12px);
            box-shadow: 0 8px 20px rgba(0,0,0,0.5);
        }

        h2 { text-align: center; margin-bottom: 25px; }

        form label {
            display: block;
            margin-top: 15px;
            font-weight: bold;
        }

        form input, form select, form textarea {
            width: 100%;
            padding: 10px;
            margin-top: 5px;
            border-radius: 10px;
            border: none;
            background: rgba(255,255,255,0.1);
            color: white;
            box-shadow: inset 0 0 5px rgba(255,255,255,0.2);
        }

        form button {
            margin-top: 25px;
            background: #ff6f00;
            color: white;
            border: none;
            padding: 12px 20px;
            border-radius: 10px;
            font-weight: bold;
            cursor: pointer;
            width: 100%;
        }

        form button:hover {
            background: #e65c00;
        }

        .error { color: red; text-align: center; margin-top: 10px; }
    </style>
</head>
<body>
<div class="overlay"></div>

<div class="container">
    <h2>Edit Stock Entry</h2>

    <?php if (isset($error)) echo "<div class='error'>{$error}</div>"; ?>

    <form method="POST">
        <label for="product_id">Product</label>
        <select name="product_id" required>
            <?php while ($row = $products->fetch_assoc()): ?>
                <option value="<?= $row['id'] ?>" <?= $entry['product_id'] == $row['id'] ? 'selected' : '' ?>>
                    <?= htmlspecialchars($row['product_name']) ?>
                </option>
            <?php endwhile; ?>
        </select>

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

        <label for="quantity">Quantity</label>
        <input type="number" name="quantity" value="<?= $entry['quantity'] ?>" required>

        <label for="cost_price">Cost Price</label>
        <input type="number" step="0.01" name="cost_price" value="<?= $entry['cost_price'] ?>" required>

        <label for="purchase_date">Purchase Date</label>
        <input type="date" name="purchase_date" value="<?= $entry['purchase_date'] ?>" required>

        <label for="remarks">Remarks</label>
        <textarea name="remarks"><?= htmlspecialchars($entry['remarks']) ?></textarea>

        <button type="submit">Update Stock Entry</button>
    </form>
</div>

</body>
</html>
