<?php
if (!defined('ABSPATH')) exit;



function buildaband_shortcode(){   
$html = '';  
if(is_user_logged_in()){     
    $priceButt = '<span id="preloader"></span><span>Price: <strong style="cursor: pointer;">'.get_woocommerce_currency_symbol().'<span id="PriceValue">35255</span></strong></span>'; 
    $buttonD ='';
}else{
   // $priceButt = '<span><strong style="cursor: pointer;"><a href="'.get_site_url().'/my-account/" style="color:#000" target="_blank">Login</a></strong> to view price. </span>'; 
   // $buttonD ='disabled=""';
    $priceButt = '<span id="preloader"></span><span>Price: <strong style="cursor: pointer;">'.get_woocommerce_currency_symbol().'<span id="PriceValue">35255</span></strong></span>'; 
    $buttonD ='';
}              
$html .= '<div class="main_band_builder_wrapper"><div class="container"><div class="row band_builder_wrapper"><div class="col-12 col-lg-6 left"><div class="builder-container"><div class="builder-img-box"><img src="'. buildaband_PLUGIN_URL .'/assets/images/BVED_AS_W_4MM.jpg" alt="Builder image" class="img-fluid" loading="eager" decoding="sync" /></div><div class="builder-filter mb-3"><ul class="d-flex align-items-center justify-content-start flex-wrap"><li class=""><span>PROFILE</span><span id="profileSelected"> Beveled Edge</span></li><li class=""><span>METAL TYPE</span><span id="selectedMetal">14K</span></li><li class=""><span>COLOR</span><span id="selectedColor"> White </span></li><li class=""><span>WIDTH </span><span id="selectedWidth">4MM</span></li><li class=""><span>FINISH</span><span id="selectedFinish">Angle Satin</span></li><li class=""><span>THICKNESS</span><span id="selectedThick">1.5mm</span></li><li class=""><span>SIZE</span><span id="selectedSize">04.25</span></li></ul></div><div class="d-flex align-items-center justify-content-center justify-content-center gap-3"><button type="submit" '. $buttonD.' class=" btn btn-none" id="AddPButtCart"> Create special order <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-right-short" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M4 8a.5.5 0 0 1 .5-.5h5.793L8.146 5.354a.5.5 0 1 1 .708-.708l3 3a.5.5 0 0 1 0 .708l-3 3a.5.5 0 0 1-.708-.708L10.293 8.5H4.5A.5.5 0 0 1 4 8" /></svg></button><p class="my-auto para-1 primary-text" style="position: relative;">'.$priceButt.'</p></div></div></div><div class="col-12 col-lg-6 right"><div class="builder-innerbox-filter"><h3 class="head-3 head-line primary-text text-uppercase"> Profile </h3><input type="hidden" id="Site_Url_BBP" value="'.get_site_url().'" /><input type="hidden" id="ajax_url_BBPWP" value="'.admin_url( "admin-ajax.php " ).'" /><div class="builder-innderbox"><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Beveled Edge"><input class="form-check-input" type="radio" name="inlineRadioOptions" onchange="profile(this.value)" id="inlineRadio1" value="BVED" checked><label class="form-check-label" for="inlineRadio1"><div class="circle"><img src="'.buildaband_PLUGIN_URL .'/assets/images/BVED.jpg" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>Beveled Edge</span></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Center Round"><input class="form-check-input" type="radio" name="inlineRadioOptions" onchange="profile(this.value)" id="inlineRadio2" value="CTRN"><label class="form-check-label" for="inlineRadio2"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/CTRN.jpg" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>Center Round</span></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Double Millgrain"><input class="form-check-input" type="radio" name="inlineRadioOptions" onchange="profile(this.value)" id="inlineRadio3" value="DBMG"><label class="form-check-label" for="inlineRadio3"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/DBMG.jpg" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>Double Millgrain</span></label></div></div></div><div class="builder-innerbox-filter"><h3 class="head-3 head-line primary-text text-uppercase"> Metal Type </h3><div class="builder-innderbox"><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="10K"><input class="form-check-input" type="radio" name="metalType" onchange="metal(this.value)" id="gold10k" value="10k" checked><label class="form-check-label" for="gold10k"><div class="circle"><div class="text w-100 h-100"><span> 10K </span></div></div></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="14K"><input class="form-check-input" type="radio" name="metalType" onchange="metal(this.value)" id="gold14k" value="14k"><label class="form-check-label" for="gold14k"><div class="circle"><div class="text w-100 h-100"><span> 14K </span></div></div></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="18K"><input class="form-check-input" type="radio" name="metalType" onchange="metal(this.value)" id="gold18k" value="18k"><label class="form-check-label" for="gold18k"><div class="circle"><div class="text w-100 h-100"><span> 18K </span></div></div></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Palladium"><input class="form-check-input" type="radio" name="metalType" onchange="metal(this.value)" id="pal" value="L"><label class="form-check-label" for="pal"><div class="circle"><div class="text w-100 h-100"><span> PAL </span></div></div></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Platinum"><input class="form-check-input" type="radio" name="metalType" onchange="metal(this.value)" id="plt" value="P"><label class="form-check-label" for="plt"><div class="circle"><div class="text w-100 h-100"><span> PLT </span></div></div></label></div></div></div><div class="builder-innerbox-filter"><h3 class="head-3 head-line primary-text text-uppercase"> Color </h3><div class="builder-innderbox" id="ColorFilterChangee"><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="White"><input class="form-check-input" type="radio" name="colorType" onchange="color(this.value)" id="colorWhite" value="W" checked><label class="form-check-label" for="colorWhite"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/W.png" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>W</span></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Yellow"><input class="form-check-input" type="radio" name="colorType" onchange="color(this.value)" id="colorYellow" value="Y"><label class="form-check-label" for="colorYellow"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/Y.png" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>Y</span></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Rose"><input class="form-check-input" type="radio" name="colorType" onchange="color(this.value)" id="colorRose" value="R"><label class="form-check-label" for="colorRose"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/R.png" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>R</span></label></div></div></div><div class="builder-innerbox-filter"><h3 class="head-3 head-line primary-text text-uppercase"> Width (MM) </h3><div class="builder-innderbox"><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="4MM" checked><input class="form-check-input" type="radio" name="widthType" onchange="widthFun(this.value)" id="width4mm" value="4MM" checked><label class="form-check-label" for="width4mm"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/4MM.jpg" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>4MM</span></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="5MM"><input class="form-check-input" type="radio" name="widthType" onchange="widthFun(this.value)" id="width5mm" value="5MM"><label class="form-check-label" for="width5mm"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/5MM.jpg" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>5MM</span></label></div></div></div><div class="builder-innerbox-filter"><h3 class="head-3 head-line primary-text text-uppercase"> Finish </h3><div class="builder-innderbox"><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Angle Satin"><input class="form-check-input" type="radio" name="finishType" onchange="finishFun(this.value)" id="finishAngleSatin" value="AS" checked><label class="form-check-label" for="finishAngleSatin"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/AS.jpg" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>Angle Satin</span></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Bark Finish"><input class="form-check-input" type="radio" name="finishType" onchange="finishFun(this.value)" id="finishBark" value="BF"><label class="form-check-label" for="finishBark"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/BF.jpg" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>Bark Finish</span></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="Circle Matt"><input class="form-check-input" type="radio" name="finishType" onchange="finishFun(this.value)" id="finishCircleMatt" value="CM"><label class="form-check-label" for="finishCircleMatt"><div class="circle"><img src="'. buildaband_PLUGIN_URL .'/assets/images/CM.jpg" alt="" class="img-fluid" loading="eager" decoding="sync"/></div><span>Circle Matt</span></label></div></div></div><div class="builder-innerbox-filter"><h3 class="head-3 head-line primary-text text-uppercase"> Thickness (MM) </h3><div class="builder-innderbox"><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="1.5MM"><input class="form-check-input" type="radio" name="thicknessType" onchange="thicknessFun(this.value)" id="thickness1_5mm" value="1.5mm" checked><label class="form-check-label" for="thickness1_5mm"><div class="circle"><div class="text w-100 h-100"><span> 1.5MM </span></div></div></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="1.9MM"><input class="form-check-input" type="radio" name="thicknessType" onchange="thicknessFun(this.value)" id="thickness1_9mm" value="1.9mm"><label class="form-check-label" for="thickness1_9mm"><div class="circle"><div class="text w-100 h-100"><span> 1.9MM </span></div></div></label></div><div class="builder-filter-circle-btn" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="2.2MM"><input class="form-check-input" type="radio" name="thicknessType" onchange="thicknessFun(this.value)" id="thickness2_2mm" value="2.2mm"><label class="form-check-label" for="thickness2_2mm"><div class="circle"><div class="text w-100 h-100"><span> 2.2MM </span></div></div></label></div></div></div><div class="builder-innerbox-filter"><h3 class="head-3 head-line primary-text text-uppercase"> Size </h3><select class="form-select form-select-sm" id="SizeD" aria-label=".form-select-sm example" onchange="sizeFun(this.value)"><option value="04.00">04.00</option><option value="04.25" checked>04.25</option><option value="04.50">04.50</option><option value="04.75">04.75</option><option value="05.00">05.00</option><option value="05.25">05.25</option><option value="05.50">05.50</option><option value="05.75">05.75</option><option value="06.00">06.00</option><option value="06.25">06.25</option><option value="06.50">06.50</option><option value="06.75">06.75</option><option value="07.00">07.00</option><option value="07.25">07.25</option><option value="07.50">07.50</option><option value="07.75">07.75</option><option value="08.00">08.00</option><option value="08.25">08.25</option><option value="08.50">08.50</option><option value="08.75">08.75</option><option value="09.00">09.00</option><option value="09.25">09.25</option><option value="09.50">09.50</option><option value="09.75">09.75</option><option value="10.00">10.00</option><option value="10.25">10.25</option><option value="10.50">10.50</option><option value="10.75">10.75</option><option value="11.00">11.00</option><option value="11.25">11.25</option><option value="11.50">11.50</option><option value="11.75">11.75</option><option value="12.00">12.00</option></select></div></div></div></div></div>';
     return $html;
}


