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

$vendorId = $_SESSION['vendor_id'];

// Get all orders for this vendor's products
$ordersQuery = mysqli_query($con, "SELECT DISTINCT o.*, p.productName, p.productImage1, p.productPrice,
                                   u.email, ud.name, ud.contactno, ud.shippingAddress, ud.shippingCity, ud.shippingState, ud.shippingPincode
                                   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' 
                                   ORDER BY o.orderDate DESC");

$totalOrders = mysqli_num_rows($ordersQuery);
?>

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

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
    <title>All 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-shopping-cart me-2"></i> All Orders</h1>
                            <p>Manage your product orders</p>
                        </div>
                    </div>

                    <!-- Stats Cards -->
                    <div class="row mb-4">
                        <div class="col-md-3 mb-3">
                            <div class="stat-card">
                                <div class="stat-icon primary">
                                    <i class="fas fa-shopping-bag"></i>
                                </div>
                                <div class="stat-value">
                                    <?php echo $totalOrders; ?>
                                </div>
                                <div class="stat-label">Total Orders</div>
                            </div>
                        </div>
                        <?php
                        // Get pending orders count
                        $pendingQuery = mysqli_query($con, "SELECT COUNT(DISTINCT o.id) as count 
                                                            FROM orders o 
                                                            JOIN products p ON o.productId = p.id 
                                                            WHERE p.vendor_id='$vendorId' 
                                                            AND (o.orderStatus IS NULL OR o.orderStatus = '')");
                        $pendingCount = mysqli_fetch_assoc($pendingQuery)['count'];
                        ?>
                        <div class="col-md-3 mb-3">
                            <div class="stat-card">
                                <div class="stat-icon warning">
                                    <i class="fas fa-clock"></i>
                                </div>
                                <div class="stat-value">
                                    <?php echo $pendingCount; ?>
                                </div>
                                <div class="stat-label">Pending Orders</div>
                            </div>
                        </div>
                    </div>

                    <!-- Orders Table -->
                    <div class="table-card">
                        <div class="card-header">
                            <h5 class="mb-0"><i class="fas fa-list me-2"></i>Order List</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>Status</th>
                                        <th>Date</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $cnt = 1;
                                    mysqli_data_seek($ordersQuery, 0); // Reset pointer
                                    while ($order = mysqli_fetch_array($ordersQuery)) {
                                        $statusClass = 'pending';
                                        $orderStatus = $order['orderStatus'] ?: 'Pending';

                                        if ($orderStatus == 'Delivered')
                                            $statusClass = 'delivered';
                                        elseif ($orderStatus == 'Dispatched')
                                            $statusClass = 'dispatched';
                                        elseif ($orderStatus == 'In Transit')
                                            $statusClass = 'dispatched';
                                        elseif ($orderStatus == 'Packed')
                                            $statusClass = 'packed';
                                        elseif ($orderStatus == 'Cancelled')
                                            $statusClass = 'cancelled';
                                        ?>
                                        <tr>
                                            <td>
                                                <?php echo $cnt; ?>
                                            </td>
                                            <td><strong>
                                                    <?php echo htmlentities($order['orderNumber'] ?: 'ORD-' . $order['id']); ?>
                                                </strong></td>
                                            <td>
                                                <div class="d-flex align-items-center">
                                                    <?php if ($order['productImage1']): ?>
                                                        <img src="../../__admin__/productimages/<?php echo $order['productId']; ?>/<?php echo $order['productImage1']; ?>"
                                                            style="width: 40px; height: 40px; object-fit: cover; border-radius: 4px; margin-right: 10px;">
                                                    <?php endif; ?>
                                                    <div>
                                                        <?php echo htmlentities($order['productName']); ?>
                                                    </div>
                                                </div>
                                            </td>
                                            <td>
                                                <?php echo htmlentities($order['name'] ?: $order['email']); ?>
                                                <br><small class="text-muted">
                                                    <?php echo htmlentities($order['contactno']); ?>
                                                </small>
                                            </td>
                                            <td>
                                                <?php echo $order['quantity']; ?>
                                            </td>
                                            <td><strong>₵
                                                    <?php echo number_format($order['productPrice'] * $order['quantity'], 2); ?>
                                                </strong></td>
                                            <td><span class="status-badge <?php echo $statusClass; ?>">
                                                    <?php echo $orderStatus; ?>
                                                </span></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" title="View Details">
                                                    <i class="fas fa-eye"></i>
                                                </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>