<?php
// admin/order-details.php
require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/functions.php';

if (!isAdmin()) {
    header('Location: ../login.php');
    exit;
}

$id = intval($_GET['id'] ?? 0);
if (!$id) {
    header('Location: orders.php');
    exit;
}

// Get order
$stmt = $pdo->prepare("SELECT * FROM orders WHERE id = ?");
$stmt->execute([$id]);
$order = $stmt->fetch();

if (!$order) {
    header('Location: orders.php');
    exit;
}

// Get order items
$stmt = $pdo->prepare("SELECT * FROM order_items WHERE order_id = ?");
$stmt->execute([$id]);
$order_items = $stmt->fetchAll();

// Update order status
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) {
    $status = $_POST['order_status'] ?? 'pending';
    $stmt = $pdo->prepare("UPDATE orders SET order_status = ? WHERE id = ?");
    $stmt->execute([$status, $id]);
    $order['order_status'] = $status;
    $success = 'Order status updated successfully!';
}

include 'header.php';
?>

<div class="admin-card">
    <div class="d-flex justify-content-between align-items-center">
        <h5 class="card-title">Order Details</h5>
        <a href="orders.php" class="btn btn-secondary btn-sm">
            <i class="fa-solid fa-arrow-left"></i> Back to Orders
        </a>
    </div>
    
    <?php if (isset($success)): ?>
    <div class="alert alert-success"><?php echo $success; ?></div>
    <?php endif; ?>
    
    <div class="row mt-3">
        <div class="col-md-6">
            <h6>Order Information</h6>
            <table class="table table-bordered">
                <tr>
                    <th>Invoice No</th>
                    <td><?php echo $order['invoice_no']; ?></td>
                </tr>
                <tr>
                    <th>Date</th>
                    <td><?php echo date('d M Y, h:i A', strtotime($order['created_at'])); ?></td>
                </tr>
                <tr>
                    <th>Payment Method</th>
                    <td><?php echo strtoupper($order['payment_method']); ?></td>
                </tr>
                <tr>
                    <th>Status</th>
                    <td>
                        <span class="badge bg-<?php echo $order['order_status'] == 'delivered' ? 'success' : ($order['order_status'] == 'cancelled' ? 'danger' : 'warning'); ?>">
                            <?php echo ucfirst($order['order_status']); ?>
                        </span>
                    </td>
                </tr>
            </table>
        </div>
        <div class="col-md-6">
            <h6>Customer Information</h6>
            <table class="table table-bordered">
                <tr>
                    <th>Name</th>
                    <td><?php echo $order['name']; ?></td>
                </tr>
                <tr>
                    <th>Phone</th>
                    <td><?php echo $order['phone']; ?></td>
                </tr>
                <tr>
                    <th>Email</th>
                    <td><?php echo $order['email'] ?: 'N/A'; ?></td>
                </tr>
                <tr>
                    <th>Address</th>
                    <td><?php echo $order['address']; ?></td>
                </tr>
                <?php if ($order['city']): ?>
                <tr>
                    <th>City</th>
                    <td><?php echo $order['city']; ?></td>
                </tr>
                <?php endif; ?>
                <?php if ($order['note']): ?>
                <tr>
                    <th>Note</th>
                    <td><?php echo $order['note']; ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </div>
    </div>
    
    <h6 class="mt-3">Order Items</h6>
    <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Attributes</th>
                    <th>Qty</th>
                    <th>Price</th>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($order_items as $item): ?>
                <tr>
                    <td><?php echo $item['product_name']; ?></td>
                    <td>
                        <?php 
                        if ($item['attributes']) {
                            $attrs = json_decode($item['attributes'], true);
                            if ($attrs) {
                                foreach ($attrs as $key => $val) {
                                    echo $key . ': ' . $val . '<br>';
                                }
                            }
                        } else {
                            echo 'N/A';
                        }
                        ?>
                    </td>
                    <td><?php echo $item['quantity']; ?></td>
                    <td><?php echo number_format($item['price']); ?> TK</td>
                    <td><?php echo number_format($item['total']); ?> TK</td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="4" class="text-end"><strong>Subtotal</strong></td>
                    <td><strong><?php echo number_format($order['subtotal']); ?> TK</strong></td>
                </tr>
                <?php if ($order['shipping_charge'] > 0): ?>
                <tr>
                    <td colspan="4" class="text-end"><strong>Shipping</strong></td>
                    <td><strong><?php echo number_format($order['shipping_charge']); ?> TK</strong></td>
                </tr>
                <?php endif; ?>
                <?php if ($order['discount'] > 0): ?>
                <tr>
                    <td colspan="4" class="text-end"><strong>Discount</strong></td>
                    <td><strong>-<?php echo number_format($order['discount']); ?> TK</strong></td>
                </tr>
                <?php endif; ?>
                <tr>
                    <td colspan="4" class="text-end"><strong>Total</strong></td>
                    <td><strong><?php echo number_format($order['total']); ?> TK</strong></td>
                </tr>
            </tfoot>
        </table>
    </div>
    
    <div class="mt-3">
        <form method="POST" class="d-flex align-items-center gap-3">
            <label class="fw-bold">Update Status:</label>
            <select name="order_status" class="form-control" style="width:200px;">
                <option value="pending" <?php echo $order['order_status'] == 'pending' ? 'selected' : ''; ?>>Pending</option>
                <option value="processing" <?php echo $order['order_status'] == 'processing' ? 'selected' : ''; ?>>Processing</option>
                <option value="shipped" <?php echo $order['order_status'] == 'shipped' ? 'selected' : ''; ?>>Shipped</option>
                <option value="delivered" <?php echo $order['order_status'] == 'delivered' ? 'selected' : ''; ?>>Delivered</option>
                <option value="cancelled" <?php echo $order['order_status'] == 'cancelled' ? 'selected' : ''; ?>>Cancelled</option>
            </select>
            <button type="submit" name="update_status" class="btn btn-primary">Update</button>
        </form>
    </div>
</div>

<?php include 'footer.php'; ?>