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

$setup     = $db->fetchOne("SELECT * FROM lta_setup WHERE is_active=1");
// FIX (07 Jul 2026): scope to current org.
[$orgClause, $orgParams] = orgScopeClause($db, 'company_id');
$employees = $db->fetchAll("SELECT id,CONCAT(employee_id,' - ',first_name,' ',last_name) AS label,lta_eligible FROM employees WHERE status='active' AND $orgClause ORDER BY first_name", $orgParams);
$eligible  = array_filter($employees, fn($e)=>$e['lta_eligible']);

include __DIR__ . "/../../includes/header.php";
?>
<div class="row g-3">
  <div class="col-md-4">
    <div class="pc-card">
      <div class="pc-card-title"><i class="ti ti-settings me-2"></i>LTA Setup</div>
      <div class="d-flex justify-content-between py-2 border-bottom small"><span class="text-muted">Cycles per 4 Years</span><span class="fw-bold"><?=$setup["cycles_in_4_years"]??2?></span></div>
      <div class="d-flex justify-content-between py-2 border-bottom small"><span class="text-muted">Max per Cycle</span><span class="fw-bold"><?=$setup["max_amount_per_cycle"]>0?"₹".number_format($setup["max_amount_per_cycle"]):"As per LTA Component"?></span></div>
      <div class="d-flex justify-content-between py-2 small"><span class="text-muted">Eligible Employees</span><span class="fw-bold"><?=count($eligible)?></span></div>
    </div>
  </div>
  <div class="col-md-8">
    <div class="pc-card">
      <div class="pc-card-title"><i class="ti ti-plane me-2"></i>LTA Eligible Employees</div>
      <div class="pc-table-wrap">
        <table class="pc-table">
          <thead><tr><th>Employee</th><th>LTA Eligible</th><th>LTA Component</th><th>Status</th></tr></thead>
          <tbody>
          <?php foreach($employees as $e):
            $ltaComp = $db->fetchOne("SELECT esc.amount FROM employee_salary_components esc JOIN salary_heads sh ON sh.id=esc.salary_head_id WHERE esc.employee_id=? AND sh.code='LTA'",[$e["id"]]);
          ?>
          <tr>
            <td class="fw-semibold"><?=clean($e["label"])?></td>
            <td><?=$e["lta_eligible"]?'<i class="ti ti-check text-success"></i> Yes':'<i class="ti ti-x text-danger"></i> No'?></td>
            <td><?=$ltaComp?"₹".number_format($ltaComp["amount"],2):"—"?></td>
            <td><?=statusBadge($e["lta_eligible"]?"active":"inactive")?></td>
          </tr>
          <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<?php include __DIR__ . "/../../includes/footer.php"; ?>