<?php
/**
 * Student Details View — with photo, fee details, and payment history
 *
 * Upload to: /gmind/branch/Views/students/show.php
 */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
$csrf = htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8');

// Resolve photo path
$photoPath = '';
if (!empty($student['photo'])) {
    $photoPath = $student['photo'];
    // Try multiple paths
    $candidates = [
        $photoPath,
        $_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 $c) {
        if (file_exists($c)) {
            // Build web URL
            $photoPath = (defined('BASE_PATH') ? BASE_PATH : '/gmind') . '/' . ltrim($photoPath, '/');
            break;
        }
    }
}
?>
<style>
    .student-detail-card { background:#fff; border-radius:12px; box-shadow:0 2px 8px rgba(0,0,0,0.04); margin-bottom:18px; }
    .student-detail-card .card-header { background:linear-gradient(135deg,#6c3fe8,#4a2db8); color:#fff; border-radius:12px 12px 0 0 !important; padding:16px 22px; font-weight:700; }
    .student-detail-card .card-body { padding:24px; }
    .detail-grid { display:grid; grid-template-columns:repeat(auto-fit, minmax(220px, 1fr)); gap:18px; }
    .detail-item { padding:10px 0; }
    .detail-label { display:block; font-size:0.78rem; font-weight:600; color:#6b7280; text-transform:uppercase; letter-spacing:0.5px; margin-bottom:4px; }
    .detail-value { display:block; font-size:0.95rem; color:#2c1e4a; font-weight:600; }
    .photo-box { width:100%; max-width:160px; margin:0 auto 14px; aspect-ratio:3/4; border-radius:10px; overflow:hidden; border:3px solid #e9ecef; 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:4rem; color:#d0d5dd; }
    .fee-stat-card { background:#fff; border-radius:10px; padding:18px; text-align:center; box-shadow:0 2px 6px rgba(0,0,0,0.04); }
    .fee-stat-card .stat-num { font-size:1.4rem; font-weight:700; }
    .fee-stat-card .stat-label { font-size:0.78rem; color:#6b7280; margin-top:4px; text-transform:uppercase; letter-spacing:0.5px; }
    .btn-collect-fee { background:linear-gradient(135deg,#16a085,#117a65); color:#fff; padding:10px 22px; border-radius:8px; font-weight:600; text-decoration:none; display:inline-flex; align-items:center; gap:8px; }
    .btn-collect-fee:hover { color:#fff; transform:translateY(-1px); }
    .btn-edit { background:#fef3c7; color:#d97706; padding:8px 18px; border-radius:6px; text-decoration:none; font-weight:600; }
    .btn-back { background:#f3f4f6; color:#6b7280; padding:8px 16px; border-radius:6px; text-decoration:none; font-weight:600; }
    .fee-table { width:100%; border-collapse:collapse; }
    .fee-table thead th { background:#f8f4ff; color:#6c3fe8; font-weight:700; font-size:0.78rem; text-transform:uppercase; padding:10px 12px; border-bottom:2px solid #6c3fe8; text-align:left; }
    .fee-table tbody td { padding:11px 12px; border-bottom:1px solid #f1f1f1; font-size:0.88rem; }
    .badge-mode { padding:3px 10px; border-radius:12px; font-size:0.72rem; font-weight:700; text-transform:uppercase; }
    .badge-cash { background:#f3f4f6; color:#4b5563; }
    .badge-online { background:#dbeafe; color:#2563eb; }
    .badge-upi { background:#dcfce7; color:#16a34a; }
    .badge-wallet { background:#fef3c7; color:#d97706; }
    .badge-pending { background:#fee2e2; color:#dc2626; }
</style>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
        <div>
            <h2 style="color:#2c1e4a; font-weight:700; margin:0;">
                <i class="fas fa-user-graduate"></i> Student Details
            </h2>
            <p class="text-muted mb-0" style="font-size:0.88rem;">
                <?= htmlspecialchars($student['enrollment_no'] ?? '', ENT_QUOTES) ?> —
                <?= htmlspecialchars($student['name'] ?? '', ENT_QUOTES) ?>
            </p>
        </div>
        <div class="d-flex gap-2 flex-wrap">
            <a href="<?= $bp ?>/branch/students" class="btn-back"><i class="fas fa-arrow-left"></i> Back</a>
            <a href="<?= $bp ?>/branch/students/<?= (int)$student['id'] ?>/edit" class="btn-edit"><i class="fas fa-edit"></i> Edit</a>
            <a href="<?= $bp ?>/branch/fees/collect/<?= (int)$student['id'] ?>" class="btn-collect-fee">
                <i class="fas fa-hand-holding-usd"></i> Collect Fee
            </a>
        </div>
    </div>

    <!-- Personal Information + Photo -->
    <div class="card student-detail-card">
        <div class="card-header">
            <i class="fas fa-id-card"></i> Personal Information
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-2 text-center">
                    <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>
                <div class="col-md-10">
                    <div class="detail-grid">
                        <div class="detail-item">
                            <span class="detail-label">Enrollment No</span>
                            <span class="detail-value"><?= htmlspecialchars($student['enrollment_no'] ?? '—', ENT_QUOTES) ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Full Name</span>
                            <span class="detail-value"><?= htmlspecialchars($student['name'] ?? '—', ENT_QUOTES) ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Email</span>
                            <span class="detail-value"><?= htmlspecialchars($student['email'] ?? '—', ENT_QUOTES) ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Phone</span>
                            <span class="detail-value"><?= htmlspecialchars($student['phone'] ?? '—', ENT_QUOTES) ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Course</span>
                            <span class="detail-value"><?= htmlspecialchars($student['course_name'] ?? '—', ENT_QUOTES) ?> <?= !empty($student['duration']) ? '(' . htmlspecialchars($student['duration'], ENT_QUOTES) . ')' : '' ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Batch</span>
                            <span class="detail-value">
                                <?= htmlspecialchars($student['batch_name'] ?? '—', ENT_QUOTES) ?>
                                <?php if (!empty($student['start_time'])): ?>
                                    <small style="color:#6b7280;"><?= htmlspecialchars($student['start_time'], ENT_QUOTES) ?> - <?= htmlspecialchars($student['end_time'] ?? '', ENT_QUOTES) ?></small>
                                <?php endif; ?>
                            </span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Date of Birth</span>
                            <span class="detail-value"><?= !empty($student['dob']) ? date('d M Y', strtotime($student['dob'])) : 'N/A' ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Admission Date</span>
                            <span class="detail-value"><?= !empty($student['admission_date']) ? date('d M Y', strtotime($student['admission_date'])) : 'N/A' ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Father's Name</span>
                            <span class="detail-value"><?= htmlspecialchars($student['father_name'] ?? 'N/A', ENT_QUOTES) ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Mother's Name</span>
                            <span class="detail-value"><?= htmlspecialchars($student['mother_name'] ?? 'N/A', ENT_QUOTES) ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Guardian Name</span>
                            <span class="detail-value"><?= htmlspecialchars($student['guardian_name'] ?? 'N/A', ENT_QUOTES) ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Guardian Phone</span>
                            <span class="detail-value"><?= htmlspecialchars($student['guardian_phone'] ?? 'N/A', ENT_QUOTES) ?></span>
                        </div>
                        <div class="detail-item">
                            <span class="detail-label">Status</span>
                            <span class="detail-value">
                                <span class="badge-mode badge-<?= ($student['status'] ?? '') === 'active' ? 'upi' : 'pending' ?>">
                                    <?= htmlspecialchars(ucfirst($student['status'] ?? 'active'), ENT_QUOTES) ?>
                                </span>
                            </span>
                        </div>
                        <div class="detail-item" style="grid-column: span 2;">
                            <span class="detail-label">Address</span>
                            <span class="detail-value" style="font-weight:400;">
                                <?= htmlspecialchars($student['address'] ?? '', ENT_QUOTES) ?>
                                <?= !empty($student['city']) ? ', ' . htmlspecialchars($student['city'], ENT_QUOTES) : '' ?>
                                <?= !empty($student['state']) ? ', ' . htmlspecialchars($student['state'], ENT_QUOTES) : '' ?>
                                <?= !empty($student['pin_code']) ? ' - ' . htmlspecialchars($student['pin_code'], ENT_QUOTES) : '' ?>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Fee Summary -->
    <div class="card student-detail-card">
        <div class="card-header" style="background:linear-gradient(135deg,#16a085,#117a65);">
            <i class="fas fa-rupee-sign"></i> Fee Summary
            <?php
            $feeDueVal = (float)($student['fee_due'] ?? 0);
            $feePaidVal = (float)($student['fee_paid'] ?? 0);
            $courseFeeVal = (float)($student['course_fee'] ?? 0);
            $isFullyPaid = ($feeDueVal <= 0 && $courseFeeVal > 0);
            $isPartial = ($feePaidVal > 0 && $feeDueVal > 0);
            $isUnpaid = ($feePaidVal <= 0 && $courseFeeVal > 0);
            ?>
            <span style="float:right; background:<?= $isFullyPaid ? '#16a34a' : ($isPartial ? '#d97706' : '#dc2626') ?>; color:#fff; padding:4px 14px; border-radius:14px; font-size:0.75rem; font-weight:700; text-transform:uppercase; letter-spacing:0.5px;">
                <?= $isFullyPaid ? '✓ FULL PAID' : ($isPartial ? 'PARTIAL' : 'UNPAID') ?>
            </span>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-4">
                    <div class="fee-stat-card" style="border-top:3px solid #6c3fe8;">
                        <div class="stat-num" style="color:#6c3fe8;">&#8377; <?= number_format($courseFeeVal, 2) ?></div>
                        <div class="stat-label">Total Fee</div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="fee-stat-card" style="border-top:3px solid #16a34a;">
                        <div class="stat-num" style="color:#16a34a;">&#8377; <?= number_format($feePaidVal, 2) ?></div>
                        <div class="stat-label">Fee Paid</div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="fee-stat-card" style="border-top:3px solid <?= $isFullyPaid ? '#16a34a' : '#dc2626' ?>;">
                        <div class="stat-num" style="color:<?= $isFullyPaid ? '#16a34a' : '#dc2626' ?>;">&#8377; <?= number_format($feeDueVal, 2) ?></div>
                        <div class="stat-label"><?= $isFullyPaid ? 'No Due' : 'Fee Due' ?></div>
                    </div>
                </div>
            </div>
            <div class="text-center mt-4">
                <?php if ($isFullyPaid): ?>
                <div style="background:linear-gradient(135deg,#16a34a,#15803d); color:#fff; padding:14px; border-radius:8px; display:inline-block; font-weight:700;">
                    <i class="fas fa-check-circle"></i> All dues cleared. No further collection allowed.
                </div>
                <?php else: ?>
                <a href="<?= $bp ?>/branch/fees/collect/<?= (int)$student['id'] ?>" class="btn-collect-fee">
                    <i class="fas fa-hand-holding-usd"></i> Collect Fee
                </a>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Fee Payment History -->
    <div class="card student-detail-card">
        <div class="card-header" style="background:linear-gradient(135deg,#d97706,#b45309);">
            <i class="fas fa-history"></i> Fee Payment History
        </div>
        <div class="card-body">
            <?php if (!empty($feePayments)): ?>
            <div class="table-responsive">
                <table class="fee-table">
                    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Receipt No</th>
                            <th>Amount</th>
                            <th>Mode</th>
                            <th>Transaction ID</th>
                            <th>Remarks</th>
                            <th style="text-align:right;">Receipt</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($feePayments as $p):
                            $mode = $p['payment_mode'] ?? 'pending';
                            $modeCls = 'badge-' . $mode;
                        ?>
                        <tr>
                            <td><?= !empty($p['payment_date']) ? date('d M Y', strtotime($p['payment_date'])) : '—' ?></td>
                            <td><?= htmlspecialchars($p['receipt_no'] ?? ('RCP-'.$p['id']), ENT_QUOTES) ?></td>
                            <td style="font-weight:600; color:#16a34a;">&#8377; <?= number_format((float)($p['amount'] ?? 0), 2) ?></td>
                            <td><span class="badge-mode <?= $modeCls ?>"><?= htmlspecialchars(ucfirst($mode), ENT_QUOTES) ?></span></td>
                            <td><?= htmlspecialchars($p['transaction_id'] ?? '—', ENT_QUOTES) ?></td>
                            <td><?= htmlspecialchars($p['remarks'] ?? '—', ENT_QUOTES) ?></td>
                            <td style="text-align:right;">
                                <?php if ($mode !== 'pending' && (float)$p['amount'] > 0): ?>
                                <a href="<?= $bp ?>/branch/fees/receipt/<?= (int)$p['id'] ?>" target="_blank" class="btn-fee-action" style="background:#dbeafe; color:#2563eb; padding:5px 10px; border-radius:6px; font-size:0.75rem; text-decoration:none;">
                                    <i class="fas fa-file-pdf"></i> View
                                </a>
                                <?php else: ?>
                                <span style="color:#9ca3af;">—</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <div style="text-align:center; padding:30px; color:#9ca3af;">
                <i class="fas fa-inbox" style="font-size:2.5rem; opacity:0.3;"></i>
                <p class="mt-2 mb-0">No payment history.</p>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Exam Results -->
    <?php if (!empty($examResults)): ?>
    <div class="card student-detail-card">
        <div class="card-header" style="background:linear-gradient(135deg,#2563eb,#1d4ed8);">
            <i class="fas fa-graduation-cap"></i> Exam Results
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="fee-table">
                    <thead>
                        <tr>
                            <th>Exam Title</th>
                            <th>Total Marks</th>
                            <th>Obtained</th>
                            <th>Percentage</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($examResults as $r):
                            $obtained = (float)($r['marks_obtained'] ?? $r['obtained_marks'] ?? 0);
                            $total = (float)($r['total_marks'] ?? 0);
                            $percentage = $total > 0 ? round(($obtained / $total) * 100, 1) : 0;
                            $passed = $percentage >= 40;
                        ?>
                        <tr>
                            <td><?= htmlspecialchars($r['exam_title'] ?? '—', ENT_QUOTES) ?></td>
                            <td><?= (int)$total ?></td>
                            <td><?= htmlspecialchars(number_format($obtained, 1), ENT_QUOTES) ?></td>
                            <td><?= $percentage ?>%</td>
                            <td>
                                <span class="badge-mode badge-<?= $passed ? 'upi' : 'pending' ?>">
                                    <?= $passed ? 'Pass' : 'Fail' ?>
                                </span>
                            </td>
                            <td><?= htmlspecialchars(date('d M Y', strtotime($r['created_at'] ?? 'now')), ENT_QUOTES) ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>
