<?php
declare(strict_types=1);
ini_set('display_errors','0');
header('Cache-Control: private, no-store, max-age=0');
require_once __DIR__.'/../includes/console_shell.php';
require_login();
$pageLink='salary-config/additional-rate-configuration.php';
$acl=acl_for($pageLink);
if((int)(current_user()['role_id']??0)===1)$acl['can_view']=true;
if(empty($acl['can_view'])){http_response_code(403);exit('Access denied.');}
require_once __DIR__.'/additional-rate-configuration-common.php';

$access=gsr_access($con);
$id=max(0,(int)($_GET['id']??0));
$record=$id?arc_record($con,$access,$id):null;
if(!$record){
    http_response_code(404);
    console_header('Additional Rate Configuration History',$pageLink);
    ?>
    <div class="rounded-2xl border border-red-100 bg-red-50 p-6 text-sm font-semibold text-red-700">
      The Additional Rate Configuration was not found or is outside your access.
    </div>
    <?php
    console_footer();
    exit;
}

$history=arc_rows(
    $con,
    'SELECT h.*,e.name employee_name,c.componentname component_name,m.month month_name
       FROM jos_app_emp_eardeduction_editlog h
       LEFT JOIN jos_app_employee e ON e.id=h.empid
       LEFT JOIN jos_app_salarycomponentmaster c ON c.id=h.component
       LEFT JOIN jos_app_mmonth m ON m.id=h.month
      WHERE h.earid=?
      ORDER BY h.id DESC',
    'i',
    [$id]
);

console_header('Additional Rate Configuration History',$pageLink);
?>
<div class="mb-6">
  <div class="mb-2 flex items-center gap-2 text-xs font-semibold text-slate-500">
    <a href="/dashboard.php">Dashboard</a>
    <i class="fa-solid fa-chevron-right text-[9px]"></i>
    <span>Salary Config</span>
    <i class="fa-solid fa-chevron-right text-[9px]"></i>
    <a href="/<?=$pageLink?>">Additional Rate Configuration</a>
    <i class="fa-solid fa-chevron-right text-[9px]"></i>
    <span class="text-brand-600">History</span>
  </div>
  <div class="flex flex-wrap items-end justify-between gap-4">
    <div>
      <h1 class="text-2xl font-bold text-slate-900">Additional Rate Configuration History</h1>
      <p class="mt-1 text-sm text-slate-500">Review the saved revisions for this employee rate.</p>
    </div>
    <a href="/salary-config/additional-rate-configuration-edit.php?id=<?=$id?>" class="rounded-xl border border-slate-200 bg-white px-5 py-3 text-sm font-bold text-slate-600">
      <i class="fa-solid fa-arrow-left mr-2"></i>Back
    </a>
  </div>
</div>

<section class="overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm">
  <div class="flex items-center justify-between border-b border-slate-100 px-6 py-5">
    <div>
      <h2 class="text-lg font-bold text-slate-800"><?=h((string)($history[0]['employee_name']??''))?></h2>
      <p class="mt-1 text-sm text-slate-400"><?=h((string)($history[0]['component_name']??''))?></p>
    </div>
    <span class="rounded-full bg-brand-50 px-3 py-1 text-xs font-bold text-brand-700"><?=count($history)?> revision<?=count($history)===1?'':'s'?></span>
  </div>
  <div class="overflow-x-auto">
    <table class="w-full min-w-[900px] text-left text-sm">
      <thead class="bg-slate-50 text-[11px] font-extrabold uppercase tracking-wide text-slate-700">
        <tr>
          <?php foreach(['Sr No','Changed On','Employee Name','OT / Allowance','Month','Year','Type','Amount'] as $heading):?>
            <th class="px-5 py-4 <?=$heading==='Amount'?'text-right':''?>"><?=h($heading)?></th>
          <?php endforeach;?>
        </tr>
      </thead>
      <tbody class="divide-y divide-slate-100">
        <?php foreach($history as $i=>$row):?>
          <tr>
            <td class="px-5 py-4 font-semibold text-slate-500"><?=$i+1?></td>
            <td class="px-5 py-4"><?=h((string)($row['systemdate']??$row['date']??'-'))?></td>
            <td class="px-5 py-4 font-bold text-slate-800"><?=h((string)($row['employee_name']??'-'))?></td>
            <td class="px-5 py-4"><?=h((string)($row['component_name']??'-'))?></td>
            <td class="px-5 py-4"><?=h((string)($row['month_name']??gsr_month_name((int)($row['month']??0))))?></td>
            <td class="px-5 py-4"><?=h((string)($row['year']??'-'))?></td>
            <td class="px-5 py-4"><?=h(arc_type_label((int)($row['s_type']??0)))?></td>
            <td class="px-5 py-4 text-right font-bold"><?=number_format((float)($row['grosspay']??0),2)?></td>
          </tr>
        <?php endforeach;?>
        <?php if(!$history):?>
          <tr><td colspan="8" class="px-6 py-20 text-center font-semibold text-slate-400">No edit history found.</td></tr>
        <?php endif;?>
      </tbody>
    </table>
  </div>
</section>
<?php console_footer();
