<?php
session_start();

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

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

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

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

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

$search = "";

$where = "";

if (isset($_GET['search']) && trim($_GET['search']) != "") {

    $search = mysqli_real_escape_string($conn, trim($_GET['search']));

    $where = " WHERE
                s.supplier_name LIKE '%$search%'
                OR po.status LIKE '%$search%'
                OR po.id LIKE '%$search%'
                OR po.invoice LIKE '%$search%'
              ";
}

$totalQuery = mysqli_query($conn,"
SELECT COUNT(*) AS total
FROM purchase_orders po
LEFT JOIN suppliers s
ON po.supplier_id=s.id
$where
");

$totalRow = mysqli_fetch_assoc($totalQuery);

$totalRecords = $totalRow['total'];

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

$query = mysqli_query($conn,"
SELECT
po.*,
s.supplier_name
FROM purchase_orders po

LEFT JOIN suppliers s
ON po.supplier_id=s.id

$where

ORDER BY po.id DESC

LIMIT $start,$limit
");

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 py-4">

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

<h3 class="fw-bold mb-3 mb-md-0">
Purchase Orders
</h3>

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

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

New Purchase Order

</a>

</div>

<?php

if(isset($_SESSION['success']))
{
?>

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

<?php
echo $_SESSION['success'];
unset($_SESSION['success']);
?>

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

</div>

<?php
}
?>

<?php

if(isset($_SESSION['error']))
{
?>

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

<?php
echo $_SESSION['error'];
unset($_SESSION['error']);
?>

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

</div>

<?php
}
?>

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

<div class="card-body">

<form method="GET">

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

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

<input
type="text"
name="search"
class="form-control"
placeholder="Search Purchase Order..."
value="<?php echo htmlspecialchars($search); ?>">

</div>

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

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

<i class="fa fa-search"></i>

Search

</button>

</div>

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

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

Reset

</a>

</div>

</div>

</form>

<div class="table-responsive">

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

<thead class="table-light">

<tr>

<th>ID</th>

<th>Supplier</th>

<th>Order Date</th>

<th>Total</th>

<th>Status</th>

<th>Invoice</th>

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

</tr>

</thead>

<tbody>

<?php

if(mysqli_num_rows($query)>0)
{

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

$statusClass="secondary";

if($row['status']=="Pending")
{
$statusClass="warning";
}

if($row['status']=="Received")
{
$statusClass="success";
}

if($row['status']=="Cancelled")
{
$statusClass="danger";
}

?>
<tr>

<td>#PO-<?php echo $row['id']; ?></td>

<td>
<?php
echo !empty($row['supplier_name'])
? htmlspecialchars($row['supplier_name'])
: "-";
?>
</td>

<td>
<?php
echo !empty($row['order_date'])
? date("d M Y", strtotime($row['order_date']))
: "-";
?>
</td>

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

<td>

<span class="badge bg-<?php echo $statusClass; ?>">

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

</span>

</td>

<td>

<?php

if(!empty($row['invoice']))
{

?>

<a
href="/beauty_pos/uploads/purchase_invoices/<?php echo $row['invoice']; ?>"
target="_blank"
class="btn btn-sm btn-outline-primary">

<i class="fa fa-file-invoice"></i>

View

</a>

<?php

}
else
{

echo "-";

}

?>

</td>

<td>

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

<a
href="/beauty_pos/purchase_orders/view.php?id=<?php echo $row['id']; ?>"
class="btn btn-info btn-sm">

<i class="fa fa-eye"></i>

</a>

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

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

</a>

<a
href="/beauty_pos/purchase_orders/delete.php?id=<?php echo $row['id']; ?>"
class="btn btn-danger btn-sm"
onclick="return confirm('Are you sure you want to delete this purchase order?');">

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

</a>

</div>

</td>

</tr>

<?php

}

}
else
{

?>

<tr>

<td colspan="7" class="text-center py-4">

No Purchase Orders Found.

</td>

</tr>

<?php

}

?>

</tbody>

</table>

</div>

<?php

if($totalPages>1)
{

?>

<nav class="mt-4">

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

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

}

?>

</ul>

</nav>

<?php

}

?>

</div>

</div>

</div>

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