<?php $__env->startSection('title', $opportunity->renewal_number); ?>

<?php $__env->startSection('content'); ?>
<?php
    $entityName = $opportunity->customer?->full_name_en ?? $opportunity->corporateEmployee?->full_name ?? $opportunity->company?->name_en ?? $opportunity->employee?->full_name ?? auth()->user()->tenant->company_name;
    $days = (int) now()->startOfDay()->diffInDays($opportunity->expiry_date->copy()->startOfDay(), false);
?>
<div class="mx-auto max-w-[1600px] space-y-5">
    <?php echo $__env->make('tenant.partials.typist-page-nav', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?>
    <section class="overflow-hidden rounded-[28px] border border-violet-100 bg-gradient-to-br from-white via-violet-50 to-orange-50 p-6 text-slate-950 shadow-sm">
        <div class="flex flex-col gap-5 xl:flex-row xl:items-center xl:justify-between">
            <div><a href="<?php echo e(route('app.renewals.index')); ?>" class="text-[9px] font-bold uppercase tracking-wider text-violet-700">Back to renewal documents</a><div class="mt-3 flex flex-wrap items-center gap-2"><h1 class="text-2xl font-bold"><?php echo e($opportunity->renewal_number); ?></h1><span class="rounded-full bg-violet-100 px-3 py-1 text-[9px] font-bold uppercase text-violet-700"><?php echo e(str($opportunity->status)->replace('_',' ')); ?></span><span class="rounded-full bg-orange-500 px-3 py-1 text-[9px] font-bold uppercase text-white"><?php echo e(str($opportunity->document_status)->replace('_',' ')); ?></span></div><p class="mt-2 text-xs text-slate-600"><?php echo e($entityName); ?> · <?php echo e(str($opportunity->document_type)->replace('_',' ')->title()); ?> · <?php echo e($opportunity->document_number ?: 'No document number'); ?></p></div>
            <div class="grid grid-cols-2 gap-2 sm:grid-cols-4 xl:min-w-[610px]">
                <div class="rounded-2xl border border-white bg-white/90 p-3 shadow-sm"><small class="text-[8px] uppercase text-slate-500">Expiry</small><strong class="mt-1 block text-xs"><?php echo e($opportunity->expiry_date->format('d M Y')); ?></strong></div>
                <div class="rounded-2xl border border-white bg-white/90 p-3 shadow-sm"><small class="text-[8px] uppercase text-slate-500">Remaining</small><strong class="mt-1 block text-xs <?php echo e($days < 0 ? 'text-rose-600' : 'text-orange-600'); ?>"><?php echo e($days < 0 ? abs($days).' days overdue' : $days.' days'); ?></strong></div>
                <div class="rounded-2xl border border-white bg-white/90 p-3 shadow-sm"><small class="text-[8px] uppercase text-slate-500">Owner</small><strong class="mt-1 block truncate text-xs"><?php echo e($opportunity->assignedUser?->name ?? 'Unassigned'); ?></strong></div>
                <div class="rounded-2xl border border-white bg-white/90 p-3 shadow-sm"><small class="text-[8px] uppercase text-slate-500">Next reminder</small><strong class="mt-1 block text-xs"><?php echo e($opportunity->next_reminder_at?->format('d M, h:i A') ?? 'Not scheduled'); ?></strong></div>
            </div>
        </div>
    </section>

    <div class="grid gap-5 xl:grid-cols-[minmax(0,1fr)_370px]">
        <main class="space-y-5">
            <section class="erp-panel">
                <div class="erp-panel-heading"><div><h2>Renewal Document Record</h2><p>Current document identity, expiry and reminder controls.</p></div><span class="erp-status" data-status="<?php echo e($opportunity->document_status); ?>"><?php echo e(str($opportunity->document_status)->replace('_',' ')->title()); ?></span></div>
                <div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
                    <?php $__currentLoopData = [
                        ['Entity', $entityName, str($opportunity->entity_type)->replace('_',' ')->title()],
                        ['Document', str($opportunity->document_type)->replace('_',' ')->title(), $opportunity->document_number ?: 'Number not recorded'],
                        ['Reminder control', $opportunity->reminder_enabled ? 'Enabled' : 'Disabled', $opportunity->reminder_channel ? str($opportunity->reminder_channel)->replace('_',' ')->title() : 'Automatic rules'],
                        ['Last reminder', $opportunity->last_reminder_at?->format('d M Y, h:i A') ?? 'Not sent', $opportunity->reminderLogs->count().' reminder records'],
                    ]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$label,$value,$note]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <div class="rounded-2xl border border-slate-100 bg-slate-50 p-4"><p class="text-[8px] font-bold uppercase tracking-wider text-slate-400"><?php echo e($label); ?></p><strong class="mt-2 block text-xs text-[#0b1f38]"><?php echo e($value); ?></strong><small class="mt-1 block text-[9px] text-slate-400"><?php echo e($note); ?></small></div>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </div>
            </section>

            <?php if($opportunity->application): ?>
                <section class="erp-panel border-blue-200 bg-blue-50/30">
                    <div class="erp-panel-heading"><div><h2>Linked Service Accountability</h2><p>Administrators can see who attended the customer and who typed the service.</p></div><a href="<?php echo e(route('app.applications.show', $opportunity->application->id)); ?>">Open Application 360</a></div>
                    <div class="mt-4 grid gap-3 sm:grid-cols-3">
                        <div class="rounded-xl bg-white p-4 shadow-sm"><p class="text-[8px] font-bold uppercase text-slate-400">Application</p><strong class="mt-1 block text-xs text-blue-700"><?php echo e($opportunity->application->application_number); ?></strong></div>
                        <div class="rounded-xl bg-white p-4 shadow-sm"><p class="text-[8px] font-bold uppercase text-slate-400">Customer attended by</p><strong class="mt-1 block text-xs text-[#0b1f38]"><?php echo e($opportunity->application->attendedBy?->name ?? 'Not recorded'); ?></strong></div>
                        <div class="rounded-xl bg-white p-4 shadow-sm"><p class="text-[8px] font-bold uppercase text-slate-400">Service typed by</p><strong class="mt-1 block text-xs text-[#0b1f38]"><?php echo e($opportunity->application->typedByUser?->name ?? 'Typing not started'); ?></strong></div>
                    </div>
                </section>
            <?php endif; ?>

            <section class="grid gap-5 lg:grid-cols-2">
                <div class="erp-panel"><div class="erp-panel-heading"><div><h2>Pipeline History</h2><p>Every renewal stage change and responsible user.</p></div></div><div class="mt-5 space-y-0"><?php $__empty_1 = true; $__currentLoopData = $opportunity->statusHistory; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $history): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><div class="relative border-l border-slate-200 pb-5 pl-6 last:border-transparent"><span class="absolute -left-1.5 top-1 h-3 w-3 rounded-full bg-violet-500 ring-4 ring-violet-50"></span><div class="flex justify-between gap-3"><div><p class="text-[11px] font-bold text-[#0b1f38]"><?php echo e(str($history->to_status)->replace('_',' ')->title()); ?></p><p class="mt-1 text-[9px] text-slate-500"><?php echo e($history->notes ?: 'Pipeline status updated.'); ?></p><small class="mt-1 block text-[8px] text-slate-400">By <?php echo e($history->changedByUser?->name ?? 'Automation'); ?></small></div><time class="whitespace-nowrap text-[8px] text-slate-400"><?php echo e($history->created_at->format('d M H:i')); ?></time></div></div><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><p class="rounded-xl bg-slate-50 p-5 text-center text-[9px] text-slate-400">No pipeline history.</p><?php endif; ?></div></div>
                <div class="erp-panel"><div class="erp-panel-heading"><div><h2>Reminder History</h2><p>Scheduled, queued and delivered renewal notices.</p></div><span class="text-[9px] font-bold text-slate-400"><?php echo e($opportunity->reminderLogs->count()); ?> records</span></div><div class="mt-4 space-y-2"><?php $__empty_1 = true; $__currentLoopData = $opportunity->reminderLogs->sortByDesc('id'); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $log): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><div class="rounded-xl border border-slate-100 bg-slate-50 p-3"><div class="flex items-center justify-between gap-2"><div><strong class="text-[9px] uppercase text-violet-700"><?php echo e(str($log->channel)->replace('_',' ')); ?></strong><small class="ml-2 text-[8px] text-slate-400"><?php echo e($log->recipient); ?></small></div><span class="erp-status" data-status="<?php echo e($log->status); ?>"><?php echo e(ucfirst($log->status)); ?></span></div><p class="mt-2 text-[8px] text-slate-500"><?php echo e($log->notes ?: 'Standard renewal reminder'); ?></p><p class="mt-1 text-[8px] text-slate-400"><?php echo e($log->scheduled_at ? 'Scheduled '.$log->scheduled_at->format('d M Y, h:i A') : ($log->sent_at ? 'Processed '.$log->sent_at->format('d M Y, h:i A') : 'Automatic schedule')); ?> · By <?php echo e($log->creator?->name ?? 'Automation'); ?></p></div><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><p class="rounded-xl bg-slate-50 p-5 text-center text-[9px] text-slate-400">No reminders scheduled for this cycle.</p><?php endif; ?></div></div>
            </section>
        </main>

        <aside class="space-y-4">
            <?php if(auth()->check() && auth()->user()->hasPermission('renewals.manage')): ?>
                <form method="POST" action="<?php echo e(route('app.renewals.document_status', $opportunity->id)); ?>" class="rounded-[22px] border border-orange-200 bg-gradient-to-br from-orange-50 to-white p-5 shadow-sm"><?php echo csrf_field(); ?>
                    <p class="text-[9px] font-bold uppercase tracking-[.16em] text-orange-600">Typist document control</p><h2 class="mt-1 text-sm font-bold text-[#0b1f38]">Update Renewal Document</h2>
                    <div class="mt-4 space-y-2.5"><select name="document_status" required class="erp-input"><?php $__currentLoopData = ['active','expiring','expired','renewal_in_progress','renewed','on_hold','cancelled']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($status); ?>" <?php if($opportunity->document_status === $status): echo 'selected'; endif; ?>><?php echo e(str($status)->replace('_',' ')->title()); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><input name="document_number" value="<?php echo e($opportunity->document_number); ?>" placeholder="Document number" class="erp-input"><input type="date" name="expiry_date" value="<?php echo e($opportunity->expiry_date->format('Y-m-d')); ?>" class="erp-input"><label class="flex items-center gap-2 rounded-xl bg-white px-3 py-2 text-[10px] font-bold text-slate-600"><input type="checkbox" name="reminder_enabled" value="1" <?php if($opportunity->reminder_enabled): echo 'checked'; endif; ?>> Enable expiry reminders</label><button class="w-full rounded-xl bg-orange-600 py-3 text-[10px] font-bold text-white">Save Document Status</button></div>
                </form>

                <form method="POST" action="<?php echo e(route('app.renewals.reminders.store', $opportunity->id)); ?>" class="rounded-[22px] border border-violet-200 bg-gradient-to-br from-violet-50 to-white p-5 shadow-sm"><?php echo csrf_field(); ?>
                    <p class="text-[9px] font-bold uppercase tracking-[.16em] text-violet-600">Customer reminder</p><h2 class="mt-1 text-sm font-bold text-[#0b1f38]">Schedule Renewal Reminder</h2>
                    <div class="mt-4 space-y-2.5"><select name="channel" class="erp-input"><option value="in_app">In-app reminder</option><option value="whatsapp">WhatsApp queue</option><option value="sms">SMS queue</option><option value="email">Email queue</option></select><input required type="datetime-local" name="scheduled_at" min="<?php echo e(now()->format('Y-m-d\TH:i')); ?>" value="<?php echo e(now()->addDay()->startOfHour()->format('Y-m-d\TH:i')); ?>" class="erp-input"><textarea name="notes" rows="3" placeholder="Reminder message or internal note" class="erp-input"></textarea><button class="w-full rounded-xl bg-violet-600 py-3 text-[10px] font-bold text-white">Schedule Reminder</button></div>
                </form>

                <?php if(in_array('contacted', $allowedStatuses, true) || $opportunity->status === 'contacted'): ?>
                    <form method="POST" action="<?php echo e(route('app.renewals.follow_up',$opportunity->id)); ?>" class="rounded-[22px] bg-[#0b1f38] p-5 text-white shadow-xl"><?php echo csrf_field(); ?><p class="text-[9px] font-bold uppercase tracking-[.16em] text-sky-300">Customer contact</p><h2 class="mt-1 text-sm font-bold">Record Follow-up</h2><div class="mt-4 space-y-2.5"><select name="activity_type" class="w-full rounded-xl border-white/10 bg-white/10 text-xs"><option value="call">Call</option><option value="whatsapp">WhatsApp</option><option value="email">Email</option><option value="meeting">Meeting</option><option value="note">Internal note</option></select><textarea name="notes" required rows="3" class="w-full rounded-xl border-white/10 bg-white/10 text-xs placeholder:text-slate-500" placeholder="Outcome and customer response"></textarea><input name="next_followup_at" type="datetime-local" class="w-full rounded-xl border-white/10 bg-white/10 text-xs"><button class="w-full rounded-xl bg-sky-600 py-3 text-[10px] font-bold">Save Follow-up</button></div></form>
                <?php endif; ?>

                <form method="POST" action="<?php echo e(route('app.renewals.transition',$opportunity->id)); ?>" class="erp-panel"><?php echo csrf_field(); ?><div class="erp-panel-heading"><div><h2>Renewal Pipeline</h2><p>Only valid next stages are available.</p></div></div><?php if(count($allowedStatuses)): ?><div class="mt-4 space-y-2.5"><select name="status" class="erp-input"><?php $__currentLoopData = $allowedStatuses; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $status): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($status); ?>"><?php echo e(str($status)->replace('_',' ')->title()); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><select name="assigned_user_id" class="erp-input"><option value="">Keep current owner</option><?php $__currentLoopData = $users; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($user->id); ?>" <?php if($opportunity->assigned_user_id === $user->id): echo 'selected'; endif; ?>><?php echo e($user->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><textarea name="notes" rows="2" placeholder="Status update notes" class="erp-input"></textarea><input name="lost_reason" placeholder="Lost reason when applicable" class="erp-input"><button class="w-full rounded-xl border border-violet-200 bg-violet-50 py-3 text-[10px] font-bold text-violet-700">Update Pipeline Stage</button></div><?php else: ?><p class="mt-4 rounded-xl bg-slate-50 p-4 text-[9px] text-slate-500">This renewal pipeline is in a final state.</p><?php endif; ?></form>

                <?php if($opportunity->customer_id && $opportunity->status === 'interested'): ?>
                    <form method="POST" action="<?php echo e(route('app.renewals.application',$opportunity->id)); ?>" class="rounded-[22px] border border-emerald-200 bg-emerald-50 p-5"><?php echo csrf_field(); ?><h2 class="text-sm font-bold text-emerald-800">Start Renewal Application</h2><p class="mt-1 text-[9px] text-slate-500">Reuse the linked customer and document history.</p><select name="service_id" required class="erp-input mt-3"><option value="">Select configured service</option><?php $__currentLoopData = $services; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $service): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($service->id); ?>"><?php echo e($service->name_en); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select><button class="mt-3 w-full rounded-xl bg-emerald-600 py-3 text-[10px] font-bold text-white">Create Application</button></form>
                <?php endif; ?>
                <?php if($opportunity->status === 'application_created'): ?><form method="POST" action="<?php echo e(route('app.renewals.complete',$opportunity->id)); ?>"><?php echo csrf_field(); ?><button class="w-full rounded-xl bg-emerald-600 py-3 text-[10px] font-bold text-white">Mark Renewal Completed</button></form><?php endif; ?>
            <?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\renewals\show.blade.php ENDPATH**/ ?>