<?php 
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
$page_name = isset($_SERVER['REQUEST_URI']) ? sanitize_url(wp_unslash($_SERVER['REQUEST_URI'])) : '';
if(str_contains($page_name, '?')) {
    $page_name = explode('?', $page_name)[0];
}
$page_name = trim($page_name, '/');
$page_name = explode('/', $page_name);
$page_name = end($page_name);
$detailpage_uri_components = explode("-", $page_name);
$detailpage_id = end($detailpage_uri_components);
$is_valid_id = kirb_isValidMongoDBObjectId($detailpage_id);
if(!$is_valid_id) {
    // Check if the second last component is a valid uid
    // This is for the case when the product slug modified by the wordpress automatically
    $detailpage_id = $detailpage_uri_components[count($detailpage_uri_components)-2];
}
$detaildata = kirb_getDiamondByUID($detailpage_id, true);
$api_error = $detaildata['error'];
$kirb_page_ids = json_decode(kirbGetMeta("_page_ids"), true);
$addons_data = json_decode(kirbGetMeta('_setting_addon_data'), true);
$pendant = kirb_get_addon_value('pendant_catg');
$kirb_ud_localization = kirb_get_user_defined_localization();

global $kirb_filters_data;
if(!$api_error) {
    $kirb_filters_data = kirb_get_filters_data();
    $diamond = $detaildata['diamond'];
    $current_category = kirb_getDiamondCategory($diamond['category_id'], $kirb_filters_data, false);
    $dimensions = array_map('trim', explode(",",str_replace(array('-','*'), ',', $diamond['measurement'])));
    if(!isset($dimensions[2])){
        $dimensions[2] = "-";
    }
    $diamond['girdle'] = isset($diamond['girdle']) ? $diamond['girdle'] : "";
    $diamond['ta'] = isset($diamond['ta']) ? $diamond['ta'] : "";
    $diamond['td'] = isset($diamond['td']) ? $diamond['td'] : "";
    
    $is_diamond = $current_category == "NATURAL" || $current_category == "LAB CREATED";
    $is_gemstone = $current_category == "GEMSTONE";
    $is_colored_diamond = $current_category == "COLORED DIAMONDS";

    $d_structures = !$is_gemstone ? kirb_diamondStructureImage($diamond['shape'], $dimensions[0] . " mm", $dimensions[1]. " mm", $dimensions[2] . " mm", $diamond['lw_ratio'], $diamond['girdle'], $diamond['culetsize'], $diamond['ta'] . "%", $diamond['td'] . "%") : null;
    
    $recommended_Diamonds = kirb_getRecommendedDiamonds($diamond['_id'])['similarProducts'];

    $kirb_disable_byor = !kirbGetMeta('_setting_ring_setting_catg');

    // Enqueue scripts for the diamond detail page
    wp_enqueue_script('kirb-diamond-detail-page-script', KIRB_PLUGIN_DIR . "/scripts/diamond-detail-page.js", array('jquery','kirb-custom-common-script'), KIRB_VERSION_CONTROL['diamond-detail-page-js'], true);

    
    wp_localize_script( 'kirb-diamond-detail-page-script', 'DDetail', [
        'diamond' => $diamond,
        'diamond_err_img' => kirb_getShapeErrorImage($diamond['shape']),
        'loggedin' => is_user_logged_in(),
        'max_recent_products' => 10,
        'is_gemstone' => $is_gemstone,
        'is_diamond' => $is_diamond,
        'current_catg' => $current_category,
        'catgUrl' => kirb_getPageUrlById($kirb_page_ids[KIRB_ALL_PAGES_MAP[$current_category]])
    ]);

    wp_localize_script( 'kirb-diamond-detail-page-script', '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'),
        'copied' => esc_html__("Copied", 'diamond-ring-builder-jewelry')
    ]);
}

$language_group = "";
if($current_category == 'NATURAL') {
    $language_group = "natural-diamond-listing";
}else if($current_category == 'LAB CREATED') {
    $language_group = "lab-diamond-listing";
} else if($current_category == 'GEMSTONE') {
    $language_group = "gemstone-listing";
} else if($current_category == 'COLORED DIAMONDS') {
    $language_group = "colored-diamond-listing";
}

// Translation Lookup
require_once(KIRB_BASE_DIR . '/translations_lookup.php');

global $kirb_filter_values_translation_mapping;

if (!is_array($kirb_filter_values_translation_mapping)) {
    $kirb_filter_values_translation_mapping = [];
}
$kirb_filter_values_translation_mapping = array_merge($kirb_filter_values_translation_mapping, array(
    'fast_shipping' => esc_html(kirb_get_string($language_group,"string_fast_shipping", $kirb_ud_localization))
));

