<?php
    // When the page loads fetch data from database

    if(isset($_POST['mt_submit_booking'])){
        global $wpdb;
        $mt_settings_data_table = $wpdb->prefix . 'mt_settings';
        $mt_settings_data_fetch = $wpdb->get_row( "SELECT * FROM $mt_settings_data_table ORDER BY id DESC LIMIT 1" );
        $admin_mail_subject = $mt_settings_data_fetch->admin_mail_subject;
        $admin_mail_body = $mt_settings_data_fetch->admin_mail_body;
        $customer_mail_subject = $mt_settings_data_fetch->customer_mail_pending_subject;
        $customer_mail_body = $mt_settings_data_fetch->customer_mail_pending_body;
        $mt_plugin_path_from = plugin_dir_url(__FILE__);
        $mt_thanks_img = $mt_plugin_path_from . 'assets/img/mt-thanks.svg';

        $service_id = $_POST['mt_selected_service_id'];
        $service_name = $_POST['mt_selected_service'];
        $service_title = $_POST['mt_selected_service_title'];
        $service_location = $_POST['mt_selected_service_location'];
        $service_price = $_POST['mt_selected_service_price'];
        $booked_date_id = $_POST["mt_selected_service_date_id"];
        $booked_slot_id = $_POST["mt_selected_slot_id"];
        $booked_date = $_POST["mt_selected_service_date"];
        $booked_time_slot = $_POST["mt_selected_time_slot"];
        $mt_booking_slot_starts_at = $_POST["mt_booking_slot_starts_at"];
        $mt_service_orders_per_slot = $_POST["mt_service_orders_per_slot"];

        
        // Count the confirmed bookings
        $mt_bookings_table = $wpdb->prefix . 'mt_bookings';
        $mt_bookings_table_data_fetch = '';
        if(!empty($booked_slot_id)){
            $booked_slot_id_xss_proof = htmlspecialchars($booked_slot_id);
            $booked_slot_id_xss_proof_and_sanitized = sanitize_text_field($booked_slot_id_xss_proof);
        
            $mt_bookings_table_data_fetch_prep_sql = $wpdb->prepare( "SELECT * FROM $mt_bookings_table WHERE booked_slot_id = %s ORDER BY id DESC LIMIT 1", $booked_slot_id_xss_proof_and_sanitized );
            $mt_bookings_table_data_fetch = $wpdb->get_row( $mt_bookings_table_data_fetch_prep_sql );
            
        }
        
        if( is_object($mt_bookings_table_data_fetch) ){ 
            $mt_bookings_availability_string = $mt_bookings_table_data_fetch->available_orders;
            $mt_bookings_availability_int = (int)$mt_bookings_availability_string;
        }

        if(is_null($mt_bookings_table_data_fetch)){
            $mt_bookings_availability_string = $mt_service_orders_per_slot; 
            $mt_bookings_availability_int = (int)$mt_bookings_availability_string;
        }

        if($mt_bookings_availability_int <= 0){ ?>
            <div class="mt-booking-ticket-container text-left">
                <div class="mt-form-submission-error">
                    <h5> <?php _e( 'Sorry, the slot is booked, please try another available date or slot', 'mt-service-booking' ); ?></h5>
                    <button id="goBackButton" class="btn btn-dark mt-ticket-button"> <?php _e( 'Try Another', 'mt-service-booking' ); ?></button>
                </div>
            </div> 
        <?php 
        } else {  
            $customer_name = $_POST['mt_user_fullName'];
            $customer_email = $_POST['mt_user_email'];
            $customer_phone = $_POST["mt_user_telephone"];
            $service_provider_email = $_POST['mt_selected_service_provider_email'];
            $customer_IP = '2700.147.206.102';

            // Validation Check 
            $valid_customer_name = !empty($customer_name);
            $valid_customer_email = filter_var($customer_email, FILTER_VALIDATE_EMAIL) && !empty($customer_email);
            $valid_customer_phone = preg_match("/[0-9]/", $customer_phone) && !empty($customer_phone);
            $valid_service_id = !empty($service_id);
            $valid_service_name = !empty($service_name);
            $valid_booked_date_id = !empty($booked_date_id);
            $valid_booked_slot_id = !empty($booked_slot_id);
            $valid_booked_date = !empty($booked_date);
            $valid_booked_time_slot = !empty($booked_time_slot);
            $valid_mt_booking_slot_starts_at = !empty($mt_booking_slot_starts_at);

            if ($valid_customer_name && $valid_customer_email && $valid_customer_phone && $valid_service_id && $valid_service_name && $valid_booked_date_id && $valid_booked_slot_id && $valid_booked_date && $valid_booked_time_slot && $valid_mt_booking_slot_starts_at ) {
                $customer_name_xss_proof = htmlspecialchars($customer_name);
                $customer_name_xss_proof_and_sanitized = sanitize_text_field($customer_name_xss_proof);

                $customer_email_xss_proof = htmlspecialchars($customer_email);
                $customer_email_xss_proof_and_sanitized = sanitize_email($customer_email_xss_proof);
                
                $customer_phone_xss_proof = htmlspecialchars($customer_phone);
                $customer_phone_xss_proof_and_sanitized = absint($customer_phone_xss_proof);
                
                $service_provider_email_xss_proof = htmlspecialchars($service_provider_email);
                $service_provider_email_xss_proof_and_sanitized = sanitize_email($service_provider_email_xss_proof);
                
                $service_id_xss_proof = htmlspecialchars($service_id);
                $service_id_xss_proof_and_sanitized = absint($service_id_xss_proof);
                
                $service_name_xss_proof = htmlspecialchars($service_name);
                $service_name_xss_proof_and_sanitized = sanitize_text_field($service_name_xss_proof);
                
                $service_title_xss_proof = htmlspecialchars($service_title);
                $service_title_xss_proof_and_sanitized = sanitize_text_field($service_title_xss_proof);
                
                $service_location_xss_proof = htmlspecialchars($service_location);
                $service_location_xss_proof_and_sanitized = sanitize_text_field($service_location_xss_proof);
                
                $service_price_xss_proof = htmlspecialchars($service_price);
                $service_price_xss_proof_and_sanitized = sanitize_text_field($service_price_xss_proof);
                
                $booked_date_id_xss_proof = htmlspecialchars($booked_date_id);
                $booked_date_id_xss_proof_and_sanitized = sanitize_text_field($booked_date_id_xss_proof);
                
                $booked_slot_id_xss_proof = htmlspecialchars($booked_slot_id);
                $booked_slot_id_xss_proof_and_sanitized = sanitize_text_field($booked_slot_id_xss_proof);
                
                $booked_date_xss_proof = htmlspecialchars($booked_date);
                $booked_date_xss_proof_and_sanitized = sanitize_text_field($booked_date_xss_proof);
                
                $booked_time_slot_xss_proof = htmlspecialchars($booked_time_slot);
                $booked_time_slot_xss_proof_and_sanitized = sanitize_text_field($booked_time_slot_xss_proof);
                
                $mt_booking_slot_starts_at_xss_proof = htmlspecialchars($mt_booking_slot_starts_at);
                $mt_booking_slot_starts_at_xss_proof_and_sanitized = sanitize_text_field($mt_booking_slot_starts_at_xss_proof);

                // If everything is okay then insert the value to database and send emails
                $mt_bookings_table_name = $wpdb->prefix . "mt_bookings";

                $mt_insert_query = $wpdb->prepare("INSERT INTO $mt_bookings_table_name 
                    (`approve_booking`, `service_id`, `service_name`, `service_price`, `booked_date_id`, `booked_slot_id`, `booked_date`, `booked_time_slot`, `customer_name`, `customer_email`, `customer_phone`, `customer_IP`, `starting_time_ms`, `available_orders`) 
                    values (%s, %d, %s, %s, %s, %s, %s, %s, %s, %s, %d, %s, %s, %d)", 
                    0, 
                    $service_id_xss_proof_and_sanitized, 
                    $service_name_xss_proof_and_sanitized, 
                    $service_price_xss_proof_and_sanitized, 
                    $booked_date_id_xss_proof_and_sanitized, 
                    $booked_slot_id_xss_proof_and_sanitized, 
                    $booked_date_xss_proof_and_sanitized, 
                    $booked_time_slot_xss_proof_and_sanitized, 
                    $customer_name_xss_proof_and_sanitized, 
                    $customer_email_xss_proof_and_sanitized, 
                    $customer_phone_xss_proof_and_sanitized, 
                    $customer_IP, 
                    $mt_booking_slot_starts_at_xss_proof_and_sanitized, 
                    $mt_bookings_availability_int
                );

                $mt_insert_booking = $wpdb->query($mt_insert_query);

                if($mt_insert_booking){

                    $headers = array('Content-Type: text/html; charset=UTF-8');

                    // prepare customer email
                    $customer_email_address = $customer_email_xss_proof_and_sanitized;
                    $customer_email_subject = $customer_mail_subject;

                    $customer_mail_body = $customer_mail_body .'<br>' ."\r\n";
                    $customer_mail_body .= ' <br>' ."\r\n";
                    $customer_mail_body .= ' <br>' ."\r\n";
                    $customer_mail_body .= 'Booking Info:  <br>' ."\r\n";
                    $customer_mail_body .= 'Your Name: ' . $customer_name_xss_proof_and_sanitized . '<br>' ."\r\n";
                    $customer_mail_body .= 'Your Phone: ' . $customer_phone_xss_proof_and_sanitized . '<br>' ."\r\n";
                    $customer_mail_body .= 'Your Email Address: ' . $customer_email_address . '<br>' ."\r\n";
                    $customer_mail_body .= 'Service: ' . $service_name_xss_proof_and_sanitized . '<br>' ."\r\n";
                    $customer_mail_body .= 'Booked Date: ' . $booked_date_xss_proof_and_sanitized . '<br>' ."\r\n";
                    $customer_mail_body .= 'Booked Time slot: ' . $booked_time_slot_xss_proof_and_sanitized . '<br>' ."\r\n";

                    $wp_customer_mail = wp_mail( $customer_email_address, $customer_email_subject, $customer_mail_body, $headers );

                    $admin_email_address =  $service_provider_email_xss_proof_and_sanitized;
                    $admin_email_subject = $admin_mail_subject ." Customer: " .$customer_name_xss_proof_and_sanitized;

                    // prepare admin email body 
                    $admin_email_Body = $admin_mail_body .'<br>' ."\r\n";  
                    $admin_email_Body .= ' <br>' ."\r\n";
                    $admin_email_Body .= ' <br>' ."\r\n";
                    $admin_email_Body .= 'Booking Info:  <br>' ."\r\n";
                    $admin_email_Body .= 'Customer Name: ' . $customer_name_xss_proof_and_sanitized . '<br>' ."\r\n";
                    $admin_email_Body .= 'Customer Phone: ' . $customer_phone_xss_proof_and_sanitized . '<br>' ."\r\n";
                    $admin_email_Body .= 'Customer Email Address: ' . $customer_email_address . '<br>' ."\r\n";
                    $admin_email_Body .= 'Service: ' . $service_name_xss_proof_and_sanitized . '<br>' ."\r\n";
                    $admin_email_Body .= 'Booked Date: ' . $booked_date_xss_proof_and_sanitized . '<br>' ."\r\n";
                    $admin_email_Body .= 'Booked Time slot: ' . $booked_time_slot_xss_proof_and_sanitized . '<br>' ."\r\n"; 
                
                    $wp_admin_mail = wp_mail( $admin_email_address, $admin_email_subject, $admin_email_Body, $headers );

                    // Get inserted data

                    $mt_bookings_data_fetch = $wpdb->get_row( "SELECT * FROM $mt_bookings_table_name ORDER BY id DESC LIMIT 1" );
                    $mt_booking_id = $mt_bookings_data_fetch->id;

                ?> 

                <div class="mt-booking-ticket-container text-left">
                    <div id="mt_booking_ticket">
                        <div class="mt-ticket-wrapper">
                            <div  class="text-center" >
                                <div class="mt-pending-notification"> <?php _e( 'Booking Approval Status:', 'mt-service-booking' ); ?> 
                                    <span class="text-warning"> <?php _e( 'Pending', 'mt-service-booking' ); ?> </span>
                                </div>
                            </div>
                            <div class="mt-ticket-wrapper-img d-flex justify-content-center ">
                                <img src="<?php echo $mt_thanks_img; ?>" alt="">
                            </div>
                            <div class="mt-booked-service-wrapper d-flex align-items-center justify-content-between flex-wrap mt-2">
                                <div class="mt-booked-service">
                                    <div class="em-service-excerpt d-flex align-items-center">
                                        <div class="mt-booked-service-info">
                                            <h4 id="mt-service-name"> <?php _e( 'Doctor Name: ', 'mt-service-booking' ); ?> <?php echo $service_name_xss_proof_and_sanitized;?> </h4>
                                            <p> <?php _e( 'Title: ', 'mt-service-booking' ); ?> <?php echo $service_title_xss_proof_and_sanitized; ?>  </p>
                                            <p> <?php echo $service_location_xss_proof_and_sanitized; ?> </p>
                                            <p class="em-reservation-service-price"> <?php _e( 'Price: ', 'mt-service-booking' ); ?> <b> <?php echo $service_price_xss_proof_and_sanitized; ?> </b> </p>
                                        </div>
                                    </div>
                                </div>
                                <div class="mt-booking-info">
                                    <h4> <?php _e( 'Booking Info ', 'mt-service-booking' ); ?> </h4>
                                    <div class="mt-booked-id">
                                        <p><?php _e( 'Booking Id: ', 'mt-service-booking' ); ?> <?php echo $mt_booking_id; ?></p>
                                    </div>
                                    <div class="mt-booked-date d-flex align-items-center">
                                        <p class="mt-date"> <?php _e( 'Booked Date: ', 'mt-service-booking' ); ?> <?php echo $booked_date_xss_proof_and_sanitized; ?></p>
                                    </div>
                                    <div class="em-booked-time-slot d-flex align-items-center">
                                        <p class="mt-time-slot"> <?php _e( 'Booked Time-slot: ', 'mt-service-booking' ); ?> <?php echo  $booked_time_slot_xss_proof_and_sanitized; ?> </p>
                                    </div>
                                </div>
                            </div>

                            <div class="mt-booking-user-info">
                                <h4 class="mt-user-name"> <?php _e( 'Patient Name: ', 'mt-service-booking' ); ?> <?php echo $customer_name_xss_proof_and_sanitized; ?></h4>
                                <div class="d-flex align-items-center">
                                    <p class="mt-user-name"> <?php _e( 'Name: ', 'mt-service-booking' ); ?> <?php echo $customer_name_xss_proof_and_sanitized; ?></p>
                                </div>
                                <div class="d-flex align-items-center">
                                    <p class="mt-user-phone"><?php _e( ' Phone no: ', 'mt-service-booking' ); ?> <?php echo $customer_phone_xss_proof_and_sanitized; ?> </p>
                                </div>
                            </div>
                        </div>
                        <div class="mt-ticket-button-wrapper">
                            <h4> <?php _e( 'Thank You', 'mt-service-booking' ); ?></h4>
                            <button id="goBackButton" class="btn btn-dark mt-ticket-button"> <?php _e( 'Finish ', 'mt-service-booking' ); ?></button>
                        </div>
                    </div>
                </div>
                <script>
                    if ( window.history.replaceState ) {
                        window.history.replaceState( null, null, window.location.href );
                    }
                </script>
            <?php
            } else { ?>
                <div class="mt-booking-ticket-container text-left">
                    <div class="mt-form-submission-error p-3">
                        <span> <?php _e( 'Please fill out the form accurately and then submit', 'mt-service-booking' ); ?></span>
                        <button id="goBackButton" class="btn btn-dark mt-ticket-button"> <?php _e( 'Try Again', 'mt-service-booking' ); ?></button>
                    </div>
                </div>
        <?php    }

        } else { ?>
            <div class="mt-booking-ticket-container text-left">
                <div class="mt-form-submission-error p-3">
                    <span> <?php _e( 'Please fill out the form accurately and then submit', 'mt-service-booking' ); ?></span>
                    <button id="goBackButton" class="btn btn-dark mt-ticket-button"> <?php _e( 'Try Again', 'mt-service-booking' ); ?></button>
                </div>
            </div>
        <?php

        }

    }

}