<?php
require_once __DIR__ . '/../config/database.php';

requireLogin();

$user = getCurrentUser();
$db = Database::getInstance();

if (!hasRole('student')) {
    redirect('dashboard.php');
}

$pendingInstallments = $db->getAll(
    "SELECT ei.*, c.title AS course_title
     FROM enrollment_installments ei
     JOIN course_enrollments ce ON ei.enrollment_id = ce.id
     JOIN courses c ON ce.course_id = c.id
     WHERE ce.student_id = ? AND ei.status = 'pending'
     ORDER BY ei.due_date ASC",
    [$user['id']]
);

$allInstallments = $db->getAll(
    "SELECT ei.*, c.title AS course_title
     FROM enrollment_installments ei
     JOIN course_enrollments ce ON ei.enrollment_id = ce.id
     JOIN courses c ON ce.course_id = c.id
     WHERE ce.student_id = ?
     ORDER BY ei.due_date DESC",
    [$user['id']]
);

$pageTitle = 'My Payments';
include __DIR__ . '/includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h4 mb-0">My Payments</h1>
        <p class="text-muted mb-0">View your upcoming and past installment payments.</p>
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header">
                <h6 class="m-0">Upcoming Installments</h6>
            </div>
            <div class="card-body">
                <?php if ($pendingInstallments): ?>
                    <ul class="list-unstyled mb-0 small">
                        <?php foreach ($pendingInstallments as $inst): ?>
                            <?php
                                $dueTs = !empty($inst['due_date']) ? strtotime($inst['due_date']) : null;
                                $daysLeft = null;
                                if ($dueTs) {
                                    $daysLeft = (int) floor(($dueTs - time()) / 86400);
                                }
                            ?>
                            <li class="mb-3">
                                <div class="fw-semibold mb-1"><?php echo htmlspecialchars($inst['course_title'] ?? 'Course'); ?></div>
                                <div class="text-muted">
                                    Installment <?php echo (int)($inst['installment_no'] ?? 1); ?>:
                                    <?php echo htmlspecialchars(number_format((float)($inst['amount'] ?? 0.0), 2)); ?>
                                    <?php echo htmlspecialchars($inst['currency'] ?? 'USD'); ?>
                                </div>
                                <?php if ($dueTs): ?>
                                    <div class="text-muted">
                                        Due: <?php echo htmlspecialchars(date('Y-m-d H:i', $dueTs)); ?>
                                        <?php if ($daysLeft !== null && $daysLeft >= 0): ?>
                                            (in <?php echo $daysLeft; ?> day<?php echo $daysLeft === 1 ? '' : 's'; ?>)
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php else: ?>
                    <p class="text-muted mb-0">You have no upcoming installment payments.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <div class="col-lg-6">
        <div class="card">
            <div class="card-header">
                <h6 class="m-0">All Installments</h6>
            </div>
            <div class="card-body">
                <?php if ($allInstallments): ?>
                    <div class="table-responsive">
                        <table class="table table-sm align-middle mb-0">
                            <thead>
                                <tr>
                                    <th>Course</th>
                                    <th>No.</th>
                                    <th>Amount</th>
                                    <th>Due Date</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($allInstallments as $inst): ?>
                                    <?php $dueTs = !empty($inst['due_date']) ? strtotime($inst['due_date']) : null; ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($inst['course_title'] ?? 'Course'); ?></td>
                                        <td><?php echo (int)($inst['installment_no'] ?? 1); ?></td>
                                        <td>
                                            <?php echo htmlspecialchars(number_format((float)($inst['amount'] ?? 0.0), 2)); ?>
                                            <?php echo htmlspecialchars($inst['currency'] ?? 'USD'); ?>
                                        </td>
                                        <td>
                                            <?php if ($dueTs): ?>
                                                <?php echo htmlspecialchars(date('Y-m-d H:i', $dueTs)); ?>
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo htmlspecialchars($inst['status'] ?? 'pending'); ?></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php else: ?>
                    <p class="text-muted mb-0">No installment records found.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/includes/footer.php'; ?>
