<?php
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$r = $receipt ?? []; $allPayments = $allPayments ?? []; $feeSummary = $feeSummary ?? [];
?>
<style>
@media print { .no-print { display:none !important; } .receipt-card { box-shadow:none !important; border:2px solid #000 !important; } }
</style>
<div class="admin-section">
    <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:14px;" class="no-print">
        <a href="<?= $bp ?>/branch/fees" style="color:#16a085;text-decoration:none;font-size:0.85rem;">← Back to Fees</a>
        <button onclick="window.print()" style="padding:8px 18px;background:#16a085;color:#fff;border:none;border-radius:8px;font-size:0.85rem;cursor:pointer;font-weight:600;">🖨 Print / Save PDF</button>
    </div>

    <div class="receipt-card" style="max-width:680px;margin:0 auto;background:#fff;border-radius:12px;padding:40px;box-shadow:0 2px 12px rgba(0,0,0,0.08);">
        <!-- Header -->
        <div style="text-align:center;border-bottom:3px double #16a085;padding-bottom:16px;margin-bottom:20px;">
            <h1 style="margin:0;font-family:'Poppins',sans-serif;font-size:1.6rem;color:#0d3b3b;">Gmind Institute</h1>
            <p style="margin:4px 0 0;font-size:0.82rem;color:#666;">
                <?= htmlspecialchars($r['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?> (<?= htmlspecialchars($r['branch_code'] ?? '', ENT_QUOTES, 'UTF-8') ?>)<br>
                <?= htmlspecialchars($r['branch_address'] ?? '', ENT_QUOTES, 'UTF-8') ?> · 📞 <?= htmlspecialchars($r['branch_phone'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                <?php if (!empty($r['gst_number'])): ?><br>GST: <?= htmlspecialchars($r['gst_number'], ENT_QUOTES, 'UTF-8') ?><?php endif; ?>
            </p>
            <h2 style="margin:14px 0 0;font-size:1.1rem;color:#16a085;text-transform:uppercase;letter-spacing:2px;">Fee Receipt</h2>
        </div>

        <!-- Receipt Details -->
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:10px;margin-bottom:16px;font-size:0.86rem;">
            <div><strong>Receipt No:</strong> <?= htmlspecialchars($r['receipt_number'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
            <div style="text-align:right;"><strong>Date:</strong> <?= date('d/m/Y g:i A', strtotime($r['payment_date'] ?? 'now')) ?></div>
            <div><strong>Student:</strong> <?= htmlspecialchars($r['student_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
            <div style="text-align:right;"><strong>Enrollment:</strong> <?= htmlspecialchars($r['enrollment_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
            <div><strong>Course:</strong> <?= htmlspecialchars($r['course_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
            <div style="text-align:right;"><strong>Payment Mode:</strong> <?= htmlspecialchars(ucfirst($r['payment_method'] ?? 'cash'), ENT_QUOTES, 'UTF-8') ?></div>
            <?php if (!empty($r['transaction_ref'])): ?>
            <div><strong>Transaction Ref:</strong> <?= htmlspecialchars($r['transaction_ref'], ENT_QUOTES, 'UTF-8') ?></div>
            <?php endif; ?>
            <?php if (!empty($r['father_name'])): ?>
            <div style="text-align:right;"><strong>Father's Name:</strong> <?= htmlspecialchars($r['father_name'], ENT_QUOTES, 'UTF-8') ?></div>
            <?php endif; ?>
        </div>

        <!-- Amount Box -->
        <div style="background:#eafaf1;border:2px solid #27ae60;border-radius:10px;padding:20px;text-align:center;margin:20px 0;">
            <div style="font-size:0.82rem;color:#666;">Amount Received</div>
            <div style="font-size:2rem;font-weight:700;color:#27ae60;font-family:'Poppins',sans-serif;">₹ <?= number_format((float)($r['amount'] ?? 0), 2) ?></div>
            <div style="font-size:0.78rem;color:#999;margin-top:4px;"><?= htmlspecialchars(ucfirst($r['payment_method'] ?? 'cash'), ENT_QUOTES, 'UTF-8') ?> payment</div>
        </div>

        <!-- Fee Summary -->
        <table style="width:100%;border-collapse:collapse;font-size:0.86rem;margin-bottom:16px;">
            <tr style="background:#f8f9fa;"><td style="padding:8px 12px;">Total Course Fee</td><td style="padding:8px 12px;text-align:right;">₹ <?= number_format((float)($feeSummary['fee_amount'] ?? 0), 2) ?></td></tr>
            <tr><td style="padding:8px 12px;">Total Paid (including this)</td><td style="padding:8px 12px;text-align:right;color:#27ae60;font-weight:600;">₹ <?= number_format((float)($feeSummary['fee_paid'] ?? 0), 2) ?></td></tr>
            <tr style="background:#fff5f5;"><td style="padding:8px 12px;font-weight:600;">Balance Due</td><td style="padding:8px 12px;text-align:right;font-weight:700;color:#e84118;">₹ <?= number_format((float)($feeSummary['fee_due'] ?? 0), 2) ?></td></tr>
        </table>

        <!-- All Payments History -->
        <?php if (count($allPayments) > 1): ?>
        <div style="margin-bottom:16px;">
            <h4 style="margin:0 0 6px;font-size:0.82rem;color:#0d3b3b;">Payment History</h4>
            <table style="width:100%;border-collapse:collapse;font-size:0.78rem;">
                <thead><tr style="border-bottom:1px solid #ddd;"><th style="padding:5px;text-align:left;">Date</th><th style="padding:5px;text-align:left;">Receipt</th><th style="padding:5px;text-align:right;">Amount</th></tr></thead>
                <tbody>
                <?php foreach ($allPayments as $ap): ?>
                <tr style="border-bottom:1px solid #f0f0f0;<?= (int)$ap['id']===(int)($r['id']??0)?'background:#eafaf1;font-weight:600;':'' ?>">
                    <td style="padding:5px;"><?= date('d/m/Y', strtotime($ap['payment_date'])) ?></td>
                    <td style="padding:5px;font-family:monospace;"><?= htmlspecialchars($ap['receipt_number'], ENT_QUOTES, 'UTF-8') ?></td>
                    <td style="padding:5px;text-align:right;">₹ <?= number_format((float)$ap['amount'], 0) ?></td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>

        <?php if (!empty($r['remarks'])): ?>
        <p style="font-size:0.82rem;color:#666;margin:10px 0;"><strong>Remarks:</strong> <?= htmlspecialchars($r['remarks'], ENT_QUOTES, 'UTF-8') ?></p>
        <?php endif; ?>

        <!-- Signature -->
        <div style="display:flex;justify-content:space-between;margin-top:40px;">
            <div style="font-size:0.8rem;color:#999;">Student Signature</div>
            <div style="text-align:center;font-size:0.8rem;color:#999;">Authorized Signatory<br><br>____________________<br><?= htmlspecialchars($r['owner_name'] ?? 'Branch Head', ENT_QUOTES, 'UTF-8') ?><br><?= htmlspecialchars($r['branch_name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
        </div>

        <div style="text-align:center;margin-top:20px;font-size:0.74rem;color:#bbb;border-top:1px solid #eee;padding-top:10px;">
            This is a computer-generated receipt. Verify at <?= $bp ?>/verify · Gmind Institute
        </div>
    </div>
</div>
