<?php
require_once __DIR__ . '/../includes/role_middleware.php';
requireRole(['Customer', 'Buyer']);

require_once __DIR__ . '/../includes/header.php';
$pdo = db();
$userId = $_SESSION['user_id'];

// Get Installments
$installments = $pdo->prepare("
    SELECT i.*, c.brand, c.model 
        FROM installments i 
        JOIN sales s ON i.sale_id = s.id 
        JOIN cars c ON s.car_id = c.id 
        WHERE s.user_id = ?
    ");
$installments->execute([$userId]);
$myInstallments = $installments->fetchAll();
?>

<div style="padding: 2rem;">
    <div class="page-header">
        <h1>My Dashboard</h1>
        <span>Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?></span>
    </div>

    <div style="background: var(--card-bg); padding: 2rem; border-radius: var(--border-radius); border: 1px solid var(--border-color); margin-bottom: 2rem;">
        <h3 style="color: var(--accent-color); margin-bottom: 1rem;">My Installment Plans</h3>
        <?php if ($myInstallments): ?>
            <table class="data-table" style="width: 100%; border-collapse: collapse;">
                <thead>
                    <tr>
                        <th style="text-align: left; padding: 0.5rem;">Vehicle</th>
                        <th style="text-align: left; padding: 0.5rem;">Total</th>
                        <th style="text-align: left; padding: 0.5rem;">Paid</th>
                        <th style="text-align: left; padding: 0.5rem;">Monthly</th>
                        <th style="text-align: left; padding: 0.5rem;">Status</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($myInstallments as $inst): ?>
                    <tr>
                        <td style="padding: 0.5rem; border-bottom: 1px solid var(--border-color);"><?php echo htmlspecialchars($inst['brand'] . ' ' . $inst['model']); ?></td>
                        <td style="padding: 0.5rem; border-bottom: 1px solid var(--border-color);">$<?php echo number_format($inst['total_amount'], 2); ?></td>
                        <td style="padding: 0.5rem; border-bottom: 1px solid var(--border-color);">$<?php echo number_format($inst['paid_amount'], 2); ?></td>
                        <td style="padding: 0.5rem; border-bottom: 1px solid var(--border-color);">$<?php echo number_format($inst['monthly_payment'], 2); ?></td>
                        <td style="padding: 0.5rem; border-bottom: 1px solid var(--border-color);">
                            <span style="padding: 0.2rem 0.5rem; border-radius: 4px; background: rgba(255, 255, 255, 0.1);">
                                <?php echo htmlspecialchars($inst['status']); ?>
                            </span>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?>
            <p style="color: var(--text-secondary);">You have no active installment plans.</p>
            <a href="/marketplace.php" class="btn-sm btn-primary" style="margin-top: 1rem; display: inline-block;">Browse Cars</a>
        <?php endif; ?>
    </div>
</div>

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