<?php
declare(strict_types=1);ini_set('display_errors','0');header('Cache-Control: private,no-store,no-cache,must-revalidate,max-age=0');header('Pragma:no-cache');
require_once __DIR__.'/../includes/console_shell.php';require_login();$pageLink='reports/monthly-attendance-log.php';require_acl('can_view',$pageLink);require_once __DIR__.'/monthly-attendance-log-common.php';date_default_timezone_set('Asia/Kolkata');
$submitted=$_SERVER['REQUEST_METHOD']==='POST';$errors=[];$report=null;try{$options=mal_options($con);$source=$submitted?$_POST:[];$filters=mal_filters($con,$source,$options);if($submitted){if(!verify_csrf($_POST['_csrf']??null))$errors[]='Security token expired. Please refresh the page and try again.';$errors=array_merge($errors,mal_validate($filters,$options));if(!$errors)$report=mal_report($con,$filters,$options);}}catch(Throwable $e){$filters=['interval'=>0,'month'=>(int)date('n'),'year'=>0,'employee'=>0];$errors[]='Unable to generate the Monthly Attendance Log.';}
$export=false;if(mal_table($con,'jos_app_exporttoexcel_config')){$x=mal_rows($con,'SELECT status FROM jos_app_exporttoexcel_config WHERE layout=? LIMIT 1','s',['Monthly Attendance Log']);$export=!empty($x[0]['status']);}
$field='mt-2 w-full rounded-xl border border-slate-200 bg-white px-3 py-3 text-sm font-semibold text-slate-700 outline-none focus:border-brand-500 focus:ring-4 focus:ring-brand-100';
$sel=static fn($a,$b):string=>$a==$b?'selected':'';
console_header('Monthly Attendance Log',$pageLink);?>
<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 Attendance Log</span></div><h1 class="text-2xl font-bold text-slate-900">Monthly Attendance Log</h1><p class="mt-1 text-sm text-slate-500">Select the employee and attendance period, then view the monthly report below.</p></div>
<?php foreach($errors as $e):?><div class="mb-5 rounded-xl border border-red-100 bg-red-50 px-5 py-4 text-sm font-semibold text-red-700"><?=h($e)?></div><?php endforeach;?>
<section id="monthly-filter-card" class="mb-6 overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm"><div class="border-b border-slate-100 px-5 py-4"><h2 class="text-lg font-bold text-slate-800">Report Filters</h2></div>
<form method="post" id="monthly-report-form" class="grid gap-4 p-5 md:grid-cols-2 xl:grid-cols-4"><input type="hidden" name="_csrf" value="<?=h(csrf_token())?>">
<?php if($options['mode']===1):?><label class="text-sm font-bold text-slate-700">Salary Interval<select required name="filt_intervals" class="<?=$field?>"><option value="">Select Salary Interval</option><?php foreach($options['intervals'] as $r):?><option value="<?=(int)$r['id']?>" <?=$sel($filters['interval'],(int)$r['id'])?>><?=h(date('d-m-Y',strtotime($r['fromdate'])).' - '.date('d-m-Y',strtotime($r['todate'])))?></option><?php endforeach;?></select></label><?php else:?>
<label class="text-sm font-bold text-slate-700">Month<select required name="filt_month" class="<?=$field?>"><option value="">Select Month</option><?php foreach($options['months'] as $r):?><option value="<?=(int)$r['id']?>" <?=$sel($filters['month'],(int)$r['id'])?>><?=h($r['name'])?></option><?php endforeach;?></select></label>
<label class="text-sm font-bold text-slate-700">Year<select required name="filt_year" class="<?=$field?>"><option value="">Select Year</option><?php foreach($options['years'] as $r):?><option value="<?=(int)$r['id']?>" <?=$sel($filters['year'],(int)$r['id'])?>><?=h($r['name'])?></option><?php endforeach;?></select></label><?php endif;?>
<label class="text-sm font-bold text-slate-700">Employee Name<select required name="filter_name1" class="<?=$field?>"><option value="">Select Employee</option><?php foreach($options['employees'] as $r):?><option value="<?=(int)$r['id']?>" <?=$sel($filters['employee'],(int)$r['id'])?>><?=h($r['name'])?></option><?php endforeach;?></select></label>
<div class="flex flex-wrap items-end gap-2 self-end"><button type="submit" class="rounded-xl bg-brand-600 px-6 py-3 text-sm font-bold text-white hover:bg-brand-700"><i class="fa-solid fa-magnifying-glass mr-2"></i>Go</button><?php if($report):?><button id="monthly-print-button" type="button" onclick="window.print()" class="rounded-xl bg-red-500 px-6 py-3 text-sm font-bold text-white hover:bg-red-600"><i class="fa-solid fa-print mr-2"></i>Print</button><?php if($export):?><button id="monthly-export-button" type="submit" formaction="/reports/monthly-attendance-log-export.php" formtarget="_blank" class="rounded-xl bg-emerald-600 px-6 py-3 text-sm font-bold text-white hover:bg-emerald-700"><i class="fa-solid fa-file-excel mr-2"></i>Export</button><?php endif;?><?php endif;?></div>
</form></section>
<?php if($report):?>
<div id="report-output" class="space-y-6"><section class="overflow-hidden rounded-2xl border border-slate-100 bg-white shadow-sm"><div class="border-b border-slate-100 bg-slate-800 p-5 text-center text-white"><div class="font-extrabold"><?=h($report['company'])?></div><h2 class="mt-2 text-xl font-bold">Monthly Attendance Log - <?=h($report['period']['label'])?> Dated on <?=h(date('d-m-Y'))?></h2></div>
<div class="border-b border-slate-100 bg-white px-5 py-3 text-center text-sm font-bold text-slate-700">Employee Name: <span class="mr-6 text-slate-900"><?=h($report['employee']['name'])?></span> Emp Code: <span class="text-slate-900"><?=h((string)($report['employee']['emp_code']??''))?></span></div>
<div class="overflow-x-auto"><table class="min-w-[1200px] w-full text-sm"><thead class="bg-slate-900 text-white"><tr><th class="px-3 py-3 text-center">Sr No</th><th class="px-3 py-3 text-center">Date</th><th colspan="6" class="px-3 py-3 text-center">Log Details</th></tr></thead><tbody class="divide-y divide-slate-100"><?php foreach($report['rows'] as $i=>$r):?><tr class="align-top hover:bg-slate-50/60"><td class="px-3 py-4 text-center font-bold"><?=$i+1?></td><td class="px-3 py-4 text-center"><b><?=h(date('l',strtotime($r['date'])))?></b><br><?=h(date('d-m-Y',strtotime($r['date'])))?></td><td class="px-3 py-4 text-center"><?php if($r['shift']):?><b><?=h(($r['shift']['shift_in']??'').' - '.($r['shift']['shift_out']??''))?></b><br>Working Hrs - <?=h((string)($r['shift']['workhrs']??''))?><br><?php endif;?><span class="inline-flex rounded-full px-2.5 py-1 text-xs font-extrabold <?=in_array($r['status'],['Present'],true)?'bg-emerald-50 text-emerald-700':'bg-red-50 text-red-700'?>"><?=h($r['status'])?></span></td><td class="px-3 py-4 text-center"><b class="underline">Logs</b><br><br><?php foreach($r['logs'] as $x):?><b><?=h($x['location']??'')?></b><br><?=h(implode(' - ',$x['pair']))?><br><br><?php endforeach;?><?php if(!$r['logs']):?>—<?php endif;?></td><td class="px-3 py-4 text-center"><b>Gate Pass / Production Logs</b><br><?php foreach($r['gate'] as $x):?><b><?=h($x['location']??'')?></b><br><?=h(implode(' - ',$x['pair']))?><br><?php endforeach;?><?php if(!$r['gate']):?>—<?php endif;?><br><b class="text-red-600"><?=h(mal_hhmm($r['gate_minutes']))?></b></td><td class="px-3 py-4 text-center font-bold"><span class="underline">Total Hrs</span><br><br><?=h(mal_hhmm($r['worked']))?></td><td class="px-3 py-4 text-center"><b class="underline">Extra / Loss Time</b><br><?php if($r['extra']):?><b>E: + <?=h(mal_hhmm($r['extra']))?></b><?php endif;?><?php if($r['loss']):?><br><b class="text-red-600">L: - <?=h(mal_hhmm($r['loss']))?></b><?php endif;?><?php if(!$r['extra']&&!$r['loss']):?>—<?php endif;?></td><td class="px-3 py-4 text-center"><b class="underline">Late Mark</b><br><?=$r['late']?h(mal_hhmm($r['late'])).' L':'—'?></td></tr><?php endforeach;?></tbody></table></div></section>
<section class="rounded-2xl border border-slate-100 bg-white p-5 shadow-sm"><h2 class="mb-4 text-center text-lg font-bold text-slate-800">Attendance Summary</h2><div class="grid gap-3 sm:grid-cols-2 lg:grid-cols-4"><?php foreach($report['summary'] as $label=>$value):?><div class="rounded-xl border border-slate-100 bg-slate-50 p-4 text-center"><div class="text-xs font-extrabold uppercase tracking-wide text-slate-500"><?=h($label)?></div><div class="mt-2 text-2xl font-black text-slate-800"><?=strpos($label,'Minutes')!==false?h(mal_hhmm((int)$value)):(int)$value?></div></div><?php endforeach;?></div></section></div><?php endif;?>
<script>
document.addEventListener('DOMContentLoaded',function(){
  var form=document.getElementById('monthly-report-form');
  var report=document.getElementById('report-output');
  var printButton=document.getElementById('monthly-print-button');
  var exportButton=document.getElementById('monthly-export-button');
  if(!form||!report)return;
  form.querySelectorAll('select').forEach(function(field){
    field.addEventListener('change',function(){
      report.classList.add('hidden');
      if(printButton)printButton.classList.add('hidden');
      if(exportButton)exportButton.classList.add('hidden');
    });
  });
});
</script>
<style>@media print{header,nav,aside,.mb-6,#monthly-filter-card{display:none!important}main{padding:0!important}body{background:#fff!important}.rounded-2xl{border-radius:0!important;box-shadow:none!important}table{font-size:9px!important}}</style>
<?php console_footer();?>
