<?php $__env->startSection('title', 'PRO Operations'); ?>

<?php $__env->startSection('content'); ?>
<div class="space-y-5" x-data="{ jobOpen: false, custodyOpen: false }">
    <div class="flex flex-col gap-3 sm:flex-row sm:items-end sm:justify-between"><div><p class="text-[10px] font-bold uppercase tracking-[.18em] text-orange-600">Field operations & document accountability</p><h1 class="mt-1 text-2xl font-bold text-[#0b1f38]">PRO Operations</h1><p class="mt-1 text-xs text-slate-500">External visits, authority submissions, expenses and original-document custody.</p></div><?php if(auth()->user()->hasPermission('pro.manage')): ?><div class="flex gap-2"><button @click="custodyOpen=!custodyOpen" class="rounded-xl border border-slate-200 bg-white px-4 py-2.5 text-xs font-bold text-slate-700">Receive Document</button><button @click="jobOpen=!jobOpen" class="rounded-xl bg-orange-600 px-4 py-2.5 text-xs font-bold text-white">+ PRO Job</button></div><?php endif; ?></div>

    <div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-4">
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-blue-50 text-blue-600">J</span><p class="erp-kpi-label">Assigned Jobs</p><p class="erp-kpi-value"><?php echo e($statusCounts['assigned'] ?? 0); ?></p><p class="erp-kpi-note">Waiting for field action</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-orange-50 text-orange-600">↗</span><p class="erp-kpi-label">In Progress</p><p class="erp-kpi-value"><?php echo e($statusCounts['in_progress'] ?? 0); ?></p><p class="erp-kpi-note">Currently outside or submitted</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-emerald-50 text-emerald-600">✓</span><p class="erp-kpi-label">Completed</p><p class="erp-kpi-value"><?php echo e($statusCounts['completed'] ?? 0); ?></p><p class="erp-kpi-note">Full historical trace retained</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-violet-50 text-violet-600">D</span><p class="erp-kpi-label">Documents in Custody</p><p class="erp-kpi-value"><?php echo e($custodyCount); ?></p><p class="erp-kpi-note">Not yet delivered to owner</p></div>
    </div>

    <?php if(auth()->user()->hasPermission('pro.manage')): ?>
    <div class="grid gap-4 xl:grid-cols-2">
        <section x-show="jobOpen" x-transition class="erp-panel"><div class="erp-panel-heading"><div><h2>Assign PRO Job</h2><p>Link every field visit to an application.</p></div><button @click="jobOpen=false">×</button></div><form method="POST" action="<?php echo e(route('app.pro.jobs.store')); ?>" class="mt-4 grid gap-3 sm:grid-cols-2"><?php echo csrf_field(); ?><select name="application_id" required class="erp-input sm:col-span-2"><option value="">Select application *</option><?php $__currentLoopData = $applications; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $application): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($application->id); ?>"><?php echo e($application->application_number); ?> · <?php echo e($application->customer?->full_name_en); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><select name="pro_user_id" class="erp-input"><option value="">Assign PRO later</option><?php $__currentLoopData = $proUsers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($user->id); ?>"><?php echo e($user->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><input name="destination" required placeholder="Authority / destination *" class="erp-input"><input name="purpose" required placeholder="Purpose *" class="erp-input"><input name="expected_return_at" type="datetime-local" class="erp-input"><textarea name="remarks" rows="2" placeholder="Instructions and documents" class="erp-input sm:col-span-2"></textarea><button class="rounded-xl bg-orange-600 py-3 text-xs font-bold text-white sm:col-span-2">Assign Job</button></form></section>
        <section x-show="custodyOpen" x-transition class="erp-panel"><div class="erp-panel-heading"><div><h2>Receive Original Document</h2><p>Generate a non-sensitive internal custody tag.</p></div><button @click="custodyOpen=false">×</button></div><form method="POST" action="<?php echo e(route('app.pro.custody.store')); ?>" class="mt-4 grid gap-3 sm:grid-cols-2"><?php echo csrf_field(); ?><select name="application_id" class="erp-input sm:col-span-2"><option value="">Link an application (optional)</option><?php $__currentLoopData = $applications; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $application): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($application->id); ?>"><?php echo e($application->application_number); ?> · <?php echo e($application->customer?->full_name_en); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><input name="document_name" required placeholder="Document name *" class="erp-input"><input name="storage_location" placeholder="Locker / shelf location" class="erp-input"><button class="rounded-xl bg-[#0b1f38] py-3 text-xs font-bold text-white sm:col-span-2">Create Custody Record</button></form></section>
    </div>
    <?php endif; ?>

    <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Field Job Board</h2><p>Application-linked external work queue.</p></div><a href="<?php echo e(route('app.pro')); ?>">Clear filter</a></div><div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Job</th><th>Application</th><th>Destination</th><th>Assigned PRO</th><th>Timing</th><th>Expense</th><th>Status / Action</th></tr></thead><tbody><?php $__empty_1 = true; $__currentLoopData = $jobs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $job): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><tr><td><strong><?php echo e($job->job_number); ?></strong><small><?php echo e($job->created_at->format('d M Y, h:i A')); ?></small></td><td><a class="font-bold text-blue-600" href="<?php echo e(route('app.applications.show', $job->application_id)); ?>"><?php echo e($job->application?->application_number); ?></a><small><?php echo e($job->application?->customer?->full_name_en); ?> · <?php echo e($job->application?->service?->name_en); ?></small></td><td><strong><?php echo e($job->destination); ?></strong><small><?php echo e($job->purpose); ?></small></td><td><strong><?php echo e($job->proUser?->name ?? 'Unassigned'); ?></strong><small><?php echo e($job->branch?->name); ?></small></td><td><strong><?php echo e($job->departure_at?->format('d M, h:i A') ?? 'Not departed'); ?></strong><small>Due <?php echo e($job->expected_return_at?->format('d M, h:i A') ?? 'not set'); ?></small></td><td class="font-bold">AED <?php echo e(number_format((float) $job->expense_amount, 2)); ?></td><td><?php if(auth()->user()->hasPermission('pro.manage') && !in_array($job->status, ['completed','cancelled'])): ?><form method="POST" action="<?php echo e(route('app.pro.jobs.status', $job->id)); ?>" class="flex gap-1"><?php echo csrf_field(); ?><select name="status" class="erp-select-compact"><?php if($job->status==='assigned'): ?><option value="in_progress">Start job</option><?php endif; ?> <?php if($job->status==='in_progress'): ?><option value="completed">Complete</option><?php endif; ?><option value="cancelled">Cancel</option></select><button class="rounded-lg bg-slate-900 px-2 text-[9px] font-bold text-white">Update</button></form><?php else: ?><span class="erp-status" data-status="<?php echo e($job->status); ?>"><?php echo e(str($job->status)->replace('_',' ')->title()); ?></span><?php endif; ?></td></tr><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><tr><td colspan="7"><div class="erp-empty"><span>P</span><strong>No PRO jobs</strong><p>Assign field work directly from an active application.</p></div></td></tr><?php endif; ?></tbody></table></div><div class="mt-4"><?php echo e($jobs->links()); ?></div></section>

    <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Original Document Custody</h2><p>Current holder, physical location and handover state.</p></div><span class="text-[10px] font-bold text-slate-400"><?php echo e($custodies->count()); ?> recent records</span></div><div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Custody Tag</th><th>Document / Owner</th><th>Application</th><th>Location</th><th>Current Holder</th><th>Status / Handover</th></tr></thead><tbody><?php $__empty_1 = true; $__currentLoopData = $custodies; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $custody): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><tr><td><strong><?php echo e($custody->qr_code); ?></strong><small>Received <?php echo e($custody->received_at?->format('d M Y')); ?></small></td><td><strong><?php echo e($custody->document_name); ?></strong><small><?php echo e($custody->customer?->full_name_en ?? $custody->application?->customer?->full_name_en ?? 'Not linked'); ?></small></td><td><?php echo e($custody->application?->application_number ?? '—'); ?></td><td><?php echo e($custody->storage_location ?: 'Not recorded'); ?></td><td><?php echo e($custody->currentHolder?->name ?? 'No current holder'); ?></td><td><?php if(auth()->user()->hasPermission('pro.manage') && $custody->status !== 'delivered'): ?><details><summary class="cursor-pointer"><span class="erp-status" data-status="<?php echo e($custody->status); ?>"><?php echo e(str($custody->status)->replace('_',' ')->title()); ?></span></summary><form method="POST" action="<?php echo e(route('app.pro.custody.handover', $custody->id)); ?>" class="mt-2 flex min-w-[420px] gap-1"><?php echo csrf_field(); ?><select name="status" class="erp-select-compact"><option value="stored">Stored</option><option value="with_pro">Handed to PRO</option><option value="submitted_externally">Submitted externally</option><option value="returned">Returned</option><option value="ready_for_collection">Ready for collection</option><option value="delivered">Delivered</option></select><select name="current_holder_id" class="erp-select-compact"><option value="">No holder</option><?php $__currentLoopData = $proUsers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($user->id); ?>"><?php echo e($user->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><input name="handover_note" placeholder="Handover note" class="erp-select-compact"><button class="rounded-lg bg-orange-600 px-2 text-[9px] font-bold text-white">Record</button></form></details><?php else: ?><span class="erp-status" data-status="<?php echo e($custody->status); ?>"><?php echo e(str($custody->status)->replace('_',' ')->title()); ?></span><?php endif; ?></td></tr><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><tr><td colspan="6"><div class="erp-empty"><span>D</span><strong>No original documents in custody</strong><p>Received passports and originals will be tracked here.</p></div></td></tr><?php endif; ?></tbody></table></div></section>
</div>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\xampp\htdocs\typingcenter\resources\views/tenant/operations/pro.blade.php ENDPATH**/ ?>