<?php
require 'config.php';
require_login();

$user_id = $_SESSION['user_id'];
$stmt = $pdo->prepare("SELECT id, name, email, balance FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    session_destroy();
    redirect('login.php');
    exit;
}

// Recent regular orders
$stmt = $pdo->prepare("
    SELECT o.*, sc.name AS subcat_name, s.name AS service_name
    FROM orders o
    JOIN subcategories sc ON o.subcategory_id = sc.id
    JOIN services s ON sc.service_id = s.id
    WHERE o.user_id = ?
    ORDER BY o.id DESC
    LIMIT 10
");
$stmt->execute([$user_id]);
$regular_orders = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Recent server copy orders
$stmt = $pdo->prepare("
    SELECT *, 'server_copy' as order_type, nameEn as service_name,
           'Server Copy' as subcat_name, 'completed' as status
    FROM server_copy
    WHERE user_id = ?
    ORDER BY id DESC
    LIMIT 10
");
$stmt->execute([$user_id]);
$server_copy_orders = $stmt->fetchAll(PDO::FETCH_ASSOC);

$all_orders = array_merge($regular_orders, $server_copy_orders);
usort($all_orders, function($a, $b) {
    return strtotime($b['created_at'] ?? $b['id']) - strtotime($a['created_at'] ?? $a['id']);
});
$recent_orders = array_slice($all_orders, 0, 8);

// Modern Status Badge Helper
function get_status_badge($status) {
    return match (strtolower($status)) {
        'completed', 'success' => '<span class="badge-modern bg-soft-success text-success"><i class="bi bi-check-circle-fill me-1"></i>সম্পন্ন</span>',
        'pending'    => '<span class="badge-modern bg-soft-warning text-warning"><i class="bi bi-clock-history me-1"></i>অপেক্ষমান</span>',
        'processing' => '<span class="badge-modern bg-soft-info text-info"><i class="bi bi-gear-wide-connected me-1"></i>প্রসেসিং</span>',
        'cancelled', 'rejected' => '<span class="badge-modern bg-soft-danger text-danger"><i class="bi bi-x-circle-fill me-1"></i>বাতিল</span>',
        default      => '<span class="badge-modern bg-soft-secondary text-secondary">'.h($status).'</span>',
    };
}

require_once 'header.php';
?>

<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Hind+Siliguri:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">

<style>
    :root {
        --primary-color: #4361ee;
        --secondary-color: #3f37c9;
        --accent-color: #4cc9f0;
        --bg-light: #f8f9fa;
        --card-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
    }

    body {
        font-family: 'Inter', 'Hind Siliguri', sans-serif;
        background-color: #f3f4f7;
        color: #334155;
    }

    /* Welcome Section */
    .welcome-card {
        background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
        border-radius: 20px;
        padding: 30px;
        color: white;
        position: relative;
        overflow: hidden;
        border: none;
        box-shadow: 0 15px 35px rgba(67, 97, 238, 0.3);
    }

    .welcome-card img.shape {
        position: absolute;
        right: -20px;
        bottom: -20px;
        opacity: 0.15;
        width: 180px;
    }

    .balance-title { font-size: 0.95rem; opacity: 0.85; font-weight: 400; }
    .balance-amount { font-size: 2.5rem; font-weight: 700; letter-spacing: -1px; }

    /* Stats & Info Cards */
    .glass-card {
        background: white;
        border-radius: 16px;
        border: 1px solid rgba(255,255,255,0.8);
        box-shadow: var(--card-shadow);
        transition: transform 0.3s ease;
    }

    .glass-card:hover { transform: translateY(-5px); }

    /* Modern Table Style */
    .table-container {
        background: white;
        border-radius: 20px;
        padding: 20px;
        box-shadow: var(--card-shadow);
    }

    .custom-table thead {
        background: #f8fafc;
    }

    .custom-table thead th {
        border: none;
        color: #64748b;
        font-weight: 600;
        text-transform: uppercase;
        font-size: 0.75rem;
        letter-spacing: 0.5px;
        padding: 15px;
    }

    .custom-table tbody tr {
        border-bottom: 1px solid #f1f5f9;
        transition: all 0.2s;
    }

    .custom-table tbody tr:hover { background-color: #f8faff; }

    /* Modern Badges */
    .badge-modern {
        padding: 6px 12px;
        border-radius: 10px;
        font-size: 0.8rem;
        font-weight: 600;
        display: inline-flex;
        align-items: center;
    }
    .bg-soft-success { background: #dcfce7; color: #15803d !important; }
    .bg-soft-warning { background: #fef9c3; color: #a16207 !important; }
    .bg-soft-info { background: #e0f2fe; color: #0369a1 !important; }
    .bg-soft-danger { background: #fee2e2; color: #b91c1c !important; }

    .action-btn {
        width: 38px;
        height: 38px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: 12px;
        background: #f1f5f9;
        color: var(--primary-color);
        transition: all 0.3s;
    }
    .action-btn:hover {
        background: var(--primary-color);
        color: white;
        transform: rotate(360deg);
    }

    /* Responsive Design */
    @media (max-width: 768px) {
        .balance-amount { font-size: 1.8rem; }
        .custom-table thead { display: none; }
        .custom-table tbody tr {
            display: block;
            padding: 15px;
            margin-bottom: 15px;
            border: 1px solid #eee;
            border-radius: 12px;
        }
        .custom-table td {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 0;
            border: none;
            text-align: right;
        }
        .custom-table td::before {
            content: attr(data-label);
            font-weight: 700;
            color: #64748b;
            font-size: 0.85rem;
        }
    }
</style>

<div class="container py-4">
    <div class="row align-items-center mb-4">
        <div class="col-6">
            <h4 class="fw-bold mb-0">ড্যাশবোর্ড</h4>
            <p class="text-muted small mb-0">স্বাগতম, <?php echo h($user['name']); ?>!</p>
        </div>
        <div class="col-6 text-end">
            <div class="dropdown">
                <button class="btn border-0 p-0" type="button" data-bs-toggle="dropdown">
                    <img src="https://ui-avatars.com/api/?name=<?php echo urlencode($user['name']); ?>&background=4361ee&color=fff" class="rounded-circle" width="45">
                </button>
            </div>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <div class="col-lg-8">
            <div class="welcome-card shadow-sm">
                <img src="https://www.transparentpng.com/download/shapes/abstract-shape-transparent-background-3.png" class="shape" alt="bg-shape">
                <div class="row align-items-center">
                    <div class="col-md-7">
                        <p class="balance-title mb-1">আপনার বর্তমান ব্যালেন্স</p>
                        <h1 class="balance-amount mb-3">৳ <?php echo number_format($user['balance'], 2); ?></h1>
                        <a href="recharge.php" class="btn btn-light rounded-pill px-4 fw-bold shadow-sm">
                            <i class="bi bi-plus-lg me-1"></i> টপ আপ করুন
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-4">
            <div class="glass-card p-4 h-100 d-flex flex-column justify-content-center">
                <div class="d-flex align-items-center mb-3">
                    <div class="bg-soft-info p-3 rounded-4 me-3">
                        <i class="bi bi-envelope-paper-fill fs-4 text-info"></i>
                    </div>
                    <div>
                        <p class="text-muted small mb-0">ইমেইল এড্রেস</p>
                        <h6 class="fw-bold mb-0"><?php echo h($user['email']); ?></h6>
                    </div>
                </div>
                <div class="d-flex align-items-center">
                    <div class="bg-soft-success p-3 rounded-4 me-3">
                        <i class="bi bi-shield-lock-fill fs-4 text-success"></i>
                    </div>
                    <div>
                        <p class="text-muted small mb-0">অ্যাকাউন্ট স্ট্যাটাস</p>
                        <h6 class="fw-bold mb-0 text-success">Active / ভেরিফাইড</h6>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="table-container shadow-sm border-0">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h5 class="fw-bold mb-0"><i class="bi bi-lightning-charge-fill text-warning me-2"></i>সাম্প্রতিক অর্ডারসমূহ</h5>
            <a href="history.php" class="btn btn-sm btn-light rounded-pill px-3">সবগুলো দেখুন</a>
        </div>

        <div class="table-responsive">
            <?php if (!empty($recent_orders)): ?>
                <table class="table custom-table align-middle">
                    <thead>
                        <tr>
                            <th>সার্ভিস তথ্য</th>
                            <th>ক্যাটাগরি</th>
                            <th class="text-center">স্ট্যাটাস</th>
                            <th class="text-end">অ্যাকশন</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recent_orders as $order): 
                            $is_server = isset($order['order_type']) && $order['order_type'] === 'server_copy';
                            $status = $order['status'];
                        ?>
                        <tr>
                            <td data-label="সার্ভিস তথ্য">
                                <div class="fw-bold text-dark"><?php echo h($order['service_name']); ?></div>
                                <div class="text-muted" style="font-size: 0.8rem;">আইডি: #<?php echo $order['id']; ?></div>
                            </td>
                            <td data-label="ক্যাটাগরি">
                                <div class="d-flex align-items-center">
                                    <div class="rounded-circle bg-light d-flex align-items-center justify-content-center me-2" style="width: 30px; height: 30px;">
                                        <i class="bi <?php echo $is_server ? 'bi-cpu text-purple' : 'bi-layers text-primary'; ?> small"></i>
                                    </div>
                                    <span class="small fw-medium"><?php echo h($order['subcat_name']); ?></span>
                                </div>
                            </td>
                            <td data-label="স্ট্যাটাস" class="text-md-center">
                                <?php echo get_status_badge($status); ?>
                            </td>
                            <td data-label="অ্যাকশন" class="text-end">
                                <?php if ($is_server): ?>
                                    <a href="server_copy_download.php?id=<?php echo $order['id']; ?>" class="action-btn" title="ডাউনলোড">
                                        <i class="bi bi-download"></i>
                                    </a>
                                <?php elseif (!empty($order['delivery_file'])): ?>
                                    <a href="uploads/<?php echo h($order['delivery_file']); ?>" download class="action-btn" title="ফাইল ডাউনলোড">
                                        <i class="bi bi-cloud-arrow-down-fill"></i>
                                    </a>
                                <?php else: ?>
                                    <span class="text-muted small fw-medium">প্রসেসিং...</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php else: ?>
                <div class="text-center py-5">
                    <img src="https://cdn-icons-png.flaticon.com/512/7486/7486744.png" width="80" class="mb-3 opacity-50" alt="no-data">
                    <p class="text-muted">আপনার বর্তমানে কোনো সক্রিয় অর্ডার নেই।</p>
                    <a href="index.php" class="btn btn-primary rounded-pill px-4 shadow">নতুন অর্ডার শুরু করুন</a>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

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