<?php
session_start();
require_once('../includes/vendor-security.php');
include_once('../../includes/config.php');

$vendorId = $_SESSION['vendor_id'];

// Get completed orders
$ordersQuery = mysqli_query($con, "SELECT DISTINCT o.*, p.productName, p.productPrice, u.email, ud.name
                                   FROM orders o 
                                   JOIN products p ON o.productId = p.id 
                                   JOIN users u ON o.userId = u.id 
                                   LEFT JOIN user_details ud ON ud.user_id = u.id 
                                   WHERE p.vendor_id='$vendorId' 
                                   AND o.orderStatus = 'Delivered'
                                   ORDER BY o.orderDate DESC");
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <title>Completed Orders - Vendor Portal</title>
    <link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" />
    <link href="../../__admin__/css/styles.css" rel="stylesheet" />
    <link href="../../__admin__/css/admin-professional.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js"></script>
</head>

<body class="sb-nav-fixed">
    <?php include_once('../includes/header.php'); ?>

    <div id="layoutSidenav">
        <?php include_once('../includes/sidebar.php'); ?>

        <div id="layoutSidenav_content">
            <main>
                <div class="container-fluid px-4">
                    <div class="page-header mt-4">
                        <div>
                            <h1><i class="fas fa-check-circle me-2"></i> Completed Orders</h1>
                            <p>Successfully delivered orders</p>
                        </div>
                    </div>

                    <div class="table-card">
                        <div class="card-header">
                            <h5 class="mb-0"><i class="fas fa-list me-2"></i>Delivered Orders</h5>
                        </div>
                        <div class="card-body">
                            <table class="table table-hover" id="datatablesSimple">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Order No.</th>
                                        <th>Product</th>
                                        <th>Customer</th>
                                        <th>Qty</th>
                                        <th>Amount</th>
                                        <th>Order Date</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $cnt = 1;
                                    while ($order = mysqli_fetch_array($ordersQuery)) {
                                        ?>
                                        <tr>
                                            <td>
                                                <?php echo $cnt; ?>
                                            </td>
                                            <td><strong>
                                                    <?php echo htmlentities($order['orderNumber'] ?: 'ORD-' . $order['id']); ?>
                                                </strong></td>
                                            <td>
                                                <?php echo htmlentities($order['productName']); ?>
                                            </td>
                                            <td>
                                                <?php echo htmlentities($order['name'] ?: $order['email']); ?>
                                            </td>
                                            <td>
                                                <?php echo $order['quantity']; ?>
                                            </td>
                                            <td><strong>₵
                                                    <?php echo number_format($order['productPrice'] * $order['quantity'], 2); ?>
                                                </strong></td>
                                            <td>
                                                <?php echo date('M d, Y', strtotime($order['orderDate'])); ?>
                                            </td>
                                            <td>
                                                <a href="order-details.php?id=<?php echo $order['id']; ?>"
                                                    class="btn btn-sm btn-outline-primary">
                                                    <i class="fas fa-eye"></i> View
                                                </a>
                                            </td>
                                        </tr>
                                        <?php
                                        $cnt++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>

                </div>
            </main>
            <?php include_once('../includes/footer.php'); ?>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="../../__admin__/js/scripts.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest"></script>
    <script src="../../__admin__/js/datatables-simple-demo.js"></script>
</body>

</html>