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

    <!-- App Header -->
    <div class="appHeader">
        <div class="left">
            <a href="<?php echo base_url('cards/view/' . $card->id); ?>" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">Fund Card</div>
        <div class="right"></div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <!-- Card Info -->
        <div class="section mt-2">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <p class="mb-1 opacity-75">Card Balance</p>
                            <h2 class="mb-0"><?php echo format_money($card->balance, $card->currency); ?></h2>
                        </div>
                        <div class="text-end">
                            <p class="mb-1 opacity-75"><?php echo $card->card_number_masked; ?></p>
                            <span class="badge bg-light text-primary"><?php echo ucfirst($card->card_brand); ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Fund Form -->
        <div class="section mt-3 mb-3">
            <div class="section-title">Fund Amount</div>
            <div class="card">
                <div class="card-body">
                    <?php echo form_open('cards/fund/' . $card->id); ?>
                        
                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Source Account</label>
                                <select class="form-control" name="account_id" id="sourceAccount" required>
                                    <option value="">Select Account</option>
                                    <?php foreach ($accounts as $acc): ?>
                                    <option value="<?php echo $acc->id; ?>" data-balance="<?php echo $acc->available_balance ?? 0; ?>" data-currency="<?php echo $acc->currency; ?>">
                                        <?php echo $acc->account_number; ?> - <?php echo format_money($acc->available_balance ?? 0, $acc->currency); ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        
                        <div class="form-group basic">
                            <label class="label">Amount (<?php echo $card->currency; ?>)</label>
                            <div class="input-group">
                                <span class="input-group-text"><?php echo $card->currency; ?></span>
                                <input type="number" class="form-control" name="amount" id="fundAmount" placeholder="0.00" step="0.01" min="1" required>
                            </div>
                            <div class="input-info" id="balanceInfo"></div>
                        </div>

                        <!-- Quick amounts -->
                        <div class="form-group basic">
                            <div class="d-flex flex-wrap gap-2">
                                <button type="button" class="btn btn-outline-primary btn-sm" onclick="setAmount(10)">$10</button>
                                <button type="button" class="btn btn-outline-primary btn-sm" onclick="setAmount(20)">$20</button>
                                <button type="button" class="btn btn-outline-primary btn-sm" onclick="setAmount(50)">$50</button>
                                <button type="button" class="btn btn-outline-primary btn-sm" onclick="setAmount(100)">$100</button>
                                <button type="button" class="btn btn-outline-primary btn-sm" onclick="setAmount(200)">$200</button>
                            </div>
                        </div>
                        
                        <?php if ($user->user_type !== 'admin'): ?>
                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Transaction PIN</label>
                                <input type="password" class="form-control" name="pin" maxlength="4" placeholder="Enter PIN" required>
                            </div>
                        </div>
                        <?php endif; ?>
                        
                        <div class="form-group basic mt-3">
                            <button type="submit" class="btn btn-primary btn-block btn-lg">
                                <ion-icon name="add-outline"></ion-icon> Fund Card
                            </button>
                        </div>
                    <?php echo form_close(); ?>
                </div>
            </div>
        </div>
    </div>

    <!-- App Bottom Menu -->
    <?php echo get_bottom_menu('cards'); ?>

    <script>
    function setAmount(amount) {
        document.getElementById('fundAmount').value = amount;
    }
    
    document.getElementById('sourceAccount').addEventListener('change', function() {
        var selected = this.options[this.selectedIndex];
        var balance = selected.dataset.balance;
        var currency = selected.dataset.currency;
        if (balance) {
            document.getElementById('balanceInfo').innerHTML = 
                'Available: ' + currency + ' ' + parseFloat(balance).toLocaleString('en-US', {minimumFractionDigits: 2});
        }
    });
    </script>

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