<?php
session_start();

if (!isset($_SESSION['user_id'])) {
    header("Location: /beauty_pos/login.php");
    exit;
}

include_once "../config/database.php";

$success = "";
$error   = "";

$search = isset($_GET['search']) ? trim(mysqli_real_escape_string($conn, $_GET['search'])) : "";

$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$page = ($page <= 0) ? 1 : $page;

$limit  = 10;
$offset = ($page - 1) * $limit;

$where = "";

if ($search != "") {
    $where = " WHERE
        c.customer_name LIKE '%$search%'
        OR e.employee_name LIKE '%$search%'
        OR a.service_name LIKE '%$search%'
        OR a.status LIKE '%$search%'";
}

$countQuery = "
SELECT COUNT(a.id) AS total
FROM appointments a
LEFT JOIN customers c ON a.customer_id=c.id
LEFT JOIN employees e ON a.employee_id=e.id
$where
";

$countResult = mysqli_query($conn, $countQuery);
$countRow = mysqli_fetch_assoc($countResult);

$totalRecords = $countRow['total'];
$totalPages = ceil($totalRecords / $limit);

$query = "
SELECT
a.*,
c.customer_name,
c.phone,
e.employee_name

FROM appointments a

LEFT JOIN customers c
ON a.customer_id=c.id

LEFT JOIN employees e
ON a.employee_id=e.id

$where

ORDER BY a.appointment_date ASC

LIMIT $offset,$limit
";

$result = mysqli_query($conn,$query);

$todayQuery = "
SELECT
a.*,
c.customer_name,
e.employee_name

FROM appointments a

LEFT JOIN customers c
ON c.id=a.customer_id

LEFT JOIN employees e
ON e.id=a.employee_id

WHERE DATE(a.appointment_date)=CURDATE()

ORDER BY a.appointment_date ASC
";

$todayResult = mysqli_query($conn,$todayQuery);

include_once "../includes/header.php";
?>

<?php include_once "../includes/sidebar.php"; ?>

<div class="overlay" id="overlay"></div>

<div class="main-content">

<?php include_once "../includes/topbar.php"; ?>

<div class="container-fluid py-4">

<div class="row mb-4">

<div class="col-12">

<div class="d-flex justify-content-between align-items-center flex-wrap gap-2">

<h3 class="fw-bold mb-0">
Appointments
</h3>

<a href="/beauty_pos/appointments/add.php"
class="btn btn-primary rounded-pill px-4">
<i class="fa fa-plus me-2"></i>Book Appointment
</a>

</div>

</div>

</div>

<div class="row g-4">

<div class="col-xl-6 col-lg-6 col-12">

<div class="card rounded-4 shadow border-0 h-100">

<div class="card-header bg-white border-0 d-flex justify-content-between align-items-center">

<h5 class="fw-bold mb-0">
<i class="fa fa-calendar-day text-danger me-2"></i>
Today's Appointments
</h5>

</div>

<div class="card-body">

<?php if(mysqli_num_rows($todayResult)>0){ ?>

<div class="table-responsive">

<table class="table align-middle">

<thead>

<tr>

<th>Time</th>

<th>Customer</th>

<th>Service</th>

<th>Staff</th>

<th>Status</th>

</tr>

</thead>

<tbody>

<?php while($today=mysqli_fetch_assoc($todayResult)){ ?>

<tr>

<td>

<?php echo date("h:i A",strtotime($today['appointment_date'])); ?>

</td>

<td>

<?php echo htmlspecialchars($today['customer_name']); ?>

</td>

<td>

<?php echo htmlspecialchars($today['service_name']); ?>

</td>

<td>

<?php echo htmlspecialchars($today['employee_name']); ?>

</td>

<td>

<?php

$status=$today['status'];

if($status=="Completed")
{
echo '<span class="badge bg-success">Completed</span>';
}
elseif($status=="Cancelled")
{
echo '<span class="badge bg-danger">Cancelled</span>';
}
else
{
echo '<span class="badge bg-warning text-dark">Pending</span>';
}

?>

</td>

</tr>

<?php } ?>

</tbody>

</table>

</div>

<?php } else { ?>

<div class="text-center py-5">

<i class="fa fa-calendar-xmark fa-4x text-secondary mb-3"></i>

<h5 class="text-muted">
No upcoming appointments
</h5>

</div>

<?php } ?>

