<?php
/**
 * Admission Preview Page — Beautiful HTML preview with PDF download link
 *
 * Shown after successful admission. Displays all submitted details + offers:
 *   - Download PDF admission form
 *   - View student profile
 *   - Back to students list
 *
 * Upload to: /gmind/branch/Views/students/admission_preview.php
 */

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

$student = $student ?? [];
$courseFee = $courseFee ?? 0;
$studentId = $studentId ?? 0;

// Resolve photo path
$photoPath = '';
if (!empty($student['photo'])) {
    $candidates = [
        $student['photo'],
        $_SERVER['DOCUMENT_ROOT'] . '/' . ltrim($student['photo'], '/'),
        $_SERVER['DOCUMENT_ROOT'] . '/gmind/' . ltrim($student['photo'], '/'),
        '/home/advaysin/public_html/' . ltrim($student['photo'], '/'),
        '/home/advaysin/public_html/gmind/' . ltrim($student['photo'], '/'),
    ];
    foreach ($candidates as $c) {
        if (file_exists($c)) {
            $photoPath = (defined('BASE_PATH') ? BASE_PATH : '/gmind') . '/' . ltrim($student['photo'], '/');
            break;
        }
    }
}
?>
<style>
    .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; }
    .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; }

    .admission-card { background:#fff; border-radius:12px; box-shadow:0 4px 24px rgba(0,0,0,0.06); overflow:hidden; }
    .admission-header { background:linear-gradient(135deg,#16a085,#117a65); color:#fff; padding:24px 32px; text-align:center; }
    .admission-header h1 { margin:0; font-size:1.8rem; font-weight:700; }
    .admission-header p { margin:6px 0 0; opacity:0.9; font-size:0.88rem; }
    .admission-title-bar { background:#f8f4ff; color:#6c3fe8; padding:10px; text-align:center; font-weight:700; font-size:1.1rem; letter-spacing:1px; }
    .admission-body { padding:32px; }

    .photo-section { text-align:center; margin-bottom:24px; }
    .photo-box { width:120px; height:150px; border:3px solid #16a085; border-radius:8px; margin:0 auto 8px; overflow:hidden; background:#f8f9fa; display:flex; align-items:center; justify-content:center; }
    .photo-box img { width:100%; height:100%; object-fit:cover; }
    .photo-box i { font-size:3rem; color:#d0d5dd; }
    .photo-label { font-size:0.78rem; color:#6b7280; text-transform:uppercase; letter-spacing:0.5px; }

    .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; }

    .terms-box { background:#fef3c7; padding:14px 18px; border-left:4px solid #d97706; border-radius:6px; margin-top:24px; }
    .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; }
    .success-banner { background:linear-gradient(135deg,#16a34a,#15803d); color:#fff; padding:18px; border-radius:10px; margin-bottom:20px; text-align:center; }
    .success-banner h3 { margin:0 0 4px; font-size:1.2rem; }
    .success-banner p { margin:0; opacity:0.95; font-size:0.88rem; }
    .email-status { background:#dbeafe; color:#1e40af; padding:10px 16px; border-radius:6px; margin-bottom:16px; font-size:0.85rem; display:flex; align-items:center; gap:8px; }
</style>

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

        <!-- Success Banner -->
        <div class="success-banner">
            <h3><i class="fas fa-check-circle"></i> Admission Successful!</h3>
            <p>Student has been admitted. Enrollment No: <strong><?= htmlspecialchars($student['enrollment_no'] ?? '', ENT_QUOTES) ?></strong></p>
        </div>

        <!-- Toolbar with download link -->
        <div class="toolbar">
            <div class="toolbar-title">
                <i class="fas fa-file-alt" style="color:#6c3fe8; font-size:1.3rem;"></i>
                Admission Form Preview — Review and Download PDF
            </div>
            <div class="toolbar-actions">
                <a href="<?= $bp ?>/branch/students/<?= (int)$studentId ?>/admission-pdf" target="_blank" class="btn-pdf">
                    <i class="fas fa-file-pdf"></i> Download PDF
                </a>
                <a href="<?= $bp ?>/branch/students/<?= (int)$studentId ?>" class="btn-student">
                    <i class="fas fa-user"></i> View Student
                </a>
                <a href="<?= $bp ?>/branch/students" class="btn-back">
                    <i class="fas fa-arrow-left"></i> All Students
                </a>
            </div>
        </div>

        <!-- Admission Card -->
        <div class="admission-card">
            <!-- Header -->
            <div class="admission-header">
                <h1>GMIND INSTITUTE</h1>
                <p>
                    <?= htmlspecialchars(strtoupper($student['branch_name'] ?? 'GMIND INSTITUTE'), ENT_QUOTES) ?>
                    <?php if (!empty($student['branch_code'])): ?> • Branch: <?= htmlspecialchars($student['branch_code'], ENT_QUOTES) ?><?php endif; ?>
                </p>
            </div>

            <div class="admission-title-bar">ADMISSION FORM</div>

            <div class="admission-body">
                <!-- Photo -->
                <div class="photo-section">
                    <div class="photo-box">
                        <?php if (!empty($photoPath)): ?>
                            <img src="<?= htmlspecialchars($photoPath, ENT_QUOTES) ?>" alt="Student Photo">
                        <?php else: ?>
                            <i class="fas fa-user"></i>
                        <?php endif; ?>
                    </div>
                    <div class="photo-label">Student Photo</div>
                </div>

                <!-- Detail table -->
                <table class="detail-table">
                    <tr>
                        <td class="label">Enrollment No</td>
                        <td class="value" style="font-weight:700; color:#16a085;"><?= htmlspecialchars($student['enrollment_no'] ?? '—', ENT_QUOTES) ?></td>
                        <td class="label">Admission Date</td>
                        <td class="value"><?= !empty($student['admission_date']) ? date('d M Y', strtotime($student['admission_date'])) : '—' ?></td>
                    </tr>
                    <tr>
                        <td class="label">Student Name</td>
                        <td class="value"><?= htmlspecialchars($student['name'] ?? '—', ENT_QUOTES) ?></td>
                        <td class="label">Father's Name</td>
                        <td class="value"><?= htmlspecialchars($student['father_name'] ?? '—', ENT_QUOTES) ?></td>
                    </tr>
                    <tr>
                        <td class="label">Email</td>
                        <td class="value"><?= htmlspecialchars($student['email'] ?? '—', ENT_QUOTES) ?></td>
                        <td class="label">Phone</td>
                        <td class="value"><?= htmlspecialchars($student['phone'] ?? '—', ENT_QUOTES) ?></td>
                    </tr>
                    <tr>
                        <td class="label">Date of Birth</td>
                        <td class="value"><?= !empty($student['dob']) ? date('d M Y', strtotime($student['dob'])) : '—' ?></td>
                        <td class="label">Course</td>
                        <td class="value"><?= htmlspecialchars($student['course_name'] ?? '—', ENT_QUOTES) ?> <?= !empty($student['duration']) ? '(' . htmlspecialchars($student['duration'], ENT_QUOTES) . ')' : '' ?></td>
                    </tr>
                    <tr>
                        <td class="label">Batch</td>
                        <td class="value"><?= htmlspecialchars($student['batch_name'] ?? '—', ENT_QUOTES) ?></td>
                        <td class="label">Total Course Fee</td>
                        <td class="value" style="font-weight:700; color:#6c3fe8;">&#8377; <?= number_format((float)$courseFee, 2) ?></td>
                    </tr>
                    <tr>
                        <td class="label">Guardian Name</td>
                        <td class="value"><?= htmlspecialchars($student['guardian_name'] ?? '—', ENT_QUOTES) ?></td>
                        <td class="label">Guardian Phone</td>
                        <td class="value"><?= htmlspecialchars($student['guardian_phone'] ?? '—', ENT_QUOTES) ?></td>
                    </tr>
                    <tr>
                        <td class="label">Address</td>
                        <td class="value" colspan="3">
                            <?= htmlspecialchars(trim(($student['address'] ?? '') . ', ' . ($student['city'] ?? '') . ', ' . ($student['state'] ?? '') . ' - ' . ($student['pin_code'] ?? '')), ENT_QUOTES) ?>
                        </td>
                    </tr>
                </table>

                <!-- 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>Student must maintain 75% attendance to be eligible for examination.</li>
                        <li>Certificate/Marksheet will be issued only after full fee payment and course completion.</li>
                        <li>The information provided above is true and correct to the best of my knowledge.</li>
                        <li>Institute reserves the right to cancel admission in case of false information.</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/students/<?= (int)$studentId ?>/admission-pdf" target="_blank" class="btn-pdf">
                <i class="fas fa-download"></i> Download PDF Admission Form
            </a>
        </div>
    </div>
</div>
