<?php
require_once __DIR__ . '/includes/auth.php';
requireAdmin();
function badgeClass($status){return match($status){'active','completed','resolved','approved','visible','confirmed'=>'success','pending','new','reviewing'=>'warning','inactive','cancelled','failed','rejected','closed'=>'danger',default=>'secondary'};}
function goAdmin($msg,$type='success',$section='overview'){header('Location: admin.php?'.http_build_query(['message'=>$msg,'type'=>$type,'section'=>$section]));exit();}
$conn=getDBConnection();
if(isset($_GET['export'])){ $map=['users'=>"SELECT id,name,email,phone,user_type,account_status,created_at FROM users ORDER BY created_at DESC",'bookings'=>"SELECT b.id,u.name AS user_name,u.email,s.name AS service_name,b.booking_date,b.booking_time,b.status,b.total_amount,b.notes,b.created_at FROM bookings b LEFT JOIN users u ON b.user_id=u.id LEFT JOIN services s ON b.service_id=s.id ORDER BY b.created_at DESC",'payments'=>"SELECT p.id,p.booking_id,u.name AS user_name,p.amount,p.payment_method,p.transaction_id,p.status,p.payment_date FROM payments p LEFT JOIN users u ON p.user_id=u.id ORDER BY p.payment_date DESC",'contacts'=>"SELECT id,name,email,phone,subject,inquiry_status,admin_notes,created_at FROM contacts ORDER BY created_at DESC",'admissions'=>"SELECT id,resident_name,contact_name,contact_email,contact_phone,care_level,inquiry_status,admin_notes,created_at FROM admissions ORDER BY created_at DESC",'services'=>"SELECT id,name,category,price,duration,active,created_at FROM services ORDER BY category,name",'testimonials'=>"SELECT id,name,relationship,rating,approved,created_at FROM testimonials ORDER BY created_at DESC"];$dataset=$_GET['export']; if(isset($map[$dataset])){ $result=$conn->query($map[$dataset]); header('Content-Type: text/csv'); header("Content-Disposition: attachment; filename={$dataset}-export.csv"); $out=fopen('php://output','w'); if($result && $result->num_rows>0){ $first=$result->fetch_assoc(); fputcsv($out,array_keys($first)); fputcsv($out,$first); while($row=$result->fetch_assoc()){ fputcsv($out,$row);} } fclose($out); exit(); } }
$bookingStatuses=['pending','confirmed','completed','cancelled']; $paymentStatuses=['pending','completed','failed','refunded']; $inquiryStatuses=['new','reviewing','resolved','closed']; $userRoles=['resident','family','staff','admin']; $accountStatuses=['active','inactive'];
if($_SERVER['REQUEST_METHOD']==='POST'){ $action=$_POST['action'] ?? ''; if($action==='save_service'){ $id=(int)($_POST['service_id']??0); $name=trim($_POST['name']??''); $desc=trim($_POST['description']??''); $cat=trim($_POST['category']??''); $price=(float)($_POST['price']??0); $duration=trim($_POST['duration']??''); $image=trim($_POST['image']??''); $active=isset($_POST['active'])?1:0; if($name===''||$cat===''||$price<=0||$duration==='') goAdmin('Please complete all required service fields.','danger','services'); if($id>0){ $stmt=$conn->prepare('UPDATE services SET name=?,description=?,category=?,price=?,duration=?,image=?,active=? WHERE id=?'); $stmt->bind_param('sssdssii',$name,$desc,$cat,$price,$duration,$image,$active,$id); $stmt->execute(); $stmt->close(); goAdmin('Service updated.','success','services'); } $stmt=$conn->prepare('INSERT INTO services (name,description,category,price,duration,image,active) VALUES (?,?,?,?,?,?,?)'); $stmt->bind_param('sssdssi',$name,$desc,$cat,$price,$duration,$image,$active); $stmt->execute(); $stmt->close(); goAdmin('Service created.','success','services'); }
if($action==='delete_service'){ $id=(int)($_POST['service_id']??0); $stmt=$conn->prepare('SELECT COUNT(*) AS total FROM bookings WHERE service_id=?'); $stmt->bind_param('i',$id); $stmt->execute(); $total=(int)$stmt->get_result()->fetch_assoc()['total']; $stmt->close(); if($total>0){ $stmt=$conn->prepare('UPDATE services SET active=0 WHERE id=?'); $stmt->bind_param('i',$id); $stmt->execute(); $stmt->close(); goAdmin('Service has bookings, so it was hidden instead of deleted.','warning','services'); } $stmt=$conn->prepare('DELETE FROM services WHERE id=?'); $stmt->bind_param('i',$id); $stmt->execute(); $stmt->close(); goAdmin('Service deleted.','success','services'); }
if($action==='update_booking_status'){ $id=(int)($_POST['booking_id']??0); $status=$_POST['status']??'pending'; if(!in_array($status,$bookingStatuses,true)) goAdmin('Invalid booking status.','danger','bookings'); $stmt=$conn->prepare('UPDATE bookings SET status=? WHERE id=?'); $stmt->bind_param('si',$status,$id); $stmt->execute(); $stmt->close(); goAdmin('Booking status updated.','success','bookings'); }
if($action==='update_payment_status'){ $id=(int)($_POST['payment_id']??0); $status=$_POST['status']??'pending'; if(!in_array($status,$paymentStatuses,true)) goAdmin('Invalid payment status.','danger','payments'); $stmt=$conn->prepare('UPDATE payments SET status=? WHERE id=?'); $stmt->bind_param('si',$status,$id); $stmt->execute(); $stmt->close(); goAdmin('Payment status updated.','success','payments'); }
if($action==='update_user'){ $id=(int)($_POST['user_id']??0); $name=trim($_POST['name']??''); $email=trim($_POST['email']??''); $phone=trim($_POST['phone']??''); $role=$_POST['user_type']??'family'; $acc=$_POST['account_status']??'active'; $newpass=trim($_POST['new_password']??''); if($name===''||$email==='') goAdmin('Name and email are required.','danger','users'); if(!in_array($role,$userRoles,true)||!in_array($acc,$accountStatuses,true)) goAdmin('Invalid role or account status.','danger','users'); $stmt=$conn->prepare('SELECT id FROM users WHERE email=? AND id!=?'); $stmt->bind_param('si',$email,$id); $stmt->execute(); if($stmt->get_result()->num_rows>0){ $stmt->close(); goAdmin('Another user already uses that email.','danger','users'); } $stmt->close(); if($newpass!==''){ if(strlen($newpass)<6) goAdmin('New password must be at least 6 characters.','danger','users'); $hash=password_hash($newpass,PASSWORD_DEFAULT); $stmt=$conn->prepare('UPDATE users SET name=?,email=?,phone=?,user_type=?,account_status=?,password=? WHERE id=?'); $stmt->bind_param('ssssssi',$name,$email,$phone,$role,$acc,$hash,$id); } else { $stmt=$conn->prepare('UPDATE users SET name=?,email=?,phone=?,user_type=?,account_status=? WHERE id=?'); $stmt->bind_param('sssssi',$name,$email,$phone,$role,$acc,$id);} $stmt->execute(); $stmt->close(); goAdmin('User updated.','success','users'); }
if($action==='update_contact'){ $id=(int)($_POST['contact_id']??0); $status=$_POST['inquiry_status']??'new'; $notes=trim($_POST['admin_notes']??''); if(!in_array($status,$inquiryStatuses,true)) goAdmin('Invalid contact status.','danger','contacts'); $stmt=$conn->prepare('UPDATE contacts SET inquiry_status=?, admin_notes=? WHERE id=?'); $stmt->bind_param('ssi',$status,$notes,$id); $stmt->execute(); $stmt->close(); goAdmin('Contact inquiry updated.','success','contacts'); }
if($action==='update_admission'){ $id=(int)($_POST['admission_id']??0); $status=$_POST['inquiry_status']??'new'; $notes=trim($_POST['admin_notes']??''); if(!in_array($status,$inquiryStatuses,true)) goAdmin('Invalid admission status.','danger','admissions'); $stmt=$conn->prepare('UPDATE admissions SET inquiry_status=?, admin_notes=? WHERE id=?'); $stmt->bind_param('ssi',$status,$notes,$id); $stmt->execute(); $stmt->close(); goAdmin('Admission inquiry updated.','success','admissions'); }
if($action==='save_testimonial'){ $id=(int)($_POST['testimonial_id']??0); $name=trim($_POST['name']??''); $rel=trim($_POST['relationship']??''); $text=trim($_POST['testimonial']??''); $rating=max(1,min(5,(int)($_POST['rating']??5))); $approved=isset($_POST['approved'])?1:0; if($name===''||$text==='') goAdmin('Testimonial name and text are required.','danger','testimonials'); if($id>0){ $stmt=$conn->prepare('UPDATE testimonials SET name=?,relationship=?,testimonial=?,rating=?,approved=? WHERE id=?'); $stmt->bind_param('sssiii',$name,$rel,$text,$rating,$approved,$id); $stmt->execute(); $stmt->close(); goAdmin('Testimonial updated.','success','testimonials'); } $stmt=$conn->prepare('INSERT INTO testimonials (name,relationship,testimonial,rating,approved) VALUES (?,?,?,?,?)'); $stmt->bind_param('sssii',$name,$rel,$text,$rating,$approved); $stmt->execute(); $stmt->close(); goAdmin('Testimonial created.','success','testimonials'); }
if($action==='delete_testimonial'){ $id=(int)($_POST['testimonial_id']??0); $stmt=$conn->prepare('DELETE FROM testimonials WHERE id=?'); $stmt->bind_param('i',$id); $stmt->execute(); $stmt->close(); goAdmin('Testimonial deleted.','success','testimonials'); } }
$message=$_GET['message']??''; $type=$_GET['type']??'success'; $section=$_GET['section']??'overview'; $q=trim($_GET['q']??''); $bookingFilter=$_GET['booking_status']??''; $userFilter=$_GET['user_filter']??''; $paymentFilter=$_GET['payment_status']??'';
$serviceEdit=null; if(isset($_GET['edit_service'])){ $id=(int)$_GET['edit_service']; $stmt=$conn->prepare('SELECT * FROM services WHERE id=?'); $stmt->bind_param('i',$id); $stmt->execute(); $serviceEdit=$stmt->get_result()->fetch_assoc(); $stmt->close(); $section='services'; }
$userEdit=null; if(isset($_GET['edit_user'])){ $id=(int)$_GET['edit_user']; $stmt=$conn->prepare('SELECT * FROM users WHERE id=?'); $stmt->bind_param('i',$id); $stmt->execute(); $userEdit=$stmt->get_result()->fetch_assoc(); $stmt->close(); $section='users'; }
$testimonialEdit=null; if(isset($_GET['edit_testimonial'])){ $id=(int)$_GET['edit_testimonial']; $stmt=$conn->prepare('SELECT * FROM testimonials WHERE id=?'); $stmt->bind_param('i',$id); $stmt->execute(); $testimonialEdit=$stmt->get_result()->fetch_assoc(); $stmt->close(); $section='testimonials'; }
$stats=[]; foreach(['users','bookings','services','contacts','admissions','payments','testimonials'] as $table){ $stats[$table]=(int)$conn->query("SELECT COUNT(*) AS total FROM {$table}")->fetch_assoc()['total']; } $stats['pending_contacts']=(int)$conn->query("SELECT COUNT(*) AS total FROM contacts WHERE inquiry_status IN ('new','reviewing')")->fetch_assoc()['total']; $stats['pending_admissions']=(int)$conn->query("SELECT COUNT(*) AS total FROM admissions WHERE inquiry_status IN ('new','reviewing')")->fetch_assoc()['total']; $stats['pending_payments']=(int)$conn->query("SELECT COUNT(*) AS total FROM payments WHERE status='pending'")->fetch_assoc()['total'];
$users=[]; $sql="SELECT id,name,email,phone,user_type,account_status,created_at FROM users WHERE 1=1"; $types=''; $params=[]; if($q!==''){ $wild="%$q%"; $sql.=" AND (name LIKE ? OR email LIKE ? OR phone LIKE ?)"; $types.='sss'; array_push($params,$wild,$wild,$wild); } if($userFilter!=='' && in_array($userFilter,$accountStatuses,true)){ $sql.=' AND account_status=?'; $types.='s'; $params[]=$userFilter; } $sql.=' ORDER BY created_at DESC'; $stmt=$conn->prepare($sql); if($params){ $stmt->bind_param($types,...$params); } $stmt->execute(); $res=$stmt->get_result(); while($row=$res->fetch_assoc()){ $users[]=$row; } $stmt->close();
$bookings=[]; $sql="SELECT b.id,b.booking_date,b.booking_time,b.status,b.notes,b.total_amount,b.created_at,u.name AS user_name,u.email AS user_email,s.name AS service_name FROM bookings b LEFT JOIN users u ON b.user_id=u.id LEFT JOIN services s ON b.service_id=s.id WHERE 1=1"; $types=''; $params=[]; if($q!==''){ $wild="%$q%"; $sql.=" AND (u.name LIKE ? OR u.email LIKE ? OR s.name LIKE ?)"; $types.='sss'; array_push($params,$wild,$wild,$wild);} if($bookingFilter!=='' && in_array($bookingFilter,$bookingStatuses,true)){ $sql.=' AND b.status=?'; $types.='s'; $params[]=$bookingFilter; } $sql.=' ORDER BY b.created_at DESC'; $stmt=$conn->prepare($sql); if($params){ $stmt->bind_param($types,...$params);} $stmt->execute(); $res=$stmt->get_result(); while($row=$res->fetch_assoc()){ $bookings[]=$row; } $stmt->close();
$payments=[]; $sql="SELECT p.id,p.booking_id,p.amount,p.payment_method,p.transaction_id,p.status,p.payment_date,u.name AS user_name FROM payments p LEFT JOIN users u ON p.user_id=u.id WHERE 1=1"; $types=''; $params=[]; if($paymentFilter!=='' && in_array($paymentFilter,$paymentStatuses,true)){ $sql.=' AND p.status=?'; $types.='s'; $params[]=$paymentFilter; } $sql.=' ORDER BY p.payment_date DESC'; $stmt=$conn->prepare($sql); if($params){ $stmt->bind_param($types,...$params); } $stmt->execute(); $res=$stmt->get_result(); while($row=$res->fetch_assoc()){ $payments[]=$row; } $stmt->close();
$services=[]; $res=$conn->query('SELECT * FROM services ORDER BY category,name'); while($row=$res->fetch_assoc()){ $services[]=$row; }
$contacts=[]; $res=$conn->query('SELECT * FROM contacts ORDER BY created_at DESC'); while($row=$res->fetch_assoc()){ $contacts[]=$row; }
$admissions=[]; $res=$conn->query('SELECT * FROM admissions ORDER BY created_at DESC'); while($row=$res->fetch_assoc()){ $admissions[]=$row; }
$testimonials=[]; $res=$conn->query('SELECT * FROM testimonials ORDER BY created_at DESC'); while($row=$res->fetch_assoc()){ $testimonials[]=$row; }
$conn->close();
?>
<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1.0'><title>Admin Panel - Oldage Home</title><link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css' rel='stylesheet'><link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css'><link href='https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap' rel='stylesheet'><link rel='stylesheet' href='css/style.css'><style>body.admin-body{background:#eff4f8}.admin-wrap{padding-top:110px;padding-bottom:50px}.admin-hero{background:linear-gradient(135deg,#133b5c,#1e5f74,#159895);color:#fff;border-radius:24px;padding:28px;box-shadow:0 20px 45px rgba(0,0,0,.12)}.admin-box{background:#fff;border-radius:22px;box-shadow:0 16px 40px rgba(31,51,73,.08)}.admin-box .card-header{background:transparent;border-bottom:1px solid rgba(0,0,0,.06)}.admin-stat{border-radius:20px;background:#fff;box-shadow:0 12px 35px rgba(31,51,73,.08)}.status-pill{display:inline-block;padding:.3rem .7rem;border-radius:999px;font-size:.78rem;font-weight:700}.anchor{scroll-margin-top:120px}textarea.form-control{min-height:100px}.table th{white-space:nowrap;font-size:.85rem;text-transform:uppercase}@media(max-width:991px){.admin-wrap{padding-top:90px}}</style></head><body class='admin-body'><?php include 'includes/header.php'; ?><div class='container admin-wrap'><div class='admin-hero mb-4'><div class='row g-3 align-items-center'><div class='col-lg-8'><span class='badge bg-light text-dark mb-3'>Full Admin Dashboard</span><h1 class='h2 fw-bold mb-3'>Manage operations, content, payments, and inquiries</h1><p class='mb-0'>This panel now includes user management, service CRUD, payment status updates, testimonial publishing, inquiry tracking, search, filters, and CSV export.</p></div><div class='col-lg-4'><form method='get' class='bg-white text-dark rounded-4 p-3'><label class='form-label fw-semibold'>Quick Search</label><input class='form-control mb-3' name='q' value='<?php echo htmlspecialchars($q); ?>' placeholder='Search users, bookings, services'><div class='d-grid gap-2'><button class='btn btn-primary'>Search</button><a href='admin.php' class='btn btn-outline-primary'>Reset</a></div></form></div></div></div><?php if($message!==''): ?><div class='alert alert-<?php echo htmlspecialchars($type); ?>'><?php echo htmlspecialchars($message); ?></div><?php endif; ?><div class='row g-3 mb-4'><div class='col-md-6 col-xl-3'><div class='p-4 admin-stat'><div class='text-muted'>Users</div><h3><?php echo $stats['users']; ?></h3><small><?php echo $stats['testimonials']; ?> testimonials in system</small></div></div><div class='col-md-6 col-xl-3'><div class='p-4 admin-stat'><div class='text-muted'>Bookings</div><h3><?php echo $stats['bookings']; ?></h3><small><?php echo $stats['pending_payments']; ?> pending payments</small></div></div><div class='col-md-6 col-xl-3'><div class='p-4 admin-stat'><div class='text-muted'>Contacts</div><h3><?php echo $stats['contacts']; ?></h3><small><?php echo $stats['pending_contacts']; ?> need follow-up</small></div></div><div class='col-md-6 col-xl-3'><div class='p-4 admin-stat'><div class='text-muted'>Admissions</div><h3><?php echo $stats['admissions']; ?></h3><small><?php echo $stats['pending_admissions']; ?> under review</small></div></div></div><div class='card admin-box mb-4 anchor' id='overview'><div class='card-header d-flex justify-content-between flex-wrap gap-2'><h3 class='h5 mb-0'>Exports</h3><div class='d-flex gap-2 flex-wrap'><a class='btn btn-sm btn-outline-primary' href='admin.php?export=users'>Users</a><a class='btn btn-sm btn-outline-primary' href='admin.php?export=bookings'>Bookings</a><a class='btn btn-sm btn-outline-primary' href='admin.php?export=payments'>Payments</a><a class='btn btn-sm btn-outline-primary' href='admin.php?export=contacts'>Contacts</a><a class='btn btn-sm btn-outline-primary' href='admin.php?export=admissions'>Admissions</a><a class='btn btn-sm btn-outline-primary' href='admin.php?export=services'>Services</a><a class='btn btn-sm btn-outline-primary' href='admin.php?export=testimonials'>Testimonials</a></div></div></div><div class='card admin-box mb-4 anchor' id='users'><div class='card-header'><h3 class='h5 mb-0'>User Management</h3></div><div class='card-body'><?php if($userEdit): ?><form method='post' class='row g-3 p-3 border rounded-4 mb-4 bg-light'><input type='hidden' name='action' value='update_user'><input type='hidden' name='user_id' value='<?php echo $userEdit['id']; ?>'><div class='col-md-6'><label class='form-label'>Name</label><input class='form-control' name='name' value='<?php echo htmlspecialchars($userEdit['name']); ?>' required></div><div class='col-md-6'><label class='form-label'>Email</label><input type='email' class='form-control' name='email' value='<?php echo htmlspecialchars($userEdit['email']); ?>' required></div><div class='col-md-4'><label class='form-label'>Phone</label><input class='form-control' name='phone' value='<?php echo htmlspecialchars($userEdit['phone']); ?>'></div><div class='col-md-4'><label class='form-label'>Role</label><select class='form-select' name='user_type'><?php foreach($userRoles as $role): ?><option value='<?php echo $role; ?>' <?php echo $userEdit['user_type']===$role?'selected':''; ?>><?php echo ucfirst($role); ?></option><?php endforeach; ?></select></div><div class='col-md-4'><label class='form-label'>Account Status</label><select class='form-select' name='account_status'><?php foreach($accountStatuses as $acc): ?><option value='<?php echo $acc; ?>' <?php echo (($userEdit['account_status'] ?? 'active')===$acc)?'selected':''; ?>><?php echo ucfirst($acc); ?></option><?php endforeach; ?></select></div><div class='col-12'><label class='form-label'>Reset Password</label><input class='form-control' name='new_password' placeholder='Leave blank to keep current password'></div><div class='col-12 d-flex gap-2'><button class='btn btn-primary'>Save User</button><a class='btn btn-outline-primary' href='admin.php?section=users#users'>Cancel</a></div></form><?php endif; ?><form method='get' class='row g-3 mb-3'><input type='hidden' name='section' value='users'><div class='col-md-8'><input class='form-control' name='q' value='<?php echo htmlspecialchars($q); ?>' placeholder='Search by name, email, phone'></div><div class='col-md-2'><select class='form-select' name='user_filter'><option value=''>All statuses</option><?php foreach($accountStatuses as $acc): ?><option value='<?php echo $acc; ?>' <?php echo $userFilter===$acc?'selected':''; ?>><?php echo ucfirst($acc); ?></option><?php endforeach; ?></select></div><div class='col-md-2 d-grid'><button class='btn btn-primary'>Filter</button></div></form><div class='table-responsive'><table class='table align-middle'><thead><tr><th>Name</th><th>Email</th><th>Phone</th><th>Role</th><th>Status</th><th>Joined</th><th>Action</th></tr></thead><tbody><?php foreach($users as $user): ?><tr><td><?php echo htmlspecialchars($user['name']); ?></td><td><?php echo htmlspecialchars($user['email']); ?></td><td><?php echo htmlspecialchars($user['phone'] ?: 'Not provided'); ?></td><td><span class='status-pill bg-<?php echo badgeClass($user['user_type']==='admin'?'approved':'new'); ?> text-white'><?php echo ucfirst($user['user_type']); ?></span></td><td><span class='status-pill bg-<?php echo badgeClass($user['account_status'] ?? 'active'); ?> text-white'><?php echo ucfirst($user['account_status'] ?? 'active'); ?></span></td><td><?php echo date('M j, Y', strtotime($user['created_at'])); ?></td><td><a class='btn btn-sm btn-outline-primary' href='admin.php?section=users&edit_user=<?php echo $user['id']; ?>#users'>Edit</a></td></tr><?php endforeach; ?></tbody></table></div></div></div>
<div class='card admin-box mb-4 anchor' id='bookings'><div class='card-header'><h3 class='h5 mb-0'>Booking Management</h3></div><div class='card-body'><form method='get' class='row g-3 mb-3'><input type='hidden' name='section' value='bookings'><div class='col-md-8'><input class='form-control' name='q' value='<?php echo htmlspecialchars($q); ?>' placeholder='Search user or service'></div><div class='col-md-2'><select class='form-select' name='booking_status'><option value=''>All statuses</option><?php foreach($bookingStatuses as $s): ?><option value='<?php echo $s; ?>' <?php echo $bookingFilter===$s?'selected':''; ?>><?php echo ucfirst($s); ?></option><?php endforeach; ?></select></div><div class='col-md-2 d-grid'><button class='btn btn-primary'>Filter</button></div></form><div class='table-responsive'><table class='table align-middle'><thead><tr><th>ID</th><th>User</th><th>Service</th><th>Schedule</th><th>Amount</th><th>Notes</th><th>Status</th><th>Update</th></tr></thead><tbody><?php foreach($bookings as $b): ?><tr><td>#<?php echo str_pad((string)$b['id'],6,'0',STR_PAD_LEFT); ?></td><td><?php echo htmlspecialchars($b['user_name'] ?: 'Unknown'); ?><br><small class='text-muted'><?php echo htmlspecialchars($b['user_email'] ?: ''); ?></small></td><td><?php echo htmlspecialchars($b['service_name'] ?: 'Unknown'); ?></td><td><?php echo date('M j, Y', strtotime($b['booking_date'])); ?><br><small class='text-muted'><?php echo date('g:i A', strtotime($b['booking_time'])); ?></small></td><td>$<?php echo number_format((float)$b['total_amount'],2); ?></td><td><?php echo htmlspecialchars($b['notes'] ?: 'None'); ?></td><td><span class='status-pill bg-<?php echo badgeClass($b['status']); ?> text-white'><?php echo ucfirst($b['status']); ?></span></td><td><form method='post' class='d-flex gap-2'><input type='hidden' name='action' value='update_booking_status'><input type='hidden' name='booking_id' value='<?php echo $b['id']; ?>'><select class='form-select form-select-sm' name='status'><?php foreach($bookingStatuses as $s): ?><option value='<?php echo $s; ?>' <?php echo $b['status']===$s?'selected':''; ?>><?php echo ucfirst($s); ?></option><?php endforeach; ?></select><button class='btn btn-sm btn-primary'>Save</button></form></td></tr><?php endforeach; ?></tbody></table></div></div></div><div class='card admin-box mb-4 anchor' id='payments'><div class='card-header'><h3 class='h5 mb-0'>Payment Management</h3></div><div class='card-body'><form method='get' class='row g-3 mb-3'><input type='hidden' name='section' value='payments'><div class='col-md-9'><select class='form-select' name='payment_status'><option value=''>All payment statuses</option><?php foreach($paymentStatuses as $s): ?><option value='<?php echo $s; ?>' <?php echo $paymentFilter===$s?'selected':''; ?>><?php echo ucfirst($s); ?></option><?php endforeach; ?></select></div><div class='col-md-3 d-grid'><button class='btn btn-primary'>Filter Payments</button></div></form><div class='table-responsive'><table class='table align-middle'><thead><tr><th>Payment</th><th>Booking</th><th>User</th><th>Method</th><th>Transaction</th><th>Amount</th><th>Status</th><th>Update</th></tr></thead><tbody><?php foreach($payments as $p): ?><tr><td>#<?php echo $p['id']; ?></td><td>#<?php echo $p['booking_id']; ?></td><td><?php echo htmlspecialchars($p['user_name'] ?: 'Unknown'); ?></td><td><?php echo htmlspecialchars($p['payment_method'] ?: 'N/A'); ?></td><td><?php echo htmlspecialchars($p['transaction_id'] ?: 'N/A'); ?></td><td>$<?php echo number_format((float)$p['amount'],2); ?></td><td><span class='status-pill bg-<?php echo badgeClass($p['status']); ?> text-white'><?php echo ucfirst($p['status']); ?></span></td><td><form method='post' class='d-flex gap-2'><input type='hidden' name='action' value='update_payment_status'><input type='hidden' name='payment_id' value='<?php echo $p['id']; ?>'><select class='form-select form-select-sm' name='status'><?php foreach($paymentStatuses as $s): ?><option value='<?php echo $s; ?>' <?php echo $p['status']===$s?'selected':''; ?>><?php echo ucfirst($s); ?></option><?php endforeach; ?></select><button class='btn btn-sm btn-primary'>Save</button></form></td></tr><?php endforeach; ?></tbody></table></div></div></div><div class='card admin-box mb-4 anchor' id='services'><div class='card-header'><h3 class='h5 mb-0'>Service Management</h3></div><div class='card-body'><form method='post' class='row g-3 p-3 border rounded-4 mb-4 bg-light'><input type='hidden' name='action' value='save_service'><input type='hidden' name='service_id' value='<?php echo $serviceEdit['id'] ?? 0; ?>'><div class='col-md-6'><label class='form-label'>Service Name</label><input class='form-control' name='name' value='<?php echo htmlspecialchars($serviceEdit['name'] ?? ''); ?>' required></div><div class='col-md-6'><label class='form-label'>Category</label><input class='form-control' name='category' value='<?php echo htmlspecialchars($serviceEdit['category'] ?? ''); ?>' required></div><div class='col-md-4'><label class='form-label'>Price</label><input type='number' step='0.01' class='form-control' name='price' value='<?php echo htmlspecialchars((string)($serviceEdit['price'] ?? '')); ?>' required></div><div class='col-md-4'><label class='form-label'>Duration</label><input class='form-control' name='duration' value='<?php echo htmlspecialchars($serviceEdit['duration'] ?? ''); ?>' required></div><div class='col-md-4'><label class='form-label'>Image Filename</label><input class='form-control' name='image' value='<?php echo htmlspecialchars($serviceEdit['image'] ?? ''); ?>'></div><div class='col-12'><label class='form-label'>Description</label><textarea class='form-control' name='description'><?php echo htmlspecialchars($serviceEdit['description'] ?? ''); ?></textarea></div><div class='col-12'><div class='form-check'><input class='form-check-input' type='checkbox' name='active' id='activeService' <?php echo !isset($serviceEdit['active']) || (int)$serviceEdit['active']===1 ? 'checked' : ''; ?>><label class='form-check-label' for='activeService'>Visible on website</label></div></div><div class='col-12 d-flex gap-2'><button class='btn btn-primary'><?php echo $serviceEdit ? 'Update Service' : 'Create Service'; ?></button><?php if($serviceEdit): ?><a class='btn btn-outline-primary' href='admin.php?section=services#services'>Cancel</a><?php endif; ?></div></form><div class='table-responsive'><table class='table align-middle'><thead><tr><th>Service</th><th>Category</th><th>Price</th><th>Duration</th><th>Status</th><th>Actions</th></tr></thead><tbody><?php foreach($services as $s): ?><tr><td><?php echo htmlspecialchars($s['name']); ?><br><small class='text-muted'><?php echo htmlspecialchars($s['description']); ?></small></td><td><?php echo htmlspecialchars($s['category']); ?></td><td>$<?php echo number_format((float)$s['price'],2); ?></td><td><?php echo htmlspecialchars($s['duration']); ?></td><td><span class='status-pill bg-<?php echo badgeClass((int)$s['active']===1?'active':'inactive'); ?> text-white'><?php echo (int)$s['active']===1?'Visible':'Hidden'; ?></span></td><td class='d-flex gap-2'><a class='btn btn-sm btn-outline-primary' href='admin.php?section=services&edit_service=<?php echo $s['id']; ?>#services'>Edit</a><form method='post' onsubmit="return confirm('Delete this service?');"><input type='hidden' name='action' value='delete_service'><input type='hidden' name='service_id' value='<?php echo $s['id']; ?>'><button class='btn btn-sm btn-outline-danger'>Delete</button></form></td></tr><?php endforeach; ?></tbody></table></div></div></div>
<div class='card admin-box mb-4 anchor' id='contacts'><div class='card-header'><h3 class='h5 mb-0'>Contact Inquiries</h3></div><div class='card-body'><?php foreach($contacts as $c): ?><div class='p-3 border rounded-4 mb-3 bg-light'><div class='d-flex justify-content-between gap-2 flex-wrap mb-2'><div><strong><?php echo htmlspecialchars($c['name']); ?></strong><div class='text-muted small'><?php echo htmlspecialchars($c['email']); ?><?php echo $c['phone'] ? ' | '.htmlspecialchars($c['phone']) : ''; ?></div><div><strong>Subject:</strong> <?php echo htmlspecialchars($c['subject']); ?></div></div><span class='status-pill bg-<?php echo badgeClass($c['inquiry_status'] ?? 'new'); ?> text-white'><?php echo ucfirst($c['inquiry_status'] ?? 'new'); ?></span></div><p><?php echo nl2br(htmlspecialchars($c['message'])); ?></p><form method='post' class='row g-3'><input type='hidden' name='action' value='update_contact'><input type='hidden' name='contact_id' value='<?php echo $c['id']; ?>'><div class='col-md-3'><label class='form-label'>Status</label><select class='form-select' name='inquiry_status'><?php foreach($inquiryStatuses as $s): ?><option value='<?php echo $s; ?>' <?php echo (($c['inquiry_status'] ?? 'new')===$s)?'selected':''; ?>><?php echo ucfirst($s); ?></option><?php endforeach; ?></select></div><div class='col-md-9'><label class='form-label'>Admin Notes</label><textarea class='form-control' name='admin_notes'><?php echo htmlspecialchars($c['admin_notes'] ?? ''); ?></textarea></div><div class='col-12 d-flex justify-content-between align-items-center'><small class='text-muted'>Received <?php echo date('M j, Y g:i A', strtotime($c['created_at'])); ?></small><button class='btn btn-primary'>Save</button></div></form></div><?php endforeach; ?></div></div><div class='card admin-box mb-4 anchor' id='admissions'><div class='card-header'><h3 class='h5 mb-0'>Admission Inquiries</h3></div><div class='card-body'><?php foreach($admissions as $a): ?><div class='p-3 border rounded-4 mb-3 bg-light'><div class='d-flex justify-content-between gap-2 flex-wrap mb-2'><div><strong><?php echo htmlspecialchars($a['resident_name']); ?></strong><div class='text-muted small'>Contact: <?php echo htmlspecialchars($a['contact_name']); ?> (<?php echo htmlspecialchars($a['contact_relationship']); ?>)</div><div class='small'><?php echo htmlspecialchars($a['contact_email']); ?> | <?php echo htmlspecialchars($a['contact_phone']); ?></div></div><span class='status-pill bg-<?php echo badgeClass($a['inquiry_status'] ?? 'new'); ?> text-white'><?php echo ucfirst($a['inquiry_status'] ?? 'new'); ?></span></div><p class='mb-2'><strong>DOB:</strong> <?php echo date('M j, Y', strtotime($a['resident_dob'])); ?> | <strong>Move Date:</strong> <?php echo $a['preferred_move_date'] ? date('M j, Y', strtotime($a['preferred_move_date'])) : 'Not provided'; ?> | <strong>Care Level:</strong> <?php echo htmlspecialchars($a['care_level'] ?: 'Not specified'); ?></p><p><strong>Special Needs:</strong> <?php echo htmlspecialchars($a['special_needs'] ?: 'None'); ?></p><form method='post' class='row g-3'><input type='hidden' name='action' value='update_admission'><input type='hidden' name='admission_id' value='<?php echo $a['id']; ?>'><div class='col-md-3'><label class='form-label'>Status</label><select class='form-select' name='inquiry_status'><?php foreach($inquiryStatuses as $s): ?><option value='<?php echo $s; ?>' <?php echo (($a['inquiry_status'] ?? 'new')===$s)?'selected':''; ?>><?php echo ucfirst($s); ?></option><?php endforeach; ?></select></div><div class='col-md-9'><label class='form-label'>Admin Notes</label><textarea class='form-control' name='admin_notes'><?php echo htmlspecialchars($a['admin_notes'] ?? ''); ?></textarea></div><div class='col-12 d-flex justify-content-between align-items-center'><small class='text-muted'>Received <?php echo date('M j, Y g:i A', strtotime($a['created_at'])); ?></small><button class='btn btn-primary'>Save</button></div></form></div><?php endforeach; ?></div></div><div class='card admin-box mb-4 anchor' id='testimonials'><div class='card-header'><h3 class='h5 mb-0'>Testimonial Management</h3></div><div class='card-body'><form method='post' class='row g-3 p-3 border rounded-4 mb-4 bg-light'><input type='hidden' name='action' value='save_testimonial'><input type='hidden' name='testimonial_id' value='<?php echo $testimonialEdit['id'] ?? 0; ?>'><div class='col-md-6'><label class='form-label'>Name</label><input class='form-control' name='name' value='<?php echo htmlspecialchars($testimonialEdit['name'] ?? ''); ?>' required></div><div class='col-md-6'><label class='form-label'>Relationship</label><input class='form-control' name='relationship' value='<?php echo htmlspecialchars($testimonialEdit['relationship'] ?? ''); ?>'></div><div class='col-md-3'><label class='form-label'>Rating</label><select class='form-select' name='rating'><?php for($i=5;$i>=1;$i--): ?><option value='<?php echo $i; ?>' <?php echo (int)($testimonialEdit['rating'] ?? 5)===$i?'selected':''; ?>><?php echo $i; ?> Star<?php echo $i>1?'s':''; ?></option><?php endfor; ?></select></div><div class='col-md-9 d-flex align-items-end'><div class='form-check'><input class='form-check-input' type='checkbox' name='approved' id='approvedTestimonial' <?php echo !isset($testimonialEdit['approved']) || (int)$testimonialEdit['approved']===1 ? 'checked' : ''; ?>><label class='form-check-label' for='approvedTestimonial'>Approved for public display</label></div></div><div class='col-12'><label class='form-label'>Testimonial</label><textarea class='form-control' name='testimonial' required><?php echo htmlspecialchars($testimonialEdit['testimonial'] ?? ''); ?></textarea></div><div class='col-12 d-flex gap-2'><button class='btn btn-primary'><?php echo $testimonialEdit ? 'Update Testimonial' : 'Create Testimonial'; ?></button><?php if($testimonialEdit): ?><a class='btn btn-outline-primary' href='admin.php?section=testimonials#testimonials'>Cancel</a><?php endif; ?></div></form><div class='table-responsive'><table class='table align-middle'><thead><tr><th>Name</th><th>Relationship</th><th>Rating</th><th>Approval</th><th>Content</th><th>Actions</th></tr></thead><tbody><?php foreach($testimonials as $t): ?><tr><td><?php echo htmlspecialchars($t['name']); ?></td><td><?php echo htmlspecialchars($t['relationship']); ?></td><td><?php echo str_repeat('?',(int)$t['rating']); ?></td><td><span class='status-pill bg-<?php echo badgeClass((int)($t['approved'] ?? 1)===1?'approved':'inactive'); ?> text-white'><?php echo (int)($t['approved'] ?? 1)===1?'Approved':'Hidden'; ?></span></td><td><?php echo htmlspecialchars($t['testimonial']); ?></td><td class='d-flex gap-2'><a class='btn btn-sm btn-outline-primary' href='admin.php?section=testimonials&edit_testimonial=<?php echo $t['id']; ?>#testimonials'>Edit</a><form method='post' onsubmit="return confirm('Delete this testimonial?');"><input type='hidden' name='action' value='delete_testimonial'><input type='hidden' name='testimonial_id' value='<?php echo $t['id']; ?>'><button class='btn btn-sm btn-outline-danger'>Delete</button></form></td></tr><?php endforeach; ?></tbody></table></div></div></div></div><?php include 'includes/footer.php'; ?><script src='https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js'></script><script src='js/main.js'></script></body></html>

