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

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

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    header("Location: index.php");
    exit;
}

$stmt = $pdo->prepare("
    SELECT 
        j.*,
        u.full_name AS installer_name
    FROM trade_jobs j
    LEFT JOIN users u ON u.id = j.assigned_user_id
    WHERE j.id = :id
    " . tenantWhere('j') . "
    LIMIT 1
");

$params = tenantParams(['id' => $id]);

$stmt->execute($params);
$job = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$job) {
    echo "<div style='padding:30px;font-family:Poppins,sans-serif;'>
            <h3>Trade Job Not Found</h3>
            <a href='index.php'>Back</a>
          </div>";
    exit;
}

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

<div class="container">

    <div class="page-header">
        <h2><?= htmlspecialchars($job['job_number']) ?></h2>
        <p class="page-subtitle"><?= htmlspecialchars($job['title']) ?></p>
    </div>

    <div class="card-form" style="max-width:100%;">

        <div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:18px;">

            <div>
                <strong>Status</strong><br>
                <span class="badge badge-warning">
                    <?= htmlspecialchars($job['status']) ?>
                </span>
            </div>

            <div>
                <strong>Priority</strong><br>
                <?= htmlspecialchars($job['priority']) ?>
            </div>

            <div>
                <strong>Assigned Installer</strong><br>
                <?= $job['installer_name'] 
                    ? htmlspecialchars($job['installer_name']) 
                    : '<span class="badge badge-muted">Unassigned</span>' ?>
            </div>

            <div>
                <strong>Linked Ticket</strong><br>
                <?= $job['ticket_id'] ?: '-' ?>
            </div>

            <div>
                <strong>Linked Meter</strong><br>
                <?= $job['meter_id'] ?: '-' ?>
            </div>

            <div>
                <strong>Created</strong><br>
                <?= $job['created_at'] 
                    ? date('Y-m-d H:i', strtotime($job['created_at'])) 
                    : '-' ?>
            </div>

            <div>
                <strong>Completed At</strong><br>
                <?= $job['completed_at'] 
                    ? date('Y-m-d H:i', strtotime($job['completed_at'])) 
                    : '-' ?>
            </div>

        </div>

        <hr style="margin:20px 0;">

        <strong>Description</strong><br><br>
        <?= nl2br(htmlspecialchars($job['description'] ?? '')) ?>

        <?php if (!empty($job['completion_notes'])): ?>
            <hr style="margin:20px 0;">
            <strong>Completion Notes</strong><br><br>
            <?= nl2br(htmlspecialchars($job['completion_notes'])) ?>
        <?php endif; ?>

        <div style="margin-top:20px;">
            <a href="update.php?id=<?= $job['id'] ?>" class="btn btn-primary">
                Update Job
            </a>

            <a href="index.php" class="btn btn-light">
                Back to List
            </a>
        </div>

    </div>

</div>

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