<?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">Withdraw from Card</div>
        <div class="right"></div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <!-- Card Info -->
        <div class="section mt-2">
            <div class="card bg-warning text-dark">
                <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-dark"><?php echo ucfirst($card->card_brand); ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Withdraw Form -->
        <div class="section mt-3 mb-3">
            <div class="section-title">Withdraw Amount</div>
            <div class="card">
                <div class="card-body">
                    <?php echo form_open('cards/withdraw/' . $card->id); ?>
                        
                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Destination Account</label>
                                <select class="form-control" name="account_id" required>
                                    <option value="">Select Account</option>
                                    <?php foreach ($accounts as $acc): ?>
                                    <option value="<?php echo $acc->id; ?>">
                                        <?php echo $acc->account_number; ?> (<?php echo $acc->currency; ?>)
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                            <div class="input-info">Funds will be credited to this account</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="withdrawAmount" placeholder="0.00" step="0.01" min="1" max="<?php echo $card->balance; ?>" required>
                            </div>
                            <div class="input-info">Available: <?php echo format_money($card->balance, $card->currency); ?></div>
                        </div>

                        <!-- Quick amounts -->
                        <div class="form-group basic">
                            <div class="d-flex flex-wrap gap-2">
                                <?php if ($card->balance >= 10): ?>
                                <button type="button" class="btn btn-outline-warning btn-sm" onclick="setAmount(10)">$10</button>
                                <?php endif; ?>
                                <?php if ($card->balance >= 20): ?>
                                <button type="button" class="btn btn-outline-warning btn-sm" onclick="setAmount(20)">$20</button>
                                <?php endif; ?>
                                <?php if ($card->balance >= 50): ?>
                                <button type="button" class="btn btn-outline-warning btn-sm" onclick="setAmount(50)">$50</button>
                                <?php endif; ?>
                                <?php if ($card->balance >= 100): ?>
                                <button type="button" class="btn btn-outline-warning btn-sm" onclick="setAmount(100)">$100</button>
                                <?php endif; ?>
                                <button type="button" class="btn btn-outline-warning btn-sm" onclick="setAmount(<?php echo $card->balance; ?>)">All</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-warning btn-block btn-lg">
                                <ion-icon name="download-outline"></ion-icon> Withdraw Funds
                            </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('withdrawAmount').value = amount;
    }
    </script>

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