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

<?php
$avColors = ['#1a73e8','#8b5cf6','#10b981','#f59e0b','#ef4444','#0ea5e9','#f97316','#ec4899'];
$PSTYLE = [
  'New'        => ['bg'=>'#eff6ff','color'=>'#1e40af','border'=>'#bfdbfe','fill'=>'#1e40af'],
  'Contacted'  => ['bg'=>'#f5f3ff','color'=>'#5b21b6','border'=>'#c4b5fd','fill'=>'#7c3aed'],
  'Positive'   => ['bg'=>'#f0fdf4','color'=>'#065f46','border'=>'#a7f3d0','fill'=>'#10b981'],
  'Follow Up'  => ['bg'=>'#fffbeb','color'=>'#92400e','border'=>'#fde68a','fill'=>'#f59e0b'],
  'Negotiation'=> ['bg'=>'#fff7ed','color'=>'#9a3412','border'=>'#fed7aa','fill'=>'#f97316'],
  'Won'        => ['bg'=>'#f0fdf4','color'=>'#14532d','border'=>'#a7f3d0','fill'=>'#059669'],
  'Lost'       => ['bg'=>'#fef2f2','color'=>'#991b1b','border'=>'#fecaca','fill'=>'#ef4444'],
];
$TARGET = 1200000;
$wonRev  = $leads->where('stage','Won')->sum('budget');
$pipeline = $leads->whereNotIn('stage',['Won','Lost'])->sum('budget');
$pct  = $TARGET > 0 ? min(100, round($wonRev/$TARGET*100)) : 0;
$rem  = max(0, $TARGET - $wonRev);
$wr   = $total > 0 ? round($won/$total*100) : 0;

$h = (int) \Carbon\Carbon::now('Asia/Kolkata')->format('H');
$greeting = $h < 12 ? 'Good morning' : ($h < 17 ? 'Good afternoon' : 'Good evening');

function Rs($n){ return $n ? 'Rs'.number_format($n,0,'.',',') : '--'; }
function ini2($name){ $p=explode(' ',$name); return strtoupper(substr($p[0],0,1).(isset($p[1])?substr($p[1],0,1):'')); }

// Sources
$sourceData = [];
foreach($leads as $l){
  $s = $l->source;
  if(!isset($sourceData[$s])) $sourceData[$s] = ['count'=>0,'won'=>0];
  $sourceData[$s]['count']++;
  if($l->stage==='Won') $sourceData[$s]['won']++;
}
arsort($sourceData);
$maxSrc = $sourceData ? max(array_column($sourceData,'count')) : 1;

// Notes activity (recent 6)
$allNotes = \App\Models\LeadNote::with('lead')->orderByDesc('created_at')->take(6)->get();

// Employee perf
$empPerf = $employees->map(function($e){
  $my = $e->leads;
  $tot = $my->count();
  $won = $my->where('stage','Won')->count();
  $act = $my->whereNotIn('stage',['Won','Lost'])->count();
  $wa  = $my->where('stage','Won')->sum('budget');
  $pa  = $my->whereNotIn('stage',['Won','Lost'])->sum('budget');
  $tgt = $e->target ?: 20;
  $tp  = min(round($tot/$tgt*100),100);
  $AMT_TGT = 500000;
  $ap  = min(round($wa/$AMT_TGT*100),100);
  $wp  = $tot>0 ? round($won/$tot*100) : 0;
  return compact('e','tot','won','act','wa','pa','tgt','tp','ap','wp');
})->sortByDesc('wa')->values();

?>

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

<!-- BANNER -->
<div class="dash-banner">
  <div class="dw-text">
    <h3><?php echo e($greeting); ?>, Admin! </h3>
    <p><?php echo e($total); ?> total leads - <?php echo e($active); ?> active - <?php echo e($won); ?> won (<?php echo e($wr); ?>% win rate) - Pipeline <?php echo e('Rs'.number_format($pipeline/100000,1)); ?>L</p>
  </div>
  <div class="dw-acts">
    <a href="<?php echo e(route('leads.create')); ?>" class="dw-btn">
      <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>
    <a href="<?php echo e(route('leads.export')); ?>" class="dw-btn">
      <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
    </a>
  </div>
</div>

