<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once __DIR__ . '/../includes/auth.php';
requireLogin();
requirePermission('manage_installers');

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

$companyId = $_SESSION['company_id'] ?? null;

if (!$companyId) {
    die("Tenant isolation error.");
}

$stmt = $pdo->prepare("
    SELECT id, full_name, email, trade_skill, status, created_at
    FROM installer_applications
    WHERE company_id = ?
    ORDER BY created_at DESC
");
$stmt->execute([$companyId]);
$applications = $stmt->fetchAll(PDO::FETCH_ASSOC);

require_once __DIR__ . '/../includes/layout.php';
?>

<div class="page-header">
    <h2>Installer Applications</h2>
    <p class="page-subtitle">Manage installer onboarding</p>
</div>


<div class="table-wrap">
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Trade</th>
                <th>Status</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($applications as $app): ?>
                <tr>
                    <td><?= htmlspecialchars($app['full_name']) ?></td>
                    <td><?= htmlspecialchars($app['email']) ?></td>
                    <td><?= htmlspecialchars($app['trade_skill']) ?></td>
                    <td><?= htmlspecialchars($app['status']) ?></td>
                    <td>
                        <a href="view.php?id=<?= $app['id'] ?>" class="btn btn-sm">
                            View
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

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