?>
<?php include_once 'common.php';?>
<main class="details-page">
    <div class="container">
        <?php kirbLoadByorStrip(); ?>
        <?php if(!$api_error) { ?>
        <section class="products-images">
                <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"><a href="" id="back-to-coll"><?php echo esc_html__('BACK TO COLLECTION', 'diamond-ring-builder-jewelry');?></a></div>
                    </div>
                </div>
                <div class="kirb-row-detail">
                <div class="col-md-6 col-12 img-section">
                    <div class="thumbnail-image d-none d-lg-block d-md-block">
                        <div class="kirb-img-item active" data-img="kirb_img1">
                            <img src="<?php echo esc_url($diamond['imageurl']);?>"
                                onerror="this.src='<?php echo esc_url(kirb_getShapeErrorImage($diamond['shape']))?>';this.onerror=null;this.style.width='auto';this.style.height='100%';"
                                alt="<?php echo esc_attr($diamond['shape']); ?>">
                        </div>

                        <?php if(!$is_gemstone) { ?>
                            <?php if (!empty($d_structures['shapeStructure'])) { ?>
                                <div class="kirb-img-item" data-img="kirb_img2">
                                    <?php echo kirb_sanitize_svg_output($d_structures['shapeStructure']);?>
                                </div>
                            <?php }?>
                            <?php if (!empty($d_structures['diamondStructure'])) { ?>
                                <div class="kirb-img-item" data-img="kirb_img3">
                                    <?php echo kirb_sanitize_svg_output($d_structures['diamondStructure']);?>
                                </div>
                            <?php }?>
                        <?php } ?>

                        <?php if (!empty($diamond['certlink']) && !$is_gemstone) { ?>
                            <?php 
                                if($diamond['lab'] == "IGI") {     
                            ?>
                                <div class="kirb-img-item prod-item certificate-div" data-img="none">
                                    <a href="<?php echo esc_url($diamond['certlink'])?>" target="_blank">
                                        <img src="<?php echo esc_url(KIRB_PLUGIN_DIR);?>/assets/images/<?php echo esc_attr(strtolower($diamond['lab']))?>.png" alt="<?php echo esc_attr($diamond['lab']); ?>">
                                    </a>
                                </div>
                            <?php }else{
                            ?>
                                <div class="kirb-img-item prod-item certificate-div" data-bs-toggle="modal" data-bs-target="#certificate-modal" data-img="none">
                                    <img src="<?php echo esc_url(KIRB_PLUGIN_DIR);?>/assets/images/<?php echo esc_attr(strtolower($diamond['lab']))?>.png" alt="<?php echo esc_attr($diamond['lab']); ?>">
                                </div>
                            <?php } ?>
                        <?php } ?>
                        <div class="kirb-img-item prod-item tryon-div" data-bs-toggle="modal" data-bs-target="#diamond-tryon-modal" data-img="none">
                            <img src="<?php echo esc_url(KIRB_PLUGIN_DIR);?>/assets/images/diamond-tryon.webp" alt="<?php echo esc_attr($diamond['lab']); ?>" style="object-fit: contain;">
                        </div>
                    </div>


                    <div class="thumbnail-image-slide slider-thumbnail d-block d-lg-none d-md-none">
                        <div class="kirb-img-item prod-item active" data-img="kirb_img1">
                            <img src="<?php echo esc_url($diamond['imageurl']);?>"
                                onerror="this.src='<?php echo esc_url(kirb_getShapeErrorImage($diamond['shape']))?>';this.onerror=null;"
                                alt="<?php echo esc_attr($diamond['shape']); ?>">
                        </div>
                        <?php if(!$is_gemstone) { ?>
                            <div class="kirb-img-item prod-item" data-img="kirb_img2">
                                <?php echo kirb_sanitize_svg_output($d_structures['shapeStructure']);?>
                            </div>
                            <div class="kirb-img-item prod-item" data-img="kirb_img3">
                                <?php echo kirb_sanitize_svg_output($d_structures['diamondStructure']);?>
                            </div>
                        <?php } ?>
                        <?php if (!empty($diamond['certlink']) && !$is_gemstone) { ?>
                            <?php 
                                if($diamond['lab'] == "IGI") {     
                            ?>
                                <div class="kirb-img-item prod-item certificate-div" data-img="none">
                                    <a href="<?php echo esc_url($diamond['certlink'])?>" target="_blank">
                                        <img src="<?php echo esc_url(KIRB_PLUGIN_DIR);?>/assets/images/<?php echo esc_attr(strtolower($diamond['lab']))?>.png" alt="<?php echo esc_attr($diamond['lab']); ?>">
                                    </a>
                                </div>
                            <?php }else{
                            ?>
                                <div class="kirb-img-item prod-item certificate-div" data-bs-toggle="modal" data-bs-target="#certificate-modal" data-img="none">
                                    <img src="<?php echo esc_url(KIRB_PLUGIN_DIR);?>/assets/images/<?php echo esc_attr(strtolower($diamond['lab']))?>.png" alt="<?php echo esc_attr($diamond['lab']); ?>">
                                </div>
                            <?php } ?>
                        <?php } ?>
                        <div class="kirb-img-item prod-item tryon-div" data-bs-toggle="modal" data-bs-target="#diamond-tryon-modal" data-img="none">
                            <img src="<?php echo esc_url(KIRB_PLUGIN_DIR);?>/assets/images/diamond-tryon.webp" alt="<?php echo esc_attr($diamond['lab']); ?>" style="object-fit: contain;">
                        </div>
                    </div>

                    <div class="main-image">
                        <div class="inner-main-image kirb_detail_img_preview kirb_img1">
                            <?php if($kirb_filters_data['sample']) { ?>
                                <div class="sample-div-detail-page"> <?php echo esc_html__('Sample', 'diamond-ring-builder-jewelry') ?></div>
                            <?php } ?>
                            <img src="<?php echo esc_url($diamond['imageurl']);?>"
                                onerror="this.src='<?php echo esc_url(kirb_getShapeErrorImage($diamond['shape']))?>';this.onerror=null;"
                                alt="<?php echo esc_attr($diamond['shape']); ?>">
                        </div>
                        <?php if(!$is_gemstone) { ?>
                            <div class="inner-main-image kirb_detail_img_preview kirb_img2" style="display:none;">
                                <?php echo kirb_sanitize_svg_output($d_structures['shapeStructure']);?>
                            </div>
                            <div class="inner-main-image kirb_detail_img_preview kirb_img3" style="display:none;">
                                <?php echo kirb_sanitize_svg_output($d_structures['diamondStructure']);?>
                            </div>
                        <?php } ?>
                        


                        <?php if(!empty($diamond['videourl'])) { ?>
                        <div>
                        <span data-bs-toggle="modal" data-bs-target="#view-360" class="view-360 view360-details-page-mobile" data-video-url="<?php echo esc_url($diamond['videourl']);?>" onclick="kirb_load_360(this);">
                            <div class="three-sixty_wrapper"><span class="bordered-icon-360-view-360"></span>
                            <span class="icon__text">360°</span></div>    
                        </span>
                        <div class="details-view view-360 view-360-mobile" data-bs-toggle="modal" data-bs-target="#view-360"
                            data-video-url="<?php echo esc_url($diamond['videourl']);?>" onclick="kirb_load_360(this);">
                            <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>
                        <?php } ?>


                    </div>
                </div>

                <div class="col-md-6 col-12 product--content">
                    <div class="diamond__content">
                        <h1 class="heading_h1 title"><?php echo esc_html(kirb_string_split_translate($diamond['title'], $kirb_filter_values_translation_mapping)); ?></h1>
                        <span class="sku"></span>
                        <div class="color-clarity-section ul-color d-flex list-unstyled">
                            <div class="inner-color-div color"><?php echo esc_html($kirb_filter_values_translation_mapping[ucfirst(strtolower($diamond['color']))] ?? ucfirst(strtolower($diamond['color']))) . ' ' . esc_html__('Color', 'diamond-ring-builder-jewelry') ?> </div>
                           <?php if($is_diamond) {  ?>  <div class="inner-color-div clarity"><?php echo esc_html($diamond['clarity']) . ' ' . esc_html__('Clarity', 'diamond-ring-builder-jewelry') ?> </div> <?php } ?>
                            <div class="inner-color-div cut-div cut"><?php echo esc_html($kirb_filter_values_translation_mapping[ucfirst($diamond['cut'])] ?? ucfirst($diamond['cut'])) . ' ' . esc_html__('Cut', 'diamond-ring-builder-jewelry'); ?></div>
                        </div>
                        <div class="diamond__content--time">
                            <i class="ri-time-line"></i>
                            <div class="delivery-time"><?php echo esc_html__('Deliver by', 'diamond-ring-builder-jewelry');?>: <b><?php echo esc_html(kirb_string_split_translate(kirb_getDeliveryDateByDiamondVendor($diamond['SettingId'], $kirb_filters_data), $kirb_filter_values_translation_mapping)); ?></b></div>
                        </div>
                        <div class="free-shipping diamond__content--info d-flex align-items-center">
                            <?php if($addons_data['free_shipping']['enabled']) { ?>
                                <div href="javascript:void(0);" class="inner-free-shipping display-icon-text">
                                    <span class="sprite-image-kirb free-shipping-icon kirb_disabled_anchor"></span>
                                    <?php
                                        $fs_link = $addons_data['free_shipping']['string'];
                                        $fs_html = stripslashes(kirb_get_string('diamond-detail','string_free_shipping', $kirb_ud_localization));
                                        if($fs_link) {
                                            $fs_html = '<a target="_blank" href="'.esc_url($fs_link).'">'.$fs_html.'</a>';
                                        }
                                        echo wp_kses_post($fs_html);
                                    ?>
                                </div>
                            <?php } ?>

                            <?php if($addons_data['easy_returns']['enabled']) { ?>
                                <div href="javascript:void(0);" class="inner-free-shipping display-easy-returns kirb_disabled_anchor">
                                    <span class="sprite-image-kirb easy-returns-icon"></span>
                                    <?php
                                        $er_link = $addons_data['easy_returns']['string'];
                                        $er_html = stripslashes(kirb_get_string('diamond-detail', 'string_easy_30_day_returns', $kirb_ud_localization));
                                        if($er_link) {
                                            $er_html = '<a target="_blank" href="'.esc_url($er_link).'">'.$er_html.'</a>';
                                        }
                                        echo wp_kses_post($er_html);
                                    ?>
                                </div>
                            <?php } ?>
                        </div>
                       
                       
                        <div class="kirb-diamond-modal">
                            <div class="price-section">
                                <span class="price">
                                    <?php echo esc_html(kirb_format_price($diamond['saleamt'], $kirb_site_config));?>
                                </span>
                                <?php 
                                    if($diamond['compPrice'] > $diamond['saleamt']) {
                                        echo '<del><span class="price-comp">' . esc_html(kirb_format_price($diamond['compPrice'], $kirb_site_config)) . '</span></del>';
                                    }
                                ?>
                            </div>
                            <span class="btn btn__blue choose--btn <?php echo esc_attr($kirb_disable_byor ? 'disabled' : ''); ?>" data-bs-toggle="modal" data-bs-target="#choose-bttn-modal" data-img="choose-bttn">
                            <?php echo $current_category == "GEMSTONE" ? esc_html__('CHOOSE GEMSTONE', 'diamond-ring-builder-jewelry') : esc_html__('CHOOSE DIAMOND', 'diamond-ring-builder-jewelry');?>
                            </span>
                            <div class="choose-bttn-popup modal fade" id="choose-bttn-modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
                                <div class="modal-dialog modal-dialog-centered" id="myModal">
                                    <div class="kirb-modal-content" >
                                    <div class="pera-popup modal-title styleHeadingTag" id=""><?php echo esc_html__('How would you like to proceed?', 'diamond-ring-builder-jewelry');?></div>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                        <span class="btn btn__blue choose--btn kirb_diamond_byor mt-2 choose_setting_btn diamond-popup-btn-width <?php echo $kirb_disable_byor ? 'disabled':''; ?>"><?php echo esc_html__('ADD TO SETTING', 'diamond-ring-builder-jewelry');?></span>
                                        <span class="btn btn__blue choose--btn kirb_diamond_choose add_cart_btn diamond-popup-btn-width disabled"><?php echo $is_gemstone ? esc_html__("ADD GEMSTONE TO CART",'diamond-ring-builder-jewelry') : esc_html__("ADD DIAMOND TO CART",'diamond-ring-builder-jewelry');?></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="drop-icon-section">
                            <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(kirb_get_string('diamond-detail', 'string_consult_an_expert', $kirb_ud_localization)); ?>
                                </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>

        <?php
            $d_lbl = $is_gemstone ? esc_html__('Gemstone', 'diamond-ring-builder-jewelry'): esc_html__('Diamond', 'diamond-ring-builder-jewelry');
            $d_lbl_y = $is_gemstone ? esc_html__('Your Gemstone', 'diamond-ring-builder-jewelry'): esc_html__('Your Diamond', 'diamond-ring-builder-jewelry');
            $carat_string = ucfirst(stripslashes(kirb_get_string('diamond-detail','dynamic_carat_string')));
            $carat_string = str_replace('{weight}', $diamond['weight'], $carat_string);
            $carat_weight_section = '
                <div class="carat-section-outer-div">
                    <div class="heading-description">
                        <h3 class="desc-heading_h2">' . esc_html__('Carat', 'diamond-ring-builder-jewelry') . ': '. '<span class="font-weight-semi-bold carat">'.$diamond['weight'].' CT</span>
                        </h3>
                        <div class="desc-para-section">
                            '.$carat_string.'
                        </div>
                    </div>
                    <div class="cut-images-outer">
                        <div class="cut-images-pointer pointer-create-css-carat pointer1-image-point">
                            '.$d_lbl_y.'
                        </div>
                        <hr class="styled-hr">
                        <div class="cut-image-assets images-4Cs-common">
                            <img src="'.esc_url(KIRB_PLUGIN_DIR).'/assets/images/Carat-with-text.webp" alt="carat image">
                        </div>
                    </div>
                </div>
            ';

            $clarity_section = '
                <div class="carat-section-outer-div '.($is_colored_diamond ? 'mt-3':'').'">
                    <div class="heading-description">
                        <h3 class="desc-heading_h2">'.esc_html__('Clarity', 'diamond-ring-builder-jewelry').':
                            <span class="text-uppercase font-weight-semi-bold clarity">'.esc_html($diamond['clarity']).'</span>
                        </h3>
                        <div class="desc-para-section">'.
                            esc_html(ucfirst(stripslashes(kirb_get_string('diamond-detail','dynamic_clarity_'.str_replace(array('FL','IF'),'FLIF',$diamond['clarity']).'_string', $kirb_ud_localization))))
                        .'</div>
                    </div>

                    <div class="cut-images-outer">
                        <div
                            class="clarity-images-pointer pointer-create-css pointer1-image-point clarity-diamond-position-'.esc_attr(sanitize_html_class($diamond['clarity'])).'">'.
                            esc_html__('Your Diamond', 'diamond-ring-builder-jewelry').'
                        </div>
                        <hr class="styled-hr">
                        <div class="cut-image-assets images-4Cs-common">
                            <img src="'.esc_url(KIRB_PLUGIN_DIR).'/assets/images/Clarity-with-text.webp" alt="cut image">
                        </div>
                    </div>
                </div>
            ';
        ?>
        
        <section class="pro__details diam-info-outer-section">
            <?php if($is_diamond) { ?>
                <h2 class="heading_2"><?php echo esc_html__('Diamond Information', 'diamond-ring-builder-jewelry');?></h2>
                <div class="description">
                <?php
                    echo esc_html(
                        sprintf(
                            __("This %1\$s Carat %2\$s %3\$s %4\$s Color %5\$s Clarity has %6\$s proportions and a diamond grading report from %7\$s",'diamond-ring-builder-jewelry'),
                            number_format($diamond['weight'], 2),
                            ($kirb_filter_values_translation_mapping[$diamond['shape']] ?? $diamond['shape']),
                            ($current_category == "LAB CREATED" ? esc_html__('Lab','diamond-ring-builder-jewelry') . ' ' : '') . $d_lbl,
                            ($kirb_filter_values_translation_mapping[$diamond['color']] ?? $diamond['color']),
                            $diamond['clarity'],
                            ($kirb_filter_values_translation_mapping[$diamond['cut']] ?? $diamond['cut']),
                            $diamond['lab']
                        )
                    );
                ?>
                </div>
            <?php } ?>
            <div class="pro__details--box diam-information-box">
                <?php 
                    $sku_key = esc_html__('SKU', 'diamond-ring-builder-jewelry');
                    $shape_key = esc_html__('Shape', 'diamond-ring-builder-jewelry'); 
                    $carat_key = esc_html__('Carat', 'diamond-ring-builder-jewelry'); 
                    $color_key = esc_html__('Color', 'diamond-ring-builder-jewelry'); 
                    $clarity_key = esc_html__('Clarity', 'diamond-ring-builder-jewelry'); 
                    $cut_key = esc_html__('Cut', 'diamond-ring-builder-jewelry'); 
                    $polish_key = esc_html__('Polish', 'diamond-ring-builder-jewelry'); 
                    $stock_no_key = esc_html__('Stock Number', 'diamond-ring-builder-jewelry'); 
                    $meas_key = esc_html__('Measurements (mm)', 'diamond-ring-builder-jewelry'); 
                    $cert_key = esc_html__('Lab Report', 'diamond-ring-builder-jewelry'); 
                    $symm_key = esc_html__('Symmetry', 'diamond-ring-builder-jewelry');
                    $flour_key = esc_html__('Fluorescence', 'diamond-ring-builder-jewelry');
                    $table_key = esc_html__('Table %', 'diamond-ring-builder-jewelry');
                    $depth_key = esc_html__('Depth %', 'diamond-ring-builder-jewelry');
                    $lwratio_key = esc_html__('L/W Ratio', 'diamond-ring-builder-jewelry');
                    $lab_key = esc_html__('Lab Report', 'diamond-ring-builder-jewelry');
                    $location_key = esc_html__('Location', 'diamond-ring-builder-jewelry');
                    $gem_type_key = esc_html__('Gemstone Type', 'diamond-ring-builder-jewelry'); 
                    $intensity_key = esc_html__('Intensity', 'diamond-ring-builder-jewelry'); 
                    
                    
                    // NOTE: Flipped the values of sku and stone_no as per the requirements
                    if($is_diamond) {
                        $dlist1 = array(
                            $sku_key => $diamond['stone_no'], 
                            $stock_no_key => $diamond['sku'], 
                            $shape_key => esc_html($kirb_filter_values_translation_mapping[$diamond['shape']] ?? $diamond['shape']), 
                            $carat_key => number_format($diamond['weight'],2) . "ct", 
                            $color_key => ucfirst(strtolower($diamond['color'])),  
                            $clarity_key => $diamond['clarity'], 
                            $cut_key =>  esc_html($kirb_filter_values_translation_mapping[ucfirst($diamond['cut'])] ?? $diamond['cut']), 
                            $polish_key => esc_html($kirb_filter_values_translation_mapping[ucfirst($diamond['polish'])] ?? $diamond['polish']),
                        );

                        $dlist2 = array(
                            $symm_key => esc_html($kirb_filter_values_translation_mapping[ucfirst($diamond['symm'])] ?? ucfirst($diamond['symm'])), 
                            $flour_key => esc_html($kirb_filter_values_translation_mapping[ucfirst($diamond['fluor'])] ?? $diamond['fluor']), 
                            $meas_key => str_replace("-","*",$diamond['measurement']), 
                            $table_key => number_format($diamond['ta'],1), 
                            $depth_key => number_format($diamond['td'],1), 
                            $lwratio_key => $diamond['lw_ratio'], $lab_key => $diamond['lab'], 
                            $location_key => isset($diamond['location']) && !empty($diamond['location']) ? $diamond['location'] : '-'
                        );
                    }else if($is_gemstone) {
                        $dlist1 = array(
                            $sku_key => $diamond['stone_no'], 
                            $stock_no_key => $diamond['sku'],
                            $shape_key => esc_html($kirb_filter_values_translation_mapping[$diamond['shape']] ?? $diamond['shape']), 
                            $carat_key => number_format($diamond['weight'],2) . "ct", 
                            $gem_type_key => esc_html($kirb_filter_values_translation_mapping[ucfirst(strtolower($diamond['gemType']))] ?? $diamond['gemType']), 
                            $meas_key => str_replace("-","*",$diamond['measurement']), 
                            $color_key => esc_html($kirb_filter_values_translation_mapping[ucfirst(strtolower($diamond['color']))] ?? $diamond['color']),
                            $cert_key  => 'Non-Cert', 
                            $location_key => isset($diamond['location']) && !empty($diamond['location']) ? $diamond['location'] : '-'
                        );

                        $dlist2 = array();
                    }else if($is_colored_diamond) {
                        $dlist1 = array(
                            $sku_key => $diamond['stone_no'], 
                            $stock_no_key => $diamond['sku'], 
                            $shape_key => esc_html($kirb_filter_values_translation_mapping[$diamond['shape']] ?? $diamond['shape']), 
                            $carat_key => number_format($diamond['weight'],2) . "ct", 
                            $clarity_key => $diamond['clarity'], 
                            $color_key => esc_html($kirb_filter_values_translation_mapping[ucfirst(strtolower($diamond['color']))] ?? $diamond['color']),  
                            $cert_key => 'Non-Cert', 
                            $intensity_key => esc_html($kirb_filter_values_translation_mapping[ucfirst(strtolower($diamond['intensity']))] ?? $diamond['intensity']),  
                            $meas_key => str_replace("-","*",$diamond['measurement']), 
                            $location_key => isset($diamond['location']) && !empty($diamond['location']) ? $diamond['location'] : '-'
                        );
                        
                        $dlist2 = array();
                    }
                    
                    $list1_keys_html = "";
                    $list1_values_html = "";
                    $list2_keys_html = "";
                    $list2_values_html = "";

                    foreach($dlist1 as $key => $value) {
                        $list1_keys_html .= "<div>$key:</div>";
                        $list1_values_html .= "<div>". esc_html($value)."</div>";
                    }

                    foreach($dlist2 as $key => $value) {
                        $list2_keys_html .= "<div>$key:</div>";
                        $list2_values_html .= "<div>". esc_html($value)."</div>";
                    }
                ?>
                <?php if($is_diamond) { ?>
                <div class="pro__details-ul mb-0 list-unstyled list1">
                    <div class="pro__details-li">
                        <?php echo wp_kses_post($list1_keys_html); ?>
                    </div>
                    <div class="pro__details-li list1-value">
                        <?php echo wp_kses_post($list1_values_html); ?>
                    </div>
                </div>
                <?php } ?>
                <?php if($is_diamond) { ?>
                <div class="pro__details-ul mb-0 list-unstyled list2">
                    <div class="pro__details-li">
                        <?php echo wp_kses_post($list2_keys_html); ?>
                    </div>
                    <div class="pro__details-li list2-value">
                        <?php echo wp_kses_post($list2_values_html); ?>
                    </div>
                </div>
                <?php }else{ ?>
                    <div class="gemstone-pro__details">
                        <h2 class="desc-heading_h2"><?php echo $is_gemstone ? esc_html__('Gemstone Information', 'diamond-ring-builder-jewelry') : esc_html__('Diamond Information', 'diamond-ring-builder-jewelry');?></h2>
                        <div class="description">
                            <?php
                                echo esc_html(
                                    sprintf(
                                        __("This %1\$s Carat %2\$s %3\$s %4\$s Color %5\$s Clarity has %6\$s proportions and a diamond grading report from %7\$s", 'diamond-ring-builder-jewelry'),
                                        number_format($diamond['weight'], 2),
                                        ($kirb_filter_values_translation_mapping[$diamond['shape']] ?? $diamond['shape']),
                                        ($current_category == "LAB CREATED" ? esc_html__('Lab', 'diamond-ring-builder-jewelry') . ' ' : '') . $d_lbl,
                                        ($kirb_filter_values_translation_mapping[$diamond['color']] ?? $diamond['color']),
                                        $diamond['clarity'],
                                        ($kirb_filter_values_translation_mapping[$diamond['cut']] ?? $diamond['cut']),
                                        $diamond['lab']
                                    )
                                );
                            ?>
                        </div>
                        <div class="pro__details-ul mb-0 list-unstyled list1">
                            <div class="pro__details-li">
                                <?php echo wp_kses_post($list1_keys_html); ?>
                            </div>
                            <div class="pro__details-li list1-value">
                                <?php echo wp_kses_post($list1_values_html); ?>
                            </div>
                        </div>
                    </div>
                    <div class="section-carat-weight <?php echo esc_attr(!$is_diamond ? 'gemstone-carat-weight' : ''); ?>">
                        <?php
                            echo wp_kses_post($carat_weight_section); 
                            if($is_colored_diamond) {
                                echo wp_kses_post($clarity_section);
                            } 
                        ?>
                    </div>
                    
                <?php } ?>
            </div>

        </section>

        <div class="divide-parts-flex">
            <?php 
                if(!$is_gemstone && !$is_colored_diamond) {
            ?>
            <div class="section-carat-weight">
                <?php echo wp_kses_post($carat_weight_section); ?>
            </div>
            <div class="section-carat-weight">
                <div class="carat-section-outer-div">
                    <div class="heading-description">
                        <h3 class="desc-heading_h2"><?php echo esc_html__('Cut', 'diamond-ring-builder-jewelry') ?>: <span class="font-weight-semi-bold cut"><?php echo esc_html($kirb_filter_values_translation_mapping[ucfirst($diamond['cut'])] ?? $diamond['cut']) ?></span>
                        </h3>
                        <?php 
                            $cut_part = esc_html(strtolower($kirb_filter_values_translation_mapping[ucfirst($diamond['cut'])]) ?? $diamond['cut']);
                            $cut_string  = stripslashes(kirb_get_string('diamond-detail','dynamic_cut_string'));
                            $cut_string = str_replace('{cut}', $cut_part, $cut_string);
                        ?>
                        <div class="desc-para-section">
                         <?php echo esc_html(ucfirst($cut_string));?>
                        </div>
                    </div>
                    <div class="cut-images-outer">
                        <div
                            class="cut-images-pointer pointer-create-css pointer1-image-point cut-diamond-position-<?php echo esc_attr(str_replace(' ','',strtolower($diamond['cut']))); ?>">
                            <?php echo esc_html__('Your Diamond', 'diamond-ring-builder-jewelry');?>
                        </div>
                        <hr class="styled-hr">
                        <div class="cut-image-assets images-4Cs-common">
                            <img src="<?php echo esc_url(KIRB_PLUGIN_DIR) ?>/assets/images/Cut-with-text.webp" alt="cut image">
                        </div>
                    </div>
                </div>
            </div>
            <?php } ?>
        </div>

        <?php 
            if(!$is_gemstone && !$is_colored_diamond) {
        ?>
        <div class="divide-parts-flex">
            <div class="section-carat-weight section-width-carat">
                <div class="carat-section-outer-div">
                    <div class="heading-description">
                        <h3 class="desc-heading_h2"> <?php echo esc_html__('Color', 'diamond-ring-builder-jewelry');?>:
                            <span class="font-weight-semi-bold color"><?php echo esc_html($kirb_filter_values_translation_mapping[ucfirst(strtolower($diamond['color']))] ?? ucfirst(strtolower($diamond['color']))); ?></span>
                        </h3>
                        <?php
                             $color_string  = stripslashes(kirb_get_string('diamond-detail','dynamic_color_'.$diamond['color'].'_string', $kirb_ud_localization));
                        ?>
                        <div class="desc-para-section">
                        <?php echo esc_html(ucfirst($color_string));?>
                        </div>
                    </div>
                    <div class="color-images-outer">
                        <div class="color-images-pointer pointer-create-css pointer1-image-point color-diamond-position-<?php echo esc_attr(sanitize_html_class($diamond['color'])); ?>">
                        <?php echo esc_html__('Your Diamond', 'diamond-ring-builder-jewelry');?>
                        </div>
                        <hr class="styled-hr">
                        <div class="cut-image-assets images-4Cs-common">
                            <img src="<?php echo esc_url(KIRB_PLUGIN_DIR) ?>/assets/images/Color-with-text.webp" alt="cut image">
                        </div>
                    </div>
                </div>
            </div>

            <div class="section-carat-weight section-width-carat">
                <?php echo wp_kses_post($clarity_section); ?>
            </div>
        </div>
        <?php } ?>

        <?php if($recommended_Diamonds) { ?>
            <div class="site-wapper">
                <div class="title compareDiamond_title fontMedium heading--2">
                    <h3 class="heading-h2"><?php echo esc_html__('You May Also Like', 'diamond-ring-builder-jewelry');?></h3>
                </div>
                <section class="compareDiamonds d-flex">
                    <div class="diamondCompare_container d-flex">
                        <ul>
                            <li class="diamondList">
                            <ul class="diamondList_first">
                                    <li class="diamondList_heading"></li>
                                    <li class="diamondList_image"></li>
                                    <li class="diamondList_text">
                                        <span class="diamondList_first_span-margin"><?php echo esc_html__('Price', 'diamond-ring-builder-jewelry');?>:</span></li>
                                    <li class="diamondList_text"><span class="diamondList_first_span-margin"><?php echo esc_html__('Difference', 'diamond-ring-builder-jewelry');?>:</span></li>
                                    <li class="diamondList_text"><span class="diamondList_first_span-margin"><?php echo esc_html__('Shape', 'diamond-ring-builder-jewelry');?>:</span></li>
                                    <li class="diamondList_text"><span class="diamondList_first_span-margin"><?php echo esc_html__('Carat', 'diamond-ring-builder-jewelry');?>:</span></li>
                                    <li class="diamondList_text"><span class="diamondList_first_span-margin"><?php echo esc_html__('Color', 'diamond-ring-builder-jewelry');?>:</span></li>
                                    <li class="diamondList_text"><span class="diamondList_first_span-margin"><?php echo esc_html__('Clarity', 'diamond-ring-builder-jewelry');?>:</span></li>
                                    <li class="diamondList_text"><span class="diamondList_first_span-margin"><?php echo esc_html__('Cut', 'diamond-ring-builder-jewelry');?>:</span></li>
                                    <li class="diamondList_lastt"></li>
                                </ul>
                            </li>

                        </ul>
                        <ul class="diamondCompare_list d-flex">
                            <li class="diamondList active-diamond-list">
                                <ul class="diamondList_second active-diamond">
                                    <li class="diamondList_heading"><?php echo esc_html__('Current Diamond', 'diamond-ring-builder-jewelry');?></li>
                                    <li class="diamondList_image">
                                        <img src="<?php echo esc_url($diamond['imageurl']);?>" width="281"
                                            height="281"
                                            onerror="this.onerror=null; this.src='<?php echo esc_url(kirb_getShapeErrorImage($diamond['shape'])) ?>'"
                                            alt="<?php echo esc_attr($diamond['shape']); ?>" loading="lazy">
                                    </li>
                                    <li class="diamondList_text"><span><?php echo esc_html(kirb_format_price($diamond['saleamt'], $kirb_site_config)); ?></span></li>
                                    <li class="diamondList_text"><span>-</span></li>
                                    <li class="diamondList_text"><span><?php echo esc_html__( $diamond['shape'], 'diamond-ring-builder-jewelry') ?></span></li>
                                    <li class="diamondList_text"><span><?php echo esc_html(number_format($diamond['weight'],2)); ?></span></li>
                                    
                                    <li class="diamondList_text"><span><?php echo ucfirst(strtolower(esc_html($diamond['color']))); ?></span></li>
                                    <li class="diamondList_text"><span><?php echo esc_html($diamond['clarity']); ?></span></li>
                                    <li class="diamondList_text"><span><?php echo esc_html__( $diamond['cut'], 'diamond-ring-builder-jewelry') ?></span></li>
                                    <li class="diamondList_lastt">
                                        <?php if(!$kirb_disable_byor) { ?>
                                            <a href="#" class="viewDiamond_btn" data-bs-toggle="modal" data-bs-target="#choose-bttn-modal" data-img="choose-bttn"><?php echo esc_html__($current_category == "GEMSTONE" ? "Choose This Gemstone" : "Choose This Diamond", 'diamond-ring-builder-jewelry');?></a>
                                        <?php } ?>
                                    </li>
                                </ul>
                            </li>
                            <?php foreach ($recommended_Diamonds as $recommend_diamond): ?>
                                <li class="diamondList">
                                    <ul class="diamondList_second">
                                        <li class="diamondList_heading">
                                            <?php echo esc_html__('Similar Diamond', 'diamond-ring-builder-jewelry');?> </li>
                                        <li class="diamondList_image">
                                            <img src="<?php echo esc_url($recommend_diamond['imageurl']) ?>" width="281" height="281"
                                                alt="<?php echo esc_attr($recommend_diamond['shape']); ?>" loading="lazy"
                                                onerror="this.onerror=null; this.src='<?php echo esc_url(kirb_getShapeErrorImage($recommend_diamond['shape'])) ?>'">
                                        </li>
                                        <li class="diamondList_text">
                                            <span><?php echo esc_html(kirb_format_price($recommend_diamond['saleamt'], $kirb_site_config)); ?></span></li>
                                        <li class="diamondList_text">
                                            <span>
                                                <?php 
                                                    if((intval($recommend_diamond['saleamt']) - intval($diamond['saleamt'])) == 0) {
                                                        echo '-';
                                                    } else {
                                                        echo esc_html(((kirb_price_roundoff($recommend_diamond['saleamt'], $kirb_site_config) - kirb_price_roundoff($diamond['saleamt'], $kirb_site_config) < 0 ? '-' : '+') . kirb_format_price(abs(kirb_price_roundoff($recommend_diamond['saleamt'], $kirb_site_config) - kirb_price_roundoff($diamond['saleamt'], $kirb_site_config)))));
                                                    } 
                                                ?>
                                            </span>
                                        </li>
                                        <li class="diamondList_text"><span><?php echo esc_html__( $recommend_diamond['shape'], 'diamond-ring-builder-jewelry')?></span></li>
                                        <li class="diamondList_text">
                                            <?php 
                                                if($recommend_diamond['weight'] > $diamond['weight']) {
                                                    $arrowClassW = 'iconUp';
                                                }else if($recommend_diamond['weight'] < $diamond['weight']) {
                                                    $arrowClassW = 'iconDown';
                                                } else {
                                                    $arrowClassW = '';
                                                }
                                            ?>
                                            <span class="updownArrow <?php echo esc_attr($arrowClassW) ?>"><?php echo esc_html(number_format($recommend_diamond['weight'],2)); ?></span>
                                        </li>
                                        
                                        <li class="diamondList_text">
                                        <?php
                                                $color_arr_sort = array("L","K","J","I","H","G","F","E","D");
                                                $idx1 = array_search($recommend_diamond['color'], $color_arr_sort);
                                                $idx2 = array_search($diamond['color'], $color_arr_sort);
                                                if($idx1 > $idx2) {
                                                    $arrowClassCol = 'iconUp';
                                                }else if($idx1 < $idx2) {
                                                    $arrowClassCol = 'iconDown';
                                                } else {
                                                    $arrowClassCol = '';
                                                }
                                            ?>
                                            <span class="updownArrow <?php echo esc_attr($arrowClassCol)?>"><?php echo esc_html(ucfirst(strtolower($recommend_diamond['color']))); ?></span>
                                        </li>
                                        <li class="diamondList_text">
                                            <?php
                                                $clarity_arr_sort = array("I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF");
                                                $idx1 = array_search(str_replace("FL","IF",$diamond['clarity']), $clarity_arr_sort);
                                                $idx2 = array_search(str_replace("FL","IF",$recommend_diamond['clarity']), $clarity_arr_sort);
                                                if($idx1 > $idx2) {
                                                    $arrowClassCl = 'iconDown';
                                                }else if($idx1 < $idx2) {
                                                    $arrowClassCl = 'iconUp';
                                                } else {
                                                    $arrowClassCl = '';
                                                }
                                            ?>
                                            <span class="updownArrow <?php echo esc_attr($arrowClassCl)?>"><?php echo esc_html($recommend_diamond['clarity']) ?></span>
                                        </li>
                                            <li class="diamondList_text">
                                            <span class="updownArrow"><?php echo esc_html__($recommend_diamond['cut'], 'diamond-ring-builder-jewelry') ?></span>
                                        </li>
                                        <li class="diamondList_lastt">
                                            <a
                                                href="<?php echo esc_url(kirb_getPageUrlById($kirb_page_ids['diamond-detail']) . kirb_prepareDiamondSlug($recommend_diamond)) . '/'; ?>"><?php echo esc_html__("View","diamond-ring-builder-jewelry");?></a>
                                        </li>
                                    </ul>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </section>
            </div>
        <?php } ?>

        <?php } else { ?>
            <div class="alert alert-danger" role="alert">
                <h4 class="alert-heading"><?php echo esc_html__('Error!', 'diamond-ring-builder-jewelry');?></h4>
                <p><?php echo esc_html__('Something went wrong. Please try again later.', 'diamond-ring-builder-jewelry');?></p>
            </div>
        <?php } ?>
    </div>
    
</main>

<?php if(!$api_error) { 
    
    ob_start();
    include_once('detail-page-modals/drop-hint-popup.php');
    $drop_hint_popup = ob_get_clean();

    ob_start();
    include_once('detail-page-modals/consult-expert-popup.php');
    $consult_expert_popup = ob_get_clean();

    ob_start();
    include_once('detail-page-modals/share-popup.php');
    $share_popup = ob_get_clean();

    ob_start();
    include_once('detail-page-modals/view-360-popup.php');
    $view_360_popup = ob_get_clean();

    ob_start();
    include_once('detail-page-modals/certificate-popup.php');
    $certificate_popup = ob_get_clean();

    ob_start();
    include_once('detail-page-modals/diamond-tryon.php');
    $diamond_tryon = ob_get_clean();

    wp_localize_script( 'kirb-diamond-detail-page-script', 'DViewModals', [
        'drop_hint_popup_code' => $drop_hint_popup,
        'consult_expert_popup_code' => $consult_expert_popup,
        'share_popup_code' => $share_popup,
        'view_360_popup_code' => $view_360_popup,
        'certificate_popup_code' => $certificate_popup,
        'diamond_tryon' => $diamond_tryon
    ]);
}
