<?php
session_start();

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

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

if(!isset($_GET['id']) || !is_numeric($_GET['id'])){
    header("Location: /beauty_pos/appointments/index.php");
    exit;
}

$id=(int)$_GET['id'];

$get=mysqli_query($conn,"
SELECT *
FROM appointments
WHERE id='$id'
LIMIT 1
");

if(mysqli_num_rows($get)==0){
    header("Location: /beauty_pos/appointments/index.php");
    exit;
}

$data=mysqli_fetch_assoc($get);

$error="";

$customers=mysqli_query($conn,"
SELECT id,customer_name
FROM customers
ORDER BY customer_name ASC
");

$employees=mysqli_query($conn,"
SELECT id,employee_name
FROM employees
WHERE status='Active'
ORDER BY employee_name ASC
");

if(isset($_POST['update'])){

    $customer_id=(int)$_POST['customer_id'];
    $employee_id=(int)$_POST['employee_id'];

    $service_name=trim(mysqli_real_escape_string($conn,$_POST['service_name']));

    $appointment_date=$_POST['appointment_date'];

    $price=$_POST['price'];

    $status=mysqli_real_escape_string($conn,$_POST['status']);

    if(
        empty($customer_id) ||
        empty($employee_id) ||
        empty($service_name) ||
        empty($appointment_date) ||
        $price==""
    ){

        $error="Please fill all required fields.";

    }else{

        $check=mysqli_query($conn,"
        SELECT id
        FROM appointments
        WHERE employee_id='$employee_id'
        AND appointment_date='$appointment_date'
        AND id<>'$id'
        LIMIT 1
        ");

        if(mysqli_num_rows($check)>0){

            $error="Selected employee already has an appointment at this time.";

        }else{

            $update=mysqli_query($conn,"
            UPDATE appointments SET

            customer_id='$customer_id',
            employee_id='$employee_id',
            service_name='$service_name',
            appointment_date='$appointment_date',
            price='$price',
            status='$status'

            WHERE id='$id'
            ");

            if($update){

                $_SESSION['success']="Appointment updated successfully.";

                header("Location: /beauty_pos/appointments/index.php");
                exit;

            }else{

                $error="Unable to update appointment.";

            }

        }

    }

    $get=mysqli_query($conn,"
    SELECT *
    FROM appointments
    WHERE id='$id'
    LIMIT 1
    ");

    $data=mysqli_fetch_assoc($get);

}

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 d-flex justify-content-between align-items-center">

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

Edit Appointment

</h3>

<a
href="/beauty_pos/appointments/index.php"
class="btn btn-secondary rounded-pill">

<i class="fa fa-arrow-left me-2"></i>

Back

</a>

</div>

</div>

<?php if($error!=""){ ?>

<div class="alert alert-danger">

<?php echo $error; ?>

</div>

<?php } ?>

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

<div class="card-body">

<form method="POST">

<div class="row g-3">
    <div class="col-lg-6 col-md-6 col-12">

<label class="form-label">

Customer <span class="text-danger">*</span>

</label>

<select
name="customer_id"
class="form-select"
required>

<option value="">

Select Customer

</option>

<?php
mysqli_data_seek($customers,0);

while($customer=mysqli_fetch_assoc($customers)){
?>

<option
value="<?php echo $customer['id'];?>"
<?php if($data['customer_id']==$customer['id']) echo "selected"; ?>>

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

</option>

<?php } ?>

</select>

</div>

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

<label class="form-label">

Employee <span class="text-danger">*</span>

</label>

<select
name="employee_id"
class="form-select"
required>

<option value="">

Select Employee

</option>

<?php
mysqli_data_seek($employees,0);

while($employee=mysqli_fetch_assoc($employees)){
?>

<option
value="<?php echo $employee['id'];?>"
<?php if($data['employee_id']==$employee['id']) echo "selected"; ?>>

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

</option>

<?php } ?>

</select>

</div>

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

<label class="form-label">

Service Name <span class="text-danger">*</span>

</label>

<input
type="text"
name="service_name"
class="form-control"
required
value="<?php echo htmlspecialchars($data['service_name']);?>">

</div>

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

<label class="form-label">

Appointment Date & Time <span class="text-danger">*</span>

</label>

<input
type="datetime-local"
name="appointment_date"
class="form-control"
required
value="<?php echo date('Y-m-d\TH:i',strtotime($data['appointment_date']));?>">

</div>

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

<label class="form-label">

Price <span class="text-danger">*</span>

</label>

<input
type="number"
step="0.01"
min="0"
name="price"
class="form-control"
required
value="<?php echo $data['price'];?>">

</div>

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

<label class="form-label">

Status

</label>

<select
name="status"
class="form-select">

<option
value="Pending"
<?php if($data['status']=="Pending") echo "selected";?>>

Pending

</option>

<option
value="Completed"
<?php if($data['status']=="Completed") echo "selected";?>>

Completed

</option>

<option
value="Cancelled"
<?php if($data['status']=="Cancelled") echo "selected";?>>

Cancelled

</option>

</select>

</div>

<div class="col-lg-4 col-md-12 col-12 d-flex align-items-end">

<div class="d-grid w-100">

<button
type="submit"
name="update"
class="btn btn-primary rounded-pill">

<i class="fa fa-save me-2"></i>

Update Appointment

</button>

</div>

</div>

</div>

</form>

</div>

</div>
</div>

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

</div>