<?php
require_once 'includes/header.php';
require_once 'includes/sidebar.php';

// ডাটাবেস থেকে পরিসংখ্যান (Statistics) বের করা
try {
    $total_categories = $pdo->query("SELECT COUNT(*) FROM categories")->fetchColumn();
    $total_products = $pdo->query("SELECT COUNT(*) FROM products")->fetchColumn();
    $total_orders = $pdo->query("SELECT COUNT(*) FROM orders")->fetchColumn();
    $pending_orders = $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'Pending'")->fetchColumn();

    // সর্বশেষ ৫টি অর্ডার আনা
    $recent_orders = $pdo->query("SELECT * FROM orders ORDER BY id DESC LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("Database Error: " . $e->getMessage());
}
?>

<div class="mb-8">
    <h1 class="text-2xl md:text-3xl font-bold text-gray-800 font-bangla">ড্যাশবোর্ড ওভারভিউ</h1>
    <p class="text-gray-500 font-bangla text-sm mt-1">আপনার প্রিন্টিং বিজনেসের বর্তমান অবস্থা একনজরে</p>
</div>

<!-- Statistics Cards -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
    
    <!-- Pending Orders Card -->
    <div class="bg-white p-6 rounded-2xl shadow-sm border border-red-100 flex items-center justify-between hover:shadow-md transition">
        <div>
            <p class="text-sm text-gray-500 font-bangla font-semibold mb-1">পেন্ডিং অর্ডার</p>
            <h3 class="text-3xl font-bold text-red-500 en-font"><?= $pending_orders ?></h3>
        </div>
        <div class="w-14 h-14 bg-red-50 rounded-full flex items-center justify-center text-red-500 text-2xl">
            <i class="fa-solid fa-clock-rotate-left"></i>
        </div>
    </div>

    <!-- Total Orders Card -->
    <div class="bg-white p-6 rounded-2xl shadow-sm border border-blue-100 flex items-center justify-between hover:shadow-md transition">
        <div>
            <p class="text-sm text-gray-500 font-bangla font-semibold mb-1">মোট অর্ডার</p>
            <h3 class="text-3xl font-bold text-blue-600 en-font"><?= $total_orders ?></h3>
        </div>
        <div class="w-14 h-14 bg-blue-50 rounded-full flex items-center justify-center text-blue-600 text-2xl">
            <i class="fa-solid fa-cart-shopping"></i>
        </div>
    </div>

    <!-- Total Products Card -->
    <div class="bg-white p-6 rounded-2xl shadow-sm border border-green-100 flex items-center justify-between hover:shadow-md transition">
        <div>
            <p class="text-sm text-gray-500 font-bangla font-semibold mb-1">মোট প্রোডাক্ট</p>
            <h3 class="text-3xl font-bold text-green-500 en-font"><?= $total_products ?></h3>
        </div>
        <div class="w-14 h-14 bg-green-50 rounded-full flex items-center justify-center text-green-500 text-2xl">
            <i class="fa-solid fa-boxes-stacked"></i>
        </div>
    </div>

    <!-- Total Categories Card -->
    <div class="bg-white p-6 rounded-2xl shadow-sm border border-purple-100 flex items-center justify-between hover:shadow-md transition">
        <div>
            <p class="text-sm text-gray-500 font-bangla font-semibold mb-1">মোট ক্যাটাগরি</p>
            <h3 class="text-3xl font-bold text-purple-500 en-font"><?= $total_categories ?></h3>
        </div>
        <div class="w-14 h-14 bg-purple-50 rounded-full flex items-center justify-center text-purple-500 text-2xl">
            <i class="fa-solid fa-layer-group"></i>
        </div>
    </div>

</div>

<!-- Recent Orders Table -->
<div class="bg-white rounded-2xl shadow-sm border border-gray-100 overflow-hidden">
    <div class="p-6 border-b border-gray-100 flex justify-between items-center">
        <h2 class="text-lg font-bold text-gray-800 font-bangla">সাম্প্রতিক অর্ডার সমূহ</h2>
        <a href="manage-orders.php" class="text-sm text-blue-600 font-semibold hover:underline font-bangla">সব দেখুন</a>
    </div>
    
    <div class="overflow-x-auto">
        <table class="w-full text-left border-collapse">
            <thead>
                <tr class="bg-gray-50 text-gray-500 text-sm font-bangla uppercase tracking-wider">
                    <th class="p-4 font-semibold">অর্ডার আইডি</th>
                    <th class="p-4 font-semibold">কাস্টমারের নাম</th>
                    <th class="p-4 font-semibold">তারিখ</th>
                    <th class="p-4 font-semibold">মোট বিল</th>
                    <th class="p-4 font-semibold">স্ট্যাটাস</th>
                </tr>
            </thead>
            <tbody class="text-gray-700 text-sm font-bangla">
                <?php if(empty($recent_orders)): ?>
                    <tr>
                        <td colspan="5" class="p-8 text-center text-gray-400">এখনো কোনো অর্ডার আসেনি।</td>
                    </tr>
                <?php else: ?>
                    <?php foreach($recent_orders as $order): ?>
                        <tr class="border-b border-gray-50 hover:bg-gray-50 transition">
                            <td class="p-4 font-bold text-gray-800 en-font">#<?= htmlspecialchars($order['order_number']) ?></td>
                            <td class="p-4">
                                <?= htmlspecialchars($order['customer_name']) ?><br>
                                <span class="text-xs text-gray-400 en-font"><i class="fa-solid fa-phone mr-1"></i><?= htmlspecialchars($order['customer_phone']) ?></span>
                            </td>
                            <td class="p-4 en-font text-gray-500"><?= date('d M, Y', strtotime($order['created_at'])) ?></td>
                            <td class="p-4 font-bold text-primary en-font">৳ <?= number_format($order['total_amount'], 2) ?></td>
                            <td class="p-4">
                                <?php 
                                    if($order['status'] == 'Pending') {
                                        echo '<span class="bg-red-100 text-red-600 px-3 py-1 rounded-full text-xs font-bold">পেন্ডিং</span>';
                                    } elseif($order['status'] == 'Processing') {
                                        echo '<span class="bg-blue-100 text-blue-600 px-3 py-1 rounded-full text-xs font-bold">প্রসেসিং</span>';
                                    } elseif($order['status'] == 'Completed') {
                                        echo '<span class="bg-green-100 text-green-600 px-3 py-1 rounded-full text-xs font-bold">ডেলিভারড</span>';
                                    } else {
                                        echo '<span class="bg-gray-100 text-gray-600 px-3 py-1 rounded-full text-xs font-bold">ক্যানসেলড</span>';
                                    }
                                ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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