<?php
/**
 * B2B Garments Marketplace - Admin Dashboard
 * File Path: /admin/dashboard.php
 * Standalone PHP & MySQL Admin Dashboard Page for XAMPP / Hosting Deployment
 */

session_start();

// Database Configuration & Connection (Auto-Fallback)
$dbHost = getenv('DB_HOST') ?: 'localhost';
$dbUser = getenv('DB_USER') ?: 'root';
$dbPass = getenv('DB_PASS') !== false ? getenv('DB_PASS') : '';
$dbName = getenv('DB_NAME') ?: 'b2b_marketplace';

$pdo = null;
try {
    $pdo = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8mb4", $dbUser, $dbPass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ]);
} catch (Exception $e) {
    // Database connection fallback for local preview or standalone demo
    $pdo = null;
}

// Sample Fallback Data if MySQL is offline or initializing
$adminUser = $_SESSION['user'] ?? [
    'name' => 'Super Administrator',
    'email' => 'admin@garments.com',
    'role' => 'Admin',
    'department' => 'Executive Management'
];

$stats = [
    'total_factories' => 24,
    'pending_approvals' => 3,
    'active_orders' => 18,
    'total_bids' => 56,
    'audit_compliance' => '99.4%'
];

$pendingQueue = [
    ['id' => 'FAC-101', 'name' => 'Apex Knitwear & Apparels', 'license' => 'TL-2026-9901', 'location' => 'Gazipur, Dhaka', 'type' => 'Knitwear', 'lines' => 18, 'status' => 'Pending'],
    ['id' => 'FAC-102', 'name' => 'Monarch Denim Works', 'license' => 'TL-2026-8812', 'location' => 'Narayanganj', 'type' => 'Denim', 'lines' => 12, 'status' => 'Pending'],
    ['id' => 'FAC-103', 'name' => 'Elegance Woven Fashion', 'license' => 'TL-2026-7734', 'location' => 'Chittagong EPZ', 'type' => 'Woven', 'lines' => 15, 'status' => 'Pending'],
];

