<?php $title = 'Create Loan'; ?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold"><i class="fas fa-plus-circle mr-2"></i>Create Loan for Member</h1>
    <a href="<?= $baseUrl ?>/loans" class="glass-btn"><i class="fas fa-arrow-left mr-1"></i>Back to Loans</a>
</div>
<div class="glass-card">
    <form method="POST" action="<?= $baseUrl ?>/loans/admin-store" id="loanForm">
        <div class="form-group">
            <label style="color:rgba(255,255,255,0.7);">Member *</label>
            <select name="user_id" class="form-control glass-input" required>
                <option value="">-- Select Member --</option>
                <?php foreach ($members as $m): ?>
                <option value="<?= $m->id ?>"><?= htmlspecialchars($m->first_name ? trim($m->first_name . ' ' . $m->last_name) : $m->name) ?> (<?= htmlspecialchars($m->email) ?>)</option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="form-group">
            <label style="color:rgba(255,255,255,0.7);">Loan Type *</label>
            <select name="loan_type_id" id="loan_type_id" class="form-control glass-input" required>
                <option value="">-- Select Loan Type --</option>
                <?php foreach ($types as $t): ?>
                <option value="<?= $t->id ?>"
                    data-rate="<?= $t->interest_rate ?>"
                    data-min="<?= $t->min_amount ?>"
                    data-max="<?= $t->max_amount ?>"
                    data-min-dur="<?= $t->min_duration_months ?>"
                    data-max-dur="<?= $t->max_duration_months ?>">
                    <?= htmlspecialchars($t->name) ?> (<?= $t->interest_rate ?>% interest)
                </option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="row">
            <div class="col-md-4 form-group">
                <label style="color:rgba(255,255,255,0.7);">Amount (GHS) *</label>
                <input type="number" step="0.01" name="amount" id="amount" class="form-control glass-input" required>
                <small id="amountHelp" style="color:rgba(255,255,255,0.4);"></small>
            </div>
            <div class="col-md-4 form-group">
                <label style="color:rgba(255,255,255,0.7);">Interest Rate (%)</label>
                <input type="number" step="0.01" name="interest_rate" id="interest_rate" class="form-control glass-input" value="0">
            </div>
            <div class="col-md-4 form-group">
                <label style="color:rgba(255,255,255,0.7);">Duration (months) *</label>
                <input type="number" min="1" name="duration_months" id="duration_months" class="form-control glass-input" required>
                <small id="durationHelp" style="color:rgba(255,255,255,0.4);"></small>
            </div>
        </div>
        <div class="row">
            <div class="col-md-4 form-group">
                <label style="color:rgba(255,255,255,0.7);">Repayment Frequency *</label>
                <select name="repayment_frequency" id="repayment_frequency" class="form-control glass-input">
                    <option value="daily">Daily</option>
                    <option value="weekly">Weekly</option>
                    <option value="bi-weekly">Bi-Weekly</option>
                    <option value="monthly" selected>Monthly</option>
                    <option value="quarterly">Quarterly</option>
                </select>
            </div>
            <div class="col-md-4 form-group">
                <label style="color:rgba(255,255,255,0.7);">Status</label>
                <select name="status" class="form-control glass-input">
                    <option value="pending">Pending</option>
                    <option value="approved">Approved (generate schedule now)</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label style="color:rgba(255,255,255,0.7);">Purpose</label>
            <textarea name="purpose" class="form-control glass-input" rows="2" placeholder="Loan purpose..."></textarea>
        </div>
        <div class="form-group">
            <label style="color:rgba(255,255,255,0.7);">Admin Notes</label>
            <textarea name="notes" class="form-control glass-input" rows="2" placeholder="Internal notes..."></textarea>
        </div>
        <div id="calcSummary" class="p-3 mb-3 rounded" style="background:rgba(255,255,255,0.05);display:none;">
            <h6 class="text-white font-weight-bold">Loan Summary</h6>
            <div class="row text-center">
                <div class="col-3"><span id="calcInstallments" style="font-size:1.2rem;font-weight:700;color:#fff;">0</span><br><small style="color:rgba(255,255,255,0.5);">Installments</small></div>
                <div class="col-3"><span id="calcAmount" style="font-size:1.2rem;font-weight:700;color:#fff;">GHS 0</span><br><small style="color:rgba(255,255,255,0.5);">Principal</small></div>
                <div class="col-3"><span id="calcTotal" style="font-size:1.2rem;font-weight:700;color:#1cc88a;">GHS 0</span><br><small style="color:rgba(255,255,255,0.5);">Total Payable</small></div>
                <div class="col-3"><span id="calcPerInstallment" style="font-size:1.2rem;font-weight:700;color:#4e73df;">GHS 0</span><br><small style="color:rgba(255,255,255,0.5);">Per Installment</small></div>
            </div>
        </div>
        <div class="text-right">
            <a href="<?= $baseUrl ?>/loans" class="glass-btn mr-2">Cancel</a>
            <button type="submit" class="glass-btn glass-btn-primary"><i class="fas fa-save mr-1"></i>Create Loan</button>
        </div>
    </form>
