<?php 
$this->load->view('layouts/header');
$this->load->helper('layout');
// QR code API (no backend lib required)
$qr_base = 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&margin=10&data=';
?>

    <!-- 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">Deposit / Receive</div>
        <div class="right"></div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        <div class="section mt-2">
            <div class="section-title">How would you like to receive funds?</div>
            
            <ul class="listview image-listview flush">
                <li>
                    <a href="#bankSection" class="item" data-bs-toggle="collapse" data-bs-target="#bankSection" aria-expanded="true">
                        <div class="icon-box bg-primary">
                            <ion-icon name="business-outline"></ion-icon>
                        </div>
                        <div class="in mt-2">
                            <strong>
                                Bank Transfer
                                <p class="text-muted small">Receive via wire or ACH to your account</p>
                            </strong>         
                        </div>
                        <!-- <ion-icon name="chevron-down-outline" class="text-muted"></ion-icon> -->
                    </a>
                </li>
                <li>
                    <a href="#cryptoSection" class="item" data-bs-toggle="collapse" data-bs-target="#cryptoSection" aria-expanded="false">
                        <div class="icon-box bg-warning">
                            <ion-icon name="logo-bitcoin"></ion-icon>
                        </div>
                        <div class="in mt-2">
                            <strong>
                                Crypto
                                <p class="text-muted small">Receive via crypto to your wallet</p>
                            </strong>
                            
                        </div>
                        <!-- <ion-icon name="chevron-down-outline" class="text-muted"></ion-icon> -->
                    </a>
                </li>
            </ul>
        </div>

        <!-- Bank Transfer Section -->
        <div class="section mt-2 collapse show" id="bankSection">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">Bank Transfer</h5>
                    <p class="text-muted small">Use your account details to receive funds from another bank or from someone else's <?php echo get_app_name(); ?> account.</p>
                    <a href="<?php echo base_url('accounts'); ?>" class="btn btn-outline-primary btn-sm">View account numbers</a>
                </div>
            </div>
        </div>

        <!-- Crypto Section -->
        <div class="section mt-2 collapse" id="cryptoSection">
            <?php if (empty($crypto_wallets)): ?>
            <div class="card">
                <div class="card-body text-center py-4">
                    <ion-icon name="logo-bitcoin" style="font-size: 48px; color: #ccc;"></ion-icon>
                    <p class="text-muted mb-0 mt-2">No crypto deposit options are available at the moment. Please check back later.</p>
                </div>
            </div>
            <?php else: ?>
            <?php foreach ($crypto_wallets as $wallet): ?>
            <?php if (empty($wallet->addresses)) continue; ?>
            <div class="card mb-3">
                <div class="card-header d-flex align-items-center">
                    <strong><?php echo htmlspecialchars($wallet->name); ?></strong>
                    <span class="badge bg-secondary ms-2"><?php echo htmlspecialchars($wallet->symbol); ?></span>
                    <?php if (!empty($wallet->network)): ?>
                    <span class="text-muted small ms-2">(<?php echo htmlspecialchars($wallet->network); ?>)</span>
                    <?php endif; ?>
                </div>
                <div class="card-body">
                    <?php foreach ($wallet->addresses as $addr): ?>
                    <div class="mb-4">
                        <?php if ($addr->label): ?>
                        <p class="text-muted small mb-1"><?php echo htmlspecialchars($addr->label); ?></p>
                        <?php endif; ?>
                        <div class="text-center mb-2">
                            <img src="<?php echo $qr_base . urlencode($addr->address); ?>" alt="QR Code" width="200" height="200" class="img-fluid rounded" style="max-width: 200px;">
                        </div>
                        <div class="input-group">
                            <input type="text" class="form-control font-monospace small" value="<?php echo htmlspecialchars($addr->address); ?>" id="addr-<?php echo $addr->id; ?>" readonly>
                            <button type="button" class="btn btn-outline-primary" onclick="copyAddress('addr-<?php echo $addr->id; ?>')">
                                <ion-icon name="copy-outline"></ion-icon> Copy
                            </button>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    <?php if (!empty($wallet->instructions)): ?>
                    <div class="border-top pt-3 mt-3">
                        <strong class="d-block mb-2">Instructions</strong>
                        <div class="text-muted small" style="white-space: pre-line;"><?php echo nl2br(htmlspecialchars($wallet->instructions)); ?></div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>

    <?php echo get_bottom_menu('dashboard'); ?>

    <script>
    function copyAddress(inputId) {
        var el = document.getElementById(inputId);
        el.select();
        el.setSelectionRange(0, 99999);
        try {
            navigator.clipboard.writeText(el.value);
            if (typeof toastbox === 'function') toastbox('toast-success', 2000);
            else alert('Address copied!');
        } catch (e) {
            alert('Address: ' + el.value);
        }
    }
    </script>

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