<?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/salary-configuration-report.php';
require_acl('can_view',$pageLink);
require_once __DIR__.'/salary-configuration-report-common.php';

$errors=[];
$access=gsr_access($con);
$options=scr_options($con,$access);
$filters=scr_filters($con,$access,$options,$errors);
$employeeName='';
foreach($options['employees'] as $employeeOption)if((int)$employeeOption['id']===(int)$filters['filter_emp']){$employeeName=(string)$employeeOption['name'];break;}
$select='mt-2 w-full rounded-xl border border-slate-200 bg-white px-3 py-3 text-sm font-semibold text-slate-700';
console_header('Salary Configuration Report Filters',$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="/salary-config/salary-configuration-report.php">Salary Configuration Results</a><i class="fa-solid fa-chevron-right text-[9px]"></i><span class="text-brand-600">Report Filters</span></div><h1 class="text-2xl font-bold text-slate-900">Report Filters</h1><p class="mt-1 text-sm text-slate-500">Select filters, then open the Salary Configuration Results.</p></div>
<?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"><?=h(implode(' ',$errors))?></div><?php endif;?>
<section class="overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm"><div class="border-b border-slate-100 px-6 py-5"><h2 class="text-lg font-bold text-slate-800">Salary Configuration Report Filters</h2><p class="mt-1 text-sm text-slate-400">Fields and sequence follow the legacy report.</p></div><form method="get" action="/salary-config/salary-configuration-report.php" class="grid gap-4 bg-slate-50/60 p-6 md:grid-cols-2 xl:grid-cols-5"><label class="text-sm font-bold text-slate-700">Payscale<select name="filter_payscale" class="<?=$select?>"><option value="">Select Payscale</option><?php foreach($options['payscales'] as $o):?><option value="<?=(int)$o['id']?>" <?=$filters['filter_payscale']==(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label><label class="text-sm font-bold text-slate-700">Employee Name<input id="employee-name" list="employee-options" value="<?=h($employeeName)?>" placeholder="Employee name" autocomplete="off" class="<?=$select?>"><input id="employee-id" type="hidden" name="filter_emp" value="<?=$filters['filter_emp']?:''?>"><datalist id="employee-options"><?php foreach($options['employees'] as $o):?><option value="<?=h($o['name'])?>"></option><?php endforeach;?></datalist></label><label class="text-sm font-bold text-slate-700">Payroll Location<select name="filt_org" class="<?=$select?>"><option value="">Select Payroll Location</option><?php foreach($options['payrolls'] as $o):?><option value="<?=(int)$o['id']?>" <?=$filters['filt_org']==(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label><label class="text-sm font-bold text-slate-700">PF Applicable<select name="filt_pf" class="<?=$select?>"><option value="" <?=$filters['filt_pf']===''?'selected':''?>>All</option><option value="1" <?=$filters['filt_pf']==='1'?'selected':''?>>Applicable</option><option value="2" <?=$filters['filt_pf']==='2'?'selected':''?>>Non Applicable</option></select></label><label class="text-sm font-bold text-slate-700">Category<select name="filt_cat" class="<?=$select?>"><option value="">Select Category</option><?php foreach($options['categories'] as $o):?><option value="<?=(int)$o['id']?>" <?=$filters['filt_cat']==(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label><?php if($options['subbranchEnabled']):?><label class="text-sm font-bold text-slate-700">Sub Branch Location<select name="filt_subbranch" class="<?=$select?>"><option value="">Select Location</option><?php foreach($options['subbranches'] as $o):?><option value="<?=(int)$o['id']?>" <?=$filters['filt_subbranch']==(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label><?php endif;?><div class="flex flex-wrap items-end 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 href="/salary-config/salary-configuration-report-filter.php" class="rounded-xl border border-slate-200 bg-white px-6 py-3 text-sm font-bold text-slate-600">Reset</a><a href="/salary-config/salary-configuration-report.php" class="rounded-xl border border-slate-200 bg-white px-6 py-3 text-sm font-bold text-slate-600">Cancel</a></div></form></section>
<script>const employeeMap=<?=json_encode(array_column($options['employees'],'id','name'),JSON_HEX_TAG|JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT)?>;const employeeName=document.getElementById('employee-name');const employeeId=document.getElementById('employee-id');employeeName?.addEventListener('input',()=>{employeeId.value=employeeMap[employeeName.value]||'';});</script>
<?php console_footer();
