<?php
declare(strict_types=1);ini_set('display_errors','0');require_once __DIR__.'/../includes/console_shell.php';require_login();$pageLink='salary-config/organisation-salary-configuration.php';$acl=require_acl('can_view',$pageLink);require_once __DIR__.'/organisation-salary-configuration-common.php';
$errors=[];$flash=$_SESSION['flash_osc']??null;unset($_SESSION['flash_osc']);$allowed=osc_allowed_payroll_ids($con);$organisations=osc_payrolls($con,$allowed);$types=osc_types($con);$org=max(0,(int)($_REQUEST['orgid']??0));$type=max(0,(int)($_REQUEST['componenttype']??0));$loaded=$org>0&&$type>0;
if($org&&!osc_has_id($organisations,$org)){$errors[]='Select a valid authorized organisation.';$loaded=false;}if($type&&!osc_has_id($types,$type)){$errors[]='Select a valid component type.';$loaded=false;}
$masters=$loaded?osc_master_components($con,$type):[];$existing=$loaded?osc_map(osc_existing($con,$org,$type)):[];$ctc=$loaded?osc_map(osc_report_rows($con,'jos_app_ctcreport',$org,$type)):[];$datewise=$loaded?osc_map(osc_report_rows($con,'jos_app_datewisereport',$org,$type)):[];
if($_SERVER['REQUEST_METHOD']==='POST'&&($_POST['action']??'')==='save'){$before=$existing;if(!verify_csrf($_POST['_csrf']??null))$errors[]='Security token expired. Please refresh and try again.';elseif(!$loaded)$errors[]='Select a valid organisation and component type.';else{$rows=osc_parse($con,$masters,$_POST,$errors);$newCount=count(array_filter($rows,fn($r)=>$r['enabled']&&!isset($before[$r['id']])));$removed=count(array_filter($before,fn($r)=>!isset($rows[(int)$r['componentname']])||!$rows[(int)$r['componentname']]['enabled']));if($newCount&&!$acl['can_add'])$errors[]='You do not have permission to add salary configuration components.';if($before&&!$acl['can_edit'])$errors[]='You do not have permission to edit salary configuration.';if($removed&&!$acl['can_delete'])$errors[]='You do not have permission to remove salary configuration components.';if(!$errors)try{osc_save($con,$org,$type,$rows);audit_log('salary_configuration.save','jos_app_salaryorganisation',(string)$org,'componenttype='.$type);$_SESSION['flash_osc']='Organisation salary configuration saved successfully.';redirect('/'.$pageLink.'?'.http_build_query(['orgid'=>$org,'componenttype'=>$type]));}catch(Throwable $e){$errors[]='The salary configuration could not be saved. No changes were committed.';}}}
console_header('Organisation Salary Configuration',$pageLink);$input='mt-2 w-full rounded-xl border border-slate-200 bg-white px-4 py-3 text-sm font-semibold text-slate-700 outline-none focus:border-brand-500 focus:ring-4 focus:ring-brand-100';?>
<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">Organisation Salary Configuration</span></div><h1 class="text-2xl font-bold text-slate-900">Organisation Salary Configuration</h1><p class="mt-1 text-sm text-slate-500">Configure organisation-level salary components, calculation mappings and payroll report visibility.</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;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="mb-5 overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm"><div class="flex flex-col gap-3 border-b border-slate-100 px-6 py-5 md:flex-row md:items-center md:justify-between"><div><h2 class="text-lg font-bold text-slate-800">Configuration Selection</h2><p class="mt-1 text-sm text-slate-400">Select the organisation and component type, then load the configuration.</p></div><a href="/salary-config/copy-organisation-salary-configuration.php" class="rounded-xl bg-emerald-600 px-5 py-3 text-center text-sm font-bold text-white hover:bg-emerald-700"><i class="fa-solid fa-copy mr-2"></i>Copy Organisation Salary Configuration</a></div><form method="get" class="grid gap-5 p-6 md:grid-cols-[1fr_1fr_auto]"><label class="text-sm font-bold text-slate-700">Organisation<select name="orgid" required class="<?=$input?>"><option value="">Select Organisation</option><?php foreach($organisations as $o):?><option value="<?=(int)$o['id']?>" <?=$org===(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label><label class="text-sm font-bold text-slate-700">Component Type<select name="componenttype" required class="<?=$input?>"><option value="">Select Component Type</option><?php foreach($types as $t):?><option value="<?=(int)$t['id']?>" <?=$type===(int)$t['id']?'selected':''?>><?=h($t['name'])?></option><?php endforeach;?></select></label><button class="mt-7 rounded-xl bg-brand-600 px-7 py-3 text-sm font-bold text-white hover:bg-brand-700"><i class="fa-solid fa-magnifying-glass mr-2"></i>Load</button></form></section>
<?php if($loaded):?><form method="post" class="overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm" onsubmit="return confirm('Save this organisation salary configuration? Unselected existing components will be removed.');"><input type="hidden" name="_csrf" value="<?=h(csrf_token())?>"><input type="hidden" name="action" value="save"><input type="hidden" name="orgid" value="<?=$org?>"><input type="hidden" name="componenttype" value="<?=$type?>"><div class="border-b border-slate-100 px-6 py-5"><h2 class="text-lg font-bold text-slate-800">Salary Component Configuration</h2><p class="mt-1 text-sm text-slate-400">Components follow Salary Component Master ordering. Add and Minus support multiple components.</p></div><div class="overflow-x-auto"><table class="min-w-[1600px] w-full text-left text-xs"><thead class="sticky top-0 z-10 bg-slate-50 text-[11px] uppercase tracking-wide text-slate-600"><tr><?php foreach(['Sr No','Component Name','Order','Color','Calculation On Add (+)','Calculation On Minus (-)','On CTC Report','On Date Wise Report','On Muster Report','Only Calculated'] as $h):?><th class="border-b border-slate-100 px-4 py-4 font-extrabold"><?=h($h)?></th><?php endforeach;?></tr></thead><tbody class="divide-y divide-slate-100"><?php foreach($masters as $i=>$m):$id=(int)$m['id'];$e=$existing[$id]??[];$c=$ctc[$id]??[];$d=$datewise[$id]??[];$add=osc_ids(explode(',',(string)($e['calculation_on']??'')));$minus=osc_ids(explode(',',(string)($e['calculation_mins']??'')));$locked=(int)$m['attend_config']===1||trim((string)$m['functionname'])!=='';?><tr class="align-top hover:bg-slate-50/60"><td class="px-4 py-4 font-bold text-slate-500"><?=$i+1?></td><td class="px-4 py-4"><label class="flex items-start gap-3 font-bold text-slate-800"><input type="checkbox" name="enabled[<?=$id?>]" value="1" <?=isset($existing[$id])?'checked':''?> class="mt-0.5 h-4 w-4 rounded border-slate-300 text-brand-600"><?=h($m['componentname'])?></label><?php if($locked):?><span class="mt-2 block max-w-52 text-[10px] font-semibold text-red-500"><?=(int)$m['attend_config']===1?'Attendance Report component':'Function-based component'?></span><?php endif;?></td><td class="px-4 py-3"><input type="number" min="0" max="9999" name="orderby[<?=$id?>]" value="<?=h((string)($e['orderby']??$m['orderby']))?>" class="w-20 rounded-lg border border-slate-200 px-3 py-2"></td><td class="px-4 py-4 text-center"><input type="checkbox" name="color[<?=$id?>]" value="1" <?=((int)($e['color']??0)===1)?'checked':''?> class="h-4 w-4 rounded border-slate-300 text-brand-600"></td><td class="px-4 py-3"><?php if(!$locked):?><select multiple name="calculation_on[<?=$id?>][]" class="h-28 w-52 rounded-lg border border-slate-200 p-2 text-xs"><option value="">None</option><?php foreach($masters as $o):if((int)$o['id']===$id)continue;?><option value="<?=(int)$o['id']?>" <?=in_array((int)$o['id'],$add,true)?'selected':''?>><?=h($o['componentname'])?></option><?php endforeach;?></select><?php else:?><span class="text-red-500">Not applicable</span><?php endif;?></td><td class="px-4 py-3"><?php if(!$locked):?><select multiple name="calculation_mins[<?=$id?>][]" class="h-28 w-52 rounded-lg border border-slate-200 p-2 text-xs"><option value="">None</option><?php foreach($masters as $o):if((int)$o['id']===$id)continue;?><option value="<?=(int)$o['id']?>" <?=in_array((int)$o['id'],$minus,true)?'selected':''?>><?=h($o['componentname'])?></option><?php endforeach;?></select><?php else:?><span class="text-red-500">Not applicable</span><?php endif;?></td><?php foreach([['ctc',$c['ctc']??0,'ctc_order',$c['orderby']??''],['datereport',$d['datereport']??0,'date_order',$d['orderby']??''],['musterreport',$d['musterreport']??0,'muster_order',$d['musterorderby']??'']] as [$name,$checked,$orderName,$order]):?><td class="px-4 py-3"><label class="flex items-center gap-2 font-semibold"><input type="checkbox" name="<?=$name?>[<?=$id?>]" value="1" <?=(int)$checked===1?'checked':''?> class="h-4 w-4 rounded border-slate-300 text-brand-600">Yes</label><input type="number" min="0" max="9999" name="<?=$orderName?>[<?=$id?>]" value="<?=h((string)$order)?>" placeholder="Order" class="mt-2 w-24 rounded-lg border border-slate-200 px-3 py-2"></td><?php endforeach;?><td class="px-4 py-4 text-center"><input type="checkbox" name="onlycalculate[<?=$id?>]" value="1" <?=((int)($e['onlycalculate']??0)===1)?'checked':''?> class="h-4 w-4 rounded border-slate-300 text-brand-600"></td></tr><?php endforeach;?></tbody></table><?php if(!$masters):?><div class="px-6 py-16 text-center font-semibold text-slate-400">No active salary components were found for this component type.</div><?php endif;?></div><div class="flex gap-3 border-t border-slate-100 px-6 py-5"><button type="submit" <?=!$masters?'disabled':''?> class="rounded-xl bg-brand-600 px-7 py-3 text-sm font-bold text-white disabled:opacity-50"><i class="fa-solid fa-floppy-disk mr-2"></i>Submit</button><a href="/<?=$pageLink?>" class="rounded-xl border border-slate-200 px-7 py-3 text-sm font-bold text-slate-600">Cancel</a></div></form><?php else:?><div class="rounded-2xl border border-dashed border-slate-200 bg-white py-16 text-center text-sm font-semibold text-slate-400">Select an organisation and component type to load salary components.</div><?php endif;console_footer();
