<!-- Byor start -->
<?php 
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $kirb_filters_data, $kirb_byor_uid, $kirb_config_file;
$kirb_filters_data = kirb_get_filters_data();
$kirb_ud_localization = kirb_get_user_defined_localization();
require_once(KIRB_BASE_DIR . '/translations_lookup.php');
?>
<?php include_once 'common.php';?>
<div class="container">
    <?php
        $include_path = "";
        if(KIRB_BYOR_STRIP_THEME == 'arrow-step') {
            $include_path = WP_CONTENT_DIR . "/kirb-ui-components/byor-view-strip-".KIRB_BYOR_STRIP_THEME.".php";
            if(!file_exists($include_path)) {
                $include_path = KIRB_BASE_DIR . "/ui/byor-view-strip-circle-step.php";
            }
        }else{
            $include_path = KIRB_BASE_DIR . "/ui/byor-view-strip-circle-step.php";
        }
        
        include_once($include_path);

        if(!$byor_data) {
            $kirb_page_ids = json_decode(kirbGetMeta("_page_ids"), true);
            echo '<script>window.location.href = "'. esc_url(kirb_getPageUrlById($kirb_page_ids['loose-diamonds'])) .'";</script>';
            exit;
        }

        $is_gemstone = $byor_data['diamond_type'] == 1;
        $delivery_date = kirb_getDeliveryDateByDiamondVendor($byor_diamond['SettingId'], $kirb_filters_data);
        $addons_data = json_decode(kirbGetMeta('_setting_addon_data'), true);

        $kirb_translations_mapping = array(
            'White Gold 14K' => esc_html__('White Gold 14K', 'diamond-ring-builder-jewelry'),
            'Yellow Gold 14K' => esc_html__('Yellow Gold 14K', 'diamond-ring-builder-jewelry'),
            'Rose Gold 14K' => esc_html__('Rose Gold 14K', 'diamond-ring-builder-jewelry'),
            'White Gold 18K' => esc_html__('White Gold 18K', 'diamond-ring-builder-jewelry'),
            'Yellow Gold 18K' => esc_html__('Yellow Gold 18K', 'diamond-ring-builder-jewelry'),
            'Rose Gold 18K' => esc_html__('Rose Gold 18K', 'diamond-ring-builder-jewelry'),
            'Platinum' => esc_html__('Platinum', 'diamond-ring-builder-jewelry'),
        );

        $variation_image_id = $byor_ring_data->get_image_id();
        if ($variation_image_id) {
            $gallery_image_ids[] = $variation_image_id;
        }
        $variation_gallery = get_post_meta(
            $byor_ring_data->get_id(),
            'woo_variation_gallery_images',
            true
        );
        if (!empty($variation_gallery)) {
            if (is_string($variation_gallery)) {
                $variation_gallery_ids = array_filter(array_map('intval', explode(',', $variation_gallery)));
            } 
            elseif (is_array($variation_gallery)) {
                $variation_gallery_ids = array_filter(array_map('intval', $variation_gallery));
            } 
            else {
                $variation_gallery_ids = [];
            }
            $gallery_image_ids = array_merge($gallery_image_ids, $variation_gallery_ids);
        }
        $parent = wc_get_product($byor_ring_data->get_parent_id());
        if ($parent instanceof WC_Product) {
            $gallery_image_ids = array_merge(
                $gallery_image_ids,
                $parent->get_gallery_image_ids()
            );
        }
        $gallery_image_ids = array_values(array_unique(array_filter($gallery_image_ids)));

        $diamond_change_url = kirb_getPageUrlById($kirb_page_ids[str_replace("kirb-","",kirb_getDiamondCategory($byor_diamond['category_id'], $kirb_filters_data))]);
        $setting_change_url = "";
        if($byor_data['setting_type'] == 1) {
            $setting_change_url = get_term_link(get_term_by('slug', $kirb_pendant_setting_catg, 'product_cat'));
        }else{
            $setting_change_url = get_term_link(get_term_by('slug', $kirb_ring_setting_catg, 'product_cat'));
        }

    ?>
    <section class="products-images byor-section mt-5 mb-5">
            <div class="back-collection col-lg-12">
                <div class="back-collection-ul col-lg-6 col-md-6 col-12 list-unstyled p-0 d-flex justify-content-between">
                    <div class="back-collection-li"><span id="kirb_byor_back_btn"><?php echo esc_html__('BACK', 'diamond-ring-builder-jewelry');?></span></div>
                </div>
            </div>
            <div class="kirb-row-detail">
            <div class="col-md-6 col-12 img-section">
                <div class="inner-byor-page">
                    <div class="thumbnail-image kirb-mobile-visible-only">
                        <div class="kirb-mobile-preview-thumbnail border-0" id="kirb-mobile-preview"></div>
                        <div class="kirb-img-item kirb-review-item kirb-360-container kirb-360-iframe-mobile-byor border-0" style="display:none;">
                            <iframe
                                id="kirb-mobile-360-frame"
                                class="kirb-review-360-frame"
                                data-video-url="<?php echo esc_url($byor_diamond['videourl']); ?>"
                                frameborder="0"
                                allow="autoplay; fullscreen"
                                allowfullscreen
                                scrolling="no">
                            </iframe>
                        </div>
                    </div>
                    <div class="thumbnail-image-slide slider-thumbnail  kirb-item-container kirb-mobile-visible-only">
                        <div>
                            <div class="kirb-img-item prod-item kirb-review-item" data-img="kirb_img2">
                                <img src="<?php echo esc_url($byor_diamond_src); ?>" 
                                    onerror="this.src='<?php echo esc_js($byor_diamond_fallback); ?>';this.onerror=null;" 
                                    alt="<?php echo esc_attr($byor_diamond['title'] ?? __('Diamond', 'diamond-ring-builder-jewelry')); ?>">
                            </div>
                        </div>
                        <div>
                        <?php if (!empty($gallery_image_ids[0])) : ?>
                                <div class="kirb-img-item prod-item normal-div kirb-review-item" data-img="kirb_ring_main">
                                    <?php echo wp_get_attachment_image($gallery_image_ids[0], 'full'); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="kirb-img-item prod-item kirb-360-container">
                            <img src="<?php echo esc_url(KIRB_PLUGIN_DIR . '/assets/images/360-icon.svg'); ?>" alt="">
                        </div>

                        <div>
                            <?php if (!empty($gallery_image_ids[1])) : ?>
                                <div class="kirb-img-item prod-item normal-div kirb-review-item" data-img="kirb_ring_main">
                                    <?php echo wp_get_attachment_image($gallery_image_ids[1], 'full'); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div>
                            <?php if (!empty($gallery_image_ids[2])) : ?>
                                <div class="kirb-img-item prod-item normal-div kirb-review-item" data-img="kirb_ring_main">
                                    <?php echo wp_get_attachment_image($gallery_image_ids[2], 'full'); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                       
                        
                    <div class="main-image kirb-review-image-section">
                        <div>
                            <div class="kirb-img-item second-view-360 360-show-div" data-img="kirb_img2">
                            <img src="<?php echo esc_url($byor_diamond_src); ?>" 
                                onerror="this.src='<?php echo esc_js($byor_diamond_fallback); ?>';this.onerror=null;" 
                                alt="<?php echo esc_attr($byor_diamond['title'] ?? __('Diamond', 'diamond-ring-builder-jewelry')); ?>">
                            </div>
                        </div>
                        <div>
                            <?php if (!empty($gallery_image_ids[0])) : ?>
                                <div class="kirb-img-item prod-item normal-div kirb-review-item" data-img="kirb_ring_main">
                                    <?php echo wp_get_attachment_image($gallery_image_ids[0], 'full'); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="kirb-img-item prod-item kirb-review-item kirb-360-container">
                            <iframe
                                id="kirb-mobile-360-frame"
                                class="kirb-review-360-frame"
                                data-video-url="<?php echo esc_url($byor_diamond['videourl']); ?>"
                                frameborder="0"
                                allow="autoplay; fullscreen"
                                allowfullscreen
                                scrolling="no">
                            </iframe>
                            <div style="width: auto;">
                                <span data-bs-toggle="modal" data-bs-target="#view-360" class="kirb-review-360-btn" data-video-url="<?php echo esc_url($byor_diamond['videourl']);?>" data-title="<?php echo esc_html(kirb_string_split_translate($byor_diamond['title'], $kirb_filter_values_translation_mapping));?>" onclick="kirb_load_360(this);">
                                    <div>
                                        <div class="item-360">
                                            <img src="<?php echo esc_url(KIRB_PLUGIN_DIR . '/assets/images/360-icon.svg'); ?>" alt="">
                                        </div>
                                    </div>   
                                </span>
                                <div class="details-view view-360 view-360-mobile kirb-view-360" data-bs-toggle="modal" data-bs-target="#view-360"
                                    data-video-url="<?php echo esc_url($byor_diamond['videourl']);?>" onclick="kirb_load_360(this);" style="display:none;">
                                    <div class="view-360-sprite image-360-view-mobile"></div>
                                    <div class="pera-text m-0 view-360-text"> <span class="view-hide-mobile"><?php echo esc_html__('VIEW 360 °', 'diamond-ring-builder-jewelry');?></span> </div>
                                </div>
                            </div>  

                        </div>

                        <div>
                            <?php if (!empty($gallery_image_ids[1])) : ?>
                                <div class="kirb-img-item prod-item normal-div kirb-review-item" data-img="kirb_ring_main">
                                    <?php echo wp_get_attachment_image($gallery_image_ids[1], 'full'); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div>
                            <?php if (!empty($gallery_image_ids[2])) : ?>
                                <div class="kirb-img-item prod-item normal-div kirb-review-item" data-img="kirb_ring_main">
                                    <?php echo wp_get_attachment_image($gallery_image_ids[2], 'full'); ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-6 col-12 product--content">
                <div class="diamond__content">
                    <div style="display: flex; align-items: baseline;" class="kirb-review-heading-h1 justify-content-between">
                        <h3 class="heading_h1 title">
                            <?php echo esc_html__('Your Unique Ring', 'diamond-ring-builder-jewelry'); ?>
                        </h3>
                        <div class="price-section">
                            <span class="price">
                               <?php echo esc_html(kirb_format_price(intval($byor_diamond['saleamt'])+intval($byor_ring_data->get_price())));?>
                            </span>
                        </div>
                    </div>
                    <span id="kirb_byor_start_over_btn" class="kirb-byor-reset-text"><?php echo esc_html__('Reset', 'diamond-ring-builder-jewelry');?></span>
                    <div class="custom-ring kirb-custom-pro-box mt-4">
                        <div class="key-card">
                            <div class="data">
                                <div class="heading3">Stone</div>
                                <div class="heading2" ><?php echo esc_html( kirb_string_split_translate( $byor_diamond['title'] ?? '', $kirb_filter_values_translation_mapping ?? array() ) ); ?>
                            <div>
                                    <?php echo esc_html(kirb_format_price($byor_diamond['saleamt'], $kirb_config_file))?>
                                    <?php 
                                        if($byor_diamond['compPrice'] > $byor_diamond['saleamt']) {
                                            echo '&nbsp;&nbsp;&nbsp;<del><span class="price-comp">' . esc_html(kirb_format_price($byor_diamond['compPrice'], $kirb_config_file)) . '</span></del>';
                                        }
                                    ?>
                                </div></div>
                                <div class="kirb-byor-review-desc"> <div><?php echo esc_html($byor_diamond['lab']);?> <?php echo esc_html($byor_diamond['lab_report_no']);?></div>
                        
                                <div>SKU <?php echo esc_html($byor_diamond['stone_no']);?></div></div>
                                </div>
                            
                            <span data-url="<?php echo esc_url($diamond_change_url); ?>" style="cursor: pointer;text-decoration: underline;" class="kirb-link-btn"><?php echo esc_html__('Change', 'diamond-ring-builder-jewelry');?></span>
                            </div>
                        </div>
                    </div>
                    <div class="custom-ring kirb-custom-pro-box">
                        <div class="key-card">
                            <div class="data">
                                <div class="heading3">Setting</div>
                                <div class="heading2">
                                    <?php 
                                        echo esc_html( kirb_string_split_translate( $byor_ring_data ? $byor_ring_data->get_title() : '', $kirb_filter_values_translation_mapping ?? array() ) );
                                    ?>
                                    <div>
                                        <?php 
                                            $ring_price = ( $byor_ring_data instanceof WC_Product ) ? $byor_ring_data->get_price() : 0;
                                            echo esc_html( kirb_format_price( $ring_price ) ); 
                                        ?>
                                    </div>
                                </div>
                                <div>
                                    <?php 
                                        $metal_name = '';
                                        if ( isset( $byor_data['ring_variation_id'] ) ) {
                                            $metal_name = kirb_get_metal_name_by_variation_id( $byor_data['ring_variation_id'] );
                                        }
                                        echo esc_html( $metal_name );
                                    ?>
                                </div>
                            </div>
                            <?php if ( ! empty( $setting_change_url ) ) : ?>
                                <span data-url="<?php echo esc_url( $setting_change_url ); ?>" class="kirb-link-btn"><?php echo esc_html__('Change', 'diamond-ring-builder-jewelry');?></span>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="already-cart my-2" id="diamond-error-msg"></div>
                    <div class="ring-size">
                        <div class="inner-ring-size">
                            <?php
                                if($addons_data['free_resizing']['enabled']) { 
                                    $fr_link = $addons_data['free_resizing']['string'];
                                    $fr_html = kirb_get_string('byor-review', 'string_free_resizing', $kirb_ud_localization);
                                    if($fr_link) {
                                        $fr_html = '<a target="_blank" href="'.esc_url($fr_link).'">'.esc_html($fr_html).'</a>';
                                    }
                                    echo wp_kses_post($fr_html);
                                }
                            ?>
                            <?php
                                if($addons_data['ring_size']['enabled']) {
                                    $ring_data_array = kirb_getRingSizeList();
                            ?>
                            <div class="ring-sizes">
                                <div class="d-block">
                                    <div>
                                <label for="kirb_ring_size_selector">Ring Size:</label></div>
                                <div>
                                <select class="field kirb_ring_size_selector" id="kirb_ring_size_selector" name="kirb_ring_size_selector">
                                    <?php
                                        for($i=0; $i < count($ring_data_array); $i++) { ?>
                                            <option value="<?php echo esc_attr($ring_data_array[$i])?>"><?php echo esc_html($ring_data_array[$i]);?></option>;
                                        <?php }
                                        echo '<option value="' . esc_attr__("I don't know", 'diamond-ring-builder-jewelry') . '">' . esc_html__("I don't know", 'diamond-ring-builder-jewelry') . '</option>';
                                    ?>
                                </select>
                                </div>
                                </div>
                            </div>
                            <?php
                                }else{
                                    echo '<input class="kirb_ring_size_selector" type="hidden" value="">';
                                } 
                            ?>
                        </div>
                    </div>
                    <?php
                        $dia_type = $is_gemstone ? 'Gemstone' : 'Diamond';
                        $deliver_str = kirb_get_string('byor-review', 'string_ring_mount_desc', $kirb_ud_localization);
                        $deliver_str = str_replace('{stone}', esc_html(strtolower($kirb_filter_values_translation_mapping[$dia_type] ?? $dia_type)), $deliver_str);
                        $deliver_str = str_replace('{setting}', esc_html__('ring setting','diamond-ring-builder-jewelry'), $deliver_str);
                    ?>
                
                    <div class="free-text-section">
                        <div class="diamond__content--info d-flex align-items-center free-shipping-byor ">
                            <div class="inner--info"><span class="delivery-time"><?php echo esc_html__('Deliver by', 'diamond-ring-builder-jewelry');?>: <b><?php echo esc_html(kirb_string_split_translate(kirb_getDeliveryDateByDiamondVendor($byor_diamond['SettingId'], $kirb_filters_data), $kirb_filter_values_translation_mapping));?></b></span></div>
                            <?php if($addons_data['free_shipping']['enabled']) { ?>
                                <div class="inner--info inner-shipping-byor kirb_disabled_anchor">         
                                    <?php
                                        $fs_link = $addons_data['free_shipping']['string'];
                                        $fs_html = kirb_get_string('byor-review', 'string_free_shipping', $kirb_ud_localization);
                                        if($fs_link) {
                                            $fs_html = '<a target="_blank" href="'.esc_url($fs_link).'">'.esc_html($fs_html).'</a>';
                                        }
                                        echo wp_kses_post($fs_html);
                                    ?>
                                </div>
                            <?php } ?>
                            <?php if($addons_data['easy_returns']['enabled']) { ?>
                                <div class="inner--info day-return kirb_disabled_anchor">
                                    <?php
                                        $er_link = $addons_data['easy_returns']['string'];
                                        $er_html = kirb_get_string('byor-review', 'string_easy_30_day_returns', $kirb_ud_localization);
                                        if($er_link) {
                                            $er_html = '<a target="_blank" href="'.esc_url($er_link).'">'.esc_html($er_html).'</a>';
                                        }
                                        echo wp_kses_post($er_html);
                                    ?>
                                </div>
                            <?php } ?>
                        </div>
                        <span>
                            <?php echo esc_html($deliver_str);?>
                        </span>
                    </div>


                    <div class="kirb-diamond-modal">
                        <span class="btn btn__blue choose--btn kirb_byor_add_to_cart disabled"><?php echo esc_html__('ADD TO CART', 'diamond-ring-builder-jewelry');?></span>
                    </div>

                    <div class="drop-icon-section" style="justify-content: flex-start; gap:25px;">
                        <div data-bs-toggle="modal" href="#drop-Modal-popup" role="button">
                            <div class="sprite-popup-iocn drop"></div>
                            <div class="pera-text"><?php echo esc_html__('Drop Hint', 'diamond-ring-builder-jewelry');?></div>
                        </div>
                        <?php if($addons_data['consult_expert']['enabled']) { ?>
                        <div data-bs-toggle="modal" href="#expert-Modal-popup" role="button">
                            <div class="sprite-popup-iocn expert"></div>
                            <div class="pera-text"><?php echo esc_html__('Consult An Expert', 'diamond-ring-builder-jewelry');?></div>
                        </div>
                        <?php } ?>
                        <div data-bs-toggle="modal" href="#share-Modal-popup" role="button">
                                <div class="sprite-popup-iocn share"></div>
                                <div class="pera-text"><?php echo esc_html__('Share', 'diamond-ring-builder-jewelry');?></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

