<?php require APPROOT . '/app/Views/layouts/header.php'; ?>

<div class="max-w-5xl mx-auto mb-10 space-y-6">
    <!-- Header & Status -->
    <div class="flex flex-col md:flex-row justify-between items-center bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6">
        <div>
            <h1 class="text-2xl font-bold text-gray-900 dark:text-white font-mono"><?php echo htmlspecialchars($data['jobcard']->jobcard_number); ?></h1>
            <p class="text-sm text-gray-500">Client: <strong class="text-primary"><?php echo htmlspecialchars($data['jobcard']->client_name); ?></strong></p>
        </div>
        <div class="mt-4 md:mt-0 flex flex-wrap items-center gap-3">
            <span class="px-3 py-1.5 text-xs font-bold uppercase tracking-wider rounded border bg-gray-100 text-gray-700 border-gray-200 dark:bg-gray-900 dark:text-gray-300 dark:border-gray-700">
                Status: <?php echo $data['jobcard']->status; ?>
            </span>
            
            <?php if ($data['jobcard']->status !== 'Signed Off'): ?>
                <a href="<?php echo URLROOT; ?>/staff/viewJobcard/<?php echo $data['jobcard']->id; ?>" class="px-4 py-2 bg-emerald-600 hover:bg-emerald-700 text-white text-xs font-bold rounded shadow-sm transition-colors flex items-center">
                    <svg class="w-4 h-4 mr-1.5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg>
                    Execute Jobcard
                </a>
            <?php endif; ?>
            
            <a href="<?php echo URLROOT; ?>/admin/jobcards" class="text-xs text-primary hover:underline font-bold ml-2">Back to Matrix</a>
        </div>
    </div>

    <!-- Overview Grid -->
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
        
        <!-- Assignment Control -->
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6 flex flex-col">
            <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider mb-4 border-b border-gray-200 dark:border-gray-700/50 pb-2">Technician Assignment</h3>
            
            <form action="<?php echo URLROOT; ?>/admin/assignTechnician" method="POST" class="space-y-4 flex-grow flex flex-col justify-between">
                <input type="hidden" name="jobcard_id" value="<?php echo $data['jobcard']->id; ?>">
                <div>
                    <select name="technician_id" class="w-full px-4 py-2.5 rounded-lg border border-gray-300 dark:border-gray-700 bg-gray-50 dark:bg-gray-900 text-gray-900 dark:text-white focus:ring-2 focus:ring-primary outline-none transition-colors" required <?php echo in_array($data['jobcard']->status, ['Completed', 'Signed Off']) ? 'disabled' : ''; ?>>
                        <option value="" class="bg-white dark:bg-gray-800 text-gray-900 dark:text-white">-- Unassigned --</option>
                        <?php foreach($data['staff'] as $staff): ?>
                            <option value="<?php echo $staff->id; ?>" class="bg-white dark:bg-gray-800 text-gray-900 dark:text-white" <?php echo ($data['jobcard']->technician_id == $staff->id) ? 'selected' : ''; ?>><?php echo htmlspecialchars($staff->name); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <?php if (!in_array($data['jobcard']->status, ['Completed', 'Signed Off'])): ?>
                <button type="submit" class="w-full bg-primary hover:opacity-90 text-white font-bold py-2.5 rounded-lg shadow-sm transition-all focus:ring-4 focus:ring-primary/50 mt-4">
                    Update Assignment
                </button>
                <?php else: ?>
                    <p class="text-[10px] text-gray-400 font-bold uppercase tracking-wider text-center mt-4">Locked (Job Completed)</p>
                <?php endif; ?>
            </form>
        </div>

        <!-- Timeline Details -->
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6">
            <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider mb-4 border-b border-gray-200 dark:border-gray-700/50 pb-2">Operational Timeline</h3>
            <div class="space-y-4">
                <div class="flex justify-between items-center">
                    <span class="text-xs font-bold text-gray-500 uppercase tracking-wider">Created At</span>
                    <span class="text-gray-900 dark:text-white font-medium text-sm font-mono"><?php echo date('M j, Y - H:i', strtotime($data['jobcard']->created_at)); ?></span>
                </div>
                <div class="flex justify-between items-center">
                    <span class="text-xs font-bold text-gray-500 uppercase tracking-wider">Started At</span>
                    <span class="text-gray-900 dark:text-white font-medium text-sm font-mono"><?php echo $data['jobcard']->started_at ? date('M j, Y - H:i', strtotime($data['jobcard']->started_at)) : '<span class="text-gray-400 italic">Not started</span>'; ?></span>
                </div>
                <div class="flex justify-between items-center">
                    <span class="text-xs font-bold text-gray-500 uppercase tracking-wider">Completed At</span>
                    <span class="text-gray-900 dark:text-white font-medium text-sm font-mono"><?php echo $data['jobcard']->completed_at ? date('M j, Y - H:i', strtotime($data['jobcard']->completed_at)) : '<span class="text-gray-400 italic">Not completed</span>'; ?></span>
                </div>
                <?php 
                $duration = '<span class="text-gray-400 italic">Not completed</span>';
                if (!empty($data['jobcard']->started_at) && !empty($data['jobcard']->completed_at)) {
                    $start = new DateTime($data['jobcard']->started_at);
                    $end = new DateTime($data['jobcard']->completed_at);
                    $diff = $start->diff($end);
                    $hours = ($diff->days * 24) + $diff->h;
                    $duration = $hours . 'h ' . $diff->i . 'm';
                }
                ?>
                <div class="flex justify-between items-center pt-2 border-t border-gray-100 dark:border-gray-700/50">
                    <span class="text-xs font-bold text-gray-500 uppercase tracking-wider">Time On-Site</span>
                    <span class="text-gray-900 dark:text-white font-medium text-sm font-mono"><?php echo $duration; ?></span>
                </div>
            </div>
        </div>
    </div>

    <!-- Instructions Block -->
    <div class="bg-blue-50 dark:bg-blue-900/10 border border-blue-200 dark:border-blue-800/30 rounded-xl p-6 shadow-sm">
        <h3 class="text-sm font-bold text-blue-900 dark:text-blue-300 mb-2 uppercase tracking-wider">Initial Instructions</h3>
        <p class="text-blue-800 dark:text-blue-200/80 leading-relaxed whitespace-pre-wrap"><?php echo htmlspecialchars($data['jobcard']->instructions); ?></p>
    </div>

    <!-- Work Performed Block -->
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6">
        <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider mb-4 border-b border-gray-200 dark:border-gray-700/50 pb-2">Work Performed</h3>
        <p class="text-gray-700 dark:text-gray-300 leading-relaxed whitespace-pre-wrap"><?php echo $data['jobcard']->work_done ? htmlspecialchars($data['jobcard']->work_done) : '<span class="text-gray-400 italic">No work logged yet.</span>'; ?></p>
    </div>

    <!-- Materials Matrix -->
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden">
        <div class="p-5 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40">
            <h3 class="text-sm font-bold text-gray-900 dark:text-white uppercase tracking-wider">Materials Used</h3>
        </div>
        <ul class="divide-y divide-gray-100 dark:divide-gray-700/50">
            <?php foreach($data['materials'] as $mat): ?>
                <li class="p-4 flex justify-between items-center hover:bg-gray-50 dark:hover:bg-gray-900/20">
                    <span class="text-sm font-medium text-gray-800 dark:text-gray-200"><?php echo htmlspecialchars($mat->material_name); ?> <span class="text-gray-400 ml-2 font-mono">x<?php echo htmlspecialchars($mat->quantity); ?></span></span>
                </li>
            <?php endforeach; ?>
            <?php if(empty($data['materials'])): ?>
                <li class="p-6 text-center text-sm text-gray-500 italic">No materials have been logged by the technician.</li>
            <?php endif; ?>
        </ul>
    </div>

    <?php if ($data['jobcard']->status === 'Signed Off'): ?>
    <!-- Final Signatures -->
    <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-6">
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6 text-center">
            <h3 class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-4">Client Signature</h3>
            <img src="<?php echo $data['jobcard']->client_signature; ?>" class="h-32 mx-auto border-b border-gray-300 dark:border-gray-600 mb-2">
            <p class="text-sm font-bold text-gray-900 dark:text-white"><?php echo htmlspecialchars($data['jobcard']->client_sign_name ?? $data['jobcard']->client_name); ?></p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 p-6 text-center">
            <h3 class="text-xs font-bold text-gray-500 uppercase tracking-wider mb-4">Technician Signature</h3>
            <img src="<?php echo $data['jobcard']->tech_signature; ?>" class="h-32 mx-auto border-b border-gray-300 dark:border-gray-600 mb-2">
            <p class="text-sm font-bold text-gray-900 dark:text-white"><?php echo htmlspecialchars($data['jobcard']->tech_sign_name ?? $data['jobcard']->tech_name); ?></p>
        </div>
    </div>
    
    <!-- DOWNLOAD PDF BUTTON -->
    <div class="mt-8 flex justify-end">
        <a href="<?php echo URLROOT; ?>/admin/downloadPdf/<?php echo $data['jobcard']->id; ?>" target="_blank" class="bg-primary hover:opacity-90 text-white font-bold py-3.5 px-8 rounded-lg shadow-sm transition-all flex items-center focus:ring-4 focus:ring-primary/50">
            <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path></svg>
            Download Finalized PDF
        </a>
    </div>
    <?php endif; ?>
</div>

<?php require APPROOT . '/app/Views/layouts/footer.php'; ?>