add_action( "wp_ajax_buildaband_filter_program", "buildaband_filter_program" );
add_action( "wp_ajax_nopriv_buildaband_filter_program", "buildaband_filter_program" );

function buildaband_filter_program() {
    if (!isset($_POST['security']) || !wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['security'])), 'buildaband_filter_program')) {
        wp_send_json_error(array('message' => 'Security check failed'));
        wp_die();
    }
    
    if (isset($_POST['profile'])) {
        $Profile = sanitize_text_field(wp_unslash($_POST['profile']));
    }
    if (isset($_POST['color'])) {
        $Color = sanitize_text_field(wp_unslash($_POST['color']));
    }
    if (isset($_POST['width'])) {
        $Width = sanitize_text_field(wp_unslash($_POST['width']));
    }
    if (isset($_POST['finish'])) {
        $Finish = sanitize_text_field(wp_unslash($_POST['finish']));
    }

    
    $price = wp_rand('1500','12000');  
     

    wp_send_json_success(array(
          'price' => $price,
          'image' => 'https://guild-and-facet.s3.us-west-2.amazonaws.com/byb/'.$Profile.'_'.$Finish.'_'.$Color.'_'.$Width.'.jpg'
          
      ));

    wp_die(); // ajax call must die to avoid trailing 0 in your response
}