</div>
<?php 
    include_once('byor-review-page-modals/drop-hint-popup.php');
    include_once('byor-review-page-modals/view-360-popup.php');
    include_once('byor-review-page-modals/certificate-popup.php');
    include_once('byor-review-page-modals/consult-expert-popup.php');
    include_once('byor-review-page-modals/share-popup.php');

    wp_enqueue_script('kirb-byor-review-page-js',esc_url(KIRB_PLUGIN_DIR).'/scripts/byor-review-page.js', array('jquery','kirb-custom-common-script'), KIRB_VERSION_CONTROL['byor_review_page_js'], true);
    
    wp_localize_script( 'kirb-byor-review-page-js', 'BYORPageData', [
        'byor_diamond_title' => $byor_diamond['title'],
        'byor_ring_title' => $byor_ring_data->get_title(),
        'byor_ring_image' => $byor_ring_thumbnail,
        'byor_diamond_url' => $byor_diamond_url,
        'byor_ring_url' => $byor_ring_url,
        'byor_uid' => $kirb_byor_uid,
    ]);
    
    wp_localize_script( 'kirb-byor-review-page-js', 'DTranslations', [
        'email_not_recept' => esc_html__('Your email should not be the same as the recipient’s email.', 'diamond-ring-builder-jewelry'),
        'field_required' => esc_html__('This field is required', 'diamond-ring-builder-jewelry'),
        'invalid_mail' => esc_html__('Invalid Email', 'diamond-ring-builder-jewelry'),
    ]);
?>