<?php
declare(strict_types=1);
function scm_render_form(array $v,array $types,array $components,string $actionLabel):void{
 $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';$yn=scm_yesno();$selected=array_map('intval',(array)$v['calculation_on']);
 ?>
 <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 Component Details</h2><p class="mt-1 text-sm text-slate-400">Fields and stored values follow the legacy Salary Component Master.</p></div><form method="post" class="p-6"><input type="hidden" name="_csrf" value="<?=h(csrf_token())?>">
 <div class="grid gap-5 md:grid-cols-2 xl:grid-cols-4">
 <label class="text-sm font-bold text-slate-700">Component Type <span class="text-red-500">*</span><select name="componenttype" required class="<?=$input?>"><option value="">Select Component Type</option><?php foreach($types as $o):?><option value="<?=(int)$o['id']?>" <?=(int)$v['componenttype']===(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label>
 <?php foreach([['Component Name','componentname',true],['Component Name in Marathi','marathiname',true],['Short Name','shortname',true]] as [$label,$name,$required]):?><label class="text-sm font-bold text-slate-700"><?=h($label)?> <span class="text-red-500">*</span><input name="<?=$name?>" value="<?=h($v[$name])?>" <?=$required?'required':''?> maxlength="<?=$name==='marathiname'?250:150?>" class="<?=$input?>"></label><?php endforeach;?>
 </div>
 <div class="mt-7 rounded-2xl border border-slate-100 bg-slate-50/60 p-5"><div class="grid items-start gap-4 lg:grid-cols-[1fr_auto_2fr]"><label class="text-sm font-bold text-slate-700">Function Name<input name="functionname" value="<?=h($v['functionname'])?>" maxlength="150" class="<?=$input?>"></label><div class="pt-11 text-center text-xs font-extrabold text-red-500">OR</div><div class="text-sm font-bold text-slate-700">Calculation On<?php $selectedNames=[];foreach($components as $o)if(in_array((int)$o['id'],$selected,true))$selectedNames[]=$o['name'];?><select id="calculation-select" class="<?=$input?>"><option value=""><?=count($selected)>1?h(count($selected).' components selected'):'None'?></option><?php foreach($components as $o):?><option value="<?=(int)$o['id']?>" <?=count($selected)===1&&in_array((int)$o['id'],$selected,true)?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select><div id="calculation-values"><?php foreach($selected as $sid):?><input type="hidden" name="calculation_on[]" value="<?=$sid?>"><?php endforeach;?></div><span class="mt-2 block text-xs font-semibold text-red-500">Example: OT Days Amt × OT / Allowance Form. Current relationships are preserved until a new option is selected.</span></div></div></div>
 <div class="mt-7 grid gap-5 md:grid-cols-2 xl:grid-cols-6">
 <?php $select=function($label,$name,$options)use($v,$input){?><label class="text-sm font-bold text-slate-700"><?=h($label)?><select name="<?=h($name)?>" class="<?=$input?>"><?php foreach($options as $key=>$text):?><option value="<?=h((string)$key)?>" <?=(int)$v[$name]===(int)$key?'selected':''?>><?=h($text)?></option><?php endforeach;?></select></label><?php };$select('Calculation Type','calculation_type',[0=>'None',1=>'Plus',2=>'Minus']);$select('Status','status',[1=>'Active',0=>'Inactive']);$select('Gross Pay Status','gross_status',$yn);?>
 <label class="text-sm font-bold text-slate-700">Order By<input type="number" min="0" max="9999" name="orderby" value="<?=h((string)$v['orderby'])?>" class="<?=$input?>"></label>
 <?php $select('Readonly Field','editable',[0=>'Yes',1=>'No']);$select('On Hrs To Day Config','dayshrsconfig',$yn);?>
 </div>
 <div class="mt-7 grid gap-5 md:grid-cols-2 xl:grid-cols-6"><?php $select('OT / Allowance Form','otallow_status',$yn);$select('Extra Earning Form','earning_status',$yn);$select('Extra Deduction Form','deduction_status',$yn);$select('Deduction Amount/Remark','deduction_remark',[0=>'Amount',1=>'Remark']);$select('On Attendance Report','attend_config',$yn);$select('Round Up','roundup',[0=>'None',1=>'Up',2=>'Down',3=>'Nearest',4=>'Mod',5=>'2 Decimals']);?></div>
 <div class="mt-7 max-w-sm"><?php $select('Recurring Deduction','recurring',$yn);?></div>
 <div class="mt-7 flex gap-3"><button class="rounded-xl bg-brand-600 px-7 py-3 text-sm font-bold text-white hover:bg-brand-700"><i class="fa-solid fa-floppy-disk mr-2"></i><?=h($actionLabel)?></button><a href="/salary-config/salary-components.php" class="rounded-xl border border-slate-200 px-7 py-3 text-sm font-bold text-slate-600">Cancel</a></div></form></section>
 <script>(function(){const select=document.getElementById('calculation-select'),values=document.getElementById('calculation-values');if(!select)return;select.addEventListener('change',()=>{values.innerHTML='';if(select.value){const input=document.createElement('input');input.type='hidden';input.name='calculation_on[]';input.value=select.value;values.appendChild(input)}})})();</script>
 <?php
}
