.elementor-2655 .elementor-element.elementor-element-9222f80{--display:flex;--flex-direction:column;--container-widget-width:100%;--container-widget-height:initial;--container-widget-flex-grow:0;--container-widget-align-self:initial;--flex-wrap-mobile:wrap;}:root{--page-title-display:none;}/* Start custom CSS for html, class: .elementor-element-81ad20c */<?php
/**
 * Clinic Appointment Form — AJAX Email Handler
 * -----------------------------------------------
 * Add this to your child theme's functions.php
 * OR paste into Code Snippets plugin (PHP type)
 * -----------------------------------------------
 * IMPORTANT: Change $clinic_email to your real clinic email address
 */

add_action( 'wp_ajax_clinic_appointment',        'clinic_appt_handle' );
add_action( 'wp_ajax_nopriv_clinic_appointment', 'clinic_appt_handle' );

function clinic_appt_handle() {

    // Basic sanitization
    $doctor = sanitize_text_field( $_POST['doctor'] ?? '' );
    $day    = sanitize_text_field( $_POST['day']    ?? '' );
    $time   = sanitize_text_field( $_POST['time']   ?? '' );
    $fname  = sanitize_text_field( $_POST['fname']  ?? '' );
    $lname  = sanitize_text_field( $_POST['lname']  ?? '' );
    $phone  = sanitize_text_field( $_POST['phone']  ?? '' );
    $email  = sanitize_email(      $_POST['email']  ?? '' );

    // Required field check
    if ( ! $doctor || ! $day || ! $time || ! $fname || ! $lname || ! $phone ) {
        wp_send_json_error( 'Missing required fields.' );
    }

    // ── YOUR CLINIC EMAIL HERE ──────────────────────────────
    $clinic_email = 'clinic@yourdomain.com';
    // ───────────────────────────────────────────────────────

    $subject = "New Appointment Request – {$doctor}";

    $body  = "A new appointment has been requested via the website.\n\n";
    $body .= "Doctor:   {$doctor}\n";
    $body .= "Day:      {$day}\n";
    $body .= "Time:     {$time}\n\n";
    $body .= "Patient:  {$fname} {$lname}\n";
    $body .= "Phone:    {$phone}\n";
    $body .= "Email:    {$email}\n";

    $headers = array( 'Content-Type: text/plain; charset=UTF-8' );
    if ( $email ) {
        $headers[] = "Reply-To: {$fname} {$lname} <{$email}>";
    }

    $sent = wp_mail( $clinic_email, $subject, $body, $headers );

    // Patient confirmation (only if email provided)
    if ( $sent && $email ) {
        $p_subject = 'Your Appointment Request – Confirmation';
        $p_body    = "Dear {$fname},\n\n";
        $p_body   .= "Thank you for booking with us. We have received your appointment request and will confirm shortly via WhatsApp.\n\n";
        $p_body   .= "Your Request Details:\n";
        $p_body   .= "Doctor: {$doctor}\n";
        $p_body   .= "Day:    {$day}\n";
        $p_body   .= "Time:   {$time}\n\n";
        $p_body   .= "If you have any questions, please call or WhatsApp us.\n\n";
        $p_body   .= "Warm regards,\nThe Clinic Team";

        wp_mail( $email, $p_subject, $p_body );
    }

    if ( $sent ) {
        wp_send_json_success( 'Appointment sent.' );
    } else {
        wp_send_json_error( 'Mail failed.' );
    }
}

// Expose ajaxUrl + nonce to the page
add_action( 'wp_footer', 'clinic_appt_localize' );
function clinic_appt_localize() {
    echo '<script>window.clinicAppt = { ajaxUrl: "' . esc_url( admin_url('admin-ajax.php') ) . '", nonce: "" };</script>';
}/* End custom CSS */