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

<div class="max-w-7xl mx-auto mb-10 space-y-8">
    <div class="flex flex-col md:flex-row md:items-center justify-between pb-4">
        <div class="mb-4 md:mb-0">
            <h1 class="text-2xl md:text-3xl font-bold text-gray-900 dark:text-white mb-1"><?php echo $data['title']; ?></h1>
            <p class="text-sm text-gray-600 dark:text-gray-400"><?php echo $data['description']; ?></p>
        </div>
        <a href="<?php echo URLROOT; ?>/admin/createJobcard" class="w-full md:w-auto bg-primary hover:opacity-90 text-white font-bold py-2 px-4 rounded-lg transition-all focus:ring-4 focus:ring-primary/50 text-sm whitespace-nowrap text-center shadow-sm">
            + Create Jobcard
        </a>
    </div>

    <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 flex justify-between items-center">
            <h2 class="text-sm font-bold text-gray-900 dark:text-white flex items-center">
                <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"></path></svg>
                Master Jobcard Matrix
            </h2>
        </div>
        
        <div class="w-full text-gray-900 dark:text-gray-100 p-5">
            <table id="jobcardsTable" class="w-full text-left whitespace-nowrap">
                <thead class="border-b border-gray-200 dark:border-gray-700/50">
                    <tr>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Jobcard #</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Client / Partner</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Technician</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Status</th>
                        <th class="px-6 py-4 text-right text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($data['jobcards'])): ?>
                        <?php foreach($data['jobcards'] as $jc) : ?>
                        <tr class="border-b border-gray-100 dark:border-gray-700/50 last:border-0 hover:bg-gray-50 dark:hover:bg-gray-700/25 transition-colors">
                            <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900 dark:text-white font-mono"><?php echo htmlspecialchars($jc->jobcard_number); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600 dark:text-gray-300">
                                <strong><?php echo htmlspecialchars($jc->client_name); ?></strong>
                                <?php if($jc->partner_name): ?>
                                    <span class="text-xs block text-gray-400">via <?php echo htmlspecialchars($jc->partner_name); ?></span>
                                <?php endif; ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <?php echo $jc->tech_name ? htmlspecialchars($jc->tech_name) : '<span class="text-xs text-yellow-500 font-bold uppercase">Unassigned</span>'; ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm">
                                <?php 
                                    $badgeColors = [
                                        'Created' => 'bg-gray-100 text-gray-700 border-gray-200',
                                        'Assigned' => 'bg-blue-50 text-blue-700 border-blue-200',
                                        'Started' => 'bg-yellow-50 text-yellow-700 border-yellow-200',
                                        'Completed' => 'bg-purple-50 text-purple-700 border-purple-200',
                                        'Signed Off' => 'bg-green-50 text-green-700 border-green-200'
                                    ];
                                    $color = $badgeColors[$jc->status] ?? $badgeColors['Created'];
                                ?>
                                <span class="px-2 py-0.5 inline-flex text-[10px] font-bold uppercase tracking-wider rounded border <?php echo $color; ?>">
                                    <?php echo $jc->status; ?>
                                </span>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                <a href="<?php echo URLROOT; ?>/admin/viewJobcard/<?php echo $jc->id; ?>" class="p-1.5 bg-gray-100 dark:bg-gray-700 hover:bg-gray-200 dark:hover:bg-gray-600 text-gray-600 dark:text-gray-300 rounded-md transition-colors shadow-sm inline-flex items-center justify-center">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"></path></svg>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#jobcardsTable').DataTable({
            responsive: true,
            order: [[ 0, "desc" ]],
            language: { searchPlaceholder: "Search jobcards..." },
            columnDefs: [{ orderable: false, targets: 4 }]
        });
    });
</script>

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