<?php

header('Content-Type: application/json');

require_once 'db.php';

$data = json_decode(file_get_contents("php://input"), true);

$name      = $data['name'] ?? '';
$phone     = $data['phone'] ?? '';
$salon     = $data['salon'] ?? '';
$services  = $data['services'] ?? '';
$date      = $data['date'] ?? '';
$time      = $data['time'] ?? '';
$notes     = $data['notes'] ?? '';
$signature = $data['signature'] ?? '';

$stmt = $conn->prepare("
INSERT INTO appointment_requests
(
 customer_name,
 phone,
 salon,
 services,
 preferred_date,
 preferred_time,
 notes,
 signature
)
VALUES
(?,?,?,?,?,?,?,?)
");

$stmt->bind_param(
"ssssssss",
$name,
$phone,
$salon,
$services,
$date,
$time,
$notes,
$signature
);

$stmt->execute();

$to = "galhotra_pooja@yahoo.com";

$subject = "New Appointment Booking - Maharaani Salon";

$message = "
New Appointment Request

Name: $name

Phone: $phone

Salon: $salon

Services:
$services

Preferred Date:
$date

Preferred Time:
$time

Notes:
$notes
";

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
$headers .= "From: bookings@maharaani.com.au\r\n";

mail($to, $subject, $message, $headers);

echo json_encode([
    "success" => true
]);