<?php
session_start();

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

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

$error = "";
$success = "";

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

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

    if ($product_id <= 0) {

        $error = "Please select product.";

    } elseif ($current_stock < 0) {

        $error = "Stock cannot be negative.";

    } else {

        $check = mysqli_query($conn,"
        SELECT id
        FROM inventory
        WHERE product_id='$product_id'
        ");

        if(mysqli_num_rows($check)>0){

            $error="Inventory already exists for selected product.";

        }else{

            mysqli_begin_transaction($conn);

            try{

                $insert=mysqli_query($conn,"
                INSERT INTO inventory
                (
                    product_id,
                    current_stock,
                    warehouse_id
                )
                VALUES
                (
                    '$product_id',
                    '$current_stock',
                    NULL
                )
                ");

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

                $update=mysqli_query($conn,"
                UPDATE products
                SET stock='$current_stock'
                WHERE id='$product_id'
                ");

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

                mysqli_commit($conn);

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

            }catch(Exception $e){

                mysqli_rollback($conn);

                $error="Unable to save inventory.";

            }

        }

    }

}

$productQuery=mysqli_query($conn,"
SELECT
id,
product_name,
sku,
stock
FROM products
WHERE status=1
ORDER BY product_name ASC
");

?>

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

Add Inventory

</h3>

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

Create Inventory Record

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

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

</label>

<select
name="product_id"
class="form-select"
required>

<option value="">

Select Product

</option>

<?php

while($product=mysqli_fetch_assoc($productQuery)){

?>

<option value="<?php echo $product['id']; ?>">

<?php

echo htmlspecialchars($product['product_name']);

if($product['sku']!=""){

echo " (".$product['sku'].")";

}

?>

</option>

<?php

}

?>

</select>

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

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

<hr>

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

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

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

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