
<?php $__env->startSection('title','All Leads'); ?>
<?php $__env->startSection('page-title','All Leads'); ?>

<?php
$avColors = ['#1a73e8','#8b5cf6','#10b981','#f59e0b','#ef4444','#0ea5e9','#f97316','#ec4899'];
$allStages = ['New','Contacted','Positive','Follow Up','Negotiation','Won','Lost'];
$allSources = ['Website','WhatsApp','Phone Call','Referral','Email','Walk-in','LinkedIn'];
?>

<?php $__env->startSection('content'); ?>


<?php echo $__env->make('partials.employee-achievement', ['employees' => $employees], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<div class="page-hd">
  <div><h2>All Leads</h2><p><?php echo e($leads->count()); ?> lead<?php echo e($leads->count()!==1?'s':''); ?></p></div>
  <div class="page-hd-right">
    <a href="<?php echo e(route('leads.export')); ?>" class="btn btn-out btn-sm">
      <svg viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>Export CSV
    </a>
    <a href="<?php echo e(route('leads.create')); ?>" class="btn btn-pri">
      <svg viewBox="0 0 24 24"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>Add Lead
    </a>
  </div>
</div>
<!-- Filter Bar -->
<form method="GET" action="<?php echo e(route('leads.index')); ?>" id="filter-form">
<div class="filter-bar">
  <div class="f-srch">
    <svg viewBox="0 0 24 24"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg>
    <input type="text" name="q" value="<?php echo e($filters['q'] ?? ''); ?>" placeholder="Search by name, company, phone..." oninput="debounce(()=>document.getElementById('filter-form').submit(),400)">
  </div>
  <select name="stage" onchange="this.form.submit()">
    <option value="">All Stages</option>
    <?php $__currentLoopData = $stages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($s); ?>" <?php echo e(($filters['stage']??'')===$s?'selected':''); ?>><?php echo e($s); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
  </select>
  <select name="source" onchange="this.form.submit()">
    <option value="">All Sources</option>
    <?php $__currentLoopData = $sources; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($s); ?>" <?php echo e(($filters['source']??'')===$s?'selected':''); ?>><?php echo e($s); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
  </select>
  <select name="assigned" onchange="this.form.submit()">
    <option value="">All Assignees</option>
    <?php $__currentLoopData = $employees; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $e): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($e->id); ?>" <?php echo e(($filters['assigned']??'')==$e->id?'selected':''); ?>><?php echo e($e->name); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
  </select>
  <select name="priority" onchange="this.form.submit()">
    <option value="">All Priority</option>
    <?php $__currentLoopData = ['High','Medium','Low']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $p): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><option value="<?php echo e($p); ?>" <?php echo e(($filters['priority']??'')===$p?'selected':''); ?>><?php echo e($p); ?></option><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
  </select>
  <a href="<?php echo e(route('leads.index')); ?>" class="btn btn-out btn-sm">Reset</a>
</div>
</form>

