<?php 
$this->load->view('layouts/header');
$this->load->helper('layout');
?>

    <!-- App Header -->
    <div class="appHeader">
        <div class="left">
            <a href="<?php echo base_url('dashboard'); ?>" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">My Cards</div>
        <div class="right">
            <?php if ($user->user_type === 'admin'): ?>
            <a href="<?php echo base_url('cards/create'); ?>" class="headerButton">
                <ion-icon name="add-outline"></ion-icon>
            </a>
            <?php endif; ?>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <?php if ($this->session->flashdata('success')): ?>
        <div class="section mt-2">
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <?php echo $this->session->flashdata('success'); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        </div>
        <?php endif; ?>

        <?php if (empty($cards)): ?>
        <div class="section mt-5 text-center">
            <ion-icon name="card-outline" style="font-size: 64px; color: #ccc;"></ion-icon>
            <h3 class="mt-2">No Cards</h3>
            <p class="text-muted">You don't have any cards yet</p>
            <?php if ($user->user_type === 'admin'): ?>
            <a href="<?php echo base_url('cards/create'); ?>" class="btn btn-primary mt-2">
                <ion-icon name="add-outline"></ion-icon> Create Virtual Card
            </a>
            <?php else: ?>
            <p class="text-muted small">Please contact an administrator to request a card.</p>
            <?php endif; ?>
        </div>
        <?php else: ?>

        <!-- Primary Card Display -->
        <div class="section mt-2">
            <?php $card = $cards[0]; ?>
            <a href="<?php echo base_url('cards/view/' . $card->id); ?>">
                <div class="card-block bg-<?php echo $card->status == 'active' ? 'primary' : ($card->status == 'frozen' ? 'secondary' : 'dark'); ?>">
                    <div class="card-main">
                        <div class="balance">
                            <span class="label">BALANCE</span>
                            <h1 class="title"><?php echo format_money($card->balance, $card->currency); ?></h1>
                        </div>
                        <div class="in">
                            <div class="card-number">
                                <span class="label">CARD NUMBER</span>
                                <?php echo $card->card_number_masked; ?>
                            </div>
                            <div class="bottom">
                                <div class="card-expiry">
                                    <span class="label">EXPIRY</span>
                                    <?php echo $card->expiry_month; ?> / <?php echo substr($card->expiry_year, -2); ?>
                                </div>
                                <div class="card-ccv">
                                    <span class="label">STATUS</span>
                                    <?php echo ucfirst($card->status); ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
        </div>

        <!-- Card Actions -->
        <div class="section mt-3">
            <div class="section-title">Quick Actions</div>
            <div class="row">
                <?php if ($user->user_type === 'admin'): ?>
                <div class="col-3 text-center mb-2">
                    <a href="<?php echo base_url('cards/create'); ?>" class="text-decoration-none">
                        <div class="rounded-circle bg-primary d-flex align-items-center justify-content-center mb-1" style="width: 50px; height: 50px; margin: 0 auto;">
                            <ion-icon name="add-outline" style="font-size: 22px; color: #fff;"></ion-icon>
                        </div>
                        <span class="small text-primary">New Card</span>
                    </a>
                </div>
                <?php endif; ?>
                <div class="col-3 text-center mb-2">
                    <a href="<?php echo base_url('cards/fund/' . $cards[0]->id); ?>" class="text-decoration-none">
                        <div class="rounded-circle bg-success d-flex align-items-center justify-content-center mb-1" style="width: 50px; height: 50px; margin: 0 auto;">
                            <ion-icon name="cash-outline" style="font-size: 22px; color: #fff;"></ion-icon>
                        </div>
                        <span class="small text-success">Fund</span>
                    </a>
                </div>
                <div class="col-3 text-center mb-2">
                    <a href="<?php echo base_url('cards/withdraw/' . $cards[0]->id); ?>" class="text-decoration-none">
                        <div class="rounded-circle bg-warning d-flex align-items-center justify-content-center mb-1" style="width: 50px; height: 50px; margin: 0 auto;">
                            <ion-icon name="download-outline" style="font-size: 22px; color: #fff;"></ion-icon>
                        </div>
                        <span class="small text-warning">Withdraw</span>
                    </a>
                </div>
                <div class="col-3 text-center mb-2">
                    <a href="<?php echo base_url('cards/transactions/' . $cards[0]->id); ?>" class="text-decoration-none">
                        <div class="rounded-circle bg-info d-flex align-items-center justify-content-center mb-1" style="width: 50px; height: 50px; margin: 0 auto;">
                            <ion-icon name="list-outline" style="font-size: 22px; color: #fff;"></ion-icon>
                        </div>
                        <span class="small text-info">History</span>
                    </a>
                </div>
            </div>
        </div>

        <!-- Cards List -->
        <div class="section mt-3 mb-3">
            <div class="section-title">All Cards</div>
            <ul class="listview image-listview flush">
                <?php foreach ($cards as $card): ?>
                <li>
                    <a href="<?php echo base_url('cards/view/' . $card->id); ?>" class="item">
                        <div class="icon-box bg-<?php echo $card->card_brand == 'visa' ? 'primary' : 'warning'; ?>">
                            <ion-icon name="card-outline"></ion-icon>
                        </div>
                        <div class="in">
                            <div>
                                <strong><?php echo ucfirst($card->card_type); ?> <?php echo ucfirst($card->card_brand); ?></strong>
                                <p><?php echo $card->card_number_masked; ?></p>
                            </div>
                            <span class="badge badge-<?php echo $card->status == 'active' ? 'success' : 'secondary'; ?>">
                                <?php echo ucfirst($card->status); ?>
                            </span>
                        </div>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>
    </div>

    <!-- App Bottom Menu -->
    <?php echo get_bottom_menu('cards'); ?>

<?php $this->load->view('layouts/footer'); ?>