</div>

</div>

</div>

<div class="col-xl-6 col-lg-6 col-12">

<div class="card rounded-4 shadow border-0 h-100">

<div class="card-header bg-white border-0">

<h5 class="fw-bold mb-0">

All Appointments

</h5>

</div>

<div class="card-body">

<form method="GET">

<div class="row g-2 mb-3">

<div class="col-lg-9 col-md-8 col-12">

<input
type="text"
name="search"
class="form-control"
placeholder="Search customer, staff, service..."
value="<?php echo htmlspecialchars($search); ?>">

</div>

<div class="col-lg-3 col-md-4 col-12">

<button class="btn btn-dark w-100">

Search

</button>

</div>

</div>

</form>

<div class="table-responsive">

<table class="table table-hover align-middle">

<thead>

<tr>

<th>Client</th>

<th>Service</th>

<th>Date</th>

<th>Staff</th>

<th>Price</th>

<th>Status</th>

<th width="120">

Action

</th>

</tr>

</thead>

<tbody>

<?php
if(mysqli_num_rows($result)>0){
while($row=mysqli_fetch_assoc($result)){
?>
<tr>

<td>

<div class="fw-semibold">
<?php echo htmlspecialchars($row['customer_name']); ?>
</div>

<?php if(!empty($row['phone'])){ ?>

<small class="text-muted">
<?php echo htmlspecialchars($row['phone']); ?>
</small>

<?php } ?>

</td>

<td>

<?php echo htmlspecialchars($row['service_name']); ?>

</td>

<td>

<div>

<?php echo date("d M Y",strtotime($row['appointment_date'])); ?>

</div>

<small class="text-muted">

<?php echo date("h:i A",strtotime($row['appointment_date'])); ?>

</small>

</td>

<td>

<?php echo htmlspecialchars($row['employee_name']); ?>

</td>

<td>

₹ <?php echo number_format($row['price'],2); ?>

</td>

<td>

<?php

if($row['status']=="Completed")
{
echo '<span class="badge bg-success">Completed</span>';
}
elseif($row['status']=="Cancelled")
{
echo '<span class="badge bg-danger">Cancelled</span>';
}
else
{
echo '<span class="badge bg-warning text-dark">Pending</span>';
}

?>

</td>

<td>

<div class="btn-group btn-group-sm">

<a
href="/beauty_pos/appointments/edit.php?id=<?php echo $row['id'];?>"
class="btn btn-primary">

<i class="fa fa-pen"></i>

</a>

<a
href="/beauty_pos/appointments/delete.php?id=<?php echo $row['id'];?>"
class="btn btn-danger"
onclick="return confirm('Delete this appointment?')">

<i class="fa fa-trash"></i>

</a>

</div>

</td>

</tr>

<?php

}

}else{

?>

<tr>

<td colspan="7" class="text-center py-5">

<i class="fa fa-calendar-xmark fa-3x text-secondary mb-3"></i>

<div class="mt-3">

No appointments found.

</div>

</td>

</tr>

<?php

}

?>

</tbody>

</table>

</div>

<?php

if($totalPages>1){

?>

<nav class="mt-4">

<ul class="pagination justify-content-end flex-wrap">

<?php

if($page>1){

?>

<li class="page-item">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo ($page-1); ?>">

Previous

</a>

</li>

<?php

}

?>

<?php

for($i=1;$i<=$totalPages;$i++){

?>

<li class="page-item <?php if($page==$i) echo 'active'; ?>">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo $i; ?>">

<?php echo $i; ?>

</a>

</li>

<?php

}

?>

<?php

if($page<$totalPages){

?>

<li class="page-item">

<a
class="page-link"
href="?search=<?php echo urlencode($search); ?>&page=<?php echo ($page+1); ?>">

Next

</a>

</li>

<?php

}

?>

</ul>

</nav>

<?php

}

?>

</div>

</div>

</div>

</div>
</div>

<?php include_once "../includes/footer.php"; ?>

</div>