<?php
/**
 * Customer Account Dashboard - PREMIUM VERSION
 * Digital Marketplace
 */

require_once __DIR__ . '/../config/database.php';

// Require login
require_login();

$page_title = 'My Account';

$user_id = get_current_user_id();
$user = get_current_user_data($pdo);

// Get order stats
$stmt = $pdo->prepare("SELECT COUNT(*) FROM orders WHERE user_id = ? AND payment_status = 'completed'");
$stmt->execute([$user_id]);
$order_count = $stmt->fetchColumn();

$stmt = $pdo->prepare("SELECT COALESCE(SUM(total), 0) FROM orders WHERE user_id = ? AND payment_status = 'completed'");
$stmt->execute([$user_id]);
$total_spent = $stmt->fetchColumn();

// Get recent orders
$stmt = $pdo->prepare("SELECT * FROM orders WHERE user_id = ? ORDER BY created_at DESC LIMIT 5");
$stmt->execute([$user_id]);
$recent_orders = $stmt->fetchAll();

// Get download count
$stmt = $pdo->prepare("SELECT COUNT(*) FROM order_items oi JOIN orders o ON oi.order_id = o.id WHERE o.user_id = ? AND o.payment_status = 'completed'");
$stmt->execute([$user_id]);
$download_count = $stmt->fetchColumn();

// Get wishlist count
$stmt = $pdo->prepare("SELECT COUNT(*) FROM wishlist WHERE user_id = ?");
$stmt->execute([$user_id]);
$wishlist_count = $stmt->fetchColumn();

// Get affiliate info
$affiliate = get_affiliate_by_user_id($user_id);

include __DIR__ . '/../includes/header.php';
?>

<section class="section">
    <div class="container">
        <!-- Welcome Banner -->
        <div class="welcome-banner mb-5" data-aos="fade-up">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <div class="d-flex align-items-center">
                        <div class="user-avatar-large me-4">
                            <?php if (!empty($user['avatar'])): ?>
                                <img src="<?php echo UPLOAD_URL . '/' . $user['avatar']; ?>" alt="<?php echo sanitize($user['name']); ?>">
                            <?php else: ?>
                                <span><?php echo strtoupper(substr($user['name'], 0, 1)); ?></span>
                            <?php endif; ?>
                        </div>
                        <div>
                            <h2 class="mb-1">Welcome back, <?php echo sanitize($user['name']); ?>!</h2>
                            <p class="text-muted mb-0"><?php echo sanitize($user['email']); ?></p>
                        </div>
                    </div>
                </div>
                <div class="col-md-4 text-md-end mt-3 mt-md-0">
                    <a href="<?php echo BASE_URL; ?>/products.php" class="btn btn-primary">
                        <i class="fas fa-shopping-bag me-2"></i>Browse Products
                    </a>
                </div>
            </div>
        </div>
        
        <!-- Stats Cards -->
        <div class="row g-4 mb-5">
            <div class="col-md-6 col-lg-3" data-aos="fade-up" data-aos-delay="0">
                <div class="stat-card">
                    <div class="stat-icon bg-primary">
                        <i class="fas fa-shopping-bag"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?php echo number_format($order_count); ?></h3>
                        <p>Total Orders</p>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6 col-lg-3" data-aos="fade-up" data-aos-delay="100">
                <div class="stat-card">
                    <div class="stat-icon bg-success">
                        <i class="fas fa-download"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?php echo number_format($download_count); ?></h3>
                        <p>Downloads</p>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6 col-lg-3" data-aos="fade-up" data-aos-delay="200">
                <div class="stat-card">
                    <div class="stat-icon bg-info">
                        <i class="fas fa-heart"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?php echo number_format($wishlist_count); ?></h3>
                        <p>Wishlist Items</p>
                    </div>
                </div>
            </div>
            
            <div class="col-md-6 col-lg-3" data-aos="fade-up" data-aos-delay="300">
                <div class="stat-card">
                    <div class="stat-icon bg-warning">
                        <i class="fas fa-dollar-sign"></i>
                    </div>
                    <div class="stat-info">
                        <h3><?php echo format_price($total_spent); ?></h3>
                        <p>Total Spent</p>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="row g-4">
            <!-- Recent Orders -->
            <div class="col-lg-8" data-aos="fade-right">
                <div class="card dashboard-card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0"><i class="fas fa-shopping-bag me-2"></i>Recent Orders</h5>
                        <a href="<?php echo BASE_URL; ?>/account/orders.php" class="btn btn-sm btn-outline-light">View All</a>
                    </div>
                    <div class="card-body p-0">
                        <?php if (empty($recent_orders)): ?>
                            <div class="text-center py-5">
                                <i class="fas fa-shopping-bag fa-3x text-muted mb-3"></i>
                                <p class="text-muted">No orders yet</p>
                                <a href="<?php echo BASE_URL; ?>/products.php" class="btn btn-primary btn-sm">Start Shopping</a>
                            </div>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-dark table-hover mb-0">
                                    <thead>
                                        <tr>
                                            <th>Order #</th>
                                            <th>Date</th>
                                            <th>Total</th>
                                            <th>Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recent_orders as $order): ?>
                                        <tr>
                                            <td><strong><?php echo $order['order_number']; ?></strong></td>
                                            <td><?php echo format_date($order['created_at']); ?></td>
                                            <td><?php echo format_price($order['total']); ?></td>
                                            <td>
                                                <span class="badge bg-<?php 
                                                    echo match($order['payment_status']) {
                                                        'completed' => 'success',
                                                        'pending' => 'warning',
                                                        'failed' => 'danger',
                                                        default => 'secondary'
                                                    };
                                                ?>">
                                                    <?php echo ucfirst($order['payment_status']); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <a href="<?php echo BASE_URL; ?>/account/order-view.php?id=<?php echo $order['id']; ?>" class="btn btn-sm btn-primary">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <!-- Sidebar -->
            <div class="col-lg-4">
                <!-- Quick Links -->
                <div class="card dashboard-card mb-4" data-aos="fade-left">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-link me-2"></i>Quick Links</h5>
                    </div>
                    <div class="card-body">
                        <div class="quick-links">
                            <a href="<?php echo BASE_URL; ?>/account/orders.php" class="quick-link">
                                <i class="fas fa-shopping-bag"></i>
                                <span>My Orders</span>
                                <i class="fas fa-chevron-right ms-auto"></i>
                            </a>
                            <a href="<?php echo BASE_URL; ?>/account/downloads.php" class="quick-link">
                                <i class="fas fa-download"></i>
                                <span>My Downloads</span>
                                <i class="fas fa-chevron-right ms-auto"></i>
                            </a>
                            <a href="<?php echo BASE_URL; ?>/account/wishlist.php" class="quick-link">
                                <i class="fas fa-heart"></i>
                                <span>My Wishlist</span>
                                <i class="fas fa-chevron-right ms-auto"></i>
                            </a>
                            <a href="<?php echo BASE_URL; ?>/account/profile.php" class="quick-link">
                                <i class="fas fa-user"></i>
                                <span>Edit Profile</span>
                                <i class="fas fa-chevron-right ms-auto"></i>
                            </a>
                            <?php if ($affiliate && $affiliate['status'] === 'active'): ?>
                            <a href="<?php echo BASE_URL; ?>/account/affiliate.php" class="quick-link">
                                <i class="fas fa-handshake"></i>
                                <span>Affiliate Program</span>
                                <i class="fas fa-chevron-right ms-auto"></i>
                            </a>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
                
                <!-- Account Info -->
                <div class="card dashboard-card" data-aos="fade-left" data-aos-delay="100">
                    <div class="card-header">
                        <h5 class="mb-0"><i class="fas fa-info-circle me-2"></i>Account Info</h5>
                    </div>
                    <div class="card-body">
                        <div class="mb-3">
                            <small class="text-muted d-block">Member Since</small>
                            <strong><?php echo format_date($user['created_at']); ?></strong>
                        </div>
                        <div class="mb-3">
                            <small class="text-muted d-block">Last Login</small>
                            <strong><?php echo $user['last_login'] ? time_ago($user['last_login']) : 'Never'; ?></strong>
                        </div>
                        <div>
                            <small class="text-muted d-block">Account Status</small>
                            <span class="badge bg-success">Active</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

