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

    <!-- App Header -->
    <div class="appHeader bg-primary text-light">
        <div class="left">
            <a href="<?php echo base_url('admin/crypto-wallets'); ?>" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle"><?php echo htmlspecialchars($wallet->name); ?> Addresses</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">
        <?php if ($this->session->flashdata('success')): ?>
        <div class="section mt-2">
            <div class="alert alert-success alert-dismissible fade show"><?php echo $this->session->flashdata('success'); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        </div>
        <?php endif; ?>
        <?php if ($this->session->flashdata('error')): ?>
        <div class="section mt-2">
            <div class="alert alert-danger alert-dismissible fade show"><?php echo $this->session->flashdata('error'); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        </div>
        <?php endif; ?>

        <!-- Add address form -->
        <div class="section mt-2">
            <div class="section-title">Add deposit address</div>
            <div class="card">
                <div class="card-body">
                    <form method="post" action="<?php echo base_url('admin/crypto-address/add/' . $wallet->id); ?>">
                        <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
                        <div class="form-group basic">
                            <label class="label">Address</label>
                            <input type="text" class="form-control font-monospace" name="address" required placeholder="Paste wallet address">
                        </div>
                        <div class="form-group basic">
                            <label class="label">Label (optional)</label>
                            <input type="text" class="form-control" name="label" placeholder="e.g. Primary">
                        </div>
                        <div class="form-group basic">
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" name="is_active" value="1" id="addrActive" checked>
                                <label class="form-check-label" for="addrActive">Active</label>
                            </div>
                        </div>
                        <button type="submit" class="btn btn-primary btn-block">Add Address</button>
                    </form>
                </div>
            </div>
        </div>

        <!-- Addresses list -->
        <div class="section mt-2 mb-3">
            <div class="section-title">Addresses (<?php echo count($addresses); ?>)</div>
            <?php if (empty($addresses)): ?>
            <div class="card">
                <div class="card-body text-center py-3">
                    <p class="text-muted mb-0">No addresses yet. Add one above.</p>
                </div>
            </div>
            <?php else: ?>
            <ul class="listview flush">
                <?php foreach ($addresses as $addr): ?>
                <li>
                    <div class="item">
                        <div class="in">
                            <div>
                                <?php if ($addr->label): ?>
                                <strong><?php echo htmlspecialchars($addr->label); ?></strong>
                                <p class="mb-0 small font-monospace text-break"><?php echo htmlspecialchars($addr->address); ?></p>
                                <?php else: ?>
                                <span class="font-monospace small text-break"><?php echo htmlspecialchars($addr->address); ?></span>
                                <?php endif; ?>
                            </div>
                            <div class="d-flex align-items-center gap-2">
                                <span class="badge badge-<?php echo $addr->is_active ? 'success' : 'secondary'; ?>"><?php echo $addr->is_active ? 'Active' : 'Inactive'; ?></span>
                                <a href="<?php echo base_url('admin/crypto-address/delete/' . $addr->id); ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this address?');">Delete</a>
                            </div>
                        </div>
                    </div>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>
    </div>

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

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