<!-- Table -->
<div class="tbl-wrap">
  <div class="tbl-hd">
    <h3><?php echo e($leads->count()); ?> Lead<?php echo e($leads->count()!==1?'s':''); ?></h3>
  </div>
  <div style="overflow-x:auto">
    <table>
      <thead>
        <tr>
          <th>#</th><th>Lead</th><th>Phone</th><th>Source</th><th>Service</th>
          <th>Budget</th><th>Status</th><th>Assign To</th><th>Priority</th><th>Last Contact</th><th>Actions</th>
        </tr>
      </thead>
      <tbody>
        <?php $__empty_1 = true; $__currentLoopData = $leads; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $l): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
        <?php
          $col = $avColors[$l->id % count($avColors)];
          $stageCls = \App\Models\Lead::stageSelectClass($l->stage);
          $srcBadge = \App\Models\Lead::sourceBadge($l->source);
          $priColor = \App\Models\Lead::priorityColor($l->priority);
        ?>
        <tr id="row-<?php echo e($l->id); ?>">
          <td style="color:#c3c9d8;font-size:12px;font-weight:600"><?php echo e($l->id); ?></td>
          <td>
            <div style="display:flex;align-items:center;gap:9px">
              <div class="lead-av" style="background:<?php echo e($col); ?>22;color:<?php echo e($col); ?>"><?php echo e($l->initials); ?></div>
              <div>
                <div style="font-weight:700;font-size:13px"><?php echo e($l->name); ?></div>
                <div style="font-size:11px;color:#9aa5b4"><?php echo e($l->company ?? '--'); ?></div>
              </div>
            </div>
          </td>
          <td style="font-size:12.5px"><?php echo e($l->phone); ?></td>
          <td><span class="badge <?php echo e($srcBadge); ?>"><?php echo e($l->source); ?></span></td>
          <td style="font-size:12px;max-width:110px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"><?php echo e($l->service ?? '--'); ?></td>
          <td style="font-weight:700"><?php echo e($l->budget_formatted); ?></td>
          <td>
            <select class="ssel <?php echo e($stageCls); ?>" onchange="quickStage(<?php echo e($l->id); ?>,this)"
              data-url="<?php echo e(route('leads.stage', $l)); ?>">
              <?php $__currentLoopData = $allStages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <option <?php echo e($l->stage===$s?'selected':''); ?>><?php echo e($s); ?></option>
              <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
          </td>
          <td>
            <select class="asel" onchange="quickAssign(<?php echo e($l->id); ?>,this)"
              data-url="<?php echo e(route('leads.assign', $l)); ?>">
              <?php $__currentLoopData = $employees; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $e): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <option value="<?php echo e($e->id); ?>" <?php echo e($l->assigned_to===$e->id?'selected':''); ?>><?php echo e($e->name); ?></option>
              <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
          </td>
          <td>
            <div style="display:flex;align-items:center;gap:5px">
              <div style="width:8px;height:8px;border-radius:50%;background:<?php echo e($priColor); ?>;flex-shrink:0"></div>
              <span style="font-size:12px"><?php echo e($l->priority); ?></span>
            </div>
          </td>
          <td style="font-size:11.5px;color:#687182;white-space:nowrap">
            <?php echo e(optional($l->last_contact)->format('d M Y') ?? '--'); ?>

          </td>
          <td>
            <div class="act-btns" style="display:flex;gap:4px">
              <!-- <a href="<?php echo e(route('leads.show', $l)); ?>" class="ab ab-v">
                <svg viewBox="0 0 24 24"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>View
              </a> -->
              <a href="<?php echo e(route('leads.show', $l)); ?>" class="ab ab-v" onclick="return openDrawer(event, '<?php echo e(route('leads.show', $l)); ?>')">
                <svg viewBox="0 0 24 24"><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z"/><circle cx="12" cy="12" r="3"/></svg>View
              </a>
              <a href="<?php echo e(route('leads.edit', $l)); ?>" class="ab ab-e">
                <svg viewBox="0 0 24 24"><path d="M11 4H4a2 2 0 00-2 2v14a2 2 0 002 2h14a2 2 0 002-2v-7"/><path d="M18.5 2.5a2.121 2.121 0 013 3L12 15l-4 1 1-4 9.5-9.5z"/></svg>Edit
              </a>
              <form method="POST" action="<?php echo e(route('leads.destroy', $l)); ?>" onsubmit="return confirm('Delete this lead? This cannot be undone.')">
                <?php echo csrf_field(); ?> <?php echo method_field('DELETE'); ?>
                <button type="submit" class="ab ab-d">
                  <svg viewBox="0 0 24 24"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a1 1 0 011-1h4a1 1 0 011 1v2"/></svg>Delete
                </button>
              </form>
            </div>
          </td>
        </tr>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
        <tr>
          <td colspan="11">
            <div class="empty">
              <svg viewBox="0 0 24 24"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/><circle cx="9" cy="7" r="4"/></svg>
              <h4>No leads found</h4><p>Try changing filters or add a new lead</p>
            </div>
          </td>
        </tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<div class="overlay" id="lead-overlay" onclick="closeDrawer()"></div>
<div class="drawer" id="lead-drawer">
  <div id="drawer-body"></div>
</div>

<?php $__env->startPush('scripts'); ?>
<script>
var STCLS = {New:'s-new',Contacted:'s-contacted',Positive:'s-positive','Follow Up':'s-followup',Negotiation:'s-negotiation',Won:'s-won',Lost:'s-lost'};
function quickStage(id,sel){
  var url = sel.getAttribute('data-url');
  ajaxPost(url,{stage:sel.value},function(r){
    sel.className='ssel '+(STCLS[sel.value]||'s-new');
  });
}
function quickAssign(id,sel){
  var url = sel.getAttribute('data-url');
  ajaxPost(url,{assigned_to:sel.value},function(r){});
}
var _dt; function debounce(fn,ms){clearTimeout(_dt);_dt=setTimeout(fn,ms);}

function openDrawer(e, url){
  e.preventDefault();
  document.getElementById('drawer-body').innerHTML = '<div style="padding:40px;text-align:center;color:#9aa5b4">Loading...</div>';
  document.getElementById('lead-overlay').classList.add('show');
  document.getElementById('lead-drawer').classList.add('open');
  fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
    .then(r => r.text())
    .then(html => { document.getElementById('drawer-body').innerHTML = html; })
    .catch(() => {
      document.getElementById('drawer-body').innerHTML = '<div style="padding:40px;text-align:center;color:#ef4444">Failed to load lead.</div>';
    });
  return false;
}
function closeDrawer(){
  document.getElementById('lead-overlay').classList.remove('show');
  document.getElementById('lead-drawer').classList.remove('open');
}
function drawerTab(btn, tabId){
  document.querySelectorAll('.dtab').forEach(t => t.classList.remove('active'));
  btn.classList.add('active');
  ['tab-overview','tab-notes','tab-timeline'].forEach(id => {
    var el = document.getElementById(id);
    if (el) el.style.display = (id === tabId) ? 'block' : 'none';
  });
}
</script>
<?php $__env->stopPush(); ?>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp\htdocs\leads-worksheet\resources\views/leads/index.blade.php ENDPATH**/ ?>