<?php
/**
 * Fee Receipt Preview Page — Beautiful HTML preview with PDF download link
 *
 * Upload to: /gmind/branch/Views/fees/receipt_preview.php
 */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';

$payment = $payment ?? [];
$courseFee = $courseFee ?? 0;
$previousPaid = $previousPaid ?? 0;
$totalPaid = $totalPaid ?? 0;
$balanceDue = $balanceDue ?? 0;
$isFullPaid = $isFullPaid ?? false;
$studentId = $studentId ?? 0;
$paymentId = $paymentId ?? 0;

// Resolve branch logo
$logoPath = '';
if (!empty($payment['logo'])) {
    $candidates = [
        $payment['logo'],
        $_SERVER['DOCUMENT_ROOT'] . '/' . ltrim($payment['logo'], '/'),
        $_SERVER['DOCUMENT_ROOT'] . '/gmind/' . ltrim($payment['logo'], '/'),
        '/home/advaysin/public_html/' . ltrim($payment['logo'], '/'),
        '/home/advaysin/public_html/gmind/' . ltrim($payment['logo'], '/'),
    ];
    foreach ($candidates as $c) {
        if (file_exists($c)) {
            $logoPath = (defined('BASE_PATH') ? BASE_PATH : '/gmind') . '/' . ltrim($payment['logo'], '/');
            break;
        }
    }
}
?>
<style>
    .receipt-preview-container { max-width: 850px; margin: 0 auto; }
    .toolbar { background:#fff; border-radius:12px; padding:16px 24px; box-shadow:0 2px 12px rgba(0,0,0,0.06); margin-bottom:18px; display:flex; justify-content:space-between; align-items:center; flex-wrap:wrap; gap:12px; position:sticky; top:0; z-index:10; }
    .toolbar-title { font-weight:700; color:#16a34a; font-size:1rem; display:flex; align-items:center; gap:8px; }
    .toolbar-actions { display:flex; gap:8px; flex-wrap:wrap; }
    .btn-pdf { background:#dc2626; color:#fff; padding:10px 22px; border-radius:8px; font-weight:600; text-decoration:none; display:inline-flex; align-items:center; gap:8px; transition:transform 0.15s; }
    .btn-pdf:hover { color:#fff; transform:translateY(-1px); box-shadow:0 4px 12px rgba(220,38,38,0.3); }
    .btn-student { background:#6c3fe8; color:#fff; padding:10px 22px; border-radius:8px; font-weight:600; text-decoration:none; display:inline-flex; align-items:center; gap:8px; }
    .btn-student:hover { color:#fff; transform:translateY(-1px); }
    .btn-back { background:#f3f4f6; color:#6b7280; padding:10px 20px; border-radius:8px; font-weight:600; text-decoration:none; display:inline-flex; align-items:center; gap:6px; }

    .receipt-card { background:#fff; border-radius:12px; box-shadow:0 4px 24px rgba(0,0,0,0.06); overflow:hidden; }
    .receipt-header { background:linear-gradient(135deg,#16a085,#117a65); color:#fff; padding:24px 32px; text-align:center; position:relative; }
    .receipt-header img { position:absolute; left:32px; top:24px; max-height:60px; max-width:60px; border-radius:8px; background:#fff; padding:4px; }
    .receipt-header h1 { margin:0; font-size:1.8rem; font-weight:700; }
    .receipt-header p { margin:6px 0 0; opacity:0.9; font-size:0.88rem; }
    .receipt-title-bar { background:#f8f4ff; color:#6c3fe8; padding:10px; text-align:center; font-weight:700; font-size:1.1rem; letter-spacing:1px; }
    .receipt-body { padding:32px; }
    .detail-table { width:100%; border-collapse:collapse; margin-bottom:24px; }
    .detail-table td { padding:11px 16px; border:1px solid #e5e7eb; font-size:0.92rem; }
    .detail-table td.label { font-weight:600; background:#f8f4ff; color:#6c3fe8; width:35%; text-transform:uppercase; font-size:0.78rem; letter-spacing:0.5px; }
    .detail-table td.value { color:#2c1e4a; }

    .fee-table { width:100%; border-collapse:collapse; margin-bottom:24px; }
    .fee-table th { background:#6c3fe8; color:#fff; padding:10px 14px; font-size:0.85rem; text-align:left; }
    .fee-table td { padding:10px 14px; border:1px solid #e5e7eb; font-size:0.9rem; }
    .fee-table tr.highlight-green { background:#dcfce7; font-weight:600; }
    .fee-table tr.highlight-red { background:#fee2e2; font-weight:600; color:#dc2626; }
    .fee-table tr.highlight-amber { background:#fef3c7; font-weight:600; }

    .terms-box { background:#fef3c7; padding:14px 18px; border-left:4px solid #d97706; border-radius:6px; margin-top:20px; }
    .terms-box h4 { margin:0 0 8px; color:#b45309; font-size:0.95rem; }
    .terms-box ol { margin:0; padding-left:20px; color:#92400e; font-size:0.83rem; line-height:1.7; }

    .sign-row { display:flex; justify-content:space-between; margin-top:50px; padding:0 20px; }
    .sign-box { text-align:center; width:45%; }
    .sign-line { border-top:1px solid #2c1e4a; margin-top:50px; padding-top:6px; font-size:0.85rem; color:#6b7280; font-weight:600; }

    .footer { text-align:center; margin-top:28px; font-size:0.78rem; color:#999; border-top:1px solid #e5e7eb; padding-top:14px; }
    .full-paid-banner { background:linear-gradient(135deg,#16a34a,#15803d); color:#fff; padding:14px; text-align:center; border-radius:8px; margin-bottom:20px; font-weight:700; font-size:1.1rem; }
    @media print { .toolbar { display:none !important; } }
</style>

<div class="container-fluid py-4">
    <div class="receipt-preview-container">

        <!-- Toolbar with download link -->
        <div class="toolbar">
            <div class="toolbar-title">
                <i class="fas fa-check-circle" style="color:#16a34a; font-size:1.3rem;"></i>
                Fee Receipt Generated Successfully
            </div>
            <div class="toolbar-actions">
                <a href="<?= $bp ?>/branch/fees/receipt/<?= (int)$paymentId ?>" target="_blank" class="btn-pdf">
                    <i class="fas fa-file-pdf"></i> Download PDF Receipt
                </a>
                <a href="<?= $bp ?>/branch/students/<?= (int)$studentId ?>" class="btn-student">
                    <i class="fas fa-user"></i> View Student
                </a>
                <a href="<?= $bp ?>/branch/fees" class="btn-back">
                    <i class="fas fa-arrow-left"></i> Fee Dashboard
                </a>
            </div>
        </div>

        <?php if ($isFullPaid): ?>
        <div class="full-paid-banner">
            <i class="fas fa-check-circle"></i> FULL FEE PAID — No dues remaining
        </div>
        <?php endif; ?>

        <!-- Receipt Card -->
        <div class="receipt-card">
            <!-- Header -->
            <div class="receipt-header">
                <?php if (!empty($logoPath)): ?>
                    <img src="<?= htmlspecialchars($logoPath, ENT_QUOTES) ?>" alt="Logo">
                <?php endif; ?>
                <h1><?= htmlspecialchars(strtoupper($payment['name'] ?? 'GMIND INSTITUTE'), ENT_QUOTES) ?></h1>
                <p>
                    <?php
                    $addrParts = array_filter([
                        $payment['address'] ?? '',
                        $payment['city'] ?? '',
                        $payment['state'] ?? '',
                        $payment['pin_code'] ?? $payment['pin'] ?? '',
                    ]);
                    echo htmlspecialchars(implode(', ', $addrParts), ENT_QUOTES);
                    ?>
                </p>
                <?php if (!empty($payment['phone']) || !empty($payment['email'])): ?>
                <p>
                    <?php if (!empty($payment['phone'])): ?>Phone: <?= htmlspecialchars($payment['phone'], ENT_QUOTES) ?><?php endif; ?>
                    <?php if (!empty($payment['phone']) && !empty($payment['email'])): ?> | <?php endif; ?>
                    <?php if (!empty($payment['email'])): ?>Email: <?= htmlspecialchars($payment['email'], ENT_QUOTES) ?><?php endif; ?>
                </p>
                <?php endif; ?>
            </div>

            <div class="receipt-title-bar">FEE RECEIPT</div>

            <div class="receipt-body">
                <!-- Receipt No + Date -->
                <div style="display:flex; justify-content:space-between; margin-bottom:20px; font-size:0.9rem;">
                    <div><strong>Receipt No:</strong> <?= htmlspecialchars($payment['receipt_no'] ?? ('RCP-'.$paymentId), ENT_QUOTES) ?></div>
                    <div><strong>Date:</strong> <?= !empty($payment['payment_date']) ? date('d M Y', strtotime($payment['payment_date'])) : date('d M Y') ?></div>
                </div>

                <!-- Student details -->
                <table class="detail-table">
                    <tr>
                        <td class="label">Student Name</td>
                        <td class="value"><?= htmlspecialchars($payment['student_name'] ?? '—', ENT_QUOTES) ?></td>
                        <td class="label">Enrollment No</td>
                        <td class="value"><?= htmlspecialchars($payment['enrollment_no'] ?? '—', ENT_QUOTES) ?></td>
                    </tr>
                    <tr>
                        <td class="label">Course</td>
                        <td class="value"><?= htmlspecialchars($payment['course_name'] ?? '—', ENT_QUOTES) ?></td>
                        <td class="label">Father's Name</td>
                        <td class="value"><?= htmlspecialchars($payment['father_name'] ?? '—', ENT_QUOTES) ?></td>
                    </tr>
                    <tr>
                        <td class="label">Phone</td>
                        <td class="value"><?= htmlspecialchars($payment['phone'] ?? '—', ENT_QUOTES) ?></td>
                        <td class="label">Branch Code</td>
                        <td class="value"><?= htmlspecialchars($payment['branch_code'] ?? '—', ENT_QUOTES) ?></td>
                    </tr>
                </table>

                <!-- Fee breakdown -->
                <table class="fee-table">
                    <thead>
                        <tr>
                            <th>Description</th>
                            <th style="text-align:right;">Amount (Rs.)</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>Total Course Fee</td>
                            <td style="text-align:right;"><?= number_format($courseFee, 2) ?></td>
                        </tr>
                        <tr>
                            <td>Previous Paid Amount</td>
                            <td style="text-align:right;"><?= number_format($previousPaid, 2) ?></td>
                        </tr>
                        <tr class="highlight-green">
                            <td>Current Payment (<?= htmlspecialchars(ucfirst($payment['payment_mode'] ?? 'cash'), ENT_QUOTES) ?>)</td>
                            <td style="text-align:right;"><?= number_format((float)$payment['amount'], 2) ?></td>
                        </tr>
                        <tr>
                            <td>Total Paid Till Date</td>
                            <td style="text-align:right;"><?= number_format($totalPaid, 2) ?></td>
                        </tr>
                        <tr class="<?= $isFullPaid ? 'highlight-amber' : 'highlight-red' ?>">
                            <td><?= $isFullPaid ? 'Balance Due (FULL PAID)' : 'Balance Due' ?></td>
                            <td style="text-align:right;"><?= number_format($balanceDue, 2) ?></td>
                        </tr>
                    </tbody>
                </table>

                <!-- Transaction details -->
                <?php if (!empty($payment['transaction_id'])): ?>
                <div style="margin-bottom:8px;"><strong>Transaction ID:</strong> <?= htmlspecialchars($payment['transaction_id'], ENT_QUOTES) ?></div>
                <?php endif; ?>
                <?php if (!empty($payment['remarks'])): ?>
                <div style="margin-bottom:8px;"><strong>Remarks:</strong> <?= htmlspecialchars($payment['remarks'], ENT_QUOTES) ?></div>
                <?php endif; ?>

                <!-- Terms & Conditions -->
                <div class="terms-box">
                    <h4><i class="fas fa-file-contract"></i> TERMS & CONDITIONS</h4>
                    <ol>
                        <li>Fee once paid will <strong>NOT</strong> be refundable under any circumstances.</li>
                        <li>Certificate will be issued only after full fee payment and course completion.</li>
                        <li>This is a computer-generated receipt — verify online via QR code on PDF.</li>
                        <li>Please retain this receipt for future reference.</li>
                    </ol>
                </div>

                <!-- Signatures -->
                <div class="sign-row">
                    <div class="sign-box">
                        <div class="sign-line">Student / Guardian Signature</div>
                    </div>
                    <div class="sign-box">
                        <div class="sign-line">Authorized Signatory</div>
                    </div>
                </div>

                <div class="footer">
                    Generated on <?= date('d M Y, h:i A') ?> • GMIND Institute Management System
                </div>
            </div>
        </div>

        <!-- Bottom action bar -->
        <div style="text-align:center; margin-top:24px;">
            <a href="<?= $bp ?>/branch/fees/receipt/<?= (int)$paymentId ?>" target="_blank" class="btn-pdf">
                <i class="fas fa-download"></i> Download PDF Receipt with QR Code
            </a>
        </div>
    </div>
</div>