</div>
<script>
var freqMultiplier = {daily:30, weekly:4, 'bi-weekly':2, monthly:1, quarterly:0.3333};
document.getElementById('loan_type_id').addEventListener('change', function() {
    var opt = this.options[this.selectedIndex];
    if (opt.value) {
        document.getElementById('interest_rate').value = opt.dataset.rate;
        document.getElementById('amount').min = opt.dataset.min;
        document.getElementById('amount').max = opt.dataset.max;
        document.getElementById('amount').placeholder = 'GHS ' + parseFloat(opt.dataset.min).toLocaleString() + ' - GHS ' + parseFloat(opt.dataset.max).toLocaleString();
        document.getElementById('amountHelp').textContent = 'Range: GHS ' + parseFloat(opt.dataset.min).toFixed(2) + ' - GHS ' + parseFloat(opt.dataset.max).toFixed(2);
        document.getElementById('duration_months').min = opt.dataset.minDur;
        document.getElementById('duration_months').max = opt.dataset.maxDur;
        document.getElementById('duration_months').placeholder = opt.dataset.minDur + ' - ' + opt.dataset.maxDur + ' months';
        document.getElementById('durationHelp').textContent = 'Range: ' + opt.dataset.minDur + ' - ' + opt.dataset.maxDur + ' months';
    }
    updateCalc();
});
document.getElementById('amount').addEventListener('input', updateCalc);
document.getElementById('interest_rate').addEventListener('input', updateCalc);
document.getElementById('duration_months').addEventListener('input', updateCalc);
document.getElementById('repayment_frequency').addEventListener('change', updateCalc);

function updateCalc() {
    var amount = parseFloat(document.getElementById('amount').value) || 0;
    var rate = parseFloat(document.getElementById('interest_rate').value) || 0;
    var months = parseInt(document.getElementById('duration_months').value) || 0;
    var freq = document.getElementById('repayment_frequency').value;
    if (!amount || !months) { document.getElementById('calcSummary').style.display = 'none'; return; }
    var total = amount + (amount * rate / 100);
    var mult = freqMultiplier[freq] || 1;
    var installments = Math.max(1, Math.round(months * mult));
    var perInstallment = installments > 0 ? total / installments : 0;
    document.getElementById('calcInstallments').textContent = installments;
    document.getElementById('calcAmount').textContent = 'GHS ' + amount.toFixed(2);
    document.getElementById('calcTotal').textContent = 'GHS ' + total.toFixed(2);
    document.getElementById('calcPerInstallment').textContent = 'GHS ' + perInstallment.toFixed(2);
    document.getElementById('calcSummary').style.display = '';
}
</script>
