<?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">Admin Dashboard</div>
        <div class="right">
            <a href="<?php echo base_url('auth/logout'); ?>" class="headerButton">
                <ion-icon name="log-out-outline"></ion-icon>
            </a>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        <!-- Stats -->
        <div class="section mt-2">
            <div class="row">
                <div class="col-6 mb-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h1 class="mb-0 text-primary"><?php echo number_format($total_users); ?></h1>
                            <p class="text-muted mb-0">Total Users</p>
                        </div>
                    </div>
                </div>
                <div class="col-6 mb-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h1 class="mb-0 text-success"><?php echo number_format($total_accounts); ?></h1>
                            <p class="text-muted mb-0">Accounts</p>
                        </div>
                    </div>
                </div>
                <div class="col-6 mb-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h1 class="mb-0 text-warning"><?php echo number_format($total_transactions); ?></h1>
                            <p class="text-muted mb-0">Transactions</p>
                        </div>
                    </div>
                </div>
                <div class="col-6 mb-2">
                    <div class="card">
                        <div class="card-body text-center">
                            <h1 class="mb-0 text-info"><?php echo number_format($total_cards); ?></h1>
                            <p class="text-muted mb-0">Cards</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Transaction Stats -->
        <?php if ($transaction_stats): ?>
        <div class="section mt-2">
            <div class="section-title">This Month</div>
            <div class="card">
                <div class="card-body">
                    <div class="row">
                        <div class="col-6">
                            <h4><?php echo format_money($transaction_stats->total_amount ?? 0); ?></h4>
                            <p class="text-muted mb-0">Total Volume</p>
                        </div>
                        <div class="col-6 text-end">
                            <h4><?php echo format_money($transaction_stats->total_fees ?? 0); ?></h4>
                            <p class="text-muted mb-0">Total Fees</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <!-- User Stats -->
        <div class="section mt-2">
            <div class="section-title">User Status</div>
            <div class="card">
                <ul class="listview flush transparent simple-listview">
                    <li class="p-2">
                        <strong>Active Users</strong>
                        <span class="text-success"><?php echo number_format($active_users); ?></span>
                    </li>
                    <li class="p-2">
                        <strong>Pending Verification</strong>
                        <span class="text-warning"><?php echo number_format($pending_users); ?></span>
                    </li>
                    <li class="p-2">
                        <strong>Suspended</strong>
                        <span class="text-danger"><?php echo number_format($suspended_users); ?></span>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="section mt-2">
            <div class="section-title">Quick Actions</div>
            <ul class="listview image-listview flush">
                <li>
                    <a href="<?php echo base_url('admin/create_user'); ?>" class="item">
                        <div class="icon-box bg-primary">
                            <ion-icon name="person-add-outline"></ion-icon>
                        </div>
                        <div class="in">Create New User</div>
                    </a>
                </li>
                <li>
                    <a href="<?php echo base_url('admin/users'); ?>" class="item">
                        <div class="icon-box bg-success">
                            <ion-icon name="people-outline"></ion-icon>
                        </div>
                        <div class="in">Manage Users</div>
                    </a>
                </li>
                <li>
                    <a href="<?php echo base_url('admin/accounts'); ?>" class="item">
                        <div class="icon-box bg-warning">
                            <ion-icon name="wallet-outline"></ion-icon>
                        </div>
                        <div class="in">Manage Accounts</div>
                    </a>
                </li>
                <li>
                    <a href="<?php echo base_url('admin/transactions'); ?>" class="item">
                        <div class="icon-box bg-info">
                            <ion-icon name="list-outline"></ion-icon>
                        </div>
                        <div class="in">All Transactions</div>
                    </a>
                </li>
            </ul>
        </div>

        <!-- Recent Users -->
        <div class="section mt-2">
            <div class="section-heading">
                <h2 class="title">Recent Users</h2>
                <a href="<?php echo base_url('admin/users'); ?>" class="link">View All</a>
            </div>
            <ul class="listview image-listview flush">
                <?php foreach ($recent_users as $u): ?>
                <li>
                    <a href="<?php echo base_url('admin/user/' . $u->id); ?>" class="item">
                        <img src="<?php echo get_user_avatar($u); ?>" alt="img" class="image rounded">
                        <div class="in">
                            <div>
                                <strong><?php echo htmlspecialchars($u->first_name . ' ' . $u->last_name); ?></strong>
                                <p><?php echo htmlspecialchars($u->email); ?></p>
                            </div>
                            <?php if (isset($u->kyc_status) && $u->kyc_status === 'pending'): ?>
                            <span class="badge badge-warning">Pending KYC</span>
                            <?php else: ?>
                            <span class="badge badge-<?php 
                                echo $u->status == 'active' ? 'success' : 
                                    ($u->status == 'suspended' ? 'danger' : 'secondary'); 
                            ?>">
                                <?php echo ucfirst($u->status); ?>
                            </span>
                            <?php endif; ?>
                        </div>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
        </div>

        <!-- Recent Transactions -->
        <div class="section mt-2 mb-3">
            <div class="section-heading">
                <h2 class="title">Recent Transactions</h2>
                <a href="<?php echo base_url('admin/transactions'); ?>" class="link">View All</a>
            </div>
            <div class="transactions">
                <?php foreach ($recent_transactions as $txn): ?>
                <?php 
                $is_debit = !empty($txn->source_account_id);
                $icon_color = $is_debit ? 'danger' : 'success';
                $icon_name = $is_debit ? 'arrow-up-outline' : 'arrow-down-outline';
                ?>
                <a href="<?php echo base_url('admin/transaction/' . $txn->id); ?>" class="item">
                    <div class="detail">
                        <div class="rounded-circle bg-<?php echo $icon_color; ?> d-flex align-items-center justify-content-center" style="width: 42px; height: 42px; min-width: 42px;">
                            <ion-icon name="<?php echo $icon_name; ?>" class="text-white" style="font-size: 20px;"></ion-icon>
                        </div>
                        <div class="ms-2">
                            <strong><?php echo ucfirst($txn->type); ?></strong>
                            <p><?php echo $txn->reference; ?></p>
                        </div>
                    </div>
                    <div class="right">
                        <div class="price text-<?php echo $icon_color; ?>" style="font-weight: 600;">
                            <?php echo ($is_debit ? '- ' : '+ ') . format_money($txn->amount, $txn->currency); ?>
                        </div>
                    </div>
                </a>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

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

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