
<?php
  $AMT_TGT = 500000; // Rs5L pipeline target per employee, same as prototype

  $rows = $employees->where('status', 'Active')->map(function ($e) use ($AMT_TGT) {
      $myLeads = $e->leads;
      $tot = $myLeads->count();
      $won = $myLeads->where('stage', 'Won')->count();
      $act = $myLeads->whereNotIn('stage', ['Won', 'Lost'])->count();
      $wa  = $myLeads->where('stage', 'Won')->sum('budget');
      $pa  = $myLeads->whereNotIn('stage', ['Won', 'Lost'])->sum('budget');
      $tgt = $e->target ?: 20;
      $tp  = $tgt > 0 ? min(round($tot / $tgt * 100), 100) : 0;
      $ap  = min(round($wa / $AMT_TGT * 100), 100);
      $wp  = $tot > 0 ? round($won / $tot * 100) : 0;

      return (object) [
          'e' => $e, 'tot' => $tot, 'won' => $won, 'act' => $act,
          'wa' => $wa, 'pa' => $pa, 'tgt' => $tgt, 'tp' => $tp, 'ap' => $ap, 'wp' => $wp,
      ];
  })->sortByDesc(fn($r) => [$r->wa, $r->tot])->values();

  $tL = $rows->sum('tot');
  $tW = $rows->sum('won');
  $tA = $rows->sum('wa');
  $tP = $rows->sum('pa');

  $rs = fn($n) => $n ? 'Rs' . number_format($n) : '--';
  $rank = ['🥇', '🥈', '🥉'];
?>

<div class="dash-card" style="margin-bottom:18px">
  <div class="dc-hd">
    <h4>🏅 Employee Performance &amp; Target Achievement</h4>
    <a href="<?php echo e(route('employees.index')); ?>" class="btn btn-out btn-sm" style="padding:5px 10px;font-size:11px">View All →</a>
  </div>

  <?php if($rows->isEmpty()): ?>
    <div style="padding:20px;text-align:center;color:#9aabb8">No active employees found.</div>
  <?php else: ?>
    
    <div class="emp-sum-grid">
      <div class="emp-sum-box" style="background:#f0f5ff">
        <div class="emp-sum-icon">📋</div>
        <div class="emp-sum-val" style="color:#1a73e8"><?php echo e($tL); ?></div>
        <div class="emp-sum-lbl">Total Leads</div>
      </div>
      <div class="emp-sum-box" style="background:#f0fdf4">
        <div class="emp-sum-icon">🏆</div>
        <div class="emp-sum-val" style="color:#059669"><?php echo e($tW); ?></div>
        <div class="emp-sum-lbl">Deals Won</div>
      </div>
      <div class="emp-sum-box" style="background:#f5f3ff">
        <div class="emp-sum-icon">💰</div>
        <div class="emp-sum-val" style="color:#7c3aed"><?php echo e($rs($tA)); ?></div>
        <div class="emp-sum-lbl">Revenue Won</div>
      </div>
      <div class="emp-sum-box" style="background:#fffbeb">
        <div class="emp-sum-icon">🔥</div>
        <div class="emp-sum-val" style="color:#d97706"><?php echo e($rs($tP)); ?></div>
        <div class="emp-sum-lbl">In Pipeline</div>
      </div>
    </div>

    
    <div class="emp-ach-grid">
      <?php $__currentLoopData = $rows; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $i => $r): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <?php
          $e = $r->e;
          $parts = explode(' ', trim($e->name));
          $ini = strtoupper(substr($parts[0],0,1) . (isset($parts[1]) ? substr($parts[1],0,1) : ''));

          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">
            <div class="emp-chip" style="background:#f0f5ff">
              <div class="emp-chip-v" style="color:#1a73e8"><?php echo e($r->tot); ?></div>
              <div class="emp-chip-l">Leads</div>
            </div>
            <div class="emp-chip" style="background:#f0fdf4">
              <div class="emp-chip-v" style="color:#059669"><?php echo e($r->won); ?></div>
              <div class="emp-chip-l">Won</div>
            </div>
            <div class="emp-chip" style="background:#fffbeb">
              <div class="emp-chip-v" style="color:#d97706"><?php echo e($r->act); ?></div>
              <div class="emp-chip-l">Active</div>
            </div>
          </div>

          
          <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:20px;overflow:hidden">
              <div style="height:100%;width:<?php echo e($r->tp); ?>%;background:<?php echo e($ltBar); ?>;border-radius:20px;transition:.5s ease"></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>

          
          <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:20px;overflow:hidden">
              <div style="height:100%;width:<?php echo e($r->ap); ?>%;background:<?php echo e($aC); ?>;border-radius:20px;transition:.5s ease"></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>

          
          <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>
  <?php endif; ?>
</div><?php /**PATH C:\xampp\htdocs\leads-worksheet\resources\views/partials/employee-achievement.blade.php ENDPATH**/ ?>