<?php 
$this->load->view('layouts/header');
$this->load->helper('layout');
$currencies = ['USD', 'EUR', 'GBP', 'NGN', 'CAD', 'AUD', 'CHF', 'JPY', 'ZAR'];
$countries = ['United States', 'United Kingdom', 'Germany', 'France', 'Canada', 'Australia', 'Nigeria', 'South Africa', 'Kenya', 'Ghana', 'Other'];
?>

    <!-- App Header -->
    <div class="appHeader">
        <div class="left">
            <a href="<?php echo base_url('transfers'); ?>" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">International Transfer</div>
        <div class="right"></div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <?php if ($this->session->flashdata('error')): ?>
        <div class="section mt-2">
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <?php echo $this->session->flashdata('error'); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        </div>
        <?php endif; ?>

        <div class="section mt-2">
            <div class="section-title">Send Money Abroad</div>
            <p class="text-muted small">After verification, your transfer will be submitted with <strong>pending</strong> status for processing.</p>
            <form action="<?php echo base_url('transfers/international'); ?>" method="POST" id="transferForm">
                <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
                <input type="hidden" name="exchange_rate" id="exchangeRate" value="1">
                
                <div class="card">
                    <div class="card-body">
                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">From Account</label>
                                <select class="form-control custom-select" name="source_account" id="sourceAccount" required>
                                    <option value="">Select Account</option>
                                    <?php foreach ($accounts as $account): ?>
                                    <option value="<?php echo $account->id; ?>">
                                        <?php echo $account->account_number; ?> - <?php echo format_money($account->available_balance ?? 0, $account->currency); ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>

                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Select Beneficiary (optional)</label>
                                <select class="form-control" id="beneficiarySelect">
                                    <option value="">Enter manually below</option>
                                    <?php foreach ($beneficiaries as $b): ?>
                                    <option value="<?php echo htmlspecialchars($b->account_number); ?>" 
                                            data-name="<?php echo htmlspecialchars($b->name); ?>" 
                                            data-swift="<?php echo htmlspecialchars($b->bank_code ?? ''); ?>" 
                                            data-country="<?php echo htmlspecialchars($b->bank_name ?? ''); ?>"
                                            data-currency="<?php echo htmlspecialchars($b->currency ?? 'USD'); ?>">
                                        <?php echo htmlspecialchars($b->name); ?> - <?php echo $b->account_number; ?>
                                    </option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>

                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Recipient Name</label>
                                <input type="text" class="form-control" name="recipient_name" id="recipientName" placeholder="Full name" required value="<?php echo isset($to_name) ? htmlspecialchars($to_name) : ''; ?>">
                            </div>
                        </div>

                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Recipient Account / IBAN</label>
                                <input type="text" class="form-control" name="recipient_account" id="recipientAccount" placeholder="Account number or IBAN" required value="<?php echo isset($to_account) ? htmlspecialchars($to_account) : ''; ?>">
                            </div>
                        </div>

                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Bank SWIFT / BIC Code</label>
                                <input type="text" class="form-control" name="recipient_bank_swift" id="recipientBankSwift" placeholder="e.g. CHASUS33" required value="<?php echo isset($to_swift) ? htmlspecialchars($to_swift) : ''; ?>">
                            </div>
                        </div>

                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Country</label>
                                <select class="form-control" name="recipient_country" id="recipientCountry" required>
                                    <option value="">Select Country</option>
                                    <?php foreach ($countries as $c): ?>
                                    <option value="<?php echo htmlspecialchars($c); ?>" <?php echo (isset($to_country) && $to_country === $c) ? 'selected' : ''; ?>><?php echo $c; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>

                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Recipient Currency</label>
                                <select class="form-control" name="recipient_currency" id="recipientCurrency">
                                    <?php foreach ($currencies as $cur): ?>
                                    <option value="<?php echo $cur; ?>" <?php echo (isset($to_currency) && $to_currency === $cur) ? 'selected' : ''; ?>><?php echo $cur; ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>

                        <div class="form-group basic">
                            <label class="label">Amount (source account currency)</label>
                            <div class="input-group mb-2">
                                <span class="input-group-text">$</span>
                                <input type="number" class="form-control" name="amount" id="amount" placeholder="0.00" required min="1" step="0.01">
                            </div>
                        </div>

                        <div class="form-group basic">
                            <div class="input-wrapper">
                                <label class="label">Narration (Optional)</label>
                                <input type="text" class="form-control" name="narration" placeholder="What's this for?">
                            </div>
                        </div>
                    </div>
                </div>

                <div class="form-button-group transparent" style="bottom: 56px;">
                    <?php if ($user->user_type !== 'admin'): ?>
                    <button type="button" class="btn btn-primary btn-block btn-lg" id="submitBtn" onclick="initiateTransfer()">
                        Continue
                    </button>
                    <?php else: ?>
                    <button type="submit" class="btn btn-primary btn-block btn-lg" id="submitBtn">
                        Submit Transfer
                    </button>
                    <?php endif; ?>
                </div>
            </form>
        </div>
    </div>

    <?php if ($user->user_type !== 'admin'): ?>
    <?php $this->load->view('partials/otp_modal'); ?>
    <?php endif; ?>

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

<?php 
$extra_js = '
<script>
document.addEventListener("DOMContentLoaded", function() {
    document.getElementById("beneficiarySelect").addEventListener("change", function() {
        var opt = this.options[this.selectedIndex];
        if (opt.value) {
            document.getElementById("recipientName").value = opt.dataset.name || "";
            document.getElementById("recipientAccount").value = opt.value;
            document.getElementById("recipientBankSwift").value = opt.dataset.swift || "";
            document.getElementById("recipientCountry").value = opt.dataset.country || "";
            document.getElementById("recipientCurrency").value = opt.dataset.currency || "USD";
        }
    });
    if (typeof initOtpVerification === "function") {
        initOtpVerification("transferForm", "international_transfer", function() {
            document.getElementById("transferForm").submit();
        });
    }
});
function initiateTransfer() {
    var sourceAccount = document.getElementById("sourceAccount").value;
    var recipientName = document.getElementById("recipientName").value.trim();
    var recipientAccount = document.getElementById("recipientAccount").value.trim();
    var swift = document.getElementById("recipientBankSwift").value.trim();
    var country = document.getElementById("recipientCountry").value;
    var amount = document.getElementById("amount").value;
    if (!sourceAccount) { alert("Please select source account"); return; }
    if (!recipientName) { alert("Please enter recipient name"); return; }
    if (!recipientAccount) { alert("Please enter recipient account/IBAN"); return; }
    if (!swift) { alert("Please enter bank SWIFT/BIC code"); return; }
    if (!country) { alert("Please select country"); return; }
    if (!amount || parseFloat(amount) <= 0) { alert("Please enter a valid amount"); return; }
    requestOtpVerification();
}
</script>
';
$this->load->view('layouts/footer', ['extra_js' => $extra_js]); 
?>
