<?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_view',$pageLink);require_once __DIR__.'/salary-component-common.php';$canAdd=!empty($acl['can_add']);$canEdit=!empty($acl['can_edit']);$types=scm_types($con);$filter=max(0,(int)($_GET['componenttype']??0));if($filter&&!scm_in($types,$filter))$filter=0;$where='';$bind='';$params=[];if($filter&&$filter!==6){$where=' WHERE m.componenttype=?';$bind='i';$params=[$filter];}$page=max(1,(int)($_GET['page']??1));$limit=25;$offset=($page-1)*$limit;$total=(int)(scm_rows($con,'SELECT COUNT(*) total FROM jos_app_salarycomponentmaster m'.$where,$bind,$params)[0]['total']??0);$rows=scm_rows($con,"SELECT m.*,t.componenttype type_name,(SELECT GROUP_CONCAT(c.componentname ORDER BY c.orderby SEPARATOR ', ') FROM jos_app_salarycomponentmaster c WHERE FIND_IN_SET(c.id,m.calculation_on)) calculation_names FROM jos_app_salarycomponentmaster m LEFT JOIN jos_app_componenttype t ON t.id=m.componenttype".$where.' ORDER BY m.id ASC LIMIT ? OFFSET ?',$bind.'ii',array_merge($params,[$limit,$offset]));$flash=$_SESSION['flash_salary_component']??null;unset($_SESSION['flash_salary_component']);console_header('Salary Component Master',$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><span class="text-brand-600">Salary Component Master</span></div><h1 class="text-2xl font-bold text-slate-900">Salary Component Master</h1><p class="mt-1 text-sm text-slate-500">Maintain payroll component definitions and calculation relationships.</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"><?=h($flash)?></div><?php endif;?>
<section class="overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm"><div class="flex items-center justify-between border-b border-slate-100 px-6 py-5"><div><h2 class="text-lg font-bold text-slate-800">Salary Component List</h2><p class="mt-1 text-sm text-slate-400">Components are shown in the original master order.</p></div><?php if($canAdd):?><a href="/salary-config/salary-component-add.php" class="rounded-xl bg-brand-600 px-6 py-3 text-sm font-bold text-white"><i class="fa-solid fa-plus mr-2"></i>Add Salary Component</a><?php endif;?></div><form method="get" class="flex flex-wrap items-end gap-3 border-b border-slate-100 bg-slate-50/60 px-6 py-5"><label class="min-w-64 text-xs font-bold uppercase text-slate-600">Component Type<select name="componenttype" class="mt-2 w-full rounded-xl border border-slate-200 bg-white px-4 py-3 text-sm font-semibold normal-case text-slate-700"><option value="">All Component Types</option><?php foreach($types as $o):?><option value="<?=(int)$o['id']?>" <?=$filter===(int)$o['id']?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label><button class="rounded-xl bg-brand-600 px-6 py-3 text-sm font-bold text-white">Go</button><a target="_blank" href="/salary-config/salary-components-print.php<?=$filter?'?componenttype='.$filter:''?>" class="rounded-xl border border-slate-200 bg-white px-6 py-3 text-sm font-bold text-slate-600"><i class="fa-solid fa-print mr-2"></i>Print</a></form><div class="overflow-x-auto"><table class="w-full min-w-[2200px] text-left text-sm"><thead class="bg-slate-50 text-[10px] font-extrabold uppercase tracking-wide text-slate-700"><tr><?php foreach(['Sr No','Component Type','Component Name','Marathi Component Name','Short Name','Function Name / Calculation On','Calculation Type / Status','Gross Pay','Readonly','Order By','Extra Earning','Extra Deduction','OT / Allowance','Attendance Report','Round Up','Recurring Deduction','Actions'] as $h):?><th class="px-4 py-4"><?=h($h)?></th><?php endforeach;?></tr></thead><tbody class="divide-y divide-slate-100"><?php foreach($rows as $i=>$r):?><tr class="hover:bg-slate-50"><td class="px-4 py-4"><?=$offset+$i+1?></td><td class="px-4 py-4"><?=h($r['type_name']??'-')?></td><td class="px-4 py-4 font-bold"><?=h($r['componentname'])?></td><td class="px-4 py-4"><?=h($r['marathiname'])?></td><td class="px-4 py-4"><?=h($r['shortname'])?></td><td class="px-4 py-4"><div><?=h($r['functionname']?:'-')?></div><div class="mt-1 text-xs text-slate-400"><?=h($r['calculation_names']?:'-')?></div></td><td class="px-4 py-4"><?=h(scm_calc_label((int)$r['calculation_type']))?><br><span class="mt-1 inline-block rounded-full px-2 py-1 text-[10px] font-bold <?=$r['status']?'bg-emerald-50 text-emerald-700':'bg-slate-100 text-slate-500'?>"><?=$r['status']?'Active':'Inactive'?></span></td><?php foreach(['gross_status','editable','orderby','earning_status','deduction_status','otallow_status','attend_config'] as $k):?><td class="px-4 py-4"><?=$k==='orderby'?h((string)$r[$k]):((int)$r[$k]===1?'<span class="font-bold text-emerald-600">Yes</span>':'-')?></td><?php endforeach;?><td class="px-4 py-4"><?=h(scm_round_label((int)$r['roundup']))?></td><td class="px-4 py-4"><?=(int)$r['recurring']===1?'<span class="font-bold text-emerald-600">Yes</span>':'-'?></td><td class="px-4 py-4"><?php if($canEdit):?><a href="/salary-config/salary-component-edit.php?id=<?=(int)$r['id']?>" class="grid h-9 w-9 place-items-center rounded-lg border border-slate-200 text-brand-600"><i class="fa-solid fa-pen"></i></a><?php endif;?></td></tr><?php endforeach;?><?php if(!$rows):?><tr><td colspan="17" class="px-6 py-16 text-center font-semibold text-slate-400">No salary components found.</td></tr><?php endif;?></tbody></table></div></section><?php console_footer();
