<?php $__env->startSection('title', 'CRM Pipeline'); ?>

<?php $__env->startSection('content'); ?>
<?php ($stages = ['new','contacted','qualified','documents_requested','quotation_sent','follow_up','negotiation','won','lost','application_created']); ?>
<div class="space-y-5" x-data="{ createOpen: 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">Sales & relationship management</p><h1 class="mt-1 text-2xl font-bold text-[#0b1f38]">CRM Pipeline</h1><p class="mt-1 text-xs text-slate-500">Corporate prospects, walk-in opportunities and structured follow-ups.</p></div>
        <?php if(auth()->user()->hasPermission('crm.manage')): ?><button @click="createOpen = !createOpen" class="rounded-xl bg-orange-600 px-4 py-2.5 text-xs font-bold text-white shadow-lg shadow-orange-200">+ New Lead</button><?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">L</span><p class="erp-kpi-label">Open Leads</p><p class="erp-kpi-value"><?php echo e($stageCounts->except(['won','lost'])->sum()); ?></p><p class="erp-kpi-note">Across the active pipeline</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-amber-50 text-amber-600">F</span><p class="erp-kpi-label">Follow-ups Due</p><p class="erp-kpi-value"><?php echo e($followUpsDue); ?></p><p class="erp-kpi-note">Requires action now</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-violet-50 text-violet-600">AED</span><p class="erp-kpi-label">Pipeline Value</p><p class="erp-kpi-value text-xl">AED <?php echo e(number_format((float) $pipelineValue, 2)); ?></p><p class="erp-kpi-note">Estimated open opportunity value</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-emerald-50 text-emerald-600">✓</span><p class="erp-kpi-label">Won</p><p class="erp-kpi-value"><?php echo e($stageCounts['won'] ?? 0); ?></p><p class="erp-kpi-note">Converted relationships</p></div>
    </div>

    <?php if(auth()->user()->hasPermission('crm.manage')): ?>
    <section x-show="createOpen" x-transition class="erp-panel">
        <div class="erp-panel-heading"><div><h2>Create Lead</h2><p>Capture the opportunity and assign the next follow-up.</p></div><button @click="createOpen = false" class="text-xl text-slate-400">×</button></div>
        <form method="POST" action="<?php echo e(route('app.crm.leads.store')); ?>" class="mt-5 grid gap-3 md:grid-cols-3 xl:grid-cols-6"><?php echo csrf_field(); ?>
            <input name="contact_name" value="<?php echo e(old('contact_name')); ?>" required placeholder="Contact name *" class="erp-input">
            <input name="company_name" value="<?php echo e(old('company_name')); ?>" placeholder="Company name" class="erp-input">
            <input name="mobile" value="<?php echo e(old('mobile')); ?>" required placeholder="Mobile number *" class="erp-input">
            <input name="email" value="<?php echo e(old('email')); ?>" type="email" placeholder="Email" class="erp-input">
            <select name="source" class="erp-input"><option value="walk_in">Walk-in</option><option value="whatsapp">WhatsApp</option><option value="website">Website</option><option value="google">Google</option><option value="referral">Referral</option><option value="corporate_sales">Corporate sales</option></select>
            <input name="estimated_value" type="number" min="0" step="0.01" placeholder="Est. value AED" class="erp-input">
            <select name="assigned_user_id" class="erp-input"><option value="">Assign later</option><?php $__currentLoopData = $users; $__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="next_followup_at" type="datetime-local" class="erp-input">
            <input name="notes" placeholder="Opportunity notes" class="erp-input md:col-span-2 xl:col-span-3">
            <button class="rounded-xl bg-[#0b1f38] px-4 py-3 text-xs font-bold text-white">Save Lead</button>
        </form>
    </section>
    <?php endif; ?>

    <section class="erp-panel">
        <div class="flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between">
            <div class="flex flex-wrap gap-1.5"><a href="<?php echo e(route('app.crm')); ?>" class="erp-filter-chip <?php echo e(!request('stage') ? 'is-active' : ''); ?>">All <?php echo e($stageCounts->sum()); ?></a><?php $__currentLoopData = $stages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $stage): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><a href="<?php echo e(route('app.crm', ['stage' => $stage])); ?>" class="erp-filter-chip <?php echo e(request('stage') === $stage ? 'is-active' : ''); ?>"><?php echo e(str($stage)->replace('_',' ')->title()); ?> <?php echo e($stageCounts[$stage] ?? 0); ?></a><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></div>
            <form class="flex gap-2"><input name="q" value="<?php echo e(request('q')); ?>" placeholder="Search lead, contact or mobile" class="erp-input min-w-[230px]"><button class="rounded-xl border border-slate-200 px-4 text-xs font-bold text-slate-600">Search</button></form>
        </div>

        <div class="mt-5 overflow-x-auto">
            <table class="erp-table"><thead><tr><th>Lead</th><th>Contact</th><th>Source</th><th>Value</th><th>Owner / Follow-up</th><th>Stage</th><th>Activity</th></tr></thead>
                <tbody><?php $__empty_1 = true; $__currentLoopData = $leads; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $lead): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><tr>
                    <td><strong><?php echo e($lead->lead_number); ?></strong><small><?php echo e($lead->company_name ?: 'Individual prospect'); ?></small></td>
                    <td><strong><?php echo e($lead->contact_name); ?></strong><small><?php echo e($lead->mobile); ?><?php echo e($lead->email ? ' · '.$lead->email : ''); ?></small></td>
                    <td><span class="erp-status"><?php echo e(str($lead->source)->replace('_',' ')->title()); ?></span></td>
                    <td class="font-bold text-[#0b1f38]">AED <?php echo e(number_format((float) $lead->estimated_value, 2)); ?></td>
                    <td><strong><?php echo e($lead->assignedUser?->name ?? 'Unassigned'); ?></strong><small class="<?php echo e($lead->next_followup_at?->isPast() ? 'text-rose-600' : ''); ?>"><?php echo e($lead->next_followup_at?->format('d M, h:i A') ?? 'No follow-up set'); ?></small></td>
                    <td><?php if(auth()->user()->hasPermission('crm.manage')): ?><form method="POST" action="<?php echo e(route('app.crm.leads.stage', $lead->id)); ?>"><?php echo csrf_field(); ?><select name="stage" onchange="this.form.submit()" class="erp-select-compact"><?php $__currentLoopData = $stages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $stage): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($stage); ?>" <?php echo e($lead->stage === $stage ? 'selected' : ''); ?>><?php echo e(str($stage)->replace('_',' ')->title()); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?></select></form><?php else: ?><span class="erp-status" data-status="<?php echo e($lead->stage); ?>"><?php echo e(str($lead->stage)->replace('_',' ')->title()); ?></span><?php endif; ?></td>
                    <td><?php if(auth()->user()->hasPermission('crm.manage')): ?><details class="relative"><summary class="cursor-pointer text-[10px] font-bold text-orange-600">+ Follow-up</summary><form method="POST" action="<?php echo e(route('app.crm.leads.activities', $lead->id)); ?>" class="absolute right-0 z-20 mt-2 w-72 space-y-2 rounded-xl border border-slate-200 bg-white p-3 shadow-xl"><?php echo csrf_field(); ?><select name="activity_type" class="erp-input"><option value="call">Call</option><option value="whatsapp">WhatsApp</option><option value="email">Email</option><option value="meeting">Meeting</option><option value="note">Note</option></select><textarea name="notes" required rows="2" placeholder="Outcome / notes" class="erp-input"></textarea><input type="datetime-local" name="scheduled_at" class="erp-input"><button class="w-full rounded-lg bg-orange-600 py-2 text-[10px] font-bold text-white">Record Activity</button></form></details><?php else: ?><span class="text-[10px] text-slate-400"><?php echo e($lead->activities->count()); ?> logged</span><?php endif; ?></td>
                </tr><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><tr><td colspan="7"><div class="erp-empty"><span>L</span><strong>No leads found</strong><p>Add a lead to begin building the corporate and retail pipeline.</p></div></td></tr><?php endif; ?></tbody>
            </table>
        </div>
        <div class="mt-4"><?php echo e($leads->links()); ?></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/crm.blade.php ENDPATH**/ ?>