<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Verified — <?= htmlspecialchars($doc['student_name'] ?? '') ?> — Gmind Institute</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, sans-serif;
            padding: 20px 0;
        }
        .verify-card {
            background: #fff;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            max-width: 900px;
            margin: 20px auto;
            overflow: hidden;
        }
        .header-banner {
            background: linear-gradient(135deg, #2c1e4a, #6c3fe8);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .header-banner h1 { margin: 0; font-size: 28px; font-weight: 700; }
        .header-banner p { margin: 5px 0 0; opacity: 0.9; }
        .verify-badge {
            display: inline-block;
            background: #28a745;
            color: white;
            padding: 8px 24px;
            border-radius: 50px;
            font-weight: 600;
            margin-top: 15px;
            box-shadow: 0 4px 12px rgba(40, 167, 69, 0.4);
        }
        .verify-badge.invalid {
            background: #dc3545;
            box-shadow: 0 4px 12px rgba(220, 53, 69, 0.4);
        }
        .body-content { padding: 40px; }
        .student-header {
            display: flex;
            align-items: center;
            gap: 25px;
            padding: 25px;
            background: linear-gradient(135deg, #f8f9fa, #e9ecef);
            border-radius: 15px;
            margin-bottom: 30px;
        }
        .student-photo {
            width: 130px;
            height: 160px;
            border-radius: 10px;
            object-fit: cover;
            border: 4px solid #6c3fe8;
            background: #e0e0e0;
        }
        .photo-placeholder {
            width: 130px;
            height: 160px;
            border-radius: 10px;
            background: linear-gradient(135deg, #e0e0e0, #bdbdbd);
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 60px;
            color: #757575;
            border: 4px solid #6c3fe8;
        }
        .student-name {
            font-size: 28px;
            color: #2c1e4a;
            margin: 0 0 8px;
            font-weight: 700;
        }
        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin-bottom: 25px;
        }
        .info-item {
            padding: 15px;
            background: #f8f9fa;
            border-radius: 10px;
            border-left: 4px solid #6c3fe8;
        }
        .info-item .label {
            font-size: 12px;
            color: #6c757d;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 5px;
        }
        .info-item .value {
            font-size: 16px;
            color: #2c1e4a;
            font-weight: 600;
        }
        .subjects-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
        }
        .subjects-table th {
            background: #2c1e4a;
            color: white;
            padding: 12px;
            text-align: center;
            font-size: 14px;
        }
        .subjects-table td {
            padding: 10px 12px;
            border-bottom: 1px solid #e0e0e0;
            text-align: center;
        }
        .subjects-table tr:hover { background: #f8f9fa; }
        .total-row {
            background: #e3f2fd !important;
            font-weight: 700;
        }
        .grade-badge {
            display: inline-block;
            padding: 3px 12px;
            border-radius: 15px;
            font-size: 12px;
            font-weight: 600;
        }
        .grade-a-plus { background: #d4edda; color: #155724; }
        .grade-a { background: #cce5ff; color: #004085; }
        .grade-b { background: #fff3cd; color: #856404; }
        .grade-c { background: #f8d7da; color: #721c24; }
        .footer-note {
            background: #f8f9fa;
            padding: 20px;
            text-align: center;
            color: #6c757d;
            font-size: 13px;
            border-top: 1px solid #e0e0e0;
        }
        .doc-type-pill {
            display: inline-block;
            background: #6c3fe8;
            color: white;
            padding: 4px 14px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
        }
        @media (max-width: 768px) {
            .student-header { flex-direction: column; text-align: center; }
            .info-grid { grid-template-columns: 1fr; }
        }
    </style>
</head>
<body>
    <div class="verify-card">
        <div class="header-banner">
            <h1><i class="fas fa-shield-alt"></i> GMIND INSTITUTE</h1>
            <p>Official Certificate Verification</p>
            <div class="verify-badge <?= $hashValid ? '' : 'invalid' ?>">
                <i class="fas fa-<?= $hashValid ? 'check-circle' : 'exclamation-triangle' ?>"></i>
                <?= $hashValid ? 'VERIFIED AUTHENTIC DOCUMENT' : 'VERIFICATION WARNING' ?>
            </div>
        </div>

        <div class="body-content">
            <div class="text-center mb-3">
                <span class="doc-type-pill"><?= htmlspecialchars(ucfirst($doc['doc_type'] ?? 'Document')) ?></span>
            </div>

            <!-- Student Header -->
            <div class="student-header">
                <?php
                $photoPath = $doc['photo'] ?? '';
                $photoFound = false;
                if (!empty($photoPath)) {
                    $candidates = [
                        $_SERVER['DOCUMENT_ROOT'] . '/' . ltrim($photoPath, '/'),
                        $_SERVER['DOCUMENT_ROOT'] . '/gmind/' . ltrim($photoPath, '/'),
                        '/home/advaysin/public_html/' . ltrim($photoPath, '/'),
                        '/home/advaysin/public_html/gmind/' . ltrim($photoPath, '/'),
                    ];
                    foreach ($candidates as $p) {
                        if (file_exists($p)) {
                            $photoFound = true;
                            $photoUrl = (strpos($photoPath, '/') === 0 ? '' : '/') . $photoPath;
                            if (strpos($photoUrl, '/gmind/') !== 0 && strpos($photoUrl, '/uploads/') === 0) {
                                $photoUrl = '/gmind' . $photoUrl;
                            }
                            break;
                        }
                    }
                }
                ?>
                <?php if ($photoFound): ?>
                    <img src="<?= htmlspecialchars($photoUrl) ?>" class="student-photo" alt="Student Photo">
                <?php else: ?>
                    <div class="photo-placeholder"><i class="fas fa-user"></i></div>
                <?php endif; ?>

                <div>
                    <h2 class="student-name"><?= htmlspecialchars($doc['student_name'] ?? '') ?></h2>
                    <p class="mb-1"><i class="fas fa-id-card text-primary"></i> <strong>Doc No:</strong> <?= htmlspecialchars($doc['certificate_no'] ?? '') ?></p>
                    <?php if (!empty($doc['enrollment_no'])): ?>
                        <p class="mb-1"><i class="fas fa-hashtag text-primary"></i> <strong>Enrollment:</strong> <?= htmlspecialchars($doc['enrollment_no']) ?></p>
                    <?php endif; ?>
                    <p class="mb-0"><i class="fas fa-graduation-cap text-primary"></i> <strong>Course:</strong> <?= htmlspecialchars($doc['course_name'] ?? '') ?></p>
                </div>
            </div>

            <!-- Info Grid -->
            <div class="info-grid">
                <div class="info-item">
                    <div class="label">Father's Name</div>
                    <div class="value"><?= htmlspecialchars($doc['father_name'] ?? '—') ?></div>
                </div>
                <div class="info-item">
                    <div class="label">Branch Code</div>
                    <div class="value"><?= htmlspecialchars($doc['branch_code'] ?? '—') ?></div>
                </div>
                <div class="info-item">
                    <div class="label">Branch Name</div>
                    <div class="value"><?= htmlspecialchars($doc['branch_name'] ?? '—') ?></div>
                </div>
                <div class="info-item">
                    <div class="label">Course Duration</div>
                    <div class="value"><?= htmlspecialchars($doc['duration'] ?? '—') ?></div>
                </div>
                <div class="info-item">
                    <div class="label">Session</div>
                    <div class="value"><?= htmlspecialchars($fromLabel) ?> to <?= htmlspecialchars($toLabel) ?></div>
                </div>
                <div class="info-item">
                    <div class="label">Issue Date</div>
                    <div class="value"><?= htmlspecialchars($issueDate) ?></div>
                </div>
                <div class="info-item">
                    <div class="label">Percentage</div>
                    <div class="value"><?= htmlspecialchars($doc['percentage'] ?? '') ?>%</div>
                </div>
                <div class="info-item">
                    <div class="label">Grade</div>
                    <div class="value">
                        <span class="grade-badge grade-a"><?= htmlspecialchars($doc['grade'] ?? '') ?></span>
                    </div>
                </div>
                <div class="info-item">
                    <div class="label">Division</div>
                    <div class="value"><?= htmlspecialchars($doc['division'] ?? '—') ?></div>
                </div>
                <div class="info-item">
                    <div class="label">Status</div>
                    <div class="value">
                        <?php if ($doc['status'] === 'generated'): ?>
                            <span class="text-success"><i class="fas fa-check-circle"></i> Approved & Generated</span>
                        <?php else: ?>
                            <span class="text-warning"><i class="fas fa-clock"></i> <?= htmlspecialchars(ucfirst($doc['status'] ?? '')) ?></span>
                        <?php endif; ?>
                    </div>
                </div>
            </div>

            <!-- Subjects Table (Marksheet only) -->
            <?php if (!empty($subjects) && ($doc['doc_type'] ?? '') === 'marksheet'): ?>
            <h4 class="mb-3"><i class="fas fa-table"></i> Subject Marks</h4>
            <table class="subjects-table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Subject</th>
                        <th>Max</th>
                        <th>Obtained</th>
                        <th>%</th>
                        <th>Grade</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $totalMax = 0; $totalObt = 0;
                    foreach ($subjects as $i => $s):
                        $totalMax += $s['max'];
                        $totalObt += $s['obtained'];
                        $gradeClass = 'grade-b';
                        if ($s['percent'] >= 80) $gradeClass = 'grade-a-plus';
                        elseif ($s['percent'] >= 60) $gradeClass = 'grade-a';
                        elseif ($s['percent'] >= 45) $gradeClass = 'grade-b';
                        else $gradeClass = 'grade-c';
                    ?>
                    <tr>
                        <td><?= $i + 1 ?></td>
                        <td style="text-align:left;"><?= htmlspecialchars($s['name']) ?></td>
                        <td><?= $s['max'] ?></td>
                        <td><?= $s['obtained'] ?></td>
                        <td><?= $s['percent'] ?>%</td>
                        <td><span class="grade-badge <?= $gradeClass ?>"><?= $s['grade'] ?></span></td>
                    </tr>
                    <?php endforeach; ?>
                    <tr class="total-row">
                        <td colspan="2" style="text-align:right;">TOTAL</td>
                        <td><?= $totalMax ?></td>
                        <td><?= $totalObt ?></td>
                        <td><?= $totalMax > 0 ? round(($totalObt/$totalMax)*100, 1) : 0 ?>%</td>
                        <td><?= $doc['grade'] ?? '' ?></td>
                    </tr>
                </tbody>
            </table>
            <?php endif; ?>

            <!-- QR Code (optional) -->
            <?php if (!empty($doc['verification_url'])): ?>
            <div class="text-center mt-4">
                <img src="https://quickchart.io/qr?text=<?= urlencode($doc['verification_url']) ?>&size=120" alt="QR">
                <p class="small text-muted mt-2">Scan to verify again</p>
            </div>
            <?php endif; ?>
        </div>

        <div class="footer-note">
            <i class="fas fa-shield-alt"></i> This is a system-generated verification.
            For any queries, contact your branch or Gmind Institute.
            <br>© <?= date('Y') ?> Gmind Institute. All rights reserved.
        </div>
    </div>
</body>
</html>
