<?php
declare(strict_types=1);ini_set('display_errors','0');header('Cache-Control: private, no-store, max-age=0');require_once __DIR__.'/../../includes/console_shell.php';require_login();require_acl('can_view','reports/attendance/monthly-first-last-log-summary.php');require_once __DIR__.'/monthly-first-last-log-summary-common.php';
$errors=[];try{$o=mfl_options($con);$f=mfl_filters($_GET,true);$errors=mfl_validate($f,$o);$r=$errors?null:mfl_report($con,$f);$s=mfl_table($con,'jos_app_usersetting')?(mfl_rows($con,'SELECT store_name FROM jos_app_usersetting LIMIT 1')[0]??[]):[];$company=$s['store_name']??APP_NAME;}catch(Throwable $e){$errors[]='Unable to generate print report.';$r=null;$company=APP_NAME;}
?><!doctype html><html><head><meta charset="utf-8"><title>Monthly First &amp; Last Log Summary</title><style>@page{size:landscape;margin:6mm}body{font:9px Arial;color:#111}h1,h2,p{text-align:center;margin:4px}table{width:100%;border-collapse:collapse;table-layout:fixed}th,td{border:1px solid #222;padding:2px;text-align:center;overflow-wrap:anywhere}.emp{text-align:left;width:90px}button{margin-bottom:8px}@media print{button{display:none}}</style></head><body><button onclick="window.print()">Print</button><?php if($errors):?><p><?=h(implode(' ',$errors))?></p><?php elseif($r):?><h2><?=h((string)$company)?></h2><h1>Employee Monthly First &amp; Last Log Summary - <?=h(date('F Y',strtotime($r['from'])))?> Dated on <?=date('d-m-Y')?></h1><table><thead><tr><th class="emp">Employee / Detail</th><?php foreach($r['dates']as$d):?><th><?=date('d-m',strtotime($d))?></th><?php endforeach;?></tr></thead><tbody><?php foreach($r['rows']as$row):$days=$row['days'];foreach([['Shift','shift'],['Status','status'],['First Log','first'],['Last Log','last'],['Total Hrs','worked'],['EX/LS','extra'],['Late Mark','late']]as$i=>$def):?><tr><?php if($i===0):?><th rowspan="7" class="emp">#<?=h((string)$row['employee']['id'])?><br><?=h($row['employee']['name'])?><br><?=h($row['employee']['department']??'')?></th><?php endif;?><?php foreach($r['dates']as$d):$v=$days[$d][$def[1]];if($def[1]==='worked')$v=mfl_hhmm((int)$v);elseif($def[1]==='extra')$v=$v?'EX '.mfl_hhmm((int)$v):($days[$d]['loss']?'LS '.mfl_hhmm($days[$d]['loss']):'-');elseif($def[1]==='late')$v=$v?mfl_hhmm((int)$v):'-';?><td title="<?=h($def[0])?>"><?=h((string)($v?:'-'))?></td><?php endforeach;?></tr><?php endforeach;endforeach;?></tbody></table><script>addEventListener('load',()=>window.print())</script><?php endif;?></body></html>
