<?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=array_merge($acl,['can_view'=>true,'can_add'=>true,'can_edit'=>true,'can_delete'=>true]);
if(empty($acl['can_view'])){http_response_code(403);exit('Access denied.');}
require_once __DIR__.'/additional-rate-configuration-common.php';

$canAdd=!empty($acl['can_add']);$canEdit=!empty($acl['can_edit']);
$errors=[];$access=gsr_access($con);$options=arc_options($con,$access);
$filters=arc_filters($options,$errors);$query=arc_filter_query($filters);
$page=max(1,(int)($_GET['page']??1));$limit=20;$offset=($page-1)*$limit;
$total=arc_list($con,$access,$filters,0,0,true);$rows=arc_list($con,$access,$filters,$limit,$offset);
$pages=max(1,(int)ceil($total/$limit));$flash=$_SESSION['flash_additional_rate']??null;unset($_SESSION['flash_additional_rate']);
console_header('Additional Rate Configuration',$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><span class="text-brand-600">Additional Rate Configuration</span></div>
  <h1 class="text-2xl font-bold text-slate-900">Additional Rate Configuration</h1>
  <p class="mt-1 text-sm text-slate-500">Filter, review and manage employee monthly OT and allowance rates.</p>
</div>
<?php if($flash):?><div class="mb-5 rounded-xl border border-emerald-100 bg-emerald-50 px-5 py-4 text-sm font-semibold text-emerald-700"><i class="fa-solid fa-circle-check mr-2"></i><?=h($flash)?></div><?php endif;?>
<?php if($errors):?><div class="mb-5 rounded-xl border border-red-100 bg-red-50 px-5 py-4 text-sm font-semibold text-red-700"><i class="fa-solid fa-circle-exclamation mr-2"></i><?=h(implode(' ',$errors))?></div><?php endif;?>

<section class="overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm">
  <div class="flex flex-wrap items-center justify-between gap-4 border-b border-slate-100 px-6 py-5">
    <div><h2 class="text-lg font-bold text-slate-800">Additional Rate Configuration List</h2><p class="mt-1 text-sm text-slate-400">Records and filters follow the legacy Additional Rate Configuration list.</p></div>
    <?php if($canAdd):?><a href="/salary-config/additional-rate-configuration-add.php" class="rounded-xl bg-brand-600 px-6 py-3 text-sm font-bold text-white"><i class="fa-solid fa-plus mr-2"></i>Add</a><?php endif;?>
  </div>
  <form method="get" class="grid gap-4 border-b border-slate-100 bg-slate-50/60 p-6 md:grid-cols-2 xl:grid-cols-5">
    <?php foreach([
      ['Employee Name','filter_emp','employees','All Employees'],
      ['Payroll Location','filt_payroll_location','payrolls','All Payroll Locations'],
      ['Branch Location','filt_orglocation','branches','All Branch Locations'],
      ['OT / Allowance','filter_type','components','All Rate Types'],
      ['Salary Type','filt_salary','types','All Salary Types'],
    ] as [$label,$name,$list,$placeholder]):?><label class="text-xs font-bold uppercase tracking-wide text-slate-600"><?=h($label)?><select name="<?=h($name)?>" class="mt-2 w-full rounded-xl border border-slate-200 bg-white px-3 py-3 text-sm font-semibold normal-case tracking-normal text-slate-700"><option value=""><?=h($placeholder)?></option><?php foreach($options[$list] as $o):?><option value="<?=(int)$o['id']?>" <?=$filters[$name]===(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label><?php endforeach;?>
    <div class="flex flex-wrap gap-2 md:col-span-2 xl:col-span-5"><button class="rounded-xl bg-brand-600 px-6 py-3 text-sm font-bold text-white"><i class="fa-solid fa-magnifying-glass mr-2"></i>GO</button><a target="_blank" href="/salary-config/additional-rate-configuration-print.php<?=$query?'?'.h($query):''?>" class="rounded-xl bg-slate-900 px-6 py-3 text-sm font-bold text-white"><i class="fa-solid fa-print mr-2"></i>Print</a><a href="/<?=$pageLink?>" class="rounded-xl border border-slate-200 bg-white px-6 py-3 text-sm font-bold text-slate-600">Reset</a></div>
  </form>
  <div class="flex items-center justify-between border-b border-slate-100 px-6 py-4"><span class="font-bold text-slate-700">Rate Records</span><span class="rounded-full bg-brand-50 px-3 py-1 text-xs font-bold text-brand-700"><?=$total?> record<?=$total===1?'':'s'?></span></div>
  <div class="overflow-x-auto"><table class="w-full min-w-[1000px] text-left text-sm"><thead class="bg-slate-50 text-[11px] font-extrabold uppercase tracking-wide text-slate-700"><tr><?php foreach(['Sr No','Employee Name','OT / Allowance','Month','Year','Type','Amount','Actions'] 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($rows as $i=>$row):?><tr class="hover:bg-slate-50/60"><td class="px-5 py-4 font-semibold text-slate-500"><?=$offset+$i+1?></td><td class="px-5 py-4 font-bold text-slate-800"><?=h($row['employee_name'])?></td><td class="px-5 py-4"><?=h($row['component_name']??'-')?></td><td class="px-5 py-4"><?=h($row['month_name']??gsr_month_name($row['month']))?></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']))?></td><td class="px-5 py-4 text-right font-bold"><?=number_format((float)$row['grosspay'],2)?></td><td class="px-5 py-4"><div class="flex gap-2"><?php if($canEdit):?><a title="Edit" href="/salary-config/additional-rate-configuration-edit.php?id=<?=(int)$row['id']?>" class="grid h-9 w-9 place-items-center rounded-lg border border-slate-200 text-brand-600 hover:bg-brand-50"><i class="fa-solid fa-pen"></i></a><?php endif;?><?php if((int)$row['history_count']>1):?><a title="View edit history" href="/salary-config/additional-rate-configuration-history.php?id=<?=(int)$row['id']?>" class="grid h-9 w-9 place-items-center rounded-lg border border-slate-200 text-slate-600 hover:bg-slate-50"><i class="fa-solid fa-eye"></i></a><?php endif;?></div></td></tr><?php endforeach;?>
  <?php if(!$rows):?><tr><td colspan="8" class="px-6 py-20 text-center font-semibold text-slate-400">No Additional Rate Configuration records found.</td></tr><?php endif;?></tbody></table></div>
  <?php if($pages>1):?><div class="flex items-center justify-between border-t border-slate-100 px-6 py-4 text-sm font-semibold text-slate-500"><span>Showing <?=$offset+1?>–<?=min($offset+$limit,$total)?> of <?=$total?></span><div class="flex gap-2"><?php if($page>1):?><a class="rounded-lg border border-slate-200 px-4 py-2" href="?<?=$query?h($query).'&amp;':''?>page=<?=$page-1?>">Previous</a><?php endif;?><?php if($page<$pages):?><a class="rounded-lg border border-slate-200 px-4 py-2" href="?<?=$query?h($query).'&amp;':''?>page=<?=$page+1?>">Next</a><?php endif;?></div></div><?php endif;?>
</section>
<?php console_footer();
