<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

$product_id = intval($_GET['product_id'] ?? 0);

if($product_id <= 0){
    echo "<div class='alert alert-danger text-center mt-5'>Invalid Product</div>";
    exit;
}

/* ================= GET CURRENT DATA ================= */
$check = $conn->query("
    SELECT 
        p.name,
        COALESCE(SUM(hki.quantity),0) AS issued_qty,
        COALESCE(SUM(CASE WHEN hki.status='used' THEN hki.quantity ELSE 0 END),0) AS used_qty
    FROM products p
    LEFT JOIN hk_requisition_items hki 
        ON p.id = hki.product_id
    WHERE p.id = '$product_id'
    GROUP BY p.id
")->fetch_assoc();

$product_name = $check['name'] ?? 'Unknown Product';
$issued_qty = $check['issued_qty'] ?? 0;
$used_qty = $check['used_qty'] ?? 0;

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

    $qty_used = intval($_POST['qty_used']);

    if($qty_used < 0){
        echo "<div class='alert alert-danger text-center'>Invalid quantity</div>";
    } else {

        $conn->query("
            UPDATE hk_requisition_items 
            SET 
                status = 'used',
                quantity_used = '$qty_used',
                used_at = NOW(),
                updated_at = NOW()
            WHERE product_id = '$product_id'
        ");

        echo "
        <div class='container text-center mt-5'>
            <div class='card shadow p-4'>
                <h3 class='text-success'>✔ Updated Successfully</h3>
                <p><b>$product_name</b> usage has been updated.</p>

                <p>Redirecting back...</p>
            </div>
        </div>

        <script>
            setTimeout(function(){
                window.location.href = 'house_keeping_inventory.php';
            }, 2000);
        </script>";
        exit;
    }
}
?>

<style>
body {
    background: #f4f6f9;
}

.container-box {
    max-width: 550px;
    margin: 50px auto;
}

.card {
    border: none;
    border-radius: 15px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.1);
}

.header-bar {
    background: linear-gradient(135deg, #0d6efd, #6610f2);
    color: white;
    padding: 15px;
    border-radius: 15px 15px 0 0;
    text-align: center;
}

.info-box {
    background: #f8f9fa;
    padding: 10px;
    border-radius: 10px;
    margin-bottom: 10px;
    font-size: 14px;
}

.btn {
    border-radius: 10px;
    width: 100%;
    padding: 10px;
    font-weight: bold;
}

.back-btn {
    position: absolute;
    left: 20px;
    top: 20px;
}
</style>

<!-- BACK BUTTON -->
<a href="javascript:history.back()" class="btn btn-secondary back-btn">← Back</a>

<div class="container container-box">

    <div class="card">

        <div class="header-bar">
            <h4>Update Usage</h4>
            <small><?= htmlspecialchars($product_name) ?></small>
        </div>

        <div class="card-body">

            <!-- INFO -->
            <div class="info-box">
                <b>Issued Qty:</b> <?= $issued_qty ?> <br>
                <b>Already Used:</b> <?= $used_qty ?>
            </div>

            <form method="POST">

                <div class="mb-3">
                    <label>Quantity Used</label>
                    <input type="number" 
                           name="qty_used" 
                           class="form-control" 
                           min="0" 
                           max="<?= $issued_qty ?>"
                           placeholder="Enter used quantity"
                           required>
                </div>

                <button type="submit" class="btn btn-success">
                    💾 Save & Update
                </button>

            </form>

        </div>
    </div>
</div>