<?php
require 'db.php';

if (!isset($_GET['id'])) { die("Job ID not specified."); }
$job_id = $_GET['id'];

// Fetch Job Header (Updated to include network_name)
$stmt = $pdo->prepare("SELECT j.*, c.name as customer_name, n.name as network_name FROM jobs j JOIN customers c ON j.customer_id = c.id LEFT JOIN networks n ON j.network_id = n.id WHERE j.id = ?");
$stmt->execute([$job_id]);
$job = $stmt->fetch();
if (!$job) { die("Job not found."); }

// Fetch Job Materials
$stmtMat = $pdo->prepare("SELECT jm.quantity, jm.serial_number, jm.cxnk_number, m.name, m.unit_type, m.inventory_number FROM job_materials jm JOIN materials m ON jm.material_id = m.id WHERE jm.job_id = ?");
$stmtMat->execute([$job_id]);
$materials = $stmtMat->fetchAll();
?>

<!DOCTYPE html>
<html lang="en" class="h-full">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= !empty($job['is_smx']) ? 'SMx Conversion' : 'Jobsheet' ?> - <?= htmlspecialchars($job['customer_name']) ?></title>
    <link rel="stylesheet" href="/assets/css/tailwind.min.css">
    
    <script>
        if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
            document.documentElement.classList.add('dark');
            document.documentElement.style.backgroundColor = '#111827';
        } else {
            document.documentElement.classList.remove('dark');
            document.documentElement.style.backgroundColor = '#f3f4f6';
        }
    </script>
    <link href="/assets/css/font-awesome.min.css" rel="stylesheet">
    <style>
        @media print {
            aside, header, #print-actions, #theme-toggle-container { display: none !important; }
            body, main { background-color: white !important; color: black !important; height: auto !important; overflow: visible !important; }
            #ticket-container { box-shadow: none !important; border: none !important; margin: 0 !important; width: 100% !important; max-width: 100% !important; padding: 0 !important; }
            button.fixed { display: none !important; }
        }
    </style>
</head>
<body class="bg-gray-100 dark:bg-gray-900 text-gray-800 dark:text-gray-200 h-full flex overflow-hidden text-sm">
    <?php include 'sidebar.php'; ?>
    <main class="flex-1 flex flex-col h-full overflow-hidden">
        <header class="h-14 bg-white dark:bg-gray-800 shadow flex items-center px-6 justify-between shrink-0">
            <div class="flex items-center gap-4">
                <a href="reports.php" class="text-gray-500 hover:text-blue-500"><i class="fas fa-arrow-left"></i> Back</a>
                <h2 class="font-bold text-lg text-gray-700 dark:text-gray-200">Job #<?= $job['id'] ?> &mdash; <?= htmlspecialchars($job['customer_name']) ?></h2>
            </div>
            <div id="print-actions" class="flex gap-2">
                <a href="job_entry.php" class="bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 px-4 py-2 rounded shadow font-bold uppercase text-xs transition"><i class="fas fa-plus mr-1"></i> New Job</a>
                <a href="job_edit.php?id=<?= $job['id'] ?>" class="bg-indigo-600 hover:bg-indigo-500 text-white px-4 py-2 rounded shadow font-bold uppercase text-xs transition"><i class="fas fa-pen mr-1"></i> Edit</a>
                <button onclick="window.print()" class="bg-blue-600 hover:bg-blue-500 text-white px-4 py-2 rounded shadow font-bold uppercase text-xs transition"><i class="fas fa-print mr-1"></i> Print Ticket</button>
            </div>
        </header>

        <div class="flex-1 overflow-auto p-8 flex justify-center bg-gray-100 dark:bg-gray-900">
            <div id="ticket-container" class="w-full max-w-4xl bg-white dark:bg-gray-800 rounded-lg shadow-xl border dark:border-gray-700 p-8 h-fit">
                <div class="flex justify-between items-end border-b-2 border-gray-800 dark:border-gray-200 pb-2 mb-4">
                    <h1 class="text-[16pt] font-bold uppercase tracking-widest text-gray-800 dark:text-gray-100 leading-none">
                        <?= (!empty($job['is_smx']) ? 'SMx Conversion' : (!empty($job['is_cobalt']) ? 'Cobalt Job' : 'Jobsheet')) ?> - <?= htmlspecialchars($job['customer_name']) ?>
                    </h1>
                    <div class="text-sm font-bold text-gray-600 dark:text-gray-400 whitespace-nowrap leading-none"><?= date('M d, Y', strtotime($job['job_date'])) ?></div>
                </div>

                <div class="mb-6 text-sm text-gray-800 dark:text-gray-300 space-y-1">
                    <?php if ($job['job_type'] === 'Fiber Burial'): ?>
                        <div><span class="font-bold uppercase text-xs text-orange-600">Job Type:</span> Fiber Burial</div>
                        <?php if (!empty($job['location'])): ?><div><span class="font-bold uppercase text-xs text-gray-500">Location:</span> <?= htmlspecialchars($job['location']) ?></div><?php endif; ?>
                    <?php endif; ?>
                    
                    <?php if (!empty($job['time_spent'])): $h = floor($job['time_spent']/60); $m = $job['time_spent']%60; ?><div><span class="font-bold uppercase text-xs text-gray-500">Time Billed:</span> <?= $h > 0 ? "{$h} hr " : "" ?><?= $m > 0 ? "{$m} min" : "" ?></div><?php endif; ?>
                    <?php if (!empty($job['mileage'])): ?><div><span class="font-bold uppercase text-xs text-gray-500">Mileage:</span> <?= htmlspecialchars($job['mileage']) ?> mi</div><?php endif; ?>

                    <?php if (!empty($job['network_name']) || !empty($job['calix_position']) || !empty($job['splitter_position']) || !empty($job['light_level'])): ?>
                        <div class="mt-2 pt-2 border-t border-dashed border-gray-300">
                            <div class="font-bold uppercase text-blue-600 text-xs mb-1">Plant Information:</div>
                            <?php if (!empty($job['network_name'])): ?><div><span class="font-bold uppercase text-xs text-gray-500">Network:</span> <?= htmlspecialchars($job['network_name']) ?></div><?php endif; ?>
                            <?php if (!empty($job['calix_position'])): ?><div><span class="font-bold uppercase text-xs text-gray-500">Calix Position:</span> <?= htmlspecialchars($job['calix_position']) ?></div><?php endif; ?>
                            <?php if (!empty($job['splitter_position'])): ?><div><span class="font-bold uppercase text-xs text-gray-500">Splitter Position:</span> <?= htmlspecialchars($job['splitter_position']) ?></div><?php endif; ?>
                            <?php if (!empty($job['light_level'])): ?><div><span class="font-bold uppercase text-xs text-gray-500">Light Level:</span> <?= htmlspecialchars($job['light_level']) ?> dBm</div><?php endif; ?>
                        </div>
                    <?php endif; ?>

                    <?php if (!empty($job['notes'])): ?><div class="flex items-baseline gap-1 pt-2"><span class="font-bold uppercase text-xs text-gray-500 shrink-0">Notes:</span> <span class="whitespace-pre-line"><?= htmlspecialchars($job['notes']) ?></span></div><?php endif; ?>
                </div>

                <?php if (!empty($materials)): ?>
                <div class="mb-6">
                    <table class="w-full text-left">
                        <thead>
                            <tr class="text-xs uppercase text-gray-500 border-b border-gray-200 dark:border-gray-700">
                                <th class="py-2 text-left">Material</th>
                                <th class="py-2 w-24 text-left">Inv #</th>
                                <th class="py-2 w-32 text-left">CXNK #</th>
                                <th class="py-2 w-32 text-left">Serial #</th>
                                <th class="py-2 w-16 text-center">Unit</th>
                                <th class="py-2 w-20 text-right">Quantity</th>
                            </tr>
                        </thead>
                        <tbody class="text-sm text-gray-700 dark:text-gray-300">
                            <?php foreach ($materials as $m): ?>
                            <tr class="border-b border-gray-100 dark:border-gray-800 align-top">
                                <td class="py-2"><?= htmlspecialchars($m['name']) ?></td>
                                <td class="py-2 text-left text-gray-500 font-mono text-xs"><?= $m['inventory_number'] ?: '-' ?></td>
                                <td class="py-2 text-left text-gray-600 font-mono text-xs"><?= !empty($m['cxnk_number']) ? htmlspecialchars($m['cxnk_number']) : '-' ?></td>
                                <td class="py-2 text-left text-gray-600 font-mono text-xs"><?= !empty($m['serial_number']) ? htmlspecialchars($m['serial_number']) : '-' ?></td>
                                <td class="py-2 text-center text-gray-500"><?= $m['unit_type'] ?></td>
                                <td class="py-2 text-right font-bold"><?= (int)$m['quantity'] ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </main>
    <script>
    </script>
</body>
</html>