<?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'])) {
    $_SESSION['error'] = "Invalid Purchase Order.";
    header("Location: /beauty_pos/purchase_orders/index.php");
    exit;
}

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

$orderQuery = mysqli_query($conn,"
SELECT
po.*,
s.supplier_name,
s.contact_person,
s.phone,
s.email
FROM purchase_orders po
LEFT JOIN suppliers s
ON po.supplier_id=s.id
WHERE po.id='$id'
LIMIT 1
");

if(mysqli_num_rows($orderQuery)==0){
    $_SESSION['error']="Purchase Order not found.";
    header("Location: /beauty_pos/purchase_orders/index.php");
    exit;
}

$order=mysqli_fetch_assoc($orderQuery);

$itemQuery=mysqli_query($conn,"
SELECT
pi.*,
p.product_name
FROM purchase_items pi
LEFT JOIN products p
ON pi.product_id=p.id
WHERE pi.purchase_id='$id'
ORDER BY pi.id ASC
");

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">
Purchase Order Details
</h3>

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

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

Back

</a>

</div>

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

<div class="card-body">

<div class="row g-3">

<div class="col-lg-4">
<strong>Purchase No</strong><br>
#PO-<?php echo $order['id']; ?>
</div>

<div class="col-lg-4">
<strong>Supplier</strong><br>
<?php echo htmlspecialchars($order['supplier_name']); ?>
</div>

<div class="col-lg-4">
<strong>Order Date</strong><br>
<?php echo date("d M Y",strtotime($order['order_date'])); ?>
</div>

<div class="col-lg-4">
<strong>Status</strong><br>

<?php

$badge="secondary";

if($order['status']=="Pending") $badge="warning";
if($order['status']=="Received") $badge="success";
if($order['status']=="Cancelled") $badge="danger";

?>

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

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

</span>

</div>

<div class="col-lg-4">
<strong>Total Amount</strong><br>

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

</div>

<div class="col-lg-4">

<strong>Invoice</strong><br>

<?php if(!empty($order['invoice'])){ ?>

<a
href="/beauty_pos/uploads/purchase_invoices/<?php echo $order['invoice'];?>"
target="_blank">

View Invoice

</a>

<?php }else{ ?>

-

<?php } ?>

</div>

</div>

</div>

</div>

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

<div class="card-header bg-white">

<h5 class="mb-0">

Purchased Products

</h5>

</div>

<div class="card-body">

<div class="table-responsive">

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

<thead>

<tr>

<th>#</th>

<th>Product</th>

<th>Quantity</th>

<th>Cost Price</th>

<th>Total</th>

</tr>

</thead>

<tbody>

<?php

$i=1;

while($item=mysqli_fetch_assoc($itemQuery)){

?>
<tr>

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

<td>
<?php echo htmlspecialchars($item['product_name']); ?>
</td>

<td>
<?php echo $item['quantity']; ?>
</td>

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

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

</tr>

<?php } ?>

</tbody>

<tfoot>

<tr>

<th colspan="4" class="text-end">

Grand Total

</th>

<th>

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

</th>

</tr>

</tfoot>

</table>

</div>

</div>

</div>

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

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

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

Edit

</a>

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

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

Delete

</a>

<a
href="javascript:window.print();"
class="btn btn-success">

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

Print

</a>

</div>

</div>

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