<?php
require_once __DIR__ . "/../../config/config.php";
require_once __DIR__ . "/../../includes/functions.php";
startSession(); requireLogin();
$db = Database::getInstance();
$page = "Gratuity";

$setup = $db->fetchOne("SELECT * FROM gratuity_setup");
// FIX (07 Jul 2026): scope to current org — this listed salary/service
// data for every organization's employees with no filter at all.
[$orgClause, $orgParams] = orgScopeClause($db, 'e.company_id');
$emps  = $db->fetchAll("SELECT e.*,es.gross_monthly,TIMESTAMPDIFF(YEAR,e.date_of_joining,NOW()) AS service_years,(SELECT basic FROM payroll_details pd JOIN payroll_runs pr ON pr.id=pd.payroll_run_id WHERE pd.employee_id=e.id ORDER BY pr.year DESC,pr.month DESC LIMIT 1) AS last_basic FROM employees e LEFT JOIN employee_salary es ON es.employee_id=e.id WHERE e.status='active' AND $orgClause ORDER BY service_years DESC", $orgParams);

include __DIR__ . "/../../includes/header.php";
?>
<div class="row g-3 mb-3">
  <div class="col-md-3"><div class="pc-stat"><div class="pc-stat-icon amber" style="width:42px;height:42px;font-size:20px"><i class="ti ti-award"></i></div><div class="pc-stat-val"><?=$setup["min_service_years"]??5?> Yrs</div><div class="pc-stat-label">Min Service</div></div></div>
  <div class="col-md-3"><div class="pc-stat"><div class="pc-stat-icon green" style="width:42px;height:42px;font-size:20px"><i class="ti ti-users"></i></div><div class="pc-stat-val"><?=count(array_filter($emps,fn($e)=>$e["service_years"]>=$setup["min_service_years"]))?></div><div class="pc-stat-label">Eligible Employees</div></div></div>
  <div class="col-md-3"><div class="pc-stat"><div class="pc-stat-icon brand" style="width:42px;height:42px;font-size:20px"><i class="ti ti-calculator"></i></div><div class="pc-stat-val">15/26</div><div class="pc-stat-label">Formula</div></div></div>
  <div class="col-md-3"><div class="pc-stat"><div class="pc-stat-icon purple" style="width:42px;height:42px;font-size:20px"><i class="ti ti-cash"></i></div><div class="pc-stat-val">₹<?=number_format(($setup["max_gratuity_amount"]??2000000)/100000,0)?>L</div><div class="pc-stat-label">Max Gratuity</div></div></div>
</div>
<div class="pc-card">
  <div class="pc-card-title"><i class="ti ti-table me-2"></i>Gratuity Calculation — All Active Employees</div>
  <div class="pc-table-wrap">
    <table class="pc-table">
      <thead><tr><th>Employee</th><th>Joining Date</th><th>Service</th><th>Last Basic (₹)</th><th>Gratuity (₹)</th><th>Eligible</th></tr></thead>
      <tbody>
      <?php foreach($emps as $e):
        $yrs = $e["service_years"] ?? 0;
        $basic = $e["last_basic"] ?? ($e["gross_monthly"] ? $e["gross_monthly"]*0.4 : 0);
        $gratuity = round($basic / 26 * 15 * $yrs, 2);
        $gratuity = min($gratuity, $setup["max_gratuity_amount"]??2000000);
        $eligible = $yrs >= ($setup["min_service_years"]??5);
      ?>
      <tr>
        <td><div class="fw-semibold"><?=clean($e["first_name"]." ".$e["last_name"])?></div><small class="text-muted"><?=clean($e["employee_id"])?></small></td>
        <td><?=fmtDate($e["date_of_joining"])?></td>
        <td><span class="badge" style="background:<?=$yrs>=5?"var(--pc-green-bg);color:var(--pc-green)":"var(--pc-amber-bg);color:var(--pc-amber)"?>"><?=$yrs?> yr<?=$yrs!=1?"s":""?></span></td>
        <td>₹<?=number_format($basic,2)?></td>
        <td class="fw-bold <?=$eligible?"":"text-muted"?>">₹<?=number_format($gratuity,2)?></td>
        <td><?=$eligible?'<i class="ti ti-circle-check text-success"></i> Yes':'<i class="ti ti-circle-x text-danger"></i> No'?></td>
      </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
  </div>
  <div class="mt-2 small text-muted">Formula: (Last Basic / 26) × 15 × Years of Service &nbsp;|&nbsp; Max: ₹<?=number_format($setup["max_gratuity_amount"]??2000000)?></div>
</div>
<?php include __DIR__ . "/../../includes/footer.php"; ?>