<!-- 5 KPI CARDS (exact same as HTML sample) -->
<div class="metrics">
  <!-- Total Leads -->
  <div class="mc" style="border-top:4px solid #1a73e8">
    <div class="mc-ico" style="background:#e8f0fe;color:#1a73e8">
      <svg viewBox="0 0 24 24"><path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><circle cx="9" cy="7" r="4" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/><path d="M23 21v-2a4 4 0 00-3-3.87M16 3.13a4 4 0 010 7.75" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
    </div>
    <div class="mc-val"><?php echo e($total); ?></div>
    <div class="mc-lbl">Total Leads</div>
    <div class="mc-tr tr-ne"><?php echo e(now()->format('F Y')); ?></div>
  </div>
  <!-- Won Deals -->
  <div class="mc" style="border-top:4px solid #10b981">
    <div class="mc-ico" style="background:#d1fae5;color:#10b981">
      <svg viewBox="0 0 24 24"><path d="M20 6 9 17 4 12" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
    </div>
    <div class="mc-val"><?php echo e($won); ?></div>
    <div class="mc-lbl">Won Deals</div>
    <div class="mc-tr tr-up"><?php echo e(Rs($wonRev)); ?></div>
  </div>
  <!-- Active -->
  <div class="mc" style="border-top:4px solid #f59e0b">
    <div class="mc-ico" style="background:#fef3c7;color:#f59e0b">
      <svg viewBox="0 0 24 24"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
    </div>
    <div class="mc-val"><?php echo e($active); ?></div>
    <div class="mc-lbl">Active</div>
    <div class="mc-tr tr-ne"><?php echo e(Rs($pipeline)); ?> pipeline</div>
  </div>
  <!-- Lost -->
  <div class="mc" style="border-top:4px solid #ef4444">
    <div class="mc-ico" style="background:#fee2e2;color:#ef4444">
      <svg viewBox="0 0 24 24"><path d="M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
    </div>
    <div class="mc-val"><?php echo e($lost); ?></div>
    <div class="mc-lbl">Lost</div>
    <div class="mc-tr tr-dn"><?php echo e($total > 0 ? round($lost/$total*100) : 0); ?>% loss</div>
  </div>
  <!-- Win Rate -->
  <div class="mc" style="border-top:4px solid #8b5cf6">
    <div class="mc-ico" style="background:#ede9fe;color:#8b5cf6">
      <svg viewBox="0 0 24 24"><path d="M22 12 18 12 15 21 9 3 6 12 2 12" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
    </div>
    <div class="mc-val"><?php echo e($wr); ?>%</div>
    <div class="mc-lbl">Win Rate</div>
    <div class="mc-tr <?php echo e($wr >= 25 ? 'tr-up' : 'tr-dn'); ?>"><?php echo e($wr >= 25 ? 'On track' : 'Needs work'); ?></div>
  </div>
</div>

<!-- MONTHLY SALES TARGET -->
<div class="target-box">
  <h4>Monthly Sales Target</h4>
  <div class="target-meta">
    <span>Won: <strong style="color:#10b981"><?php echo e(Rs($wonRev)); ?></strong></span>
    <span>Target: <strong>Rs12,00,000</strong></span>
    <span>Remaining: <strong style="color:#f59e0b"><?php echo e(Rs($rem)); ?></strong></span>
    <strong style="color:#10b981"><?php echo e($pct); ?>% achieved</strong>
  </div>
  <div class="t-track"><div class="t-fill" style="width:<?php echo e($pct); ?>%"></div></div>
  <div class="t-labels"><span>Rs0</span><span><?php echo e(Rs($wonRev)); ?> won</span><span>Rs12,00,000</span></div>
</div>

<!-- PIPELINE STAGES -->
<div class="pipe-box">
  <h4>Pipeline Stages</h4>
  <div class="pipe-stages">
    <?php $__currentLoopData = ['New','Contacted','Positive','Follow Up','Negotiation','Won','Lost']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $stage): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <?php
      $cnt = $leads->where('stage',$stage)->count();
      $val = $leads->where('stage',$stage)->sum('budget');
      $ps  = $PSTYLE[$stage];
      $barW = $total > 0 ? round($cnt/$total*100) : 0;
    ?>
    <div class="pipe-st" style="background:<?php echo e($ps['bg']); ?>;border-color:<?php echo e($ps['border']); ?>">
      <div class="pn" style="color:<?php echo e($ps['color']); ?>"><?php echo e($cnt); ?></div>
      <div class="pl" style="color:<?php echo e($ps['color']); ?>"><?php echo e($stage); ?></div>
      <div class="pa" style="color:<?php echo e($ps['color']); ?>"><?php echo e($val ? Rs($val) : '--'); ?></div>
      <div class="pb" style="background:<?php echo e($ps['fill']); ?>;width:<?php echo e($barW); ?>%"></div>
    </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
  </div>
</div>

