<?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=[];$allowed=osc_allowed_payroll_ids($con);$organisations=osc_payrolls($con,$allowed);$from=max(0,(int)($_POST['from_org']??0));$to=max(0,(int)($_POST['to_org']??0));if($_SERVER['REQUEST_METHOD']==='POST'){if(!verify_csrf($_POST['_csrf']??null))$errors[]='Security token expired. Please refresh and try again.';if(!$acl['can_add'])$errors[]='You do not have permission to copy salary configuration.';if(!osc_has_id($organisations,$from)||!osc_has_id($organisations,$to))$errors[]='Select valid authorized source and destination organisations.';if($from===$to&&$from>0)$errors[]='Source and destination organisations must be different.';if(($_POST['confirm_copy']??'')!=='1')$errors[]='Confirm that the destination is intended to receive the copied configuration.';if(!$errors)try{$count=osc_copy($con,$from,$to);audit_log('salary_configuration.copy','jos_app_salaryorganisation',(string)$to,'source='.$from.'; rows='.$count);$_SESSION['flash_osc']=$count.' salary configuration components copied successfully.';redirect('/salary-config/organisation-salary-configuration.php?orgid='.$to.'&componenttype=6');}catch(DomainException $e){$errors[]=$e->getMessage();}catch(Throwable $e){$errors[]='The salary configuration could not be copied. No changes were committed.';}}
console_header('Copy 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><a href="/salary-config/organisation-salary-configuration.php">Organisation Salary Configuration</a><i class="fa-solid fa-chevron-right text-[9px]"></i><span class="text-brand-600">Copy</span></div><h1 class="text-2xl font-bold text-slate-900">Copy Organisation Salary Configuration</h1><p class="mt-1 text-sm text-slate-500">Copy all organisation salary-component mappings to an empty destination organisation.</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"><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="border-b border-slate-100 px-6 py-5"><h2 class="text-lg font-bold text-slate-800">Copy Configuration</h2><p class="mt-1 text-sm text-slate-400">The legacy workflow copies all component types and only permits a destination with no existing salary configuration.</p></div><form method="post" class="p-6" onsubmit="return confirm('Copy the complete salary configuration to the selected destination?');"><input type="hidden" name="_csrf" value="<?=h(csrf_token())?>"><div class="grid items-end gap-6 md:grid-cols-[1fr_auto_1fr]"><label class="text-sm font-bold text-slate-700">Copy From Organisation Location <span class="text-red-500">*</span><select name="from_org" required class="<?=$input?>"><option value="">Select Organisation Location</option><?php foreach($organisations as $o):?><option value="<?=(int)$o['id']?>" <?=$from===(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label><i class="fa-solid fa-arrow-right pb-4 text-xl text-brand-600"></i><label class="text-sm font-bold text-slate-700">Copy To Organisation Location <span class="text-red-500">*</span><select name="to_org" required class="<?=$input?>"><option value="">Select Organisation Location</option><?php foreach($organisations as $o):?><option value="<?=(int)$o['id']?>" <?=$to===(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label></div><label class="mt-6 flex items-start gap-3 rounded-xl border border-amber-100 bg-amber-50 p-4 text-sm font-semibold text-amber-800"><input type="checkbox" name="confirm_copy" value="1" required class="mt-0.5 h-4 w-4 rounded">I confirm the destination is correct and understand that copying is allowed only when it has no existing salary configuration.</label><div class="mt-6 flex gap-3"><button class="rounded-xl bg-brand-600 px-7 py-3 text-sm font-bold text-white"><i class="fa-solid fa-copy mr-2"></i>Proceed to Copy Salary Configuration</button><a href="/salary-config/organisation-salary-configuration.php" class="rounded-xl border border-slate-200 px-7 py-3 text-sm font-bold text-slate-600">Cancel</a></div></form></section><?php console_footer();
