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

    <!-- App Header -->
    <div class="appHeader bg-primary text-light">
        <div class="left">
            <a href="#" class="headerButton" data-bs-toggle="modal" data-bs-target="#adminSidebar">
                <ion-icon name="menu-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">Manage Cards</div>
        <div class="right">
            <a href="<?php echo base_url('admin/dashboard'); ?>" class="headerButton">
                <ion-icon name="home-outline"></ion-icon>
            </a>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <!-- Filter -->
        <div class="section mt-2">
            <div class="card">
                <div class="card-body">
                    <form method="GET" action="">
                        <div class="row">
                            <div class="col-6">
                                <select name="status" class="form-control" onchange="this.form.submit()">
                                    <option value="">All Status</option>
                                    <option value="active" <?php echo ($filters['status'] ?? '') == 'active' ? 'selected' : ''; ?>>Active</option>
                                    <option value="inactive" <?php echo ($filters['status'] ?? '') == 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                                    <option value="frozen" <?php echo ($filters['status'] ?? '') == 'frozen' ? 'selected' : ''; ?>>Frozen</option>
                                    <option value="blocked" <?php echo ($filters['status'] ?? '') == 'blocked' ? 'selected' : ''; ?>>Blocked</option>
                                </select>
                            </div>
                            <div class="col-6">
                                <select name="card_type" class="form-control" onchange="this.form.submit()">
                                    <option value="">All Types</option>
                                    <option value="virtual" <?php echo ($filters['card_type'] ?? '') == 'virtual' ? 'selected' : ''; ?>>Virtual</option>
                                    <option value="physical" <?php echo ($filters['card_type'] ?? '') == 'physical' ? 'selected' : ''; ?>>Physical</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Cards List -->
        <div class="section mt-2 mb-3">
            <div class="section-title">Cards (<?php echo number_format($total); ?>)</div>
            
            <?php if (empty($cards)): ?>
            <div class="card">
                <div class="card-body text-center py-4">
                    <ion-icon name="card-outline" style="font-size: 48px; color: #ccc;"></ion-icon>
                    <p class="text-muted mt-2 mb-0">No cards found</p>
                </div>
            </div>
            <?php else: ?>
            
            <ul class="listview image-listview">
                <?php foreach ($cards as $card): ?>
                <li>
                    <a href="<?php echo base_url('admin/card/' . $card->id); ?>" class="item">
                        <div class="rounded-circle bg-<?php 
                            echo $card->status == 'active' ? 'primary' : 
                                ($card->status == 'frozen' ? 'info' : 
                                ($card->status == 'blocked' ? 'danger' : 'secondary')); 
                        ?> d-flex align-items-center justify-content-center" style="width: 42px; height: 42px; min-width: 42px;">
                            <ion-icon name="card-outline" class="text-white" style="font-size: 20px;"></ion-icon>
                        </div>
                        <div class="in">
                            <div>
                                <strong><?php echo $card->card_number_masked; ?></strong>
                                <p class="mb-0 small text-muted">
                                    <?php echo htmlspecialchars($card->first_name . ' ' . $card->last_name); ?><br>
                                    <?php echo ucfirst($card->card_type); ?> <?php echo ucfirst($card->card_brand); ?> - <?php echo $card->expiry_month; ?>/<?php echo $card->expiry_year; ?>
                                </p>
                            </div>
                            <div class="text-end">
                                <span><?php echo format_money($card->balance ?? 0, $card->currency); ?></span>
                                <p class="mb-0">
                                    <span class="badge badge-<?php 
                                        echo $card->status == 'active' ? 'success' : 
                                            ($card->status == 'frozen' ? 'info' : 
                                            ($card->status == 'blocked' ? 'danger' : 'secondary')); 
                                    ?>">
                                        <?php echo ucfirst($card->status); ?>
                                    </span>
                                </p>
                            </div>
                        </div>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
            
            <!-- Pagination -->
            <?php if ($pages > 1): ?>
            <div class="mt-2 text-center">
                <div class="btn-group">
                    <?php if ($page > 1): ?>
                    <a href="?page=<?php echo $page - 1; ?>&status=<?php echo urlencode($filters['status'] ?? ''); ?>&card_type=<?php echo urlencode($filters['card_type'] ?? ''); ?>" class="btn btn-secondary btn-sm">
                        <ion-icon name="chevron-back-outline"></ion-icon>
                    </a>
                    <?php endif; ?>
                    
                    <button class="btn btn-secondary btn-sm" disabled>
                        Page <?php echo $page; ?> of <?php echo $pages; ?>
                    </button>
                    
                    <?php if ($page < $pages): ?>
                    <a href="?page=<?php echo $page + 1; ?>&status=<?php echo urlencode($filters['status'] ?? ''); ?>&card_type=<?php echo urlencode($filters['card_type'] ?? ''); ?>" class="btn btn-secondary btn-sm">
                        <ion-icon name="chevron-forward-outline"></ion-icon>
                    </a>
                    <?php endif; ?>
                </div>
            </div>
            <?php endif; ?>
            
            <?php endif; ?>
        </div>
    </div>

    <!-- Admin Sidebar -->
    <?php $this->load->view('admin/partials/sidebar'); ?>

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