<!-- RECENT LEADS + LEAD SOURCES -->
<div class="dash-bottom">
  <!-- Recent Leads -->
  <div class="dash-card">
    <div class="dc-hd">
      <h4>Recent Leads</h4>
      <a href="<?php echo e(route('leads.index')); ?>" class="btn btn-out btn-sm">View All</a>
    </div>
    <div class="dc-body">
      <?php $__currentLoopData = $leads->sortByDesc('updated_at')->take(5); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $l): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
      <?php $col = $avColors[$l->id % count($avColors)]; ?>
      <div class="rl-item">
        <div class="rl-av" style="background:<?php echo e($col); ?>22;color:<?php echo e($col); ?>"><?php echo e(ini2($l->name)); ?></div>
        <div style="flex:1;min-width:0">
          <div class="rl-name"><?php echo e($l->name); ?></div>
          <div class="rl-co"><?php echo e($l->company ?? '--'); ?></div>
        </div>
        <div style="text-align:right">
          <div class="rl-bud"><?php echo e(Rs($l->budget)); ?></div>
          <div style="margin-top:3px"><span class="badge <?php echo e(\App\Models\Lead::stageBadge($l->stage)); ?>" style="font-size:9.5px"><?php echo e($l->stage); ?></span></div>
        </div>
      </div>
      <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>
  </div>
  <!-- Lead Sources -->
  <div class="dash-card">
    <div class="dc-hd"><h4>Lead Sources</h4></div>
    <div class="dc-body">
      <?php $__currentLoopData = $sourceData; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $src => $data): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
      <?php
        $wp = $data['count'] > 0 ? round($data['won']/$data['count']*100) : 0;
      ?>
      <div class="src-row">
        <span class="src-lbl"><?php echo e($src); ?></span>
        <div class="src-track">
          <div class="src-fill" style="width:<?php echo e(round($data['count']/$maxSrc*100)); ?>%">
            <div class="src-won" style="width:<?php echo e($wp); ?>%"></div>
          </div>
        </div>
        <span class="src-val"><?php echo e($data['count']); ?> <span style="font-size:10px;color:#10b981"><?php echo e($wp); ?>%</span></span>
      </div>
      <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
    </div>
  </div>
</div>

<!-- RECENT ACTIVITY -->
<div class="dash-card" style="margin-bottom:16px">
  <div class="dc-hd"><h4>Recent Activity</h4></div>
  <div class="dc-body">
    <?php $__empty_1 = true; $__currentLoopData = $allNotes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $note): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
    <div class="act-item">
      <div class="act-dot" style="background:<?php echo e($note->avatar_color); ?>"></div>
      <div>
        <div class="act-txt"><strong><?php echo e($note->added_by); ?></strong> - <?php echo e($note->via); ?> with <strong><?php echo e(optional($note->lead)->name); ?></strong></div>
        <div class="act-time"><?php echo e($note->created_at->format('d M Y, g:i A')); ?></div>
      </div>
    </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
    <div style="color:#9aa5b4;font-size:13px;padding:10px 0">No recent activity.</div>
    <?php endif; ?>
  </div>
</div>

