<?php $__env->startSection('title', 'Application 360 - '.$application->application_number); ?>

<?php $__env->startSection('content'); ?>
<?php
    $stageCode = $application->currentStage?->stage_code;
    $visitedStageIds = $application->stageHistory->pluck('to_stage_id')->unique();
    $contact = $application->customer?->contact;
    $activeInvoice = $application->invoiceItems->pluck('invoice')->filter()->sortByDesc('id')->first();
?>
<div class="space-y-5" x-data="{ tab: 'overview', slaOpen: false }">
    <?php if($errors->any()): ?>
        <div class="rounded-2xl border border-rose-200 bg-rose-50 p-4 text-xs text-rose-700"><strong class="block">This action needs attention</strong><ul class="mt-2 list-disc space-y-1 pl-5"><?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><li><?php echo e($error); ?></li><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></ul></div>
    <?php endif; ?>

    <section class="overflow-hidden rounded-3xl border border-slate-200 bg-white shadow-sm">
        <div class="bg-gradient-to-r from-[#081a30] via-[#0d2b4b] to-[#123d65] p-5 text-white sm:p-6">
            <div class="flex flex-col gap-4 xl:flex-row xl:items-start xl:justify-between">
                <div>
                    <div class="flex flex-wrap items-center gap-2"><span class="rounded-lg bg-orange-500 px-2.5 py-1 text-[9px] font-bold uppercase tracking-wider"><?php echo e($application->priority); ?></span><span class="rounded-lg bg-white/10 px-2.5 py-1 text-[9px] font-bold uppercase"><?php echo e(str($application->status)->replace('_',' ')); ?></span><span class="rounded-lg bg-white/10 px-2.5 py-1 text-[9px] font-bold uppercase"><?php echo e($application->service?->authority?->code ?? 'UAE Service'); ?></span></div>
                    <h1 class="mt-3 text-2xl font-bold"><?php echo e($application->application_number); ?></h1>
                    <p class="mt-1 text-xs text-slate-300"><?php echo e($application->customer?->full_name_en); ?> · <?php echo e($application->service?->name_en); ?> · <?php echo e($application->branch?->name ?? 'All branches'); ?></p>
                </div>
                <div class="grid grid-cols-2 gap-2 sm:grid-cols-4 xl:min-w-[600px]">
                    <div class="rounded-2xl bg-white/8 p-3"><small class="block text-[9px] uppercase text-slate-400">Current Stage</small><strong class="mt-1 block text-xs text-orange-300"><?php echo e($application->currentStage?->stage_name_en ?? 'Not configured'); ?></strong></div>
                    <div class="rounded-2xl bg-white/8 p-3"><small class="block text-[9px] uppercase text-slate-400">Assigned Staff</small><strong class="mt-1 block text-xs"><?php echo e($application->assignedUser?->name ?? 'Role queue'); ?></strong></div>
                    <div class="rounded-2xl bg-white/8 p-3"><small class="block text-[9px] uppercase text-slate-400">Payment</small><strong class="mt-1 block text-xs"><?php echo e(str($application->payment_status)->replace('_',' ')->title()); ?></strong></div>
                    <div class="rounded-2xl bg-white/8 p-3"><small class="block text-[9px] uppercase text-slate-400">Stage SLA</small><strong class="mt-1 block text-xs <?php echo e($application->sla_status === 'overdue' ? 'text-rose-300' : 'text-emerald-300'); ?>"><?php echo e($application->sla_paused_at ? 'Paused' : ($application->due_at?->diffForHumans() ?? 'No deadline')); ?></strong></div>
                </div>
            </div>
        </div>

        <div class="border-t border-slate-100 p-4 sm:p-5">
            <p class="mb-3 text-[9px] font-bold uppercase tracking-[.18em] text-slate-400">UAE Application Lifecycle</p>
            <div class="flex gap-2 overflow-x-auto pb-2">
                <?php $__currentLoopData = $allStages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $stage): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php $isCurrent = $stage->id === $application->current_stage_id; $isVisited = $visitedStageIds->contains($stage->id); ?>
                    <div class="min-w-[145px] rounded-2xl border p-3 <?php echo e($isCurrent ? 'border-orange-300 bg-orange-50' : ($isVisited ? 'border-emerald-200 bg-emerald-50/60' : 'border-slate-200 bg-slate-50')); ?>">
                        <div class="flex items-center justify-between"><span class="flex h-6 w-6 items-center justify-center rounded-full text-[9px] font-bold <?php echo e($isCurrent ? 'bg-orange-600 text-white' : ($isVisited ? 'bg-emerald-600 text-white' : 'bg-slate-200 text-slate-500')); ?>"><?php echo e($loop->iteration); ?></span><?php if($stage->sla_minutes): ?><small class="text-[8px] text-slate-400"><?php echo e($stage->sla_minutes); ?>m</small><?php endif; ?></div>
                        <strong class="mt-2 block text-[10px] text-[#0b1f38]"><?php echo e($stage->stage_name_en); ?></strong><small class="mt-1 block text-[8px] uppercase text-slate-400"><?php echo e($stage->queue_role ? str($stage->queue_role)->replace('_',' ') : $stage->stage_type); ?></small>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </div>
        </div>
    </section>

    <div class="grid gap-5 xl:grid-cols-[1fr_320px]">
        <main class="min-w-0 space-y-5">
            <nav class="flex gap-1 overflow-x-auto rounded-2xl border border-slate-200 bg-white p-2 text-[10px] font-bold shadow-sm">
                <?php $__currentLoopData = ['overview'=>'Overview','documents'=>'Documents','government'=>'Government','outputs'=>'Outputs & Delivery','finance'=>'Fees & Tasks','timeline'=>'Timeline']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key=>$label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <button @click="tab='<?php echo e($key); ?>'" :class="tab==='<?php echo e($key); ?>' ? 'bg-[#0b1f38] text-white' : 'text-slate-500 hover:bg-slate-50'" class="whitespace-nowrap rounded-xl px-3 py-2"><?php echo e($label); ?></button>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </nav>

            <section x-show="tab==='overview'" class="space-y-5">
                <div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
                    <article class="erp-kpi-card"><div class="erp-kpi-icon bg-blue-50 text-blue-600">C</div><div><p>Customer</p><strong class="!text-sm"><?php echo e($application->customer?->full_name_en); ?></strong><small><?php echo e($contact?->primary_mobile ?? 'No mobile'); ?></small></div></article>
                    <article class="erp-kpi-card"><div class="erp-kpi-icon bg-violet-50 text-violet-600">D</div><div><p>Documents</p><strong><?php echo e($application->documents->where('status','verified')->count()); ?>/<?php echo e($application->documents->where('is_mandatory',true)->count()); ?></strong><small><?php echo e(str($application->document_status)->replace('_',' ')->title()); ?></small></div></article>
                    <article class="erp-kpi-card"><div class="erp-kpi-icon bg-emerald-50 text-emerald-600">AED</div><div><p>Total Fee</p><strong><?php echo e(number_format((float)$application->total_price,2)); ?></strong><small>Government + service + VAT</small></div></article>
                    <article class="erp-kpi-card"><div class="erp-kpi-icon bg-orange-50 text-orange-600">T</div><div><p>Open Tasks</p><strong><?php echo e($application->workflowTasks->whereNotIn('status',['completed','cancelled'])->count()); ?></strong><small><?php echo e($application->workflowTasks->count()); ?> workflow tasks</small></div></article>
                </div>

                <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Current Stage Instructions</h2><p><?php echo e($application->currentStage?->instructions ?? 'Follow the configured service workflow.'); ?></p></div><span class="erp-status" data-status="<?php echo e($application->sla_status); ?>"><?php echo e(str($application->sla_status)->replace('_',' ')->title()); ?></span></div>
                    <div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
                        <?php $__currentLoopData = [
                            ['Mandatory documents', !$application->currentStage?->requires_documents || $application->document_status === 'verified'],
                            ['Payment arrangement', !$application->currentStage?->requires_payment || in_array($application->payment_status,['paid','credit_account','corporate_credit']) || (float)$application->total_price <= 0],
                            ['Supervisor approval', !$application->currentStage?->requires_approval || $stageApproval?->status === 'approved'],
                            ['Government submission', !$application->currentStage?->requires_submission || $application->submissions->isNotEmpty()],
                            ['Approved output', !$application->currentStage?->requires_output || $application->outputs->where('status','ready')->isNotEmpty()],
                            ['Customer notification', !$application->currentStage?->requires_notification || $application->customer_notified_at],
                        ]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label,$ready]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <div class="flex items-center gap-2 rounded-xl border <?php echo e($ready ? 'border-emerald-200 bg-emerald-50' : 'border-amber-200 bg-amber-50'); ?> p-3 text-[10px] font-bold"><span class="flex h-6 w-6 items-center justify-center rounded-full <?php echo e($ready ? 'bg-emerald-600' : 'bg-amber-500'); ?> text-white"><?php echo e($ready ? '✓' : '!'); ?></span><?php echo e($label); ?></div>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </div>
                </section>

                <section class="erp-panel border-blue-200 bg-blue-50/30"><div class="erp-panel-heading"><div><h2>Service Accountability</h2><p>Permanent staff attribution remains visible after every workflow handoff.</p></div><span class="erp-status" data-status="verified">Audit ready</span></div>
                    <div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
                        <div class="rounded-xl bg-white p-4 shadow-sm"><p class="text-[8px] font-bold uppercase tracking-wider text-slate-400">Customer attended by</p><strong class="mt-2 block text-xs text-[#0b1f38]"><?php echo e($application->attendedBy?->name ?? $application->creator?->name ?? 'Not recorded'); ?></strong><small class="mt-1 block text-[9px] text-slate-400">Intake / front desk</small></div>
                        <div class="rounded-xl bg-white p-4 shadow-sm"><p class="text-[8px] font-bold uppercase tracking-wider text-slate-400">Service typed by</p><strong class="mt-2 block text-xs text-[#0b1f38]"><?php echo e($application->typedByUser?->name ?? 'Typing not started'); ?></strong><small class="mt-1 block text-[9px] text-slate-400">Responsible typist</small></div>
                        <div class="rounded-xl bg-white p-4 shadow-sm"><p class="text-[8px] font-bold uppercase tracking-wider text-slate-400">Typing started</p><strong class="mt-2 block text-xs text-[#0b1f38]"><?php echo e($application->typing_started_at?->format('d M Y, h:i A') ?? 'Not started'); ?></strong><small class="mt-1 block text-[9px] text-slate-400">Latest typing cycle</small></div>
                        <div class="rounded-xl bg-white p-4 shadow-sm"><p class="text-[8px] font-bold uppercase tracking-wider text-slate-400">Typing completed</p><strong class="mt-2 block text-xs text-[#0b1f38]"><?php echo e($application->typing_completed_at?->format('d M Y, h:i A') ?? 'In progress'); ?></strong><small class="mt-1 block text-[9px] text-slate-400">Submitted for quality check</small></div>
                    </div>
                </section>
            </section>

            <section x-show="tab==='documents'" class="erp-panel">
                <div class="erp-panel-heading"><div><h2>Application Document Checklist</h2><p>Mandatory evidence must be uploaded and verified before the workflow can advance.</p></div><span class="erp-status" data-status="<?php echo e($application->document_status); ?>"><?php echo e(str($application->document_status)->replace('_',' ')->title()); ?></span></div>
                <div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Requirement</th><th>Source</th><th>File</th><th>Status</th><th>Controlled Action</th></tr></thead><tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $application->documents; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $document): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr><td><strong><?php echo e($document->display_name); ?></strong><small><?php echo e($document->is_mandatory ? 'Mandatory' : 'Optional'); ?> · <?php echo e($document->documentType?->code); ?></small></td><td><strong><?php echo e(str($document->source_entity)->replace('_',' ')->title()); ?></strong><small><?php echo e($document->customer_document_id ? 'Linked vault version' : 'Application upload'); ?></small></td><td><strong><?php echo e($document->attachment_path || $document->customer_document_id ? 'Available' : 'Missing'); ?></strong><?php if($document->rejection_reason): ?><small class="text-rose-600"><?php echo e($document->rejection_reason); ?></small><?php endif; ?></td><td><span class="erp-status" data-status="<?php echo e($document->status); ?>"><?php echo e(ucfirst($document->status)); ?></span></td><td>
                            <?php if(auth()->user()->hasPermission('documents.upload')): ?>
                                <details><summary class="cursor-pointer text-[10px] font-bold text-orange-600">Upload / Review</summary><div class="mt-2 min-w-[270px] space-y-2">
                                    <form method="POST" enctype="multipart/form-data" action="<?php echo e(route('app.applications.documents.upload',$application->id)); ?>" class="flex gap-1"><?php echo csrf_field(); ?><input type="hidden" name="application_document_id" value="<?php echo e($document->id); ?>"><input required type="file" name="attachment" accept=".pdf,.jpg,.jpeg,.png,.webp" class="erp-input !py-1"><button class="rounded-lg bg-slate-900 px-2 text-[9px] font-bold text-white">Upload</button></form>
                                    <form method="POST" action="<?php echo e(route('app.applications.documents.review',[$application->id,$document->id])); ?>" class="flex gap-1"><?php echo csrf_field(); ?><select name="status" class="erp-select-compact"><option value="verified">Verify</option><option value="rejected">Reject</option><option value="exception">Exception</option></select><input name="rejection_reason" placeholder="Reason if rejected" class="erp-input !py-1"><button class="rounded-lg bg-orange-600 px-2 text-[9px] font-bold text-white">Save</button></form>
                                </div></details>
                            <?php else: ?><span class="text-[9px] text-slate-400">Read only</span><?php endif; ?>
                        </td></tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                        <tr><td colspan="5"><div class="erp-empty"><span>✓</span><strong>No document rules configured</strong><p>This service can pass document verification without attachments.</p></div></td></tr>
                    <?php endif; ?>
                </tbody></table></div>
            </section>

            <section x-show="tab==='government'" class="space-y-5">
                <?php if($stageCode === 'GOVERNMENT_SUBMISSION' && auth()->user()->hasPermission('applications.submit')): ?>
                    <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Record Government Submission</h2><p>Every portal attempt is immutable and receives its own attempt number.</p></div><span class="erp-status" data-status="submitted">Attempt <?php echo e($application->submissions->count()+1); ?></span></div><form method="POST" enctype="multipart/form-data" action="<?php echo e(route('app.applications.submission',$application->id)); ?>" class="mt-4 grid gap-3 sm:grid-cols-2"><?php echo csrf_field(); ?>
                        <input required name="authority_code" value="<?php echo e($application->service?->authority?->code); ?>" placeholder="Authority code *" class="erp-input"><input name="portal_name" placeholder="Portal name" class="erp-input"><input required name="government_reference" placeholder="Government reference *" class="erp-input"><input name="request_number" placeholder="Request number" class="erp-input"><input name="transaction_number" placeholder="Transaction number" class="erp-input"><input name="receipt_number" placeholder="Government receipt" class="erp-input"><input type="number" min="0" step="0.01" name="gov_fee_paid" placeholder="Government fee paid" class="erp-input"><input type="file" name="attachment" accept=".pdf,.jpg,.jpeg,.png,.webp" class="erp-input"><textarea name="remarks" rows="2" placeholder="Portal remarks" 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">Submit Attempt & Move to Government Processing</button>
                    </form></section>
                <?php endif; ?>

                <?php if($stageCode === 'GOVERNMENT_PROCESSING'): ?>
                    <section class="grid gap-3 md:grid-cols-3">
                        <?php if(auth()->user()->hasPermission('applications.approve')): ?><form method="POST" action="<?php echo e(route('app.applications.approval',$application->id)); ?>" class="rounded-2xl border border-emerald-200 bg-emerald-50 p-4"><?php echo csrf_field(); ?><strong class="text-xs text-emerald-800">Government Approved</strong><textarea name="remarks" rows="2" placeholder="Approval remarks" class="erp-input mt-3"></textarea><button class="mt-2 w-full rounded-lg bg-emerald-600 py-2 text-[10px] font-bold text-white">Record Approval</button></form><?php endif; ?>
                        <?php if(auth()->user()->hasPermission('applications.edit')): ?><form method="POST" action="<?php echo e(route('app.applications.return',$application->id)); ?>" class="rounded-2xl border border-amber-200 bg-amber-50 p-4"><?php echo csrf_field(); ?><strong class="text-xs text-amber-800">Returned for Correction</strong><textarea required name="reason" rows="2" placeholder="Authority reason and required correction *" class="erp-input mt-3"></textarea><button class="mt-2 w-full rounded-lg bg-amber-600 py-2 text-[10px] font-bold text-white">Record Return</button></form><?php endif; ?>
                        <?php if(auth()->user()->hasPermission('applications.reject')): ?><form method="POST" action="<?php echo e(route('app.applications.rejection',$application->id)); ?>" class="rounded-2xl border border-rose-200 bg-rose-50 p-4"><?php echo csrf_field(); ?><strong class="text-xs text-rose-800">Government Rejected</strong><textarea required name="reason" rows="2" placeholder="Structured rejection reason *" class="erp-input mt-3"></textarea><button class="mt-2 w-full rounded-lg bg-rose-600 py-2 text-[10px] font-bold text-white">Record Rejection</button></form><?php endif; ?>
                    </section>
                <?php endif; ?>

                <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Government Attempt History</h2><p>Returns and resubmissions never overwrite earlier attempts.</p></div><span class="text-[10px] font-bold text-slate-400"><?php echo e($application->submissions->count()); ?> attempt(s)</span></div><div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Attempt</th><th>Authority / Portal</th><th>References</th><th>Government Fee</th><th>Status</th><th>Response</th></tr></thead><tbody>
                    <?php $__empty_1 = true; $__currentLoopData = $application->submissions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $submission): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <tr><td><strong>#<?php echo e($submission->attempt_number); ?></strong><small><?php echo e($submission->created_at->format('d M Y, h:i A')); ?></small></td><td><strong><?php echo e($submission->authority_code); ?></strong><small><?php echo e($submission->portal_name); ?></small></td><td><strong><?php echo e($submission->government_reference); ?></strong><small><?php echo e($submission->request_number ?: $submission->transaction_number); ?></small></td><td><strong>AED <?php echo e(number_format((float)$submission->gov_fee_paid,2)); ?></strong><small><?php echo e($submission->receipt_number); ?></small></td><td><span class="erp-status" data-status="<?php echo e($submission->status); ?>"><?php echo e(ucfirst($submission->status)); ?></span></td><td><strong><?php echo e($submission->response_at?->format('d M Y, h:i A') ?? 'Awaiting response'); ?></strong><?php if($submission->return_reason): ?><small class="text-amber-600"><?php echo e($submission->return_reason); ?></small><?php endif; ?></td></tr>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><tr><td colspan="6"><div class="erp-empty"><span>G</span><strong>No submission attempts</strong><p>Advance to Government Submission to record the first attempt.</p></div></td></tr><?php endif; ?>
                </tbody></table></div></section>
            </section>

            <section x-show="tab==='outputs'" class="space-y-5">
                <?php if(in_array($stageCode,['GOVERNMENT_APPROVED','OUTPUT_READY']) && auth()->user()->hasPermission('documents.upload')): ?>
                    <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Upload Approved Government Output</h2><p>Store the exact delivered permit, visa, form, license or official receipt.</p></div></div><form method="POST" enctype="multipart/form-data" action="<?php echo e(route('app.applications.outputs.store',$application->id)); ?>" class="mt-4 grid gap-3 sm:grid-cols-2"><?php echo csrf_field(); ?><input required name="document_name" placeholder="Output document name *" class="erp-input"><select required name="output_type" class="erp-input"><option value="government_output">Government output</option><option value="visa">Visa</option><option value="permit">Permit</option><option value="work_permit">Work permit</option><option value="emirates_id_form">Emirates ID form</option><option value="license">License</option><option value="government_receipt">Government receipt</option><option value="other">Other</option></select><input name="reference_number" placeholder="Output reference" class="erp-input"><input required type="file" name="attachment" accept=".pdf,.jpg,.jpeg,.png,.webp" class="erp-input"><input type="date" name="issue_date" class="erp-input"><input type="date" name="expiry_date" class="erp-input"><button class="rounded-xl bg-orange-600 py-3 text-xs font-bold text-white sm:col-span-2">Upload Secure Output</button></form></section>
                <?php endif; ?>
                <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Output Register</h2><p>Customer-visible final records attached to this application.</p></div><span class="erp-status" data-status="<?php echo e($application->output_status); ?>"><?php echo e(ucfirst($application->output_status)); ?></span></div><div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Document</th><th>Type</th><th>Reference</th><th>Validity</th><th>Status</th></tr></thead><tbody><?php $__empty_1 = true; $__currentLoopData = $application->outputs; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $output): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><tr><td><strong><?php echo e($output->document_name); ?></strong><small>By <?php echo e($output->uploader?->name); ?> · <?php echo e($output->created_at->diffForHumans()); ?></small></td><td><strong><?php echo e(str($output->output_type)->replace('_',' ')->title()); ?></strong></td><td><strong><?php echo e($output->reference_number ?: 'Not recorded'); ?></strong></td><td><strong><?php echo e($output->issue_date?->format('d M Y') ?? '—'); ?></strong><small><?php echo e($output->expiry_date ? 'Expires '.$output->expiry_date->format('d M Y') : 'No expiry'); ?></small></td><td><span class="erp-status" data-status="<?php echo e($output->status); ?>"><?php echo e(ucfirst($output->status)); ?></span></td></tr><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><tr><td colspan="5"><div class="erp-empty"><span>O</span><strong>No approved outputs uploaded</strong><p>Outputs become available after government approval.</p></div></td></tr><?php endif; ?></tbody></table></div></section>

                <?php if(in_array($stageCode,['OUTPUT_READY','CUSTOMER_NOTIFICATION']) && auth()->user()->hasPermission('communications.manage')): ?>
                    <section class="grid gap-5 lg:grid-cols-2">
                        <div class="erp-panel"><div class="erp-panel-heading"><div><h2>Notify Customer</h2><p>Log the channel and exact collection message.</p></div></div><form method="POST" action="<?php echo e(route('app.applications.notify',$application->id)); ?>" class="mt-4 space-y-3"><?php echo csrf_field(); ?><select name="channel" class="erp-input"><option value="whatsapp">WhatsApp</option><option value="sms">SMS</option><option value="email">Email</option><option value="phone">Phone call</option></select><input name="recipient" placeholder="Recipient (uses customer contact if empty)" class="erp-input"><textarea required name="message" rows="3" class="erp-input">Your application <?php echo e($application->application_number); ?> is ready for collection. Please bring valid identification.</textarea><button class="w-full rounded-xl bg-emerald-600 py-3 text-xs font-bold text-white">Record Customer Notification</button></form></div>
                        <?php if($stageCode==='CUSTOMER_NOTIFICATION'): ?><div class="erp-panel"><div class="erp-panel-heading"><div><h2>Secure Delivery Acknowledgement</h2><p>Identify the recipient before closing the application.</p></div></div><form method="POST" action="<?php echo e(route('app.applications.deliver',$application->id)); ?>" class="mt-4 grid gap-3 sm:grid-cols-2"><?php echo csrf_field(); ?><select name="delivery_method" class="erp-input"><option value="counter_collection">Counter collection</option><option value="courier">Courier</option><option value="email">Email</option><option value="digital_portal">Digital portal</option></select><input required name="recipient_name" placeholder="Recipient name *" class="erp-input"><input name="recipient_mobile" placeholder="Recipient mobile" class="erp-input"><input name="identification_type" placeholder="ID type" class="erp-input"><input name="identification_reference" placeholder="Masked ID reference" class="erp-input"><textarea required name="acknowledgement" rows="2" placeholder="Acknowledgement / handover notes *" class="erp-input sm:col-span-2"></textarea><button class="rounded-xl bg-[#0b1f38] py-3 text-xs font-bold text-white sm:col-span-2">Acknowledge Delivery & Close</button></form></div><?php endif; ?>
                    </section>
                <?php endif; ?>
            </section>

            <section x-show="tab==='finance'" class="space-y-5">
                <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Immutable Fee Snapshot</h2><p>Government money remains separated from typing-center revenue.</p></div><?php if(auth()->user()->hasPermission('payments.view')): ?><a href="<?php echo e($activeInvoice ? route('app.billing.invoices.show',$activeInvoice->id) : route('app.billing.pos',['q'=>$application->application_number])); ?>"><?php echo e($activeInvoice ? 'Open Invoice' : 'Open POS'); ?></a><?php endif; ?></div><div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Fee</th><th>Classification</th><th>Tax</th><th>Amount</th></tr></thead><tbody><?php $__currentLoopData = $application->feeItems; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $fee): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><tr><td><strong><?php echo e($fee->name_en); ?></strong><small><?php echo e($fee->fee_code); ?></small></td><td><strong><?php echo e(str($fee->revenue_classification)->replace('_',' ')->title()); ?></strong></td><td><strong><?php echo e(str($fee->tax_behavior)->replace('_',' ')->title()); ?></strong></td><td><strong>AED <?php echo e(number_format((float)$fee->amount,2)); ?></strong></td></tr><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><tr class="bg-slate-50"><td colspan="3"><strong>Total</strong></td><td><strong class="text-orange-600">AED <?php echo e(number_format((float)$application->total_price,2)); ?></strong></td></tr></tbody></table></div></section>
                <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Workflow Tasks</h2><p>Role handoffs create traceable operational assignments.</p></div><a href="<?php echo e(route('app.tasks.index')); ?>">Task Center</a></div><div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Task</th><th>Assigned</th><th>Due</th><th>Status</th></tr></thead><tbody><?php $__empty_1 = true; $__currentLoopData = $application->workflowTasks; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $task): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><tr><td><strong><?php echo e($task->task_number); ?> · <?php echo e($task->title); ?></strong><small><?php echo e($task->description); ?></small></td><td><strong><?php echo e($task->assignedUser?->name ?? 'Role queue'); ?></strong></td><td><strong><?php echo e($task->due_at?->format('d M Y, h:i A') ?? 'No deadline'); ?></strong></td><td><span class="erp-status" data-status="<?php echo e($task->status); ?>"><?php echo e(str($task->status)->replace('_',' ')->title()); ?></span></td></tr><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><tr><td colspan="4"><div class="erp-empty"><span>T</span><strong>No workflow tasks yet</strong><p>The next role handoff creates an operational task.</p></div></td></tr><?php endif; ?></tbody></table></div></section>
            </section>

            <section x-show="tab==='timeline'" class="erp-panel"><div class="erp-panel-heading"><div><h2>Complete Application Timeline</h2><p>Stage movements, assignments and government actions are immutable.</p></div></div><div class="mt-5 space-y-4 border-l-2 border-slate-100 pl-5">
                <?php $__currentLoopData = $application->stageHistory->sortByDesc('created_at'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $history): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><article class="relative rounded-2xl border border-slate-200 bg-slate-50 p-4 before:absolute before:-left-[27px] before:top-5 before:h-3 before:w-3 before:rounded-full before:bg-orange-500"><div class="flex flex-wrap items-start justify-between gap-2"><div><strong class="text-xs text-[#0b1f38]"><?php echo e($history->action_name); ?></strong><p class="mt-1 text-[10px] text-slate-500"><?php echo e($history->fromStage?->stage_name_en ?? 'Application created'); ?> → <?php echo e($history->toStage?->stage_name_en); ?></p></div><small class="text-[9px] text-slate-400"><?php echo e($history->created_at->format('d M Y, h:i A')); ?></small></div><p class="mt-2 text-[10px] text-slate-500"><?php echo e($history->remarks); ?></p><small class="mt-2 block text-[9px] font-bold text-slate-400">By <?php echo e($history->performedByUser?->name); ?></small></article><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </div></section>
        </main>

        <aside class="space-y-5">
            <section class="erp-panel">
                <div class="erp-panel-heading"><div><h2>Next Actions</h2><p>Only permitted direct transitions are shown.</p></div></div>
                <div class="mt-4 space-y-3">
                    <?php $__empty_1 = true; $__currentLoopData = $availableTransitions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $transition): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                        <div class="rounded-2xl border <?php echo e($transition['ready'] ? 'border-emerald-200 bg-emerald-50' : 'border-amber-200 bg-amber-50'); ?> p-3">
                            <strong class="block text-xs text-[#0b1f38]"><?php echo e($transition['action_name_en']); ?></strong><small class="mt-1 block text-[9px] text-slate-500">Next: <?php echo e($transition['to_stage']?->stage_name_en); ?></small>
                            <?php if($transition['ready']): ?>
                                <form method="POST" action="<?php echo e(route('app.applications.transition',$application->id)); ?>" class="mt-3 space-y-2"><?php echo csrf_field(); ?><input type="hidden" name="target_stage_id" value="<?php echo e($transition['to_stage_id']); ?>"><input name="remarks" placeholder="Handover remarks" class="erp-input"><button class="w-full rounded-lg bg-[#0b1f38] py-2 text-[10px] font-bold text-white">Complete & Handoff</button></form>
                            <?php else: ?>
                                <ul class="mt-2 space-y-1"><?php $__currentLoopData = $transition['blockers']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $blocker): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><li class="text-[9px] text-amber-700">• <?php echo e($blocker); ?></li><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></ul>
                            <?php endif; ?>
                        </div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><div class="erp-empty !p-4"><span>✓</span><strong>No direct action</strong><p>Use the stage-specific form or wait for the assigned role.</p></div><?php endif; ?>
                </div>
            </section>

            <?php if($stageApproval): ?>
                <section class="rounded-2xl border <?php echo e($stageApproval->status==='approved' ? 'border-emerald-200 bg-emerald-50' : 'border-violet-200 bg-violet-50'); ?> p-4"><p class="text-[9px] font-bold uppercase text-slate-500">Supervisor Approval</p><strong class="mt-1 block text-xs"><?php echo e($stageApproval->approval_number); ?></strong><span class="erp-status mt-2" data-status="<?php echo e($stageApproval->status); ?>"><?php echo e(ucfirst($stageApproval->status)); ?></span><?php if($stageApproval->status==='pending' && auth()->user()->hasPermission('approvals.manage')): ?><a href="<?php echo e(route('app.tasks.index',['approval_status'=>'pending'])); ?>" class="mt-3 block text-[10px] font-bold text-violet-700">Review in Approval Center →</a><?php endif; ?></section>
            <?php endif; ?>

            <section class="erp-panel"><div class="erp-panel-heading"><div><h2>SLA Control</h2><p>Pauses require an operational reason.</p></div></div>
                <?php if($application->sla_paused_at): ?>
                    <div class="mt-4 rounded-xl bg-amber-50 p-3 text-[10px] text-amber-800"><strong>Paused: <?php echo e(str($application->sla_pause_reason)->replace('_',' ')->title()); ?></strong><small class="mt-1 block">Since <?php echo e($application->sla_paused_at->diffForHumans()); ?></small></div>
                    <?php if(auth()->user()->hasPermission('applications.edit')): ?><form method="POST" action="<?php echo e(route('app.applications.sla.resume',$application->id)); ?>" class="mt-3 space-y-2"><?php echo csrf_field(); ?><input name="remarks" placeholder="Resume notes" class="erp-input"><button class="w-full rounded-lg bg-emerald-600 py-2 text-[10px] font-bold text-white">Resume SLA</button></form><?php endif; ?>
                <?php elseif(auth()->user()->hasPermission('applications.edit')): ?>
                    <form method="POST" action="<?php echo e(route('app.applications.sla.pause',$application->id)); ?>" class="mt-4 space-y-2"><?php echo csrf_field(); ?><select name="reason" class="erp-input"><option value="waiting_customer">Waiting customer</option><option value="documents_pending">Documents pending</option><option value="payment_pending">Payment pending</option><option value="government_delay">Government delay</option><option value="portal_down">Portal down</option></select><input name="remarks" placeholder="Reason details" class="erp-input"><button class="w-full rounded-lg border border-slate-200 bg-white py-2 text-[10px] font-bold text-slate-700">Pause SLA</button></form>
                <?php endif; ?>
            </section>

            <?php if(auth()->user()->hasPermission('applications.edit')): ?><section class="erp-panel"><div class="erp-panel-heading"><div><h2>Add Timeline Note</h2><p>Internal or customer-visible context.</p></div></div><form method="POST" action="<?php echo e(route('app.applications.note',$application->id)); ?>" class="mt-4 space-y-2"><?php echo csrf_field(); ?><textarea required name="note_text" rows="3" placeholder="Note *" class="erp-input"></textarea><label class="flex items-center gap-2 text-[10px] text-slate-600"><input type="checkbox" name="is_customer_visible" value="1"> Customer visible</label><button class="w-full rounded-lg bg-orange-600 py-2 text-[10px] font-bold text-white">Add Note</button></form></section><?php endif; ?>
        </aside>
    </div>
</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\applications\show.blade.php ENDPATH**/ ?>