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

$fy = $_GET["fy"] ?? (date("Y")."-".(date("Y")+1));
[$fyStart,$fyEnd] = explode("-",$fy."-".(intval($fy)+1));
// FIX (07 Jul 2026): scope to current org.
[$orgClause, $orgParams] = orgScopeClause($db, 'e.company_id');
$tdsData = $db->fetchAll("SELECT t.*,CONCAT(e.first_name,' ',e.last_name) AS emp_name,e.employee_id AS emp_code FROM tds_calculations t JOIN employees e ON e.id=t.employee_id WHERE t.financial_year=? AND $orgClause ORDER BY e.first_name,t.month", array_merge([$fy], $orgParams));

$byEmp = [];
foreach($tdsData as $t) {
    $byEmp[$t["employee_id"]]["name"] = $t["emp_name"];
    $byEmp[$t["employee_id"]]["code"] = $t["emp_code"];
    $byEmp[$t["employee_id"]]["months"][$t["month"]] = $t["tds_this_month"];
    $byEmp[$t["employee_id"]]["total"] = ($byEmp[$t["employee_id"]]["total"]??0) + $t["tds_this_month"];
}

$monthNames = ["","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Jan","Feb","Mar"];
$fyMonths   = [4,5,6,7,8,9,10,11,12,1,2,3];

include __DIR__ . "/../../includes/header.php";
?>
<div class="pc-card mb-3">
  <div class="d-flex gap-2 align-items-end">
    <div><label class="pc-label">Financial Year</label>
      <select class="form-select form-select-sm" onchange="location='?fy='+this.value">
        <?php for($y=date("Y");$y>=date("Y")-3;$y--): $fyOpt=$y."-".($y+1); ?>
        <option value="<?=$fyOpt?>" <?=$fy===$fyOpt?"selected":""?>><?=$fyOpt?></option>
        <?php endfor; ?>
      </select></div>
    <span class="text-muted small ms-2"><?=count($byEmp)?> employees · FY <?=clean($fy)?></span>
  </div>
</div>
<div class="pc-card">
  <div class="pc-card-title"><i class="ti ti-chart-line me-2"></i>Monthly TDS Chart — FY <?=clean($fy)?></div>
  <div class="pc-table-wrap">
    <table class="pc-table">
      <thead>
        <tr><th>Employee</th><?php foreach($fyMonths as $m): ?><th style="text-align:right"><?=$monthNames[$m]?></th><?php endforeach; ?><th style="text-align:right">Total</th></tr>
      </thead>
      <tbody>
      <?php if($byEmp): foreach($byEmp as $empId=>$d): ?>
      <tr>
        <td><div class="fw-semibold"><?=clean($d["name"])?></div><small class="text-muted"><?=clean($d["code"])?></small></td>
        <?php foreach($fyMonths as $m): $amt=$d["months"][$m]??0; ?>
        <td style="text-align:right;color:<?=$amt>0?'var(--pc-text)':'var(--pc-text3)'?>"><?=$amt>0?'₹'.number_format($amt):'—'?></td>
        <?php endforeach; ?>
        <td style="text-align:right" class="fw-bold">₹<?=number_format($d["total"],2)?></td>
      </tr>
      <?php endforeach; else: ?>
      <tr><td colspan="15" class="text-center text-muted py-4">No TDS data for FY <?=clean($fy)?>. Process payroll first.</td></tr>
      <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php include __DIR__ . "/../../includes/footer.php"; ?>