<?php
declare(strict_types=1);ini_set('display_errors','0');require_once __DIR__.'/../includes/console_shell.php';require_login();$pageLink='salary-config/salary-components.php';$acl=require_acl('can_edit',$pageLink);require_once __DIR__.'/salary-component-common.php';require_once __DIR__.'/salary-component-form.php';$id=max(0,(int)($_GET['id']??0));$record=scm_record($con,$id);if(!$record){http_response_code(404);console_header('Salary Component Not Found',$pageLink);?><div class="rounded-xl border border-red-100 bg-red-50 p-5 font-semibold text-red-700">Salary component was not found.</div><?php console_footer();exit;}$errors=[];$types=scm_types($con);$components=scm_components($con,$id);$v=array_merge(scm_defaults(),$record);$v['calculation_on']=array_values(array_filter(array_map('intval',explode(',',(string)$record['calculation_on']))));if($_SERVER['REQUEST_METHOD']==='POST'){$v=scm_post();if(!verify_csrf($_POST['_csrf']??null))$errors[]='Security token expired. Please refresh and try again.';else{$errors=scm_validate($con,$v,$id,$types,$components);if(!$errors&&scm_cycle($con,$id,$v['calculation_on']))$errors[]='Calculation On creates a circular component dependency.';if(!$errors)try{scm_save($con,$v,$id);audit_log('salary_component.update','jos_app_salarycomponentmaster',(string)$id);$_SESSION['flash_salary_component']='Salary component updated successfully.';redirect('/salary-config/salary-components.php');}catch(Throwable $e){$errors[]='The salary component could not be updated. No changes were committed.';}}}console_header('Edit Salary Component',$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><a href="/salary-config/salary-components.php">Salary Component Master</a><i class="fa-solid fa-chevron-right text-[9px]"></i><span class="text-brand-600">Edit</span></div><h1 class="text-2xl font-bold text-slate-900">Edit Salary Component</h1><p class="mt-1 text-sm text-slate-500">Update the component without changing its legacy calculation semantics.</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;?><?php scm_render_form($v,$types,$components,'Update');console_footer();
