<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
$adminTitle = 'Booking Dashboard';
$adminSubtitle = 'Appointment requests, confirmed bookings, cancelled bookings and no-show tracking.';
$has = table_exists('mj_appointments') || table_exists('mj_bookings');
$table = table_exists('mj_appointments') ? 'mj_appointments' : (table_exists('mj_bookings') ? 'mj_bookings' : '');
$stats = ['today'=>0,'pending'=>0,'confirmed'=>0,'cancelled'=>0,'noshow'=>0];
$rows=[];
if($table){
 $dateCol = mj_has_col($table,'appointment_datetime') ? 'appointment_datetime' : (mj_has_col($table,'appointment_at') ? 'appointment_at' : (mj_has_col($table,'booking_start') ? 'booking_start' : (mj_has_col($table,'start_at') ? 'start_at' : (mj_has_col($table,'booking_at') ? 'booking_at' : 'created_at'))));
 try{$stats['today']=safe_count('SELECT COUNT(*) c FROM '.$table.' WHERE DATE(`'.$dateCol.'`)=CURDATE()');}catch(Throwable $e){}
 try{$stats['pending']=safe_count('SELECT COUNT(*) c FROM '.$table.' WHERE status IN ("pending","requested")');}catch(Throwable $e){}
 try{$stats['confirmed']=safe_count('SELECT COUNT(*) c FROM '.$table.' WHERE status IN ("confirmed","approved")');}catch(Throwable $e){}
 try{$stats['cancelled']=safe_count('SELECT COUNT(*) c FROM '.$table.' WHERE status IN ("cancelled","rejected")');}catch(Throwable $e){}
 try{$stats['noshow']=safe_count('SELECT COUNT(*) c FROM '.$table.' WHERE status IN ("no_show","noshow")');}catch(Throwable $e){}
 $rows=mj_v290_simple_rows('SELECT * FROM '.$table.' ORDER BY id DESC LIMIT 30');
}
include __DIR__.'/_header.php';
?>
<div class="row mj-admin-stat-row"><?php foreach([['Today',$stats['today'],'primary'],['Pending',$stats['pending'],'warning'],['Confirmed',$stats['confirmed'],'success'],['Cancelled',$stats['cancelled'],'secondary'],['No Show',$stats['noshow'],'danger']] as $s): ?><div class="col-md"><div class="small-box bg-<?=e($s[2])?>"><div class="inner"><h3><?=e($s[1])?></h3><p><?=e($s[0])?></p></div><div class="icon"><i class="fas fa-calendar-check"></i></div></div></div><?php endforeach; ?></div>
<div class="card"><div class="card-header"><h3 class="card-title">Latest Bookings</h3><a href="bookings.php" class="btn btn-sm btn-primary float-right">Open Booking List</a></div><div class="card-body table-responsive"><table class="table"><thead><tr><th>ID</th><th>Customer</th><th>Date</th><th>Status</th><th>Note</th></tr></thead><tbody><?php foreach($rows as $r): ?><tr><td>#<?=e($r['id']??'')?></td><td><?=e($r['customer_name']??$r['name']??$r['user_id']??'')?></td><td><?=e($r['appointment_datetime']??$r['appointment_at']??$r['booking_start']??$r['start_at']??$r['booking_at']??$r['created_at']??'')?></td><td><?=mj_v289_render_status_badge($r['status']??'pending')?></td><td><?=e(mb_strimwidth($r['note']??$r['message']??'',0,70,'...'))?></td></tr><?php endforeach; ?><?php if(!$rows): ?><tr><td colspan="5" class="text-center text-muted p-4">No booking table/data found yet. Enable booking module and run migration.</td></tr><?php endif; ?></tbody></table></div></div>
<?php include __DIR__.'/_footer.php'; ?>
