<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
require_once 'includes/db.php';

// লগইন চেক
if (!isset($_SESSION['customer_id'])) {
    header("Location: login.php");
    exit;
}

$user_id = $_SESSION['customer_id'];
$order_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

try {
    // ১. অর্ডারের সাধারণ তথ্য আনা (এবং নিশ্চিত করা যে এই অর্ডারটি এই ইউজারেরই)
    $stmt = $pdo->prepare("SELECT * FROM orders WHERE id = ? AND user_id = ?");
    $stmt->execute([$order_id, $user_id]);
    $order = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$order) {
        // যদি অর্ডার খুঁজে না পাওয়া যায় বা অন্য কারো হয়, তবে প্রোফাইলে পাঠিয়ে দাও
        header("Location: profile.php");
        exit;
    }

    // ২. অর্ডারের ভেতরে থাকা সব প্রোডাক্ট বা আইটেম আনা
    $stmt_items = $pdo->prepare("
        SELECT oi.*, p.name as product_name, p.image 
        FROM order_items oi 
        LEFT JOIN products p ON oi.product_id = p.id 
        WHERE oi.order_id = ?
    ");
    $stmt_items->execute([$order_id]);
    $order_items = $stmt_items->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    die("Error: " . $e->getMessage());
}

// স্ট্যাটাস কালার ফাংশন
function getStatusBadge($status) {
    switch ($status) {
        case 'Pending': return '<span class="bg-red-100 text-red-600 px-3 py-1 rounded-full text-xs font-bold">পেন্ডিং</span>';
        case 'Processing': return '<span class="bg-blue-100 text-blue-600 px-3 py-1 rounded-full text-xs font-bold">প্রসেসিং</span>';
        case 'Completed': return '<span class="bg-green-100 text-green-600 px-3 py-1 rounded-full text-xs font-bold">ডেলিভারড</span>';
        case 'Cancelled': return '<span class="bg-gray-100 text-gray-600 px-3 py-1 rounded-full text-xs font-bold">ক্যানসেলড</span>';
        default: return '<span class="bg-gray-100 text-gray-600 px-3 py-1 rounded-full text-xs font-bold">অজানা</span>';
    }
}

require_once 'includes/header.php';
?>

<!-- Breadcrumb -->
<div class="bg-gray-100 py-3 border-b border-gray-200">
    <div class="container mx-auto px-4">
        <ul class="flex items-center text-xs md:text-sm font-bangla text-gray-600">
            <li><a href="index.php" class="hover:text-primary">হোম</a></li>
            <li><span class="mx-2 text-gray-400">/</span></li>
            <li><a href="profile.php" class="hover:text-primary">প্রোফাইল</a></li>
            <li><span class="mx-2 text-gray-400">/</span></li>
            <li class="text-primary font-bold">অর্ডার বিস্তারিত</li>
        </ul>
    </div>
</div>

