<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: /beauty_pos/login.php");
    exit;
}

include_once "../config/database.php";

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: /beauty_pos/inventory/index.php?error=Invalid Inventory Record.");
    exit;
}

$id = (int)$_GET['id'];

$getInventory = mysqli_query($conn,"
SELECT
i.*,
p.product_name,
p.sku
FROM inventory i
INNER JOIN products p
ON p.id=i.product_id
WHERE i.id='$id'
");

if(mysqli_num_rows($getInventory)==0){

    header("Location: /beauty_pos/inventory/index.php?error=Inventory record not found.");
    exit;

}

$inventory=mysqli_fetch_assoc($getInventory);

$error="";

if(isset($_POST['update_inventory'])){

    $current_stock=(int)$_POST['current_stock'];

    if($current_stock<0){

        $error="Stock cannot be negative.";

    }else{

        mysqli_begin_transaction($conn);

        try{

            $updateInventory=mysqli_query($conn,"
            UPDATE inventory
            SET
            current_stock='$current_stock',
            updated_at=CURRENT_TIMESTAMP
            WHERE id='$id'
            ");

            if(!$updateInventory){
                throw new Exception();
            }

            $updateProduct=mysqli_query($conn,"
            UPDATE products
            SET stock='$current_stock'
            WHERE id='".$inventory['product_id']."'
            ");

            if(!$updateProduct){
                throw new Exception();
            }

            mysqli_commit($conn);

            header("Location: /beauty_pos/inventory/index.php?success=Inventory updated successfully.");
            exit;

        }catch(Exception $e){

            mysqli_rollback($conn);

            $error="Unable to update inventory.";

        }

    }

}

?>

<?php include_once "../includes/header.php"; ?>

<?php include_once "../includes/sidebar.php"; ?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid py-4">

<div class="d-flex justify-content-between align-items-center mb-4">

<div>

<h3 class="fw-bold mb-1">

Edit Inventory

</h3>

<p class="text-muted mb-0">

Update Product Stock

</p>

</div>

<div>

<a
href="/beauty_pos/inventory/index.php"
class="btn btn-secondary rounded-3">

<i class="fas fa-arrow-left me-2"></i>

Back

</a>

</div>

</div>

<?php if($error!=""){ ?>

<div class="alert alert-danger alert-dismissible fade show">

<?php echo $error; ?>

<button
type="button"
class="btn-close"
data-bs-dismiss="alert"></button>

</div>

<?php } ?>

<form method="POST">

<div class="card border-0 shadow rounded-4">

<div class="card-body">

<div class="row g-3">

<div class="col-lg-6 col-md-6 col-12">

<label class="form-label">

Product

</label>

<input
type="text"
class="form-control"
value="<?php echo htmlspecialchars($inventory['product_name']); ?>"
readonly>

</div>

<div class="col-lg-6 col-md-6 col-12">

<label class="form-label">

SKU

</label>

<input
type="text"
class="form-control"
value="<?php echo htmlspecialchars($inventory['sku']); ?>"
readonly>

</div>

<div class="col-lg-6 col-md-6 col-12">

<label class="form-label">

Current Stock

<span class="text-danger">*</span>

</label>

<input
type="number"
name="current_stock"
class="form-control"
min="0"
required
value="<?php echo $inventory['current_stock']; ?>">

</div>
<div class="col-12">

<hr>

<div class="d-flex flex-wrap gap-2">

<button
type="submit"
name="update_inventory"
class="btn btn-primary">

<i class="fas fa-save me-2"></i>

Update Inventory

</button>

<a
href="/beauty_pos/inventory/index.php"
class="btn btn-secondary">

Cancel

</a>

</div>

</div>

</div>

</div>

</div>

</form>

</div>

<?php include_once "../includes/footer.php"; ?>

</div>