add_action( "wp_ajax_buildaband_addTocartOrder", "buildaband_addTocartOrder" );
add_action( "wp_ajax_nopriv_buildaband_addTocartOrder", "buildaband_addTocartOrder" );

function buildaband_addTocartOrder() {

    if (!isset($_POST['security']) || !wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['security'])), 'buildaband_addTocartOrder')) {
        wp_send_json_error(array('message' => 'Security check failed'));
        wp_die();
    }
    

    if (isset($_POST['profile'])) {
        $Profile = sanitize_text_field(wp_unslash($_POST['profile']));
    }
    if (isset($_POST['color'])) {
        $Color = sanitize_text_field(wp_unslash($_POST['color']));
    }
    if (isset($_POST['width'])) {
        $Width = sanitize_text_field(wp_unslash($_POST['width']));
    }
    if (isset($_POST['finish'])) {
        $Finish = sanitize_text_field(wp_unslash($_POST['finish']));
    }
    if (isset($_POST['metaltype'])) {
        $MetalType = sanitize_text_field(wp_unslash($_POST['metaltype']));
    }
    if (isset($_POST['thickness'])) {
        $Thickness = sanitize_text_field(wp_unslash($_POST['thickness']));
    }
    if (isset($_POST['size'])) {
        $Size = sanitize_text_field(wp_unslash($_POST['size']));
    }
    if (isset($_POST['price'])) {
        $price = sanitize_text_field(wp_unslash($_POST['price']));
    }
    $sku = $MetalType.'_'. $Color .'_'.$Size ;
    
    $product_id = buildaband_get_product_id_by_sku($sku);
    $quantity = 1;
if ($product_id) {
      if ( ! WC()->cart ) {
        return false;
    }

    $added = WC()->cart->add_to_cart($product_id, $quantity);

      if ( $added ) {
          // Send a success response
          wp_send_json_success( array( 'message' => 'Product added to cart successfully!' ) );
      } else {
          // Send a failure response
          wp_send_json_error( array( 'message' => 'Failed to add product to cart.' ) );
      }

} else {
     global $wpdb;   
      $title =  $MetalType.'_'. $Color .'_'. $Profile.'_'.$Width.'_'. $Finish.'_'. $Thickness.'_'.$Size ;   
      $imageURLTitle =   $Profile.'_'.$Finish.'_'. $Color.'_'. $Width ;     
        
             // Create the parent variable product
            $post_id = wp_insert_post(array(
                'post_title'   => $title ,
                'post_content' => '',
                'post_status'  => 'publish',
                'post_type'    => 'product',
            ));
      
         // Set the product type to 'variable'
          //  wp_set_object_terms($post_id, 'variable', 'product_type');
            update_post_meta($post_id, '_sku', $sku); 
            update_post_meta($post_id, '_price', $price);    
          
          // wp_set_object_terms($post_id, $category_ids, 'product_cat');               
                                          
                       
    
          if ( ! WC()->cart ) {
        return false;
    }

    $added = WC()->cart->add_to_cart($post_id, $quantity);

      if ( $added ) {
          // Send a success response
          wp_send_json_success( array( 'message' => 'Product added to cart successfully!' ) );
      } else {
          // Send a failure response
          wp_send_json_error( array( 'message' => 'Failed to add product to cart.' ) );
      }
    
}
    
}




function buildaband_get_product_id_by_sku($sku) {
   global $wpdb;
    
    // Check if SKU is empty or not a valid string
    if (empty($sku)) {
        return null;
    }
    // Query the product by SKU
    $product_id = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery  
         $wpdb->prepare(
            "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_sku' AND meta_value =%s",$sku
        )
    );
    return $product_id ? $product_id : null;
}




     ?>
