<?php
require_once __DIR__ . '/../../../includes/auth.php';
requirePermission('view_network_invites');

$BASE = app_base_path();
$userId = $_SESSION['user_id'];

/* ===============================
   FETCH INVITED JOBS
=================================*/

$stmt = $pdo->prepare("
    SELECT 
        j.id,
        j.title,
        j.category,
        j.suburb,
        j.city,
        j.status,
        i.expires_at
    FROM network_job_invites i
    JOIN network_jobs j ON j.id = i.job_id
    WHERE i.installer_user_id = ?
    AND i.status = 'INVITED'
    ORDER BY j.created_at DESC
");

$stmt->execute([$userId]);
$jobs = $stmt->fetchAll(PDO::FETCH_ASSOC);

require_once __DIR__ . '/../../../includes/header.php';
require_once __DIR__ . '/../../../includes/sidebar.php';
?>

<div class="main-content">

    <div class="page-header">
        <h2>National Network - My Invites</h2>
        <p class="page-subtitle">
            Jobs you have been invited to quote on.
        </p>
    </div>

    <div class="card">

<div class="table-wrap">
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Location</th>
                    <th>Expires</th>
                    <th></th>
                </tr>
            </thead>

            <tbody>
                <?php if (empty($jobs)): ?>
                    <tr>
                        <td colspan="6" style="text-align:center;">
                            No invites available.
                        </td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($jobs as $job): ?>
                        <tr>
                            <td>#<?= $job['id'] ?></td>
                            <td><?= htmlspecialchars($job['title']) ?></td>
                            <td><?= $job['category'] ?></td>
                            <td><?= htmlspecialchars($job['suburb'] . ', ' . $job['city']) ?></td>
                            <td><?= $job['expires_at'] ?></td>
                            <td>
                                <a class="btn-primary btn-sm"
                                   href="<?= $BASE ?>/installer/network/quote.php?id=<?= (int)$job['id'] ?>">
                                   Submit Quote
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>

    </div>

</div>

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