function save_booking(){

    global $wpdb;

    // SECURITY
    if (
        !isset($_POST['nonce']) ||
        !wp_verify_nonce($_POST['nonce'], 'apricorn_nonce')
    ) {
        wp_send_json_error(['msg' => 'Security check failed']);
    }

    // INPUT
    $name  = sanitize_text_field($_POST['name'] ?? '');
    $email = sanitize_email($_POST['email'] ?? '');
    $phone = sanitize_text_field($_POST['phone'] ?? '');
    $date  = sanitize_text_field($_POST['date'] ?? '');

    if (!is_email($email)) {
        wp_send_json_error(['msg' => 'Invalid email']);
    }

    if (empty($date) || !strtotime($date)) {
        wp_send_json_error(['msg' => 'Invalid date']);
    }

    // JSON
    $tickets = json_decode($_POST['tickets'] ?? '', true);

    if (json_last_error() !== JSON_ERROR_NONE || !is_array($tickets)) {
        wp_send_json_error(['msg' => 'Invalid tickets format']);
    }

    if (empty($tickets)) {
        wp_send_json_error(['msg' => 'No tickets selected']);
    }

    // TOTAL
    $total = 0;

    foreach ($tickets as $key => $qty) {

        $qty = intval($qty);
        if ($qty <= 0) continue;

        if (strpos($key,'ticket_') !== false) {

            $id = intval(str_replace('ticket_','',$key));

            $row = $wpdb->get_row(
                $wpdb->prepare(
                    "SELECT price_weekday, price_weekend FROM {$wpdb->prefix}ticket_types WHERE id=%d",
                    $id
                )
            );

            if (!$row) continue;

            $day = date('w', strtotime($date));
            $price = ($day == 0 || $day == 6)
                ? $row->price_weekend
                : $row->price_weekday;

            $total += $price * $qty;
        }
    }

    // COUPON
    $discount = 0;
    $coupon_id = null;

    if (!empty($_POST['coupon'])) {

        require_once APRICORN_PATH.'includes/coupon-functions.php';

        $validation = apricorn_validate_coupon(
            sanitize_text_field($_POST['coupon']),
            get_current_user_id(),
            $total
        );

        if ($validation['status']) {

            $coupon = $validation['coupon'];
            $coupon_id = $coupon->id;

            $discount = apricorn_calculate_discount($coupon, $total);
            $discount = min($discount, $total);

            $total -= $discount;
        }
    }

    // SAVE
    $inserted = $wpdb->insert($wpdb->prefix.'bookings', [
        'name' => $name,
        'email' => $email,
        'phone' => $phone,
        'date' => $date,
        'tickets' => wp_json_encode($tickets),
        'amount' => $total,
        'created_at' => current_time('mysql')
    ]);

    if (!$inserted) {
        wp_send_json_error(['msg' => 'Booking insert failed']);
    }

    wp_send_json_success([
        'msg' => 'Booking saved successfully',
        'booking_id' => $wpdb->insert_id,
        'total' => $total,
        'discount' => $discount
    ]);
}<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://thehillswaterpark.in/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://thehillswaterpark.in/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://thehillswaterpark.in/wp-sitemap-posts-post-2.xml</loc></sitemap><sitemap><loc>https://thehillswaterpark.in/wp-sitemap-posts-post-3.xml</loc></sitemap><sitemap><loc>https://thehillswaterpark.in/wp-sitemap-posts-post-4.xml</loc></sitemap><sitemap><loc>https://thehillswaterpark.in/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://thehillswaterpark.in/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://thehillswaterpark.in/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
