<?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 Accounts</div>
        <div class="right">
            <a href="<?php echo base_url('admin/users'); ?>" class="headerButton">
                <ion-icon name="people-outline"></ion-icon>
            </a>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <!-- Search & Filter -->
        <div class="section mt-2">
            <div class="card">
                <div class="card-body">
                    <form method="GET" action="">
                        <div class="row">
                            <div class="col-8">
                                <div class="form-group basic">
                                    <input type="text" name="search" class="form-control" placeholder="Search by account number..." value="<?php echo htmlspecialchars($filters['search'] ?? ''); ?>">
                                </div>
                            </div>
                            <div class="col-4">
                                <button type="submit" class="btn btn-primary w-100">
                                    <ion-icon name="search-outline"></ion-icon>
                                </button>
                            </div>
                        </div>
                        <div class="row mt-1">
                            <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="frozen" <?php echo ($filters['status'] ?? '') == 'frozen' ? 'selected' : ''; ?>>Frozen</option>
                                    <option value="dormant" <?php echo ($filters['status'] ?? '') == 'dormant' ? 'selected' : ''; ?>>Dormant</option>
                                </select>
                            </div>
                            <div class="col-6">
                                <select name="account_type" class="form-control" onchange="this.form.submit()">
                                    <option value="">All Types</option>
                                    <option value="savings" <?php echo ($filters['account_type'] ?? '') == 'savings' ? 'selected' : ''; ?>>Savings</option>
                                    <option value="current" <?php echo ($filters['account_type'] ?? '') == 'current' ? 'selected' : ''; ?>>Current</option>
                                    <option value="virtual" <?php echo ($filters['account_type'] ?? '') == 'virtual' ? 'selected' : ''; ?>>Virtual</option>
                                </select>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- Accounts List -->
        <div class="section mt-2 mb-3">
            <div class="section-title">Accounts (<?php echo number_format($total); ?>)</div>
            
            <?php if (empty($accounts)): ?>
            <div class="card">
                <div class="card-body text-center py-4">
                    <ion-icon name="wallet-outline" style="font-size: 48px; color: #ccc;"></ion-icon>
                    <p class="text-muted mt-2 mb-0">No accounts found</p>
                </div>
            </div>
            <?php else: ?>
            
            <ul class="listview image-listview">
                <?php foreach ($accounts as $acc): ?>
                <li>
                    <a href="<?php echo base_url('admin/user/' . $acc->user_id); ?>" class="item">
                        <div class="icon-box bg-<?php echo $acc->status == 'active' ? 'success' : ($acc->status == 'frozen' ? 'danger' : 'secondary'); ?>">
                            <ion-icon name="wallet-outline"></ion-icon>
                        </div>
                        <div class="in">
                            <div>
                                <strong><?php echo $acc->account_number; ?></strong>
                                <p class="mb-0 small text-muted">
                                    <?php echo htmlspecialchars($acc->account_name); ?> - <?php echo ucfirst($acc->account_type); ?>
                                </p>
                            </div>
                            <div class="text-end">
                                <span class="text-<?php echo $acc->status == 'active' ? 'success' : 'muted'; ?>">
                                    <?php echo format_money($acc->available_balance ?? 0, $acc->currency); ?>
                                </span>
                                <p class="mb-0 small">
                                    <span class="badge badge-<?php echo $acc->status == 'active' ? 'success' : ($acc->status == 'frozen' ? 'danger' : 'secondary'); ?>">
                                        <?php echo ucfirst($acc->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'] ?? ''); ?>&account_type=<?php echo urlencode($filters['account_type'] ?? ''); ?>&search=<?php echo urlencode($filters['search'] ?? ''); ?>" 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'] ?? ''); ?>&account_type=<?php echo urlencode($filters['account_type'] ?? ''); ?>&search=<?php echo urlencode($filters['search'] ?? ''); ?>" 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'); ?>
