<?php
require_once __DIR__ . '/../include/db.php';
$res = $conn->query("
    SELECT id, order_date, buyer_name, style_no, po_no, total_qty, status 
    FROM orders 
    WHERE status != 'delivered'
    ORDER BY id DESC
");

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Orders</title>
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">



<style>
/* Global */
body {
    background: #f2f4f8;
    margin: 0;
    font-family: Arial, sans-serif;
}

/* Container */
.container {
    max-width: 1100px;
    margin: 30px auto;
    background: #fff;
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 3px 20px rgba(0,0,0,0.08);
}

/* Header */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 25px;
}

.header h2 {
    margin: 0;
    color: #222;
}

.addBtn {
    background: #28a745;
    padding: 10px 15px;
    color: #fff;
    border-radius: 6px;
    text-decoration: none;
    font-size: 14px;
}

/* Table Wrapper */
.tableWrapper {
    overflow-x: auto;
}

/* Table */
table {
    width: 100%;
    border-collapse: collapse;
    min-width: 900px;
}

table th {
    background: #007bff;
    color: #fff;
    padding: 5px 5px;
    text-align: left;
    font-size: 15px;
}

table td {
    padding: 5px 5px;
    border-bottom: 1px solid #e5e5e5;
    border-right:1px solid #e5e5e5;
}

tr:hover td {
    background: #f6faff;
}

/* Action Buttons */
.actionBtn {
    background: #007bff;
    color: #fff;
    padding: 6px 10px;
    border-radius: 4px;
    text-decoration: none;
    font-size: 13px;
}

.actionBtn.edit { background: #17a2b8; }
.actionBtn.assign { background: #6f42c1; }
.actionBtn.delete { background: #dc3545; }

/* Status Badge */
.badge {
    padding: 5px 10px;
    border-radius: 6px;
    font-size: 13px;
    color: #fff;
}

.badge.pending { background: #6c757d; }
.badge.partial { background: #ffc107; color:#000; }
.badge.completed { background: #28a745; }
.badge.cancelled { background: #dc3545; }

@media(max-width: 600px){
    .header { flex-direction: column; gap: 15px; }
}
</style>

</head>
<body>

<div class="container">

    <div class="header">
    <h2>Nava Poly Orders Managements</h2> 
    <div>
        <a href="add_order.php" class="addBtn me-2">+ Add New Order</a>
        <a href="delivery_history.php" class="addBtn" style="background:#0d6efd;">
            📦 Delivery History
        </a>
    </div>
</div>


    <div class="tableWrapper">
        <table>
            <tr>
            
                <th> Order Date</th>
                <th>Buyer</th>
                <th>Style</th>
                <th>PO</th>
                <th>Total Qty</th>
                <th>Status</th>
                <th>Actions</th>
            </tr>

            <?php while($r = $res->fetch_assoc()): ?>
            <tr>
                
                <td><?= $r['order_date'] ?></td>
                <td><?= htmlspecialchars($r['buyer_name']) ?></td>
                <td><?= htmlspecialchars($r['style_no']) ?></td>
                <td><?= htmlspecialchars($r['po_no']) ?></td>
                <td><?= $r['total_qty'] ?></td>

                <td>
                    <span class="badge <?= $r['status'] ?>">
                        <?= ucfirst($r['status']) ?>
                    </span>
                </td>

                <td>
                    <a class="actionBtn" href="view_order.php?id=<?= $r['id'] ?>">View</a>
                    <a class="actionBtn assign" href="assign_order.php?id=<?= $r['id'] ?>">Assign</a>
                    

                    <a class="actionBtn delete" href="delete_order.php?id=<?= $r['id'] ?>" onclick="return confirm('Delete this order?')">Delete</a>
                </td>
            </tr>
            <?php endwhile; ?>

        </table>
    </div>

</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
