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

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

$BASE = app_base_path();

/* =====================================
   COUNT JOBS AWAITING VERIFICATION
===================================== */
$pendingStmt = $pdo->prepare("
    SELECT COUNT(*)
    FROM network_jobs
    WHERE status = 'COMPLETION_PENDING'
");
$pendingStmt->execute();
$pendingCount = (int)$pendingStmt->fetchColumn();

/* ===============================
   FETCH NETWORK JOBS
   (Pending first, newest first)
=================================*/
$stmt = $pdo->query("
    SELECT id, title, category, suburb, city, status, created_at
    FROM network_jobs
    ORDER BY 
        (status = 'COMPLETION_PENDING') DESC,
        created_at DESC
");

$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</h2>
    <p class="page-subtitle">Manage electrical & plumbing service requests nationwide.</p>
</div>

<?php if ($pendingCount > 0): ?>
    <div style="
        background:#fff3cd;
        border:1px solid #ffeeba;
        padding:12px 16px;
        border-radius:8px;
        margin-bottom:15px;
        font-weight:600;
        color:#856404;">
        🔔 <?= $pendingCount ?> Job(s) Awaiting Completion Verification
    </div>
<?php endif; ?>

<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>Status</th>
            <th>Date</th>
            <th></th>
        </tr>
    </thead>

    <tbody>
        <?php if (empty($jobs)): ?>
            <tr>
                <td colspan="7" style="text-align:center;">No jobs found.</td>
            </tr>
        <?php else: ?>
            <?php foreach ($jobs as $job): ?>
                <tr <?= $job['status'] === 'COMPLETION_PENDING' ? 'style="background:#fff8e1;"' : '' ?>>
                    <td>#<?= $job['id'] ?></td>
                    <td><?= htmlspecialchars($job['title']) ?></td>
                    <td><?= htmlspecialchars($job['category']) ?></td>
                    <td><?= htmlspecialchars($job['suburb'] . ', ' . $job['city']) ?></td>
                    <td>
                        <?php if ($job['status'] === 'COMPLETION_PENDING'): ?>
                            <span style="color:#d97706;font-weight:700;">
                                <?= $job['status'] ?>
                            </span>
                        <?php else: ?>
                            <span><?= $job['status'] ?></span>
                        <?php endif; ?>
                    </td>
                    <td><?= date('d M Y', strtotime($job['created_at'])) ?></td>
                    <td>
                        <a class="btn-primary btn-sm"
                           href="<?= $BASE ?>/operations/network/view.php?id=<?= (int)$job['id'] ?>">
                           View
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>

</div>
</div>
</div>

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