<?php
require_once __DIR__ . '/../../includes/auth.php';
requireLogin();
requirePermission('trade_jobs.manage');

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

/* =============================
   TENANT SAFE QUERY
============================= */

$stmt = $pdo->prepare("
    SELECT 
        j.id,
        j.job_number,
        j.title,
        j.priority,
        j.status,
        j.created_at,
        u.full_name AS installer_name
    FROM trade_jobs j
    LEFT JOIN users u ON u.id = j.assigned_user_id
    " . tenantWhere('j') . "
    ORDER BY j.id DESC
");

$stmt->execute(tenantParams());
$jobs = $stmt->fetchAll(PDO::FETCH_ASSOC);

$breadcrumb = "Trade Jobs";
require_once __DIR__ . '/../../includes/layout.php';
?>

    <div class="page-header">
        <h2>Trade Jobs</h2>
        <p class="page-subtitle">All field service assignments in your company.</p>
    </div>

    <div style="margin-bottom:20px;">
        <a href="create.php" class="btn btn-primary">+ Create Job</a>
    </div>

    <?php if (empty($jobs)): ?>
        <div class="card-form">
            <p>No trade jobs found.</p>
        </div>
    <?php else: ?>

        <div class="table-wrap">

            <table style="width:100%; border-collapse:collapse;">
                <thead>
                    <tr>
                        <th style="text-align:left;padding:10px;">Job #</th>
                        <th style="text-align:left;padding:10px;">Title</th>
                        <th style="text-align:left;padding:10px;">Installer</th>
                        <th style="text-align:left;padding:10px;">Priority</th>
                        <th style="text-align:left;padding:10px;">Status</th>
                        <th style="text-align:left;padding:10px;">Created</th>
                        <th style="text-align:left;padding:10px;">Action</th>
                    </tr>
                </thead>
                <tbody>

                <?php foreach ($jobs as $job): ?>
                    <tr style="border-top:1px solid #eee;">
                        <td style="padding:10px;">
                            <?= htmlspecialchars($job['job_number']) ?>
                        </td>

                        <td style="padding:10px;">
                            <?= htmlspecialchars($job['title']) ?>
                        </td>

                        <td style="padding:10px;">
                            <?= $job['installer_name']
                                ? htmlspecialchars($job['installer_name'])
                                : '<span class="badge badge-muted">Unassigned</span>' ?>
                        </td>

                        <td style="padding:10px;">
                            <?= htmlspecialchars($job['priority']) ?>
                        </td>

                        <td style="padding:10px;">
                            <span class="badge badge-warning">
                                <?= htmlspecialchars($job['status']) ?>
                            </span>
                        </td>

                        <td style="padding:10px;">
                            <?= date('Y-m-d H:i', strtotime($job['created_at'])) ?>
                        </td>

                        <td style="padding:10px;">
                            <a href="view.php?id=<?= $job['id'] ?>" class="btn btn-light">
                                View
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>

                </tbody>
            </table>

        </div>

    <?php endif; ?>

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