<?php
session_start();
include_once "../config/database.php";

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

/* ---------------- INPUTS ---------------- */
$search = isset($_GET['search']) ? mysqli_real_escape_string($conn, $_GET['search']) : "";
$category_id = isset($_GET['category']) ? (int)$_GET['category'] : 0;

/* ---------------- PAGINATION ---------------- */
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if($page < 1) $page = 1;

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

/* ---------------- CATEGORIES ---------------- */
$catQuery = "SELECT * FROM categories ORDER BY category_name ASC";
$catResult = mysqli_query($conn, $catQuery);

/* ---------------- WHERE CONDITIONS ---------------- */
$where = "WHERE 1=1";

if($search != ""){
    $where .= " AND products.product_name LIKE '%$search%'";
}

if($category_id > 0){
    $where .= " AND products.category_id = '$category_id'";
}

/* ---------------- TOTAL RECORDS ---------------- */
$totalQuery = "
SELECT COUNT(*) as total
FROM products
LEFT JOIN categories ON products.category_id = categories.id
LEFT JOIN brands ON products.brand_id = brands.id
$where
";

$totalResult = mysqli_query($conn, $totalQuery);
$totalRow = mysqli_fetch_assoc($totalResult);
$totalRecords = $totalRow['total'];

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

/* ---------------- PRODUCTS ---------------- */
$query = "
SELECT
products.*,
categories.category_name,
brands.brand_name
FROM products
LEFT JOIN categories ON products.category_id = categories.id
LEFT JOIN brands ON products.brand_id = brands.id
$where
ORDER BY products.id DESC
LIMIT $limit OFFSET $offset
";

$result = mysqli_query($conn, $query);

include_once "../includes/header.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">

<!-- CATEGORY FILTER BUTTONS -->
<div class="mb-3 d-flex flex-wrap gap-2">

<a href="/beauty_pos/products/index.php"
   class="btn btn-sm <?php if($category_id==0) echo 'btn-dark'; else echo 'btn-outline-dark'; ?>">
   All
</a>

<?php while($cat = mysqli_fetch_assoc($catResult)){ ?>
    <a href="/beauty_pos/products/index.php?category=<?php echo $cat['id']; ?>"
       class="btn btn-sm <?php if($category_id==$cat['id']) echo 'btn-dark'; else echo 'btn-outline-dark'; ?>">
        <?php echo $cat['category_name']; ?>
    </a>
<?php } ?>

</div>

<!-- HEADER -->
<div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0">Products</h4>

    <a href="/beauty_pos/products/add.php" class="btn btn-primary rounded-3">
        <i class="fa fa-plus"></i> Add Product
    </a>
</div>

<!-- SEARCH -->
<form method="GET" class="mb-3">
    <div class="row g-2">
        <div class="col-md-10 col-12">
            <input type="text" name="search"
                   value="<?php echo $search; ?>"
                   class="form-control rounded-3"
                   placeholder="Search products...">
        </div>

        <div class="col-md-2 col-12">
            <button class="btn btn-dark w-100 rounded-3">Search</button>
        </div>
    </div>
</form>

<!-- TABLE -->
<div class="card-box table-box rounded-4 shadow">
<div class="table-responsive">

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

<thead class="table-dark">
<tr>
    <th>#</th>
    <th>Product</th>
    <th>Category</th>
    <th>Brand</th>
    <th>Cost</th>
    <th>Sell</th>
    <th>Stock</th>
    <th>Expiry</th>
    <th>Status</th>
    <th>Action</th>
</tr>
</thead>

<tbody>

<?php if(mysqli_num_rows($result) > 0){ ?>
<?php $i=$offset+1; while($row=mysqli_fetch_assoc($result)){ ?>

<tr>
    <td><?php echo $i++; ?></td>

    <td>
        <?php if($row['image']){ ?>
            <img src="/beauty_pos/uploads/<?php echo $row['image']; ?>"
                 width="40" height="40" class="rounded me-2">
        <?php } ?>
        <?php echo $row['product_name']; ?>
    </td>

    <td><?php echo $row['category_name']; ?></td>
    <td><?php echo $row['brand_name']; ?></td>

    <td><?php echo $row['cost_price']; ?></td>
    <td><?php echo $row['selling_price']; ?></td>

    <td>
        <?php if($row['stock'] <= 5){ ?>
            <span class="badge bg-danger"><?php echo $row['stock']; ?></span>
        <?php } else { ?>
            <span class="badge bg-success"><?php echo $row['stock']; ?></span>
        <?php } ?>
    </td>

    <td><?php echo $row['expiry_date']; ?></td>

    <td>
        <?php if($row['status']==1){ ?>
            <span class="badge bg-success">Active</span>
        <?php } else { ?>
            <span class="badge bg-secondary">Inactive</span>
        <?php } ?>
    </td>

    <td>
        <a href="/beauty_pos/products/edit.php?id=<?php echo $row['id']; ?>"
           class="btn btn-sm btn-warning">
           <i class="fa fa-edit"></i>
        </a>

        <a href="/beauty_pos/products/delete.php?id=<?php echo $row['id']; ?>"
           class="btn btn-sm btn-danger"
           onclick="return confirm('Delete this product?')">
           <i class="fa fa-trash"></i>
        </a>
    </td>
</tr>

<?php } ?>
<?php } else { ?>

<tr>
    <td colspan="10" class="text-center py-4">No Products Found</td>
</tr>

<?php } ?>

</tbody>
</table>

</div>
</div>

<!-- PAGINATION -->
<nav class="mt-3">
<ul class="pagination justify-content-center">

<?php for($p=1; $p<=$totalPages; $p++){ ?>
<li class="page-item <?php if($p==$page) echo 'active'; ?>">
<a class="page-link"
   href="/beauty_pos/products/index.php?page=<?php echo $p; ?>&search=<?php echo $search; ?>&category=<?php echo $category_id; ?>">
   <?php echo $p; ?>
</a>
</li>
<?php } ?>

</ul>
</nav>

</div>

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

</div>