<?php
declare(strict_types=1);
ini_set('display_errors','0');
require_once __DIR__.'/../includes/console_shell.php';require_login();$pageLink='salary-config/pay-scales.php';$pageAcl=require_acl('can_view',$pageLink);if(empty($pageAcl['can_edit'])){http_response_code(403);exit('Access denied.');}require_once __DIR__.'/pay-scale-common.php';require_once __DIR__.'/pay-scale-form.php';
$errors=[];$access=psm_access($con);$payrolls=psm_payrolls($con,$access);$id=filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT,['options'=>['min_range'=>1]])?:0;$record=psm_record($con,$id,$payrolls);if(!$record){http_response_code(404);console_header('Pay Scale Not Found',$pageLink);?><div class="rounded-2xl border border-red-100 bg-red-50 p-6 font-semibold text-red-700">The requested Pay Scale does not exist or is outside your authorized scope.</div><?php console_footer();exit;}if((int)($record['scalelock']??0)===1){http_response_code(409);console_header('Pay Scale Locked',$pageLink);?><div class="rounded-2xl border border-amber-100 bg-amber-50 p-6 font-semibold text-amber-700">This approved Pay Scale is locked. It must be unlocked through the authorized list action before editing.</div><?php console_footer();exit;}
$opts=psm_options($con);$v=psm_defaults();foreach(array_keys($v) as $key)if($key!=='details'&&array_key_exists($key,$record))$v[$key]=$record[$key];$employees=psm_employees($con,$access,(int)$v['orgid']);$components=psm_components($con,(int)$v['orgid']);$stored=psm_detail_rows($con,$id);$v['details']=$stored;$systemDefaults=psm_defaults_for_components($con,(int)$v['orgid']);
if($_SERVER['REQUEST_METHOD']==='POST'){$components=psm_components($con,(int)$record['orgid']);$employees=psm_employees($con,$access,(int)$record['orgid']);$v=psm_post($components);$v['orgid']=(int)$record['orgid'];if(!verify_csrf($_POST['_csrf']??null))$errors[]='Security token expired. Please refresh and try again.';else{$errors=psm_validate($con,$v,$id,$payrolls,$employees,$opts,$components);if(!$errors)try{psm_save($con,$v,$id);if(function_exists('audit_log'))audit_log('pay_scale.update','jos_app_payscale_nongrid',(string)$id);$_SESSION['flash_pay_scale']='Pay Scale updated successfully.';redirect('/salary-config/pay-scales.php');}catch(Throwable $e){$errors[]='The Pay Scale could not be updated. No changes were committed.';}}}
console_header('Edit Pay Scale',$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/pay-scales.php">Pay Scale 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 Pay Scale</h1><p class="mt-1 text-sm text-slate-500">Update the Pay Scale without changing processed payroll records.</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"><ul class="list-disc space-y-1 pl-5"><?php foreach($errors as $error):?><li><?=h($error)?></li><?php endforeach;?></ul></div><?php endif;?><?php psm_render_form($v,$payrolls,$employees,$opts,$components,$stored,$systemDefaults,'Update');console_footer();
