<?php
require_once 'functions.php';
requireAuth();

$user = getCurrentUser();
$cards = getUserCards($user['id']);

$flash = getFlash();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Cards - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        .card-display {
            width: 100%;
            max-width: 360px;
            height: 210px;
            border-radius: var(--border-radius-lg);
            padding: 24px;
            color: var(--white);
            position: relative;
            overflow: hidden;
            transition: var(--transition);
        }
        .card-display:hover {
            transform: translateY(-4px);
            box-shadow: var(--shadow-xl);
        }
        .card-display.visa {
            background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
        }
        .card-display.mastercard {
            background: linear-gradient(135deg, #f7931a 0%, #c0392b 100%);
        }
        .card-display.amex {
            background: linear-gradient(135deg, #00a8e8 0%, #0077b6 100%);
        }
        .card-display::before {
            content: '';
            position: absolute;
            top: -30%;
            right: -20%;
            width: 180px;
            height: 180px;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            border-radius: 50%;
        }
        .card-display::after {
            content: '';
            position: absolute;
            bottom: -20%;
            left: -10%;
            width: 140px;
            height: 140px;
            background: radial-gradient(circle, rgba(255,255,255,0.05) 0%, transparent 70%);
            border-radius: 50%;
        }
        .card-chip {
            width: 44px;
            height: 32px;
            background: linear-gradient(135deg, #d4af37, #f4d03f, #d4af37);
            border-radius: 8px;
            margin-bottom: 28px;
            position: relative;
        }
        .card-chip::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: rgba(0,0,0,0.2);
        }
        .card-chip::after {
            content: '';
            position: absolute;
            left: 50%;
            top: 0;
            bottom: 0;
            width: 1px;
            background: rgba(0,0,0,0.2);
        }
        .card-number-display {
            font-family: 'Courier New', monospace;
            font-size: 20px;
            letter-spacing: 3px;
            margin-bottom: 24px;
            position: relative;
            z-index: 1;
        }
        .card-footer-display {
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            position: relative;
            z-index: 1;
        }
        .card-meta-label {
            font-size: 9px;
            text-transform: uppercase;
            letter-spacing: 1px;
            opacity: 0.8;
            margin-bottom: 2px;
        }
        .card-meta-value {
            font-size: 13px;
            font-weight: 600;
        }
        .card-info-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 16px;
        }
        .card-info-item {
            background: var(--gray-50);
            border-radius: var(--border-radius);
            padding: 16px;
        }
        .card-info-label {
            font-size: 11px;
            color: var(--gray);
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 4px;
        }
        .card-info-value {
            font-size: 16px;
            font-weight: 700;
            color: var(--secondary);
        }
    </style>
</head>
<body>
    <?php include 'includes/client-sidebar.php'; ?>
    
    <div class="main-content">
        <?php include 'includes/client-header.php'; ?>
        
        <div class="page-content">
            <?php if ($flash): ?>
                <div class="alert alert-<?php echo $flash['type']; ?> fade-in">
                    <span><?php echo $flash['type'] === 'success' ? '✓' : '⚠'; ?></span> <?php echo e($flash['message']); ?>
                </div>
            <?php endif; ?>
            
            <?php if ($cards): ?>
                <div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 24px; margin-bottom: 32px;">
                    <?php foreach ($cards as $card): ?>
                        <div>
                            <div class="card-display <?php echo e($card['card_type']); ?>" style="margin-bottom: 20px;">
                                <div style="display: flex; justify-content: space-between; align-items: start;">
                                    <div class="card-chip"></div>
                                    <span style="font-size: 14px; font-weight: 700; text-transform: uppercase; position: relative; z-index: 1;"><?php echo strtoupper(e($card['card_type'])); ?></span>
                                </div>
                                <div class="card-number-display"><?php echo maskCard(e($card['card_number'])); ?></div>
                                <div class="card-footer-display">
                                    <div>
                                        <div class="card-meta-label">Card Holder</div>
                                        <div class="card-meta-value"><?php echo e($card['card_holder']); ?></div>
                                    </div>
                                    <div style="text-align: right;">
                                        <div class="card-meta-label">Expires</div>
                                        <div class="card-meta-value"><?php echo e($card['expiry_month']); ?>/<?php echo e($card['expiry_year']); ?></div>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="card-info-grid">
                                <div class="card-info-item">
                                    <div class="card-info-label">Card Brand</div>
                                    <div class="card-info-value"><?php echo e($card['card_brand']); ?></div>
                                </div>
                                <div class="card-info-item">
                                    <div class="card-info-label">Status</div>
                                    <div class="card-info-value" style="color: <?php echo $card['is_active'] ? 'var(--success)' : 'var(--danger)'; ?>">
                                        <?php echo $card['is_active'] ? '● Active' : '● Inactive'; ?>
                                    </div>
                                </div>
                                <div class="card-info-item">
                                    <div class="card-info-label">Daily Limit</div>
                                    <div class="card-info-value"><?php echo formatCurrency($card['daily_limit']); ?></div>
                                </div>
                                <div class="card-info-item">
                                    <div class="card-info-label">Monthly Limit</div>
                                    <div class="card-info-value"><?php echo formatCurrency($card['monthly_limit']); ?></div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php else: ?>
                <div class="empty-state">
                    <div class="empty-state-icon">💳</div>
                    <h3>No Cards Found</h3>
                    <p>You don't have any cards linked to your account yet. Contact support to request a card.</p>
                    <a href="support.php" class="btn btn-primary" style="margin-top: 20px;">Contact Support</a>
                </div>
            <?php endif; ?>
        </div>
    </div>
    
    <script src="assets/js/main.js"></script>
</body>
</html>