<!-- EMPLOYEE PERFORMANCE (same card as HTML sample) -->
<div class="dash-card" style="margin-bottom:0">
  <div class="dc-hd" style="display:flex;align-items:center;justify-content:space-between">
    <h4 style="margin:0;display:flex;align-items:center;gap:7px">🏆 Employee Performance &amp; Target Achievement</h4>
    <a href="<?php echo e(route('employees.index')); ?>" class="btn btn-out btn-sm">View All →</a>
  </div>
  <?php
  $AMT_TGT = 500000;
  $rank = ['🥇','🥈','🥉'];
  ?>
  <!-- Summary row -->
  <div class="emp-sum-grid">
    <?php
    $tL = $empPerf->sum('tot'); $tW = $empPerf->sum('won'); $tA = $empPerf->sum('wa'); $tP = $empPerf->sum('pa');
    $eBoxes = [['📋','Total Leads',$tL,'#1a73e8','#f0f5ff'],['🏆','Deals Won',$tW,'#059669','#f0fdf4'],['💰','Revenue Won',Rs($tA),'#7c3aed','#f5f3ff'],['🔥','In Pipeline',Rs($tP),'#d97706','#fffbeb']];
    ?>
    <?php $__currentLoopData = $eBoxes; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$ico,$lbl,$val,$color,$bg]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <div style="background:<?php echo e($bg); ?>;border-radius:0px;padding:12px 14px;text-align:center">
      <div style="font-size:20px;margin-bottom:4px"><?php echo e($ico); ?></div>
      <div style="font-size:18px;font-weight:800;color:<?php echo e($color); ?>"><?php echo e($val); ?></div>
      <div style="font-size:10.5px;color:#6b7280;font-weight:500"><?php echo e($lbl); ?></div>
    </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
  </div>
  <!-- Achievement cards grid -->
  <div class="emp-ach-grid">
    <?php $__currentLoopData = $empPerf; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $r): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
    <?php
    $e = $r['e'];
    $ini = ini2($e->name);
    if($r['tp']>=100){$ltI='🏆';$ltC='#065f46';$ltB='#d1fae5';$ltBar='#059669';$ltL='Target Hit!';}
    elseif($r['tp']>=75){$ltI='🔥';$ltC='#92400e';$ltB='#fef3c7';$ltBar='#d97706';$ltL='Almost There';}
    elseif($r['tp']>=50){$ltI='📈';$ltC='#1e40af';$ltB='#dbeafe';$ltBar='#1a73e8';$ltL='On Track';}
    elseif($r['tp']>=25){$ltI='⏳';$ltC='#7c2d12';$ltB='#ffedd5';$ltBar='#ea580c';$ltL='Needs Push';}
    else{$ltI='🐢';$ltC='#991b1b';$ltB='#fee2e2';$ltBar='#dc2626';$ltL='Lagging';}
    $aC=$r['ap']>=75?'#059669':($r['ap']>=50?'#1a73e8':($r['ap']>=25?'#d97706':'#dc2626'));
    $wC=$r['wp']>=60?'#059669':($r['wp']>=30?'#d97706':'#dc2626');
    ?>
    <div class="emp-ach-card">
      <?php if(isset($rank[$i])): ?><div style="position:absolute;top:10px;right:10px;font-size:18px"><?php echo e($rank[$i]); ?></div><?php endif; ?>
      <div style="display:flex;align-items:center;gap:9px;margin-bottom:11px">
        <div style="width:40px;height:40px;border-radius:50%;background:<?php echo e($e->avatar_color); ?>;display:flex;align-items:center;justify-content:center;font-size:13px;font-weight:700;color:#fff;flex-shrink:0"><?php echo e($ini); ?></div>
        <div>
          <div style="font-size:13.5px;font-weight:700;color:#1a2340"><?php echo e($e->name); ?></div>
          <div style="font-size:10.5px;color:#9aabb8"><?php echo e($e->role); ?></div>
        </div>
      </div>
      <div class="emp-chip-grid">
        <?php $__currentLoopData = [[$r['tot'],'Leads','#f0f5ff','#1a73e8'],[$r['won'],'Won','#f0fdf4','#059669'],[$r['act'],'Active','#fffbeb','#d97706']]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as [$cv,$cl,$cb,$cc]): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <div style="background:<?php echo e($cb); ?>;border-radius:8px;padding:7px 5px;text-align:center">
          <div style="font-size:16px;font-weight:800;color:<?php echo e($cc); ?>"><?php echo e($cv); ?></div>
          <div style="font-size:9.5px;color:#6b7280"><?php echo e($cl); ?></div>
        </div>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
      </div>
      <!-- Lead target bar -->
      <div style="margin-bottom:9px">
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:4px">
          <span style="font-size:10.5px;font-weight:600;color:#6b7280"><?php echo e($ltI); ?> Lead Target</span>
          <span style="font-size:10.5px;font-weight:700;color:<?php echo e($ltC); ?>"><?php echo e($r['tot']); ?>/<?php echo e($r['tgt']); ?> (<?php echo e($r['tp']); ?>%)</span>
        </div>
        <div style="height:7px;background:#f0f4fa;border-radius:4px;overflow:hidden"><div style="height:100%;width:<?php echo e($r['tp']); ?>%;background:<?php echo e($ltBar); ?>;border-radius:4px"></div></div>
        <div style="margin-top:4px"><span style="font-size:10px;font-weight:700;padding:2px 8px;border-radius:20px;background:<?php echo e($ltB); ?>;color:<?php echo e($ltC); ?>"><?php echo e($ltI); ?> <?php echo e($ltL); ?></span></div>
      </div>
      <!-- Amount won bar -->
      <div style="margin-bottom:9px">
        <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:4px">
          <span style="font-size:10.5px;font-weight:600;color:#6b7280">💰 Amount Won</span>
          <span style="font-size:10.5px;font-weight:700;color:#059669"><?php echo e(Rs($r['wa'])); ?></span>
        </div>
        <div style="height:7px;background:#f0f4fa;border-radius:4px;overflow:hidden"><div style="height:100%;width:<?php echo e($r['ap']); ?>%;background:<?php echo e($aC); ?>;border-radius:4px"></div></div>
        <div style="display:flex;justify-content:space-between;margin-top:3px">
          <span style="font-size:10px;color:#9aabb8">Pipeline: <b style="color:#d97706"><?php echo e(Rs($r['pa'])); ?></b></span>
          <span style="font-size:10px;color:#9aabb8">of Rs5L target</span>
        </div>
      </div>
      <!-- Win rate -->
      <div style="display:flex;justify-content:space-between;padding-top:9px;border-top:1px solid #f0f4fa">
        <span style="font-size:11px;color:#9aabb8">Win Rate</span>
        <span style="font-size:14px;font-weight:800;color:<?php echo e($wC); ?>"><?php echo e($r['wp']); ?>%</span>
      </div>
    </div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
  </div>
</div>

<?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/dashboard/index.blade.php ENDPATH**/ ?>