<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/auth.php';
require_once __DIR__ . '/../includes/functions.php';

requireLogin();
$id = (int)($_GET['id'] ?? 0);
if (!$id) { die('Invalid job ID.'); }

$pdo = getDB();
$stmt = $pdo->prepare("SELECT rj.*, c.name as customer_name, c.phone as customer_phone, u.name as technician_name FROM repair_jobs rj LEFT JOIN customers c ON rj.customer_id = c.id LEFT JOIN users u ON rj.assigned_technician_id = u.id WHERE rj.id = ?");
$stmt->execute([$id]);
$job = $stmt->fetch();
if (!$job) { die('Job not found.'); }

$shop_name = getSetting('shop_name', 'DigiMobiz');
$shop_address = getSetting('shop_address', '');
$shop_phone = getSetting('shop_phone', '');
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Job Card - <?= htmlspecialchars($job['job_no']) ?></title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 12px; margin: 20px; }
        .header { text-align: center; margin-bottom: 20px; }
        .header h2 { margin: 0; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
        td, th { padding: 5px 8px; border: 1px solid #000; text-align: left; }
        th { background: #f0f0f0; }
        .label { font-weight: bold; width: 150px; }
        .status { padding: 3px 10px; border-radius: 3px; font-weight: bold; }
        .signature { margin-top: 40px; }
        .signature td { border: none; padding-top: 20px; }
        @media print { body { margin: 0; } .no-print { display: none; } }
    </style>
</head>
<body>
    <div class="header">
        <h2><?= htmlspecialchars($shop_name) ?></h2>
        <p><?= nl2br(htmlspecialchars($shop_address)) ?> | Tel: <?= htmlspecialchars($shop_phone) ?></p>
        <h3>REPAIR JOB CARD</h3>
        <h3>#<?= htmlspecialchars($job['job_no']) ?></h3>
    </div>

    <table>
        <tr><td class="label">Customer:</td><td><?= htmlspecialchars($job['customer_name'] ?? '-') ?></td>
            <td class="label">Phone:</td><td><?= htmlspecialchars($job['customer_phone'] ?? '-') ?></td></tr>
        <tr><td class="label">Technician:</td><td><?= htmlspecialchars($job['technician_name'] ?? 'Unassigned') ?></td>
            <td class="label">Date:</td><td><?= formatDate($job['created_at']) ?></td></tr>
        <tr><td class="label">Priority:</td><td><?= $job['priority'] ?></td>
            <td class="label">Expected Delivery:</td><td><?= formatDate($job['expected_delivery_date']) ?></td></tr>
    </table>

    <table>
        <tr><th colspan="4">Device Information</th></tr>
        <tr><td class="label">Brand:</td><td><?= htmlspecialchars($job['device_brand'] ?? '-') ?></td>
            <td class="label">Model:</td><td><?= htmlspecialchars($job['device_model'] ?? '-') ?></td></tr>
        <tr><td class="label">IMEI/Serial:</td><td><?= htmlspecialchars($job['device_imei'] ?? '-') ?></td>
            <td class="label">Color:</td><td><?= htmlspecialchars($job['device_color'] ?? '-') ?></td></tr>
        <tr><td class="label">Accessories:</td><td colspan="3"><?= htmlspecialchars($job['accessories_received'] ?? '-') ?></td></tr>
    </table>

    <table>
        <tr><th>Problem Description</th></tr>
        <tr><td style="height:60px;"><?= nl2br(htmlspecialchars($job['problem_description'])) ?></td></tr>
    </table>

    <table>
        <tr><th>Condition Notes</th></tr>
        <tr><td style="height:40px;"><?= nl2br(htmlspecialchars($job['device_condition'] ?? '')) ?></td></tr>
    </table>

    <table>
        <tr><td class="label">Password/Pattern:</td><td><?= htmlspecialchars($job['device_password_note'] ?? '-') ?></td>
            <td class="label">Estimated Cost:</td><td><?= formatCurrency($job['estimated_cost']) ?></td></tr>
        <tr><td class="label">Advance Paid:</td><td><?= formatCurrency($job['advance_payment']) ?></td>
            <td class="label">Status:</td><td><?= $job['repair_status'] ?></td></tr>
    </table>

    <table class="signature">
        <tr><td style="width:50%;">Customer Signature: ___________________</td>
            <td style="width:50%;">Technician Signature: ___________________</td></tr>
    </table>

    <div class="no-print" style="text-align:center;margin-top:20px;">
        <button onclick="window.print()">Print</button>
        <button onclick="window.close()">Close</button>
    </div>
    <script>window.print();</script>
</body>
</html>
