<?php
session_start();

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

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

$search = "";

if (isset($_GET['search'])) {
    $search = mysqli_real_escape_string($conn, trim($_GET['search']));
}

$limit = 10;

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

if ($page <= 0) {
    $page = 1;
}

$offset = ($page - 1) * $limit;

$where = "";

if ($search != "") {

    $where = " WHERE
                p.product_name LIKE '%$search%'
                OR p.sku LIKE '%$search%'
                OR p.barcode LIKE '%$search%' ";
}

$countQuery = mysqli_query($conn, "
SELECT COUNT(i.id) AS total
FROM inventory i
INNER JOIN products p ON p.id=i.product_id
$where
");

$countRow = mysqli_fetch_assoc($countQuery);

$totalRecords = $countRow['total'];

$totalPages = ceil($totalRecords / $limit);

$inventoryQuery = mysqli_query($conn,"
SELECT
i.*,
p.product_name,
p.sku,
p.barcode,
p.stock,
p.selling_price,
c.category_name
FROM inventory i

INNER JOIN products p
ON p.id=i.product_id

LEFT JOIN categories c
ON c.id=p.category_id

$where

ORDER BY p.product_name ASC

LIMIT $offset,$limit
");

$totalProducts=mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(*) total
FROM inventory
"));

$totalStock=mysqli_fetch_assoc(mysqli_query($conn,"
SELECT SUM(current_stock) total
FROM inventory
"));

$outStock=mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(*) total
FROM inventory
WHERE current_stock<=0
"));

$lowStock=mysqli_fetch_assoc(mysqli_query($conn,"
SELECT COUNT(*) total
FROM inventory
WHERE current_stock>0
AND current_stock<=10
"));

?>

<?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 flex-wrap justify-content-between align-items-center mb-4">

<div>

<h3 class="fw-bold mb-1">
Inventory Management
</h3>

<p class="text-muted mb-0">
Manage Product Stock
</p>

</div>

<div>

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

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

Add Inventory

</a>

</div>

</div>

<?php

if(isset($_GET['success'])){

?>

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

<?php echo htmlspecialchars($_GET['success']); ?>

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

</div>

<?php

}

?>

<?php

if(isset($_GET['error'])){

?>

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

<?php echo htmlspecialchars($_GET['error']); ?>

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

</div>

<?php

}

?>

<div class="row g-4 mb-4">

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

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

<div class="card-body">

<div class="d-flex justify-content-between">

<div>

<h6 class="text-muted">

Products

</h6>

<h2>

<?php echo $totalProducts['total']; ?>

</h2>

</div>

<div>

<i class="fas fa-box fa-2x text-primary"></i>

</div>

</div>

</div>

</div>

</div>

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

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

<div class="card-body">

<div class="d-flex justify-content-between">

<div>

<h6 class="text-muted">

Total Stock

</h6>

<h2>

<?php

echo $totalStock['total']=="" ? 0 : $totalStock['total'];

?>

</h2>

</div>

<div>

<i class="fas fa-layer-group fa-2x text-success"></i>

</div>

</div>

</div>

</div>

</div>

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

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

<div class="card-body">

<div class="d-flex justify-content-between">

<div>

<h6 class="text-muted">

Low Stock

</h6>

<h2>

<?php echo $lowStock['total']; ?>

</h2>

</div>

<div>

<i class="fas fa-exclamation-triangle fa-2x text-warning"></i>

</div>

</div>

</div>

</div>

</div>

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

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

<div class="card-body">

<div class="d-flex justify-content-between">

<div>

<h6 class="text-muted">

Out Of Stock

</h6>

<h2>

<?php echo $outStock['total']; ?>

</h2>

</div>

<div>

<i class="fas fa-times-circle fa-2x text-danger"></i>

</div>

</div>

</div>

</div>

</div>

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

    <div class="card-body">

        <form method="GET">

            <div class="row g-3 align-items-center">

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

                    <input
                        type="text"
                        name="search"
                        class="form-control"
                        placeholder="Search Product / SKU / Barcode"
                        value="<?php echo htmlspecialchars($search); ?>">

                </div>

                <div class="col-lg-2 col-md-3 col-6">

                    <button
                        type="submit"
                        class="btn btn-primary w-100">

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

                        Search

                    </button>

                </div>

                <div class="col-lg-2 col-md-3 col-6">

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

                        Reset

                    </a>

                </div>

            </div>

        </form>

    </div>

</div>

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

    <div class="card-body">

        <div class="table-responsive">

            <table class="table table-hover align-middle">

                <thead class="table-dark">

                <tr>

                    <th>#</th>

                    <th>Product</th>

                    <th>Category</th>

                    <th>SKU</th>

                    <th>Barcode</th>

                    <th>Current Stock</th>

                    <th>Selling Price</th>

                    <th>Status</th>

                    <th width="170">Action</th>

                </tr>

                </thead>

                <tbody>

                <?php

                if(mysqli_num_rows($inventoryQuery)>0){

                    $sr=$offset+1;

                    while($row=mysqli_fetch_assoc($inventoryQuery)){

                ?>

                <tr>

                    <td>

                        <?php echo $sr++; ?>

                    </td>

                    <td>

                        <strong>

                            <?php echo htmlspecialchars($row['product_name']); ?>

                        </strong>

                    </td>

                    <td>

                        <?php

                        echo $row['category_name']!=""
                        ? htmlspecialchars($row['category_name'])
                        : "-";

                        ?>

                    </td>

                    <td>

                        <?php

                        echo $row['sku']!=""
                        ? htmlspecialchars($row['sku'])
                        : "-";

                        ?>

                    </td>

                    <td>

                        <?php

                        echo $row['barcode']!=""
                        ? htmlspecialchars($row['barcode'])
                        : "-";

                        ?>

                    </td>

                    <td>

                        <strong>

                            <?php echo $row['current_stock']; ?>

                        </strong>

                    </td>

                    <td>

                        ₹ <?php echo number_format($row['selling_price'],2); ?>

                    </td>

                    <td>

                        <?php

                        if($row['current_stock']<=0){

                            echo '<span class="badge bg-danger">Out Of Stock</span>';

                        }

                        elseif($row['current_stock']<=10){

                            echo '<span class="badge bg-warning text-dark">Low Stock</span>';

                        }

                        else{

                            echo '<span class="badge bg-success">In Stock</span>';

                        }

                        ?>

                    </td>

                    <td>

                        <a

                        href="/beauty_pos/inventory/edit.php?id=<?php echo $row['id']; ?>"

                        class="btn btn-sm btn-warning">

                        <i class="fas fa-edit"></i>

                        </a>

                        <a

                        href="/beauty_pos/inventory/delete.php?id=<?php echo $row['id']; ?>"

                        class="btn btn-sm btn-danger"

                        onclick="return confirm('Delete this inventory record?')">

                        <i class="fas fa-trash"></i>

                        </a>

                    </td>

                </tr>

                <?php

                    }

                }

                else{

                ?>

                <tr>

                    <td colspan="9" class="text-center py-5">

                        No Inventory Found

                    </td>

                </tr>

                <?php

                }

                ?>

                </tbody>

            </table>

        </div>

    </div>

</div>
<?php

if($totalPages>1){

?>

<nav class="mt-4">

<ul class="pagination justify-content-center flex-wrap">

<?php

if($page>1){

?>

<li class="page-item">

<a
class="page-link"
href="?page=<?php echo $page-1; ?>&search=<?php echo urlencode($search); ?>">

Previous

</a>

</li>

<?php

}

?>

<?php

for($i=1;$i<=$totalPages;$i++){

?>

<li class="page-item <?php echo ($page==$i)?'active':''; ?>">

<a
class="page-link"
href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>">

<?php echo $i; ?>

</a>

</li>

<?php

}

?>

<?php

if($page<$totalPages){

?>

<li class="page-item">

<a
class="page-link"
href="?page=<?php echo $page+1; ?>&search=<?php echo urlencode($search); ?>">

Next

</a>

</li>

<?php

}

?>

</ul>

</nav>

<?php

}

?>

</div>

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

</div>