/*
Theme Name: Bw GiftXtore
Theme URI: https://giftxtore.bzotech.com/intro/
Author: BZOTech
Author URI: https://bzotech.com/
Description: GiftXtore is a clean beautiful WordPress theme for selling Jewelry Diamond, Jewelry Gemstone, Fashion Jewelry, Earrings, Necklace and so on.
Version: 1.7.3
Tested up to: 6.6
Requires PHP: 7.4
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright: © 2024 BZOTech Theme. All rights reserved.
Tags: custom-colors, custom-menu, custom-logo, editor-style, featured-images, rtl-language-support, post-formats, sticky-post, threaded-comments, block-styles, blog, news
Text Domain: bw-giftxtore
*/

// Start PHP session on init
add_action('init', function() {
    if (!session_id()) {
        session_start();
    }
});

// Create user after WPForms submission
add_action('wpforms_process_complete', 'custom_register_user_with_phone', 10, 4);

function custom_register_user_with_phone($fields, $entry, $form_data, $entry_id) {
    if ($form_data['id'] != 19659) {
        return;
    }

    // Extract submitted values
    $full_name   = sanitize_text_field($fields[1]['value']); // Full Name
    $mother_name = sanitize_text_field($fields[2]['value']); // Mother's Name
    $phone       = sanitize_text_field($fields[3]['value']); // Phone Number
    $password    = $fields[4]['value'];                      // Password
    $cnic        = sanitize_text_field($fields[5]['value']); // CNIC

    // Generate dummy email using phone number
    $email = $phone . '@b2bsoftwares.com';

    // Abort if required fields are missing or username exists
    if (empty($phone) || empty($password)) return;
    if (username_exists($phone)) return;

    // Create user
    $user_id = wp_create_user($phone, $password, $email);
    if (is_wp_error($user_id)) return;

    // Set user role to WooCommerce customer
    $user = new WP_User($user_id);
    $user->set_role('customer');

    // Save user meta
    update_user_meta($user_id, 'full_name', $full_name);
    update_user_meta($user_id, 'mother_name', $mother_name);
    update_user_meta($user_id, 'cnic', $cnic);
    update_user_meta($user_id, 'phone', $phone);

    // Store data in session for display
    $_SESSION['registered_user_data'] = [
        'Full Name'    => $full_name,
        'Mother Name'  => $mother_name,
        'Phone'        => $phone,
        'CNIC'         => $cnic,
        'Password'     => $password,
        'Email'        => $email,
    ];
}

// Shortcode to show submitted user data
add_shortcode('show_user_data_debug', function () {
    if (!session_id()) session_start();
    if (!isset($_SESSION['registered_user_data'])) return '';

    $output = "<h3>Debug: Registered User Data</h3><pre>";
    foreach ($_SESSION['registered_user_data'] as $key => $value) {
        $output .= "$key: $value\n";
    }
    $output .= "</pre>";

    // Clear session after showing once
    unset($_SESSION['registered_user_data']);
    return $output;
});