<section class="py-10 bg-gray-50 min-h-screen pb-24">
    <div class="container mx-auto px-4">
        
        <div class="max-w-4xl mx-auto">
            
            <!-- Order Status Header -->
            <div class="bg-white p-6 rounded-2xl shadow-sm border border-gray-100 mb-6 flex flex-col md:flex-row justify-between items-center gap-4">
                <div class="flex items-center gap-4">
                    <div class="w-12 h-12 bg-blue-50 text-primary rounded-full flex items-center justify-center text-2xl shadow-inner">
                        <i class="fa-solid fa-file-invoice"></i>
                    </div>
                    <div>
                        <h1 class="text-xl font-bold text-gray-800 font-bangla">অর্ডার নাম্বার: <span class="en-font text-primary">#<?= htmlspecialchars($order['order_number']) ?></span></h1>
                        <p class="text-gray-400 text-xs en-font italic mt-0.5">অর্ডারের তারিখ: <?= date('d M Y, h:i A', strtotime($order['created_at'])) ?></p>
                    </div>
                </div>
                <div>
                    <?= getStatusBadge($order['status']) ?>
                </div>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
                
                <!-- Left Column: Items List -->
                <div class="lg:col-span-2 space-y-4">
                    <div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
                        <div class="p-4 border-b border-gray-50 bg-slate-50 font-bold text-gray-700 font-bangla">
                            অর্ডারকৃত প্রোডাক্ট সমূহ
                        </div>
                        <div class="divide-y divide-gray-50">
                            <?php foreach ($order_items as $item): ?>
                                <div class="p-5 flex gap-4">
                                    <!-- Small Preview Image -->
                                    <div class="w-16 h-16 bg-gray-100 rounded-lg flex-shrink-0 flex items-center justify-center overflow-hidden border border-gray-200">
                                        <?php if($item['image']): ?>
                                            <img src="uploads/products/<?= htmlspecialchars($item['image']) ?>" class="w-full h-full object-cover">
                                        <?php else: ?>
                                            <i class="fa-solid fa-image text-gray-300 text-xl"></i>
                                        <?php endif; ?>
                                    </div>

                                    <!-- Product Info & Options -->
                                    <div class="flex-grow">
                                        <h4 class="font-bold text-gray-800 font-bangla text-base"><?= htmlspecialchars($item['product_name']) ?></h4>
                                        <div class="text-xs font-bold text-primary en-font mb-2">
                                            ৳ <?= number_format($item['unit_price'], 2) ?> x <?= $item['quantity'] ?>
                                        </div>

                                        <!-- Options Box -->
                                        <?php 
                                            $options = !empty($item['selected_options']) ? json_decode($item['selected_options'], true) : [];
                                            if(!empty($options)):
                                        ?>
                                            <div class="flex flex-wrap gap-2 mb-2">
                                                <?php foreach($options as $k => $v): ?>
                                                    <span class="text-[10px] bg-gray-100 text-gray-600 px-2 py-0.5 rounded border border-gray-200 font-bangla">
                                                        <strong class="text-gray-700"><?= htmlspecialchars($k) ?>:</strong> <?= htmlspecialchars($v) ?>
                                                    </span>
                                                <?php endforeach; ?>
                                            </div>
                                        <?php endif; ?>

                                        <!-- Design File Info -->
                                        <?php if(!empty($item['file_path'])): ?>
                                            <div class="text-[10px] text-green-600 font-bangla flex items-center gap-1 font-bold">
                                                <i class="fa-solid fa-circle-check"></i> আপনার ডিজাইন ফাইলটি সফলভাবে পাওয়া গেছে।
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                    <div class="text-right flex-shrink-0 font-bold text-gray-800 en-font text-sm">
                                        ৳ <?= number_format($item['unit_price'] * $item['quantity'], 2) ?>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                </div>

                <!-- Right Column: Delivery & Payment Summary -->
                <div class="lg:col-span-1 space-y-6">
                    <!-- Delivery Info -->
                    <div class="bg-white rounded-2xl shadow-sm border border-gray-100 p-5">
                        <h3 class="font-bold text-gray-800 font-bangla border-b pb-2 mb-3"><i class="fa-solid fa-truck text-primary mr-2"></i> ডেলিভারি তথ্য</h3>
                        <div class="space-y-3 font-bangla text-sm text-gray-600 leading-relaxed">
                            <div>
                                <p class="text-[10px] uppercase text-gray-400 font-bold">প্রাপক</p>
                                <p class="font-bold text-gray-800"><?= htmlspecialchars($order['customer_name']) ?></p>
                            </div>
                            <div>
                                <p class="text-[10px] uppercase text-gray-400 font-bold">ফোন</p>
                                <p class="font-bold text-primary en-font"><?= htmlspecialchars($order['customer_phone']) ?></p>
                            </div>
                            <div>
                                <p class="text-[10px] uppercase text-gray-400 font-bold">ঠিকানা</p>
                                <p class="bg-gray-50 p-2 rounded border border-gray-100 mt-1"><?= nl2br(htmlspecialchars($order['customer_address'])) ?></p>
                            </div>
                        </div>
                    </div>

                    <!-- Payment Summary -->
                    <div class="bg-white rounded-2xl shadow-sm border border-gray-100 p-5">
                        <h3 class="font-bold text-gray-800 font-bangla border-b pb-2 mb-3"><i class="fa-solid fa-receipt text-primary mr-2"></i> পেমেন্ট সামারি</h3>
                        <div class="space-y-2 text-sm font-bangla">
                            <?php 
                                $subtotal = 0;
                                foreach($order_items as $itm) $subtotal += ($itm['unit_price'] * $itm['quantity']);
                                $delivery_charge = $order['total_amount'] - $subtotal;
                            ?>
                            <div class="flex justify-between">
                                <span class="text-gray-500">প্রোডাক্ট টোটাল:</span>
                                <span class="en-font font-bold">৳ <?= number_format($subtotal, 2) ?></span>
                            </div>
                            <div class="flex justify-between">
                                <span class="text-gray-500">ডেলিভারি চার্জ:</span>
                                <span class="en-font font-bold">৳ <?= number_format($delivery_charge, 2) ?></span>
                            </div>
                            <div class="border-t pt-2 mt-2 flex justify-between text-base font-bold text-primary">
                                <span>সর্বমোট:</span>
                                <span class="en-font">৳ <?= number_format($order['total_amount'], 2) ?></span>
                            </div>
                        </div>
                        <div class="mt-4 bg-green-50 text-green-700 text-[10px] font-bold text-center py-2 rounded uppercase tracking-wider border border-green-100">
                            পেমেন্ট মেথড: ক্যাশ অন ডেলিভারি
                        </div>
                    </div>

                    <!-- Help Box -->
                    <a href="tel:<?= $contact_phone ?>" class="block bg-primary text-white p-4 rounded-2xl text-center shadow-lg shadow-blue-200 hover:bg-blue-800 transition">
                        <p class="text-xs opacity-80 font-bangla">অর্ডার নিয়ে কোনো প্রশ্ন?</p>
                        <p class="font-bold en-font text-lg"><i class="fa-solid fa-headset mr-2"></i> <?= $contact_phone ?></p>
                    </a>
                </div>

            </div>
        </div>
    </div>
</section>

<?php require_once 'includes/footer.php'; ?>