<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

// যদি লগিন করা না থাকে, তাহলে admin_login.php তে পাঠিয়ে দেবে
if (!isset($_SESSION['admin_logged_in']) || !isset($_SESSION['admin_user']) || !isset($_SESSION['admin_pass'])) {
    header("Location: admin_login.php");
    exit;
}

// লগআউট রিকোয়েস্ট
if (isset($_GET['action']) && $_GET['action'] === 'logout') {
    session_unset();
    session_destroy();
    header("Location: admin_login.php");
    exit;
}

// Config ফাইল লোড
$config_loaded = false;
$config_paths = ['Config.php', 'config.php', '../Config.php', '../config.php'];
foreach ($config_paths as $path) {
    if (file_exists($path)) {
        require_once $path;
        $config_loaded = true;
        break;
    }
}

if (!$config_loaded) {
    die("<div style='background:#ff4d4d; color:#fff; padding:20px; font-family:sans-serif; text-align:center; border-radius:12px; margin:50px auto; max-width:600px;'>
            <h2>Configuration File Missing!</h2>
            <p>আপনার Config.php ফাইলটি সিস্টেমে খুঁজে পাওয়া যায়নি।</p>
         </div>");
}

if (!isset($conn) && isset($con)) {
    $conn = $con;
}

if (!isset($conn) || !is_object($conn)) {
    die("Database Connection Failed");
}

// সেশন থেকে ইউজারনেম এবং পাসওয়ার্ড নেওয়া
$admin_user = $_SESSION['admin_user'];
$admin_pass = $_SESSION['admin_pass'];
$row = null;

// প্রতি রিফ্রেশে ডাটাবেজ থেকে লাইভ পারমিশন চেক করা হচ্ছে
$sql = "SELECT * FROM modarator_permissions WHERE username = ? AND password = ? LIMIT 1";
$stmt = @$conn->prepare($sql);
if ($stmt) {
    $stmt->bind_param("ss", $admin_user, $admin_pass);
    if (@$stmt->execute()) {
        $res = $stmt->get_result();
        if ($res && $res->num_rows > 0) {
            $row = $res->fetch_assoc();
        } else {
            // যদি ডাটাবেজ থেকে ইউজার বা পাসওয়ার্ড ডিলিট/চেঞ্জ করে দেওয়া হয়, তবে সাথে সাথে লগআউট
            session_unset();
            session_destroy();
            header("Location: admin_login.php");
            exit;
        }
    }
    $stmt->close();
}

// লাইভ পারমিশন ভেরিয়েবলে সেট করা (Yes হলে true, না হলে false)
$can_users_control    = strtolower(trim((string)($row['users_control'] ?? 'no'))) === 'yes';
$can_money_history    = strtolower(trim((string)($row['money_history'] ?? 'no'))) === 'yes';
$can_database_editing = strtolower(trim((string)($row['database_editing'] ?? 'no'))) === 'yes';
$can_notifications    = strtolower(trim((string)($row['notifications'] ?? 'no'))) === 'yes';
$can_match_rules      = strtolower(trim((string)($row['match_rules'] ?? 'no'))) === 'yes';
$can_refund           = strtolower(trim((string)($row['refund'] ?? 'no'))) === 'yes';
$can_match_editing    = strtolower(trim((string)($row['match_editing'] ?? 'no'))) === 'yes';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>Admin Workspace</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

    <style>
        :root {
            --bg: #f3f6fc;
            --surface: #ffffff;
            --surface-hover: #f8fafc;
            --card-border: #e8eef6;
            --card-border-hover: #d1dcf0;
            --text-primary: #0f172a;
            --text-muted: #64748b;
            --primary: linear-gradient(135deg, #4f46e5 0%, #3b82f6 100%);
            --primary-solid: #4f46e5;
            --primary-glow: rgba(79, 70, 229, 0.25);
            --danger: #ef4444;
            --danger-bg: rgba(239, 68, 68, 0.1);
            --success: #10b981;
            --shadow-sm: 0 4px 15px rgba(0, 0, 0, 0.03);
            --shadow-md: 0 10px 30px rgba(0, 0, 0, 0.06);
            --radius-lg: 24px;
            --radius-md: 16px;
            --radius-sm: 12px;
            --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
        }

        * { box-sizing: border-box; outline: none; -webkit-tap-highlight-color: transparent; }
        body { margin: 0; font-family: 'Plus Jakarta Sans', sans-serif; background-color: var(--bg); color: var(--text-primary); min-height: 100vh; }
        a { color: inherit; text-decoration: none; }

        .container { width: min(1200px, 95%); margin: 0 auto; padding: 30px 0 60px; }
        .topbar { display: flex; justify-content: space-between; align-items: center; background: var(--surface); padding: 16px 24px; border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); border: 1px solid var(--card-border); margin-bottom: 30px; flex-wrap: wrap; gap: 16px; }
        .brand { display: flex; align-items: center; gap: 14px; }
        .brand-icon { width: 48px; height: 48px; border-radius: var(--radius-sm); background: var(--primary); color: white; display: grid; place-items: center; font-size: 20px; box-shadow: 0 6px 16px var(--primary-glow); }
        .brand h1 { margin: 0; font-size: 20px; font-weight: 800; letter-spacing: -0.5px; }
        .brand p { margin: 4px 0 0; color: var(--text-muted); font-size: 13px; font-weight: 500; }
        .header-actions { display: flex; align-items: center; gap: 16px; }
        .profile-pill { display: flex; align-items: center; gap: 12px; padding: 6px 20px 6px 6px; background: var(--bg); border: 1px solid var(--card-border); border-radius: 999px; }
        .avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--primary); color: white; display: grid; place-items: center; font-size: 14px; }
        .profile-info strong { font-size: 14px; display: block; font-weight: 700; }
        .profile-info span { font-size: 11px; color: var(--text-muted); font-weight: 600; }
        .btn-logout { background: var(--danger-bg); color: var(--danger); border: none; padding: 10px 18px; border-radius: var(--radius-sm); font-weight: 700; font-size: 13px; cursor: pointer; display: flex; align-items: center; gap: 8px; transition: var(--transition); }
        .btn-logout:hover { background: var(--danger); color: white; transform: translateY(-2px); }

        .stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px; margin-bottom: 30px; }
        .stat { background: var(--surface); padding: 24px; border-radius: var(--radius-md); border: 1px solid var(--card-border); box-shadow: var(--shadow-sm); display: flex; align-items: center; gap: 20px; transition: var(--transition); }
        a.stat.interactive:hover { transform: translateY(-4px); box-shadow: var(--shadow-md); border-color: var(--primary-solid); }
        .stat-icon { width: 56px; height: 56px; border-radius: var(--radius-sm); background: var(--bg); color: var(--primary-solid); display: grid; place-items: center; font-size: 24px; }
        .stat-details .value { font-size: 28px; font-weight: 800; line-height: 1; margin-bottom: 6px; color: var(--text-primary); }
        .stat-details .label { color: var(--text-muted); font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; }

        .section-title { margin: 40px 0 20px; font-size: 20px; font-weight: 800; color: var(--text-primary); }
        .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 20px; }
        .card { background: var(--surface); padding: 24px; border-radius: var(--radius-md); border: 1px solid var(--card-border); box-shadow: var(--shadow-sm); transition: var(--transition); display: flex; flex-direction: column; position: relative; overflow: hidden; }
        .card::before { content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 4px; background: var(--primary); opacity: 0; transition: var(--transition); }
        .card:hover { transform: translateY(-5px); border-color: var(--card-border-hover); box-shadow: var(--shadow-md); }
        .card:hover::before { opacity: 1; }
        .card-header { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
        .card-icon { width: 48px; height: 48px; border-radius: 12px; background: var(--bg); color: var(--primary-solid); display: grid; place-items: center; font-size: 20px; }
        .card h4 { margin: 0; font-size: 16px; font-weight: 800; color: var(--text-primary); }
        .card p { margin: 0 0 20px; color: var(--text-muted); font-size: 14px; line-height: 1.6; font-weight: 500; flex-grow: 1; }
        .card-footer { padding-top: 16px; border-top: 1px solid var(--card-border); display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--text-muted); font-weight: 700; transition: var(--transition); }
        .card:hover .card-footer { color: var(--primary-solid); }
        .card:hover .card-footer i { transform: translateX(4px); }
        .card-footer i { transition: transform 0.3s ease; }

        @media (max-width: 768px) { .topbar { flex-direction: column; align-items: stretch; } .header-actions { justify-content: space-between; width: 100%; } .stat-grid { grid-template-columns: 1fr; } }
        @media (max-width: 480px) { .profile-info { display: none; } .profile-pill { padding: 8px; } .container { padding: 16px 0 40px; } }
    </style>
