<?php
require_once __DIR__ . '/../php/db_connect.php';

$orderId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($orderId <= 0) {
    echo "<p>Invalid order ID.</p>";
    exit;
}

$stmt = $conn->prepare("
    SELECT *
    FROM orders
    WHERE id = ?
    LIMIT 1
");
$stmt->bind_param("i", $orderId);
$stmt->execute();
$order = $stmt->get_result()->fetch_assoc();

if (!$order) {
    echo "<p>Order not found.</p>";
    exit;
}
?>

<div class="order-details-card">
    <h2>Order Details</h2>

    <p><strong>Order Code:</strong> <?php echo htmlspecialchars($order['order_code'] ?? ''); ?></p>
    <p><strong>Customer:</strong> <?php echo htmlspecialchars($order['customer_name'] ?? 'N/A'); ?></p>
    <p><strong>Email:</strong> <?php echo htmlspecialchars($order['customer_email'] ?? 'N/A'); ?></p>
    <p><strong>Contact:</strong> <?php echo htmlspecialchars($order['customer_mobile'] ?? 'N/A'); ?></p>

    <p><strong>Service Type:</strong>
        <?php echo htmlspecialchars($order['service_type'] ?? $order['service_category'] ?? 'N/A'); ?>
    </p>

    <p><strong>Item Name:</strong>
        <?php echo htmlspecialchars($order['item_name'] ?? $order['selected_service'] ?? 'N/A'); ?>
    </p>

    <p><strong>Quantity:</strong>
        <?php echo htmlspecialchars($order['quantity'] ?? $order['qty'] ?? 'N/A'); ?>
    </p>

    <p><strong>Size:</strong>
        <?php echo htmlspecialchars($order['size'] ?? 'N/A'); ?>
    </p>

    <p><strong>Unit Price:</strong>
        ₱<?php echo number_format((float)($order['unit_price'] ?? 0), 2); ?>
    </p>

    <p><strong>Order Date:</strong>
        <?php
        $orderDate = $order['order_date'] ?? '';
        echo (!empty($orderDate) && $orderDate !== '0000-00-00') ? htmlspecialchars($orderDate) : 'N/A';
        ?>
    </p>

    <p><strong>Due Date:</strong>
        <?php
        $dueDate = $order['due_date'] ?? '';
        echo (!empty($dueDate) && $dueDate !== '0000-00-00') ? htmlspecialchars($dueDate) : 'N/A';
        ?>
    </p>

    <p><strong>Status:</strong>
        <?php echo htmlspecialchars($order['status'] ?? 'N/A'); ?>
    </p>

    <p><strong>Payment Status:</strong>
        <?php echo htmlspecialchars($order['payment_status'] ?? 'N/A'); ?>
    </p>

    <p><strong>Notes / Instructions:</strong></p>
    <div class="notes-box">
        <?php echo nl2br(htmlspecialchars($order['notes'] ?? $order['instructions'] ?? 'No notes provided.')); ?>
    </div>

    <?php if (!empty($order['design_file'])): ?>
        <div class="design-wrap">
            <p><strong>Design Reference:</strong></p>
            <?php
            $file = htmlspecialchars($order['design_file']);
            $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
            ?>
            <?php if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'])): ?>
                <img src="../uploads/orders/<?php echo $file; ?>" alt="Design Reference">
            <?php else: ?>
                <a href="../uploads/orders/<?php echo $file; ?>" target="_blank">View Uploaded File</a>
            <?php endif; ?>
        </div>
    <?php else: ?>
        <p><strong>Design Reference:</strong> No file uploaded.</p>
    <?php endif; ?>

    <p class="price-text"><strong>Total Price:</strong>
        ₱<?php echo number_format((float)($order['total_price'] ?? $order['total_amount'] ?? 0), 2); ?>
    </p>
</div>

<style>
    .order-details-card {
    max-width: 900px;
    background: #ffffff;
    border-radius: 18px;
    padding: 32px;
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
    margin: 30px 0;
}

.order-details-card h2 {
    margin: 0 0 24px;
    font-size: 38px;
    font-weight: 800;
    color: #1f2937;
}

.order-details-card p {
    margin: 0 0 16px;
    font-size: 18px;
    line-height: 1.6;
    color: #374151;
}

.order-details-card strong {
    color: #111827;
    font-weight: 700;
}

.order-details-card .notes-box {
    padding: 14px 16px;
    background: #f3f4f6;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    margin: 10px 0 20px;
    font-size: 16px;
    line-height: 1.7;
    color: #374151;
    white-space: pre-wrap;
}

.order-details-card .design-wrap {
    margin: 20px 0;
}

.order-details-card .design-wrap p {
    margin-bottom: 10px;
}

.order-details-card img {
    max-width: 320px;
    width: 100%;
    height: auto;
    display: block;
    border-radius: 12px;
    border: 1px solid #e5e7eb;
    background: #f9fafb;
    padding: 6px;
}

.order-details-card a {
    color: #2563eb;
    font-weight: 600;
    text-decoration: none;
}

.order-details-card a:hover {
    text-decoration: underline;
}

.order-details-card .price-text {
    font-size: 20px;
    font-weight: 800;
    color: #0f172a;
}

@media (max-width: 768px) {
    .order-details-card {
        padding: 22px 18px;
        border-radius: 14px;
    }

    .order-details-card h2 {
        font-size: 30px;
    }

    .order-details-card p {
        font-size: 16px;
    }

    .order-details-card .price-text {
        font-size: 18px;
    }
}
</style>