<?php
require_once "auth-check.php";

$conn = new mysqli("localhost", "isiuser", "isi490pass", "isi490db");

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$result = $conn->query("SELECT * FROM purchase_orders ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html>
<head>
<title>Purchase Orders</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>
body {
    margin: 0;
    font-family: 'Segoe UI', Arial, sans-serif;
    background: #0a0f1c;
    color: #f1f5f9;
}

.wrapper {
    max-width: 1200px;
    margin: 40px auto;
    padding: 25px;
}

.card {
    background: #111827;
    border: 1px solid #1f2937;
    border-radius: 24px;
    padding: 34px;
    box-shadow: 0 20px 45px rgba(0,0,0,0.45);
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 28px;
}

h1 {
    margin: 0;
    font-size: 34px;
}

.subtitle {
    color: #94a3b8;
    margin-top: 8px;
}

.btn {
    background: #2563eb;
    color: white;
    padding: 11px 16px;
    border-radius: 12px;
    text-decoration: none;
    font-weight: 700;
    display: inline-flex;
    gap: 8px;
    align-items: center;
    font-size: 14px;
}

.btn:hover {
    background: #1d4ed8;
}

.back {
    background: #1e293b;
    color: #cbd5e1;
}

.back:hover {
    background: #334155;
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;
}

th {
    background: #0f172a;
    color: #94a3b8;
    padding: 14px;
    text-align: left;
    font-size: 12px;
    text-transform: uppercase;
    border-bottom: 1px solid #1f2937;
    letter-spacing: 0.4px;
}

td {
    padding: 16px 14px;
    border-bottom: 1px solid #1f2937;
    color: #e2e8f0;
    vertical-align: middle;
    font-size: 14px;
}

tr:hover {
    background: rgba(255,255,255,0.03);
}

.po-number {
    font-weight: 800;
    color: white;
    line-height: 1.4;
}

.product-name {
    font-weight: 700;
    color: white;
}

.status-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 125px;
    padding: 8px 12px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 800;
    text-align: center;
    white-space: nowrap;
}

.pending {
    background: rgba(245,158,11,0.16);
    color: #fcd34d;
    border: 1px solid rgba(245,158,11,0.30);
}

.approved {
    background: rgba(22,163,74,0.16);
    color: #86efac;
    border: 1px solid rgba(22,163,74,0.30);
}

.action-cell {
    text-align: center;
}

.view-btn {
    background: #2563eb;
    color: white;
    padding: 9px 13px;
    border-radius: 10px;
    text-decoration: none;
    font-size: 12px;
    font-weight: 800;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.view-btn:hover {
    background: #1d4ed8;
}

.empty {
    text-align: center;
    padding: 30px;
    color: #94a3b8;
}

.footer {
    text-align: center;
    margin-top: 24px;
    color: #64748b;
    font-size: 13px;
}

.col-po { width: 15%; }
.col-product { width: 18%; }
.col-reorder { width: 14%; }
.col-cost { width: 13%; }
.col-status { width: 16%; }
.col-created { width: 12%; }
.col-date { width: 15%; }
.col-action { width: 10%; }
</style>
</head>

<body>

<div class="wrapper">
<div class="card">

<div class="header">
    <div>
        <h1><i class="fa-solid fa-file-invoice"></i> Live Purchase Orders</h1>
        <p class="subtitle">Saved purchase orders generated by the Smart Recommendation Engine.</p>
    </div>

    <a class="btn back" href="hello-db.php">
        <i class="fa-solid fa-arrow-left"></i> Back to Dashboard
    </a>
</div>

<div class="table-wrap">
<table>
<tr>
    <th class="col-po">PO Number</th>
    <th class="col-product">Product</th>
    <th class="col-reorder">Suggested Reorder</th>
    <th class="col-cost">Total Cost</th>
    <th class="col-status">Status</th>
    <th class="col-created">Created By</th>
    <th class="col-date">Date</th>
    <th class="col-action">Action</th>
</tr>

<?php
if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {

        $statusClass = ($row["status"] == "Approved") ? "approved" : "pending";

        echo "<tr>";
        echo "<td><div class='po-number'>" . htmlspecialchars($row["po_number"]) . "</div></td>";
        echo "<td><div class='product-name'>" . htmlspecialchars($row["product_name"]) . "</div></td>";
        echo "<td>" . (int)$row["suggested_reorder"] . " units</td>";
        echo "<td>$" . number_format((float)$row["estimated_cost"], 2) . "</td>";
        echo "<td><span class='status-pill " . $statusClass . "'>" . htmlspecialchars($row["status"]) . "</span></td>";
        echo "<td>" . htmlspecialchars($row["created_by"]) . "</td>";
        echo "<td>" . htmlspecialchars($row["created_at"]) . "</td>";
        echo "<td class='action-cell'>
                <a class='view-btn' href='purchase-order.php?po_id=" . $row["id"] . "'>
                    <i class='fa-solid fa-eye'></i> View PO
                </a>
              </td>";
        echo "</tr>";
    }
} else {
    echo "<tr><td colspan='8' class='empty'>No purchase orders have been generated yet.</td></tr>";
}

$conn->close();
?>

</table>
</div>

<div class="footer">
InventoryPro • Live Purchase Order Registry
</div>

</div>
</div>

</body>
</html>