<style>
.welcome-banner {
    padding: 2rem;
    background: var(--gradient-card);
    border: 1px solid var(--dark-lighter);
    border-radius: var(--radius-lg);
}

.user-avatar-large {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: var(--gradient-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    font-weight: 700;
    color: var(--white);
    overflow: hidden;
}

.user-avatar-large img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.stat-card {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1.5rem;
    background: var(--gradient-card);
    border: 1px solid var(--dark-lighter);
    border-radius: var(--radius);
    transition: var(--transition);
}

.stat-card:hover {
    border-color: var(--primary);
    transform: translateY(-5px);
}

.stat-icon {
    width: 60px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius);
    font-size: 1.5rem;
    color: var(--white);
}

.stat-info h3 {
    font-size: 1.75rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
    color: var(--white);
}

.stat-info p {
    margin: 0;
    color: var(--gray-500);
    font-size: 0.875rem;
}

.dashboard-card {
    border: 1px solid var(--dark-lighter);
    background: var(--gradient-card);
}

.dashboard-card .card-header {
    background: transparent;
    border-bottom: 1px solid var(--dark-lighter);
    padding: 1.25rem 1.5rem;
}

.quick-links {
    display: flex;
    flex-direction: column;
}

.quick-link {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1rem;
    color: var(--gray-300);
    border-radius: var(--radius-sm);
    transition: var(--transition);
    text-decoration: none;
}

.quick-link:hover {
    background: var(--dark-lighter);
    color: var(--white);
    text-decoration: none;
}

.quick-link i:first-child {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--dark);
    border-radius: var(--radius-sm);
    color: var(--primary);
}
</style>

<?php include __DIR__ . '/../includes/footer.php'; ?>
