<?php


//shortcode
function booking_form_shortcode() {
    ob_start(); ?>
    
    <form id="booking-form" enctype="multipart/form-data" method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
        <input type="hidden" name="action" value="booking_form">
        <input type="hidden" name="booking_nonce" value="<?php echo wp_create_nonce('booking_nonce'); ?>">
        <div class="row" data-aos="fade-up">
            <div class="col-md-6 mb-3" >
                <label class="form-label fw-bold" for="first_name">First Name*</label>
                <input type="text" class="form-control" name="first_name" required>
            </div>
            <div class="col-md-6 mb-3">
                <label class="form-label fw-bold" for="last_name">Last Name*</label>
                <input type="text" class="form-control" name="last_name" required>
            </div>
            <div class="col-md-6 mb-3">
                <label class="form-label fw-bold"  for="email">Email*</label>
                <input type="email" class="form-control" name="email" required>
            </div>
            <div class="col-md-6 mb-3">
                <label class="form-label fw-bold" for="phone">Phone Number*</label>
                <input type="tel" class="form-control" name="phone" placeholder="+256 xxx xxxxx " required>
            </div>
            <div class="col-md-12 mb-3">
                <label class="form-label fw-bold" for="location">Your Home/Business Location*</label>
                <input type="text" class="form-control" name="location" placeholder="Town/City, District, Uganda" required>
            </div>
            <div class="col-md-12 mb-3">
                <label class="form-label" for="description">
                   Share as much information with us as you like. To help us create you a detailed quotation.
                </label>
                <textarea class="form-control" name="description" rows="5" required></textarea>
            </div>
            <div class="col-md-12 mb-3">
                <p>Once we have received your request one of our team will be in touch with you to discuss further the project.</p>
                <input type="checkbox" value="Yes" id="confirm" required>
                <label class="form-label" for="confirm" >
                   By using this form you agree with the storage and handling of your data by this website.
                </label>
            </div>
            <div class="col-md-12 d-block mb-3">
                <input type="text" name="honeypot" style="display:none;">
                <button type="submit" class="btn btn-success btn-lg d-block">Submit Now</button>
            </div>
        </div>
    </form>
    
    <?php return ob_get_clean();
}
add_shortcode('booking_form', 'booking_form_shortcode');

//Process the Form
function handle_recruitment_form_submission() {
    if (!empty($_POST['honeypot'])) {
        //wp_die('Spam detected.');
        wp_redirect(home_url('/error-page'));
        exit;
    }
    
    if (substr_count($_POST['description'], 'http') > 1) {
        //wp_die('Spam detected: too many links.');
        wp_redirect(home_url('/error-page'));
        exit;
    }

    $ip_address = $_SERVER['REMOTE_ADDR'];
    if (get_transient("form_submission_$ip_address")) {
        //wp_die('Too many submissions. Please try again later.');
        wp_redirect(home_url('/error-page'));
        exit;
    }
    set_transient("form_submission_$ip_address", true, 60); // Limit to 1 submission per minute


    if (!isset($_POST['booking_nonce']) || !wp_verify_nonce($_POST['booking_nonce'], 'booking_nonce')) {
        wp_die('Invalid request.');
    }
    
    $fname = sanitize_text_field($_POST['first_name']);
    $lname = sanitize_text_field($_POST['last_name']);
    $email = sanitize_email($_POST['email']);
    $phone = sanitize_text_field($_POST['phone']);
    $location = sanitize_text_field($_POST['location']);
    $description = sanitize_textarea_field($_POST['description']);
    
    // Validate required fields
    if (empty($fname) || empty($lname) || empty($email)) {
        wp_die('Required fields are missing.');
    }
    

    // Send emails
    $admin_email_body = "New Online Quote Request:\nName: $fname $lname\nEmail: $email\nLocation: $location\nDescription\n$description\n...";
    $client_email_body = "Dear $fname,\n\nThank you for contacting Passions Services Ltd. We will get back to you shortly.\n...";
    wp_mail(get_option('admin_email'), 'New Online Quote Request', $admin_email_body);
    wp_mail($email, 'Quote Request Received', $client_email_body);

    // Redirect with success
    wp_redirect(home_url('/thank-you'));
    exit;

}
add_action('admin_post_nopriv_booking_form', 'handle_recruitment_form_submission');
add_action('admin_post_booking_form', 'handle_recruitment_form_submission');

