<?php
declare(strict_types=1);ini_set('display_errors','0');header('Cache-Control: private, no-store, no-cache, must-revalidate, max-age=0');
require_once __DIR__.'/../../includes/console_shell.php';require_login();$pageLink='reports/attendance/monthly-first-last-log-summary.php';require_acl('can_view',$pageLink);require_once __DIR__.'/monthly-first-last-log-summary-common.php';date_default_timezone_set('Asia/Kolkata');
$submitted=isset($_GET['run']);$errors=[];$report=null;try{$options=mfl_options($con);$filters=mfl_filters($_GET);if($submitted){$errors=mfl_validate($filters,$options);if(!$errors)$report=mfl_report($con,$filters);}}catch(Throwable $e){$options=$options??['employees'=>[],'months'=>[],'years'=>[],'departments'=>[],'branches'=>[],'subbranches'=>[],'payrolls'=>[],'categories'=>[]];$filters=mfl_filters($_GET);$errors[]='Unable to generate the Monthly First & Last Log Summary.';}
$setting=mfl_table($con,'jos_app_usersetting')?(mfl_rows($con,'SELECT store_name,subbranch,catonof FROM jos_app_usersetting LIMIT 1')[0]??[]):[];$company=(string)($setting['store_name']??APP_NAME);$showSub=!empty($setting['subbranch']);$showCat=!empty($setting['catonof']);$additional=$filters['department']||$filters['branch']||$filters['subbranch']||$filters['category']||$filters['status']!==1||$filters['limit']!==40;$input='mt-1 w-full rounded-lg border border-slate-200 bg-white px-3 py-2.5 text-sm outline-none focus:border-brand-500 focus:ring-2 focus:ring-brand-100';$sel=fn($a,$b)=>$a==$b?'selected':'';
console_header('Monthly First & Last Log Summary',$pageLink);?>
<style>@page{size:landscape;margin:7mm}.mfl-table{border-collapse:collapse}.mfl-table th,.mfl-table td{border:1px solid #e2e8f0}@media print{header,nav,footer,.no-print{display:none!important}main{padding:0!important}.mfl-card{border:0!important;box-shadow:none!important}.mfl-table{font-size:6px!important}.mfl-table th,.mfl-table td{padding:2px!important}}</style>
<div class="mb-6"><div class="mb-2 flex items-center gap-2 text-xs font-semibold text-slate-500"><span>Reports</span><i class="fa-solid fa-chevron-right text-[9px]"></i><span>Attendance Report</span><i class="fa-solid fa-chevron-right text-[9px]"></i><span class="text-brand-600">Monthly First &amp; Last Log Summary</span></div><h1 class="text-2xl font-bold text-slate-900">Monthly First &amp; Last Log Summary</h1><p class="mt-1 text-sm text-slate-500">Employee monthly first punch, last punch and attendance-hour summary.</p></div>
<?php foreach($errors as$e):?><div class="mb-4 rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm font-semibold text-red-700"><?=h($e)?></div><?php endforeach;?>
<form method="get" id="mfl-form" class="no-print mb-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm"><div class="grid gap-4 md:grid-cols-2 xl:grid-cols-4">
<label class="text-sm font-bold text-slate-700">Employee Name<select multiple name="filter_name1[]" size="1" class="<?=$input?>"><option value="">All Employees</option><?php foreach($options['employees']as$o):?><option value="<?=(int)$o['id']?>" <?=in_array((int)$o['id'],$filters['employees'],true)?'selected':''?>><?=h($o['name'])?></option><?php endforeach;?></select></label>
<label class="text-sm font-bold text-slate-700">Month<select required name="filt_month" class="<?=$input?>"><option value="">Select Month</option><?php for($m=1;$m<=12;$m++):?><option value="<?=$m?>" <?=$sel($filters['month'],$m)?>><?=h(date('F',mktime(0,0,0,$m,1)))?></option><?php endfor;?></select></label>
<label class="text-sm font-bold text-slate-700">Year<select required name="filt_year" class="<?=$input?>"><option value="">Select Year</option><?php for($y=(int)date('Y')+1;$y>=(int)date('Y')-4;$y--):?><option value="<?=$y?>" <?=$sel($filters['year'],$y)?>><?=$y?></option><?php endfor;?></select></label>
<label class="text-sm font-bold text-slate-700">Payroll Location<select name="filt_payroll_location" class="<?=$input?>"><option value="0">Select Payroll Location</option><?php foreach($options['payrolls']as$o):?><option value="<?=(int)$o['id']?>" <?=$sel($filters['payroll'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label></div>
<div class="mt-4 flex flex-wrap items-center justify-between gap-3"><button type="button" id="mfl-more" aria-expanded="<?=$additional?'true':'false'?>" class="rounded-lg border border-slate-200 px-4 py-2.5 text-sm font-bold text-slate-700"><i class="fa-solid fa-sliders mr-2"></i><span><?=$additional?'Hide Additional Filters':'Use Additional Filters'?></span></button><div class="flex flex-wrap gap-2"><button name="run" value="1" class="rounded-lg bg-red-500 px-5 py-2.5 text-sm font-bold text-white"><i class="fa-solid fa-filter mr-2"></i>Go</button><button type="submit" name="run" value="1" formaction="/reports/attendance/monthly-first-last-log-summary-print.php" formtarget="_blank" class="rounded-lg bg-red-500 px-5 py-2.5 text-sm font-bold text-white"><i class="fa-solid fa-print mr-2"></i>Print</button><button type="submit" name="run" value="1" formaction="/reports/attendance/monthly-first-last-log-summary-export.php" class="rounded-lg bg-emerald-600 px-5 py-2.5 text-sm font-bold text-white"><i class="fa-solid fa-file-excel mr-2"></i>Export to XLS</button></div></div>
<div id="mfl-extra" class="mt-4 <?=$additional?'':'hidden'?> border-t border-slate-100 pt-4"><div class="grid gap-4 md:grid-cols-2 xl:grid-cols-5">
<label class="text-sm font-bold">Department<select name="filter_dept" class="<?=$input?>"><option value="0">Select All</option><?php foreach($options['departments']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['department'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label><label class="text-sm font-bold">Branch Location<select name="filt_branch_location" class="<?=$input?>"><option value="0">Select Branch Location</option><?php foreach($options['branches']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['branch'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label>
<?php if($showSub):?><label class="text-sm font-bold">Sub Branch Location<select name="filt_subbranch" class="<?=$input?>"><option value="0">Select Sub Branch</option><?php foreach($options['subbranches']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['subbranch'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label><?php endif;?><?php if($showCat):?><label class="text-sm font-bold">Category<select name="filt_cat" class="<?=$input?>"><option value="0">Select category</option><?php foreach($options['categories']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['category'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label><?php endif;?>
<label class="text-sm font-bold">Select Status<select name="filt_emp" class="<?=$input?>"><option value="1" <?=$sel($filters['status'],1)?>>Active</option><option value="0" <?=$sel($filters['status'],0)?>>Inactive</option><option value="2" <?=$sel($filters['status'],2)?>>Left From Company</option></select></label><label class="text-sm font-bold">List Limit<select name="filter_limit" class="<?=$input?>"><?php foreach([20,40,50,100,200]as$n):?><option value="<?=$n?>" <?=$sel($filters['limit'],$n)?>>1-<?=$n?></option><?php endforeach;?></select></label></div></div></form>
<?php if($report!==null):?><section class="mfl-card overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm"><div class="p-5 text-center"><b><?=h($company)?></b><h2 class="text-lg font-bold">Employee Monthly First &amp; Last Log Summary - <?=h(date('F Y',strtotime($report['from'])))?> Dated on <?=date('d-m-Y')?></h2></div><?php if(!$report['rows']):?><div class="p-12 text-center text-slate-500">No employees match this Employee, Payroll Location and Status combination. Select All Employees or choose the employee's assigned payroll location.</div><?php else:?><div class="overflow-x-auto"><table class="mfl-table min-w-max w-full text-[11px]"><thead class="bg-slate-900 text-white"><tr><th class="p-3 text-left">Employee / Detail</th><?php foreach($report['dates']as$d):?><th class="p-2 text-center"><?=date('d-m',strtotime($d))?></th><?php endforeach;?></tr></thead><tbody><?php foreach($report['rows']as$r):$days=$r['days'];?><tr><th rowspan="7" class="min-w-40 p-3 text-left align-top">#<?=h((string)$r['employee']['id'])?><br><?=h($r['employee']['name'])?><br><span class="font-normal text-slate-500"><?=h($r['employee']['department']??'')?></span></th><?php foreach($report['dates']as$d):?><td class="p-2 text-center font-bold"><?=h($days[$d]['shift'])?></td><?php endforeach;?></tr><tr><?php foreach($report['dates']as$d):?><td class="p-2 text-center font-bold <?=$days[$d]['status']==='P'?'text-emerald-600':'text-red-500'?>"><?=h($days[$d]['status'])?></td><?php endforeach;?></tr><tr><?php foreach($report['dates']as$d):?><td class="p-2 text-center"><?=h($days[$d]['first']?:'-')?></td><?php endforeach;?></tr><tr><?php foreach($report['dates']as$d):?><td class="p-2 text-center"><?=h($days[$d]['last']?:'-')?></td><?php endforeach;?></tr><tr><?php foreach($report['dates']as$d):?><td class="p-2 text-center"><?=h(mfl_hhmm($days[$d]['worked']))?></td><?php endforeach;?></tr><tr><?php foreach($report['dates']as$d):?><td class="p-2 text-center"><?=h($days[$d]['extra']?'EX '.mfl_hhmm($days[$d]['extra']):($days[$d]['loss']?'LS '.mfl_hhmm($days[$d]['loss']):'-'))?></td><?php endforeach;?></tr><tr><?php foreach($report['dates']as$d):?><td class="p-2 text-center"><?=h($days[$d]['late']?mfl_hhmm($days[$d]['late']):'-')?></td><?php endforeach;?></tr><?php endforeach;?></tbody></table></div><?php endif;?></section><?php endif;?>
<script>const b=document.getElementById('mfl-more'),x=document.getElementById('mfl-extra');b?.addEventListener('click',()=>{const o=x.classList.contains('hidden');x.classList.toggle('hidden');b.setAttribute('aria-expanded',o?'true':'false');b.querySelector('span').textContent=o?'Hide Additional Filters':'Use Additional Filters';});</script>
<?php console_footer();
