<?php
/**
 * Student List View — Modern Responsive Design with Photo Column
 *
 * Features:
 *   - Photo column (circular avatars)
 *   - Compact table layout (no overflow)
 *   - Color-coded fee status badges
 *   - Responsive — works on all screen sizes
 *
 * Upload to: /gmind/branch/Views/students/index.php
 */

$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>
<style>
    .student-page-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 12px;
        margin-bottom: 20px;
    }
    .student-page-header h2 {
        margin: 0;
        color: #2c1e4a;
        font-weight: 700;
        font-size: 1.4rem;
    }
    .btn-add-student {
        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;
        transition: transform 0.15s;
    }
    .btn-add-student:hover { color: #fff; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(22,160,133,0.3); }

    .student-filter-card {
        background: #fff;
        border-radius: 10px;
        padding: 16px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.04);
        margin-bottom: 16px;
    }
    .student-filter-row {
        display: flex;
        gap: 10px;
        flex-wrap: wrap;
        align-items: end;
    }
    .student-filter-row > div { flex: 1; min-width: 160px; }
    .student-filter-row label {
        display: block;
        font-size: 0.78rem;
        font-weight: 600;
        color: #6b7280;
        margin-bottom: 4px;
    }
    .student-filter-row input, .student-filter-row select {
        width: 100%;
        padding: 8px 12px;
        border: 1px solid #d0d5dd;
        border-radius: 6px;
        font-size: 0.85rem;
    }
    .student-filter-row .btn-filter {
        background: #6c3fe8;
        color: #fff;
        padding: 8px 18px;
        border: none;
        border-radius: 6px;
        font-size: 0.85rem;
        font-weight: 600;
        cursor: pointer;
        height: fit-content;
    }
    .student-filter-row .btn-reset {
        background: #f3f4f6;
        color: #6b7280;
        padding: 8px 16px;
        border: 1px solid #d0d5dd;
        border-radius: 6px;
        font-size: 0.85rem;
        text-decoration: none;
        font-weight: 600;
        height: fit-content;
    }

    .student-table-card {
        background: #fff;
        border-radius: 10px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.04);
        overflow: hidden;
    }
    .table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .student-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 0.85rem;
        min-width: 800px;
    }
    .student-table thead th {
        background: #f8f4ff;
        color: #6c3fe8;
        font-weight: 700;
        font-size: 0.74rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        padding: 12px 10px;
        text-align: left;
        white-space: nowrap;
        border-bottom: 2px solid #6c3fe8;
    }
    .student-table tbody td {
        padding: 10px 10px;
        border-bottom: 1px solid #f1f1f1;
        vertical-align: middle;
    }
    .student-table tbody tr:hover { background: #faf8ff; }
    .student-table tbody tr:last-child td { border-bottom: none; }

    /* Photo column */
    .student-avatar {
        width: 38px;
        height: 38px;
        border-radius: 50%;
        object-fit: cover;
        border: 2px solid #e9ecef;
        background: #f8f9fa;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        color: #9ca3af;
        font-size: 1rem;
        flex-shrink: 0;
    }

    /* Name cell */
    .student-name-cell { display: flex; align-items: center; gap: 10px; }
    .student-name-cell .name { font-weight: 600; color: #2c1e4a; white-space: nowrap; }
    .student-name-cell .email { font-size: 0.74rem; color: #9ca3af; white-space: nowrap; }

    /* Enrollment cell */
    .enrollment-badge {
        background: #f8f4ff;
        color: #6c3fe8;
        padding: 3px 8px;
        border-radius: 4px;
        font-family: monospace;
        font-size: 0.78rem;
        font-weight: 600;
        white-space: nowrap;
    }

    /* Phone */
    .student-phone { color: #4b5563; white-space: nowrap; }

    /* Course */
    .student-course { color: #4b5563; white-space: nowrap; max-width: 150px; overflow: hidden; text-overflow: ellipsis; }

    /* Fee columns */
    .fee-amount { color: #2c1e4a; font-weight: 600; white-space: nowrap; }
    .fee-paid { color: #16a34a; font-weight: 600; white-space: nowrap; }
    .fee-due { font-weight: 600; white-space: nowrap; }

    /* Status badges */
    .status-badge {
        display: inline-block;
        padding: 3px 10px;
        border-radius: 12px;
        font-size: 0.7rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.3px;
        white-space: nowrap;
    }
    .status-full-paid { background: #16a34a; color: #fff; }
    .status-partial   { background: #d97706; color: #fff; }
    .status-unpaid    { background: #dc2626; color: #fff; }
    .status-active    { background: #dcfce7; color: #16a34a; }
    .status-inactive  { background: #f3f4f6; color: #6b7280; }

    /* Actions */
    .actions-cell { white-space: nowrap; text-align: right; }
    .action-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 28px;
        height: 28px;
        border-radius: 6px;
        text-decoration: none;
        margin-left: 4px;
        cursor: pointer;
        border: none;
        font-size: 0.78rem;
    }
    .action-view    { background: #dbeafe; color: #2563eb; }
    .action-view:hover    { background: #bfdbfe; }
    .action-edit    { background: #fef3c7; color: #d97706; }
    .action-edit:hover    { background: #fde68a; }
    .action-fee     { background: #dcfce7; color: #16a34a; }
    .action-fee:hover     { background: #bbf7d0; }
    .action-delete  { background: #fee2e2; color: #dc2626; }
    .action-delete:hover  { background: #fecaca; }

    .empty-state {
        text-align: center;
        padding: 60px 20px;
        color: #9ca3af;
    }
    .empty-state i { font-size: 3rem; opacity: 0.3; margin-bottom: 12px; }
    .empty-state a { color: #16a085; font-weight: 600; }

    .table-footer {
        padding: 10px 16px;
        background: #f8f9fa;
        font-size: 0.8rem;
        color: #6b7280;
        text-align: center;
        border-top: 1px solid #f1f1f1;
    }

    /* Mobile responsive — convert table to cards */
    @media (max-width: 768px) {
        .student-table thead { display: none; }
        .student-table, .student-table tbody, .student-table tr, .student-table td {
            display: block;
            width: 100% !important;
            min-width: 0 !important;
        }
        .student-table tr {
            margin-bottom: 12px;
            border: 1px solid #e9ecef;
            border-radius: 8px;
            padding: 10px;
            background: #fff;
        }
        .student-table tbody td {
            border: none;
            padding: 6px 4px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            text-align: right;
        }
        .student-table tbody td::before {
            content: attr(data-label);
            font-weight: 600;
            color: #6c3fe8;
            text-align: left;
            font-size: 0.74rem;
            text-transform: uppercase;
            padding-right: 10px;
        }
        .student-name-cell { justify-content: flex-end; }
        .actions-cell { justify-content: flex-end; }
    }
</style>

<div class="container-fluid py-3">
    <!-- Page Header -->
    <div class="student-page-header">
        <h2><i class="fas fa-user-graduate"></i> Manage Students</h2>
        <a href="<?= $bp ?>/branch/students/create" class="btn-add-student">
            <i class="fas fa-plus"></i> Add Student
        </a>
    </div>

    <!-- Filter Card -->
    <div class="student-filter-card">
        <form method="GET" action="<?= $bp ?>/branch/students">
            <div class="student-filter-row">
                <div>
                    <label>Search</label>
                    <input type="text" name="search" placeholder="Name, enrollment, email..."
                           value="<?= htmlspecialchars($search ?? '', ENT_QUOTES, 'UTF-8') ?>">
                </div>
                <div>
                    <label>Course</label>
                    <select name="course_id">
                        <option value="">All Courses</option>
                        <?php foreach ($courses ?? [] as $c): ?>
                        <option value="<?= (int)($c['id'] ?? 0) ?>" <?= ($courseFilter ?? '') == ($c['id'] ?? '') ? 'selected' : '' ?>>
                            <?= htmlspecialchars($c['name'] ?? '', ENT_QUOTES, 'UTF-8') ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div style="flex: 0 0 auto;">
                    <button type="submit" class="btn-filter"><i class="fas fa-filter"></i> Filter</button>
                </div>
                <div style="flex: 0 0 auto;">
                    <a href="<?= $bp ?>/branch/students" class="btn-reset">Reset</a>
                </div>
            </div>
        </form>
    </div>

    <!-- Students Table -->
    <div class="student-table-card">
        <?php if (!empty($students)): ?>
        <div class="table-wrap">
            <table class="student-table">
                <thead>
                    <tr>
                        <th style="width:50px;">Photo</th>
                        <th>Student</th>
                        <th>Enrollment</th>
                        <th>Phone</th>
                        <th>Course</th>
                        <th>Total Fee</th>
                        <th>Paid</th>
                        <th>Due</th>
                        <th>Status</th>
                        <th style="text-align:right;">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($students as $s):
                        // Resolve photo path
                        $photoPath = '';
                        if (!empty($s['photo'])) {
                            $candidates = [
                                $s['photo'],
                                $_SERVER['DOCUMENT_ROOT'] . '/' . ltrim($s['photo'], '/'),
                                $_SERVER['DOCUMENT_ROOT'] . '/gmind/' . ltrim($s['photo'], '/'),
                                '/home/advaysin/public_html/' . ltrim($s['photo'], '/'),
                                '/home/advaysin/public_html/gmind/' . ltrim($s['photo'], '/'),
                            ];
                            foreach ($candidates as $c) {
                                if (file_exists($c)) {
                                    $photoPath = (defined('BASE_PATH') ? BASE_PATH : '/gmind') . '/' . ltrim($s['photo'], '/');
                                    break;
                                }
                            }
                        }

                        // Fee calculations
                        $courseFeeVal = (float)($s['course_fee'] ?? $s['fee_amount'] ?? 0);
                        $paidVal = (float)($s['fee_paid'] ?? 0);
                        $dueVal = (float)($s['fee_due'] ?? 0);
                        $fullyPaid = ($dueVal <= 0 && $courseFeeVal > 0);
                        $partial = ($paidVal > 0 && $dueVal > 0);
                    ?>
                    <tr>
                        <!-- Photo -->
                        <td data-label="Photo">
                            <?php if (!empty($photoPath)): ?>
                                <img src="<?= htmlspecialchars($photoPath, ENT_QUOTES) ?>" alt="Photo" class="student-avatar">
                            <?php else: ?>
                                <div class="student-avatar"><i class="fas fa-user"></i></div>
                            <?php endif; ?>
                        </td>

                        <!-- Name + Email -->
                        <td data-label="Student">
                            <div class="student-name-cell">
                                <div>
                                    <div class="name"><?= htmlspecialchars($s['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                    <div class="email"><?= htmlspecialchars($s['email'] ?? '', ENT_QUOTES, 'UTF-8') ?></div>
                                </div>
                            </div>
                        </td>

                        <!-- Enrollment -->
                        <td data-label="Enrollment">
                            <span class="enrollment-badge"><?= htmlspecialchars($s['enrollment_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></span>
                        </td>

                        <!-- Phone -->
                        <td data-label="Phone">
                            <span class="student-phone"><?= htmlspecialchars($s['phone'] ?? '—', ENT_QUOTES, 'UTF-8') ?></span>
                        </td>

                        <!-- Course -->
                        <td data-label="Course">
                            <span class="student-course" title="<?= htmlspecialchars($s['course_name'] ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                <?= htmlspecialchars($s['course_name'] ?? '—', ENT_QUOTES, 'UTF-8') ?>
                            </span>
                        </td>

                        <!-- Total Fee -->
                        <td data-label="Total Fee">
                            <span class="fee-amount">&#8377; <?= number_format($courseFeeVal, 0) ?></span>
                        </td>

                        <!-- Paid -->
                        <td data-label="Paid">
                            <span class="fee-paid">&#8377; <?= number_format($paidVal, 0) ?></span>
                        </td>

                        <!-- Due -->
                        <td data-label="Due">
                            <span class="fee-due" style="color: <?= $fullyPaid ? '#16a34a' : '#dc2626' ?>;">
                                &#8377; <?= number_format($dueVal, 0) ?>
                            </span>
                        </td>

                        <!-- Status -->
                        <td data-label="Status">
                            <?php if ($fullyPaid): ?>
                                <span class="status-badge status-full-paid">✓ Full Paid</span>
                            <?php elseif ($partial): ?>
                                <span class="status-badge status-partial">Partial</span>
                            <?php else: ?>
                                <span class="status-badge status-unpaid">Unpaid</span>
                            <?php endif; ?>
                            <br>
                            <span class="status-badge <?= ($s['status'] ?? '') === 'active' ? 'status-active' : 'status-inactive' ?>" style="margin-top:3px;">
                                <?= htmlspecialchars(ucfirst($s['status'] ?? 'active'), ENT_QUOTES, 'UTF-8') ?>
                            </span>
                        </td>

                        <!-- Actions -->
                        <td data-label="Actions" class="actions-cell">
                            <a href="<?= $bp ?>/branch/students/<?= (int)($s['id'] ?? 0) ?>" class="action-btn action-view" title="View"><i class="fas fa-eye"></i></a>
                            <a href="<?= $bp ?>/branch/students/<?= (int)($s['id'] ?? 0) ?>/edit" class="action-btn action-edit" title="Edit"><i class="fas fa-edit"></i></a>
                            <?php if (!$fullyPaid): ?>
                            <a href="<?= $bp ?>/branch/fees/collect/<?= (int)($s['id'] ?? 0) ?>" class="action-btn action-fee" title="Collect Fee"><i class="fas fa-rupee-sign"></i></a>
                            <?php endif; ?>
                            <form method="POST" action="<?= $bp ?>/branch/students/<?= (int)($s['id'] ?? 0) ?>/delete" style="display:inline;" onsubmit="return confirm('Delete this student?');">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                <button type="submit" class="action-btn action-delete" title="Delete"><i class="fas fa-trash"></i></button>
                            </form>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div class="table-footer">
            Showing <?= count($students) ?> student(s)
        </div>
        <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-users"></i>
            <p style="font-size:1.05rem; margin:8px 0;">No students found</p>
            <p><a href="<?= $bp ?>/branch/students/create">Add your first student</a></p>
        </div>
        <?php endif; ?>
    </div>
</div>