$activityLogs = [
    ['tag' => 'Contract Match', 'color' => 'emerald', 'time' => '10:45 AM', 'text' => 'Contract Matched: "Apex Knitwear" awarded sourcing requirement for 25,000 pcs Hoodie (@ ৳850/pc)'],
    ['tag' => 'Proposal Bid', 'color' => 'purple', 'time' => '09:30 AM', 'text' => 'New Bid Submitted: "Monarch Denim" submitted FOB proposal for Denim Jacket order'],
    ['tag' => 'RFQ Posted', 'color' => 'amber', 'time' => '08:15 AM', 'text' => 'Sourcing Requirement Posted: "H&M Sourcing" posted contract for 50,000 pcs Polo Shirts'],
    ['tag' => 'Factory Added', 'color' => 'blue', 'time' => 'Yesterday', 'text' => 'Factory Registration: "Elegance Woven Fashion" registered with Trade License TL-2026-7734']
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard — B2B Garments Marketplace</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&family=JetBrains+Mono:wght@500;700&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Plus Jakarta Sans', sans-serif; }
        .font-mono-custom { font-family: 'JetBrains Mono', monospace; }
    </style>
</head>
<body class="bg-slate-950 text-slate-100 min-h-screen">

    <!-- Top Navigation Header -->
    <header class="border-b border-slate-800 bg-slate-900/90 backdrop-blur sticky top-0 z-50">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 h-16 flex items-center justify-between">
            <div class="flex items-center space-x-3">
                <div class="bg-amber-500 p-2 rounded-xl text-slate-950 font-black text-lg">
                    🛡️
                </div>
                <div>
                    <span class="font-extrabold text-base tracking-tight text-white">Garments B2B</span>
                    <span class="ml-2 text-[10px] bg-amber-500/10 text-amber-400 border border-amber-500/30 px-2 py-0.5 rounded font-mono-custom font-bold uppercase">Admin Console</span>
                </div>
            </div>

            <!-- Header Menu Nav -->
            <nav class="flex items-center space-x-1.5 overflow-x-auto">
                <a href="/admin/dashboard.php" class="px-3 py-1.5 rounded-lg text-xs font-bold bg-amber-500 text-slate-950 shadow transition">Dashboard</a>
                <a href="/admin/users.php" class="px-3 py-1.5 rounded-lg text-xs font-semibold text-slate-400 hover:text-white transition">User</a>
                <a href="/admin/employees.php" class="px-3 py-1.5 rounded-lg text-xs font-semibold text-slate-400 hover:text-white transition">Employee</a>
                <a href="/admin/factories.php" class="px-3 py-1.5 rounded-lg text-xs font-semibold text-slate-400 hover:text-white transition">My Factories</a>
                <a href="/admin/analysis.php" class="px-3 py-1.5 rounded-lg text-xs font-semibold text-slate-400 hover:text-white transition">Order Analysis</a>
                <a href="/admin/profile.php" class="px-3 py-1.5 rounded-lg text-xs font-semibold text-slate-400 hover:text-white transition">Profile Settings</a>
                <a href="/auth/login.php" class="px-3 py-1.5 rounded-lg text-xs font-bold text-rose-400 hover:text-rose-300 transition border border-rose-500/20">Logout</a>
            </nav>
        </div>
    </header>

    <!-- Main Container -->
    <main class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8 space-y-8">

        <!-- Admin Hero Banner -->
        <div class="bg-gradient-to-r from-slate-900 via-slate-900 to-amber-950/40 rounded-2xl p-6 border border-amber-500/20 shadow-xl flex flex-col md:flex-row justify-between items-start md:items-center gap-6">
            <div class="space-y-2">
                <div class="inline-flex items-center space-x-2 bg-amber-500/10 text-amber-400 px-3 py-1 rounded-full text-xs font-mono-custom font-bold border border-amber-500/20">
                    <span>STATUS: ACTIVE ADMIN SESSION</span>
                </div>
                <h1 className="text-2xl sm:text-3xl font-extrabold tracking-tight text-white">
                    Marketplace Master Control & Compliance Panel
                </h1>
                <p class="text-slate-400 text-xs sm:text-sm max-w-xl">
                    Welcome <span class="text-amber-400 font-semibold"><?= htmlspecialchars($adminUser['name']) ?></span>. Oversee factory trade approvals, audit contract executions, and monitor global marketplace activity.
                </p>
            </div>
            <div class="flex gap-3">
                <div class="bg-slate-900/90 border border-slate-800 px-4 py-2.5 rounded-xl text-center min-w-[110px]">
                    <p class="text-[10px] text-slate-500 uppercase tracking-wider font-mono-custom">Pending Queue</p>
                    <p class="text-2xl font-black text-amber-400"><?= $stats['pending_approvals'] ?></p>
                </div>
                <div class="bg-slate-900/90 border border-slate-800 px-4 py-2.5 rounded-xl text-center min-w-[110px]">
                    <p class="text-[10px] text-slate-500 uppercase tracking-wider font-mono-custom">Compliance</p>
                    <p class="text-2xl font-black text-emerald-400"><?= $stats['audit_compliance'] ?></p>
                </div>
            </div>
        </div>

        <!-- ROW 1: Factory Registration Status (Summary Cards) -->
        <div class="space-y-3">
            <h2 class="text-base font-bold text-white flex items-center gap-2">
                <span>Factory Registration Status</span>
                <span class="text-xs text-slate-400 font-normal">(Summary Cards)</span>
            </h2>
            <div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-5 gap-4">
                <div class="bg-slate-900 p-4 rounded-2xl border border-slate-800 shadow-sm">
                    <span class="text-[11px] font-bold text-slate-400 uppercase tracking-wider">All</span>
                    <p class="text-2xl font-black text-white mt-1"><?= $stats['total_factories'] ?? 24 ?></p>
                    <p class="text-[11px] text-slate-500 mt-2">Total Registered</p>
                </div>
                <div class="bg-slate-900 p-4 rounded-2xl border border-slate-800 shadow-sm">
                    <span class="text-[11px] font-bold text-amber-400 uppercase tracking-wider">Pending</span>
                    <p class="text-2xl font-black text-amber-400 mt-1"><?= $stats['pending_approvals'] ?? 3 ?></p>
                    <p class="text-[11px] text-slate-500 mt-2">Awaiting Approval</p>
                </div>
                <div class="bg-slate-900 p-4 rounded-2xl border border-slate-800 shadow-sm">
                    <span class="text-[11px] font-bold text-emerald-400 uppercase tracking-wider">Approved</span>
                    <p class="text-2xl font-black text-emerald-400 mt-1"><?= ($stats['total_factories'] ?? 24) - ($stats['pending_approvals'] ?? 3) ?></p>
                    <p class="text-[11px] text-slate-500 mt-2">Approved Units</p>
                </div>
                <div class="bg-slate-900 p-4 rounded-2xl border border-slate-800 shadow-sm">
                    <span class="text-[11px] font-bold text-sky-400 uppercase tracking-wider">On-Hold</span>
                    <p class="text-2xl font-black text-sky-400 mt-1">1</p>
                    <p class="text-[11px] text-slate-500 mt-2">Kept On Hold</p>
                </div>
                <div class="bg-slate-900 p-4 rounded-2xl border border-slate-800 shadow-sm">
                    <span class="text-[11px] font-bold text-rose-400 uppercase tracking-wider">Rejected</span>
                    <p class="text-2xl font-black text-rose-400 mt-1">0</p>
                    <p class="text-[11px] text-slate-500 mt-2">Rejected Applications</p>
                </div>
            </div>
        </div>

        <!-- ROW 2: Order & Contract Metrics -->
        <div class="space-y-3">
            <h2 class="text-base font-bold text-white">Order & Contract Metrics</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                <div class="bg-slate-900 p-5 rounded-2xl border border-slate-800 shadow-sm">
                    <div class="flex justify-between items-start pb-3 border-b border-slate-800">
                        <div>
                            <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Sourcing Contracts</span>
                            <p class="text-3xl font-black text-white mt-1"><?= $stats['active_orders'] ?? 18 ?></p>
                        </div>
                    </div>
                    <div class="grid grid-cols-2 gap-3 mt-4 text-xs">
                        <div class="bg-slate-950 p-3 rounded-xl border border-slate-800">
                            <span class="text-slate-400 block mb-1">Orders Processed</span>
                            <span class="text-base font-bold text-emerald-400">12</span>
                        </div>
                        <div class="bg-slate-950 p-3 rounded-xl border border-slate-800">
                            <span class="text-slate-400 block mb-1">Remaining Balance</span>
                            <span class="text-base font-bold text-indigo-400">6</span>
                        </div>
                    </div>
                </div>

                <div class="bg-slate-900 p-5 rounded-2xl border border-slate-800 shadow-sm">
                    <div class="flex justify-between items-start pb-3 border-b border-slate-800">
                        <div>
                            <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Capacity Bookings</span>
                            <p class="text-3xl font-black text-white mt-1">10</p>
                        </div>
                    </div>
                    <div class="grid grid-cols-2 gap-3 mt-4 text-xs">
                        <div class="bg-slate-950 p-3 rounded-xl border border-slate-800">
                            <span class="text-slate-400 block mb-1">Orders Processed</span>
                            <span class="text-base font-bold text-emerald-400">7</span>
                        </div>
                        <div class="bg-slate-950 p-3 rounded-xl border border-slate-800">
                            <span class="text-slate-400 block mb-1">Remaining Balance</span>
                            <span class="text-base font-bold text-purple-400">3</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- ROW 3: Financial Summary -->
        <div class="space-y-3">
            <h2 class="text-base font-bold text-white">Financial Summary</h2>
            <div class="bg-slate-900 p-6 rounded-2xl border border-slate-800 shadow-sm">
                <div class="pb-3 border-b border-slate-800">
                    <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Commission Overview</span>
                    <p class="text-3xl font-black text-white mt-1">৳1,45,000</p>
                    <p class="text-xs text-slate-400 mt-1">Marketplace Platform Commission Summary</p>
                </div>
                <div class="grid grid-cols-1 sm:grid-cols-3 gap-4 mt-4 text-xs">
                    <div class="bg-slate-950 p-4 rounded-xl border border-slate-800">
                        <span class="text-slate-400 block mb-1">Total Commission Earned</span>
                        <span class="text-lg font-bold text-white">৳1,45,000</span>
                    </div>
                    <div class="bg-slate-950 p-4 rounded-xl border border-slate-800">
                        <span class="text-slate-400 block mb-1">Total Amount Received</span>
                        <span class="text-lg font-bold text-emerald-400">৳1,01,500</span>
                    </div>
                    <div class="bg-slate-950 p-4 rounded-xl border border-slate-800">
                        <span class="text-slate-400 block mb-1">Outstanding Balance</span>
                        <span class="text-lg font-bold text-amber-400">৳43,500</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- ROW 4: Platform-Wide Statistics (Bottom Section) -->
        <div class="bg-slate-900 p-6 rounded-2xl border border-slate-800 shadow-xl space-y-4">
            <div class="flex items-center justify-between pb-3 border-b border-slate-800">
                <h2 class="text-base font-bold text-white">Platform-Wide Statistics</h2>
                <span class="text-xs text-slate-400">Global Overview</span>
            </div>
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
                <div class="bg-slate-950 p-4 rounded-xl border border-slate-800">
                    <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Users</span>
                    <p class="text-3xl font-black text-white mt-2">42</p>
                    <p class="text-[11px] text-slate-500 mt-1">Registered Platform Members</p>
                </div>
                <div class="bg-slate-950 p-4 rounded-xl border border-slate-800">
                    <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Factories</span>
                    <p class="text-3xl font-black text-white mt-2"><?= $stats['total_factories'] ?? 24 ?></p>
                    <p class="text-[11px] text-slate-500 mt-1">Registered Manufacturing Units</p>
                </div>
                <div class="bg-slate-950 p-4 rounded-xl border border-slate-800">
                    <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Orders</span>
                    <p class="text-3xl font-black text-white mt-2"><?= $stats['active_orders'] ?? 18 ?></p>
                    <p class="text-[11px] text-slate-500 mt-1">Total Posted Orders</p>
                </div>
                <div class="bg-slate-950 p-4 rounded-xl border border-slate-800">
                    <span class="text-xs font-bold text-slate-400 uppercase tracking-wider">Our Need Orders</span>
                    <p class="text-3xl font-black text-white mt-2">10</p>
                    <p class="text-[11px] text-slate-500 mt-1">Total Capacity Posts</p>
                </div>
            </div>
        </div>

    </main>

    <footer class="border-t border-slate-800 mt-12 py-6 text-center text-xs text-slate-500">
        <p>© 2026 B2B Garments Marketplace Administration. All rights reserved.</p>
    </footer>

</body>
</html>
