<?php
/**
 * Branch Admin — Students Listing View
 *
 * Table listing all students belonging to the branch with
 * columns: Name, Email, Course, Fee Paid, Fee Total, Status, Actions.
 * All amounts in &#8377; (Indian Rupee).
 */
$bp = defined('BASE_PATH') ? BASE_PATH : '/gmind';
?>

<div class="admin-card">
    <div class="admin-card-header">
        <h2 class="admin-card-title">All Students</h2>
        <a href="<?= $bp ?>/branch/students/create" class="admin-btn admin-btn-primary">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" width="16" height="16" style="vertical-align:middle;margin-right:4px;"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
            Add Student
        </a>
    </div>
    <div class="admin-card-body">
        <?php if (empty($students)): ?>
        <p class="admin-empty-text">No students found. Click "Add Student" to enroll your first student.</p>
        <?php else: ?>
        <div class="admin-table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Course</th>
                        <th>Fee Paid</th>
                        <th>Fee Total</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($students as $student): ?>
                    <tr>
                        <td>
                            <strong><?= htmlspecialchars($student['name'] ?? '', ENT_QUOTES, 'UTF-8') ?></strong>
                            <br><small style="color:#888;"><?= htmlspecialchars($student['enrollment_no'] ?? '', ENT_QUOTES, 'UTF-8') ?></small>
                        </td>
                        <td><?= htmlspecialchars($student['email'] ?? '', ENT_QUOTES, 'UTF-8') ?></td>
                        <td><?= htmlspecialchars($student['course_name'] ?? 'N/A', ENT_QUOTES, 'UTF-8') ?></td>
                        <td>&#8377;<?= number_format((float) ($student['fee_paid'] ?? 0), 2) ?></td>
                        <td>&#8377;<?= number_format((float) ($student['fee_total'] ?? 0), 2) ?></td>
                        <td>
                            <?php $status = $student['status'] ?? 'pending'; ?>
                            <?php if ($status === 'active'): ?>
                            <span class="admin-badge admin-badge-success">Active</span>
                            <?php elseif ($status === 'partial'): ?>
                            <span class="admin-badge admin-badge-warning">Partial</span>
                            <?php elseif ($status === 'pending'): ?>
                            <span class="admin-badge admin-badge-danger">Pending</span>
                            <?php else: ?>
                            <span class="admin-badge admin-badge-secondary"><?= htmlspecialchars(ucfirst($status), ENT_QUOTES, 'UTF-8') ?></span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <div style="display:flex;gap:6px;flex-wrap:wrap;">
                                <a href="<?= $bp ?>/branch/students/<?= (int) ($student['id'] ?? 0) ?>" class="admin-btn admin-btn-sm admin-btn-secondary" title="View">View</a>
                                <a href="<?= $bp ?>/branch/students/<?= (int) ($student['id'] ?? 0) ?>/edit" class="admin-btn admin-btn-sm admin-btn-primary" title="Edit">Edit</a>
                                <form method="POST" action="<?= $bp ?>/branch/students/<?= (int) ($student['id'] ?? 0) ?>/delete" style="display:inline;" class="delete-form">
                                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken ?? '', ENT_QUOTES, 'UTF-8') ?>">
                                    <button type="submit" class="admin-btn admin-btn-sm admin-btn-danger" title="Delete" onclick="return confirm('Are you sure you want to delete this student? This cannot be undone.')">Delete</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <?php endif; ?>
    </div>
</div>