</head>
<body>

    <div class="container">
        <div class="topbar">
            <div class="brand">
                <div class="brand-icon"><i class="fa-solid fa-server"></i></div>
                <div>
                    <h1>System Workspace</h1>
                    <p>Central Control Panel</p>
                </div>
            </div>

            <div class="header-actions">
                <div class="profile-pill">
                    <div class="avatar"><i class="fa-solid fa-user-shield"></i></div>
                    <div class="profile-info">
                        <strong><?php echo htmlspecialchars($admin_user); ?></strong>
                        <span>Active Moderator</span>
                    </div>
                </div>

                <a href="index.php?action=logout" class="btn-logout">
                    <i class="fa-solid fa-arrow-right-from-bracket"></i> <span>Logout</span>
                </a>
            </div>
        </div>

        <div class="stat-grid">
            
            <?php if ($can_users_control): ?>
            <a href="UserInfoAdm.php" class="stat interactive" title="Click to view User Info">
                <div class="stat-icon"><i class="fa-solid fa-users"></i></div>
                <div class="stat-details">
                    <div class="value">128</div>
                    <div class="label">Total User</div>
                </div>
            </a>
            <?php endif; ?>

            <?php if ($can_money_history): ?>
            <div class="stat">
                <div class="stat-icon" style="color: #f59e0b;"><i class="fa-solid fa-file-invoice-dollar"></i></div>
                <div class="stat-details">
                    <div class="value">54</div>
                    <div class="label">Pending Orders</div>
                </div>
            </div>
            
            <div class="stat">
                <div class="stat-icon" style="color: #10b981;"><i class="fa-solid fa-wallet"></i></div>
                <div class="stat-details">
                    <div class="value">৳ 8,400</div>
                    <div class="label">All Balance</div>
                </div>
            </div>
            <?php endif; ?>

        </div>

        <div class="section-title">Core Modules</div>
        <div class="grid">
            
            <?php if ($can_match_editing): ?>
            <a class="card" href="match_schedule_manager.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-calendar-days"></i></div>
                    <h4>Match Schedule</h4>
                </div>
                <p>Configure and manage match schedules, timings, and brackets.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>

            <a class="card" href="freefire_manage.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-gamepad"></i></div>
                    <h4>Create Match</h4>
                </div>
                <p>Deploy new match rooms and define operational parameters.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>
            <?php endif; ?>

            <?php if ($can_database_editing): ?>
            <a class="card" href="category_manage.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-layer-group"></i></div>
                    <h4>Category Configuration</h4>
                </div>
                <p>Structure game categories, types, and classification tags.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>
            <?php endif; ?>

            <?php if ($can_money_history): ?>
            <a class="card" href="bunasex_dashboard.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-gift"></i></div>
                    <h4>Bonus Management</h4>
                </div>
                <p>Monitor promotional balances and user reward distributions.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>
            
            <a class="card" href="bunasex_topup_dashboard.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-credit-card"></i></div>
                    <h4>Topup Gateway</h4>
                </div>
                <p>Review and verify user deposit transactions and gateways.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>

            <a class="card" href="bunasex_withdraw_dashboard.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-building-columns"></i></div>
                    <h4>Withdrawals</h4>
                </div>
                <p>Approve, decline, and track user payout requests.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>

            <a class="card" href="bunasex_orders_dashboard.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-box-open"></i></div>
                    <h4>Order Fulfillment</h4>
                </div>
                <p>Track in-game orders, item delivery, and overall fulfillment.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>
            <?php endif; ?>

            <?php if ($can_refund): ?>
            <a class="card" href="bunasex_refund_dashboard.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-rotate-left"></i></div>
                    <h4>Refund System</h4>
                </div>
                <p>Process automated and manual user refund requests securely.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>
            <?php endif; ?>

            <?php if ($can_notifications): ?>
            <a class="card" href="SendNotice.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-bullhorn"></i></div>
                    <h4>Push Notifications</h4>
                </div>
                <p>Broadcast system alerts and push notifications to users.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>
            <?php endif; ?>

            <?php if ($can_match_rules): ?>
            <a class="card" href="match_rules.php">
                <div class="card-header">
                    <div class="card-icon"><i class="fa-solid fa-gavel"></i></div>
                    <h4>Policy & Rules</h4>
                </div>
                <p>Update compliance policies, game rules, and terms of service.</p>
                <div class="card-footer">
                    <span>Manage Module</span>
                    <i class="fa-solid fa-arrow-right"></i>
                </div>
            </a>
            <?php endif; ?>

        </div>
    </div>

</body>
</html>