<?php

if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Prefixed variables: gridpress_
 */

$gridpress_terms_class = '';

if (isset($gridpress_card_data['type']) && $gridpress_card_data['type'] === 'post') {

    if (
        isset($gridpress_display_settings['top_filter_tax']) &&
        !empty($gridpress_display_settings['top_filter_tax'])
    ) {
        $gridpress_taxonomy = $gridpress_display_settings['top_filter_tax'];

        $gridpress_terms_class = apply_filters(
            'gridpress_card_item_term_classes',
            $gridpress_terms_class,
            $gridpress_taxonomy,
            $gridpress_card_data['id']
        );
    }
}

/**
 * Background style handling (safe + conditional)
 */
$gridpress_style_attr = '';

if (!empty($gridpress_bg_color)) {
    $gridpress_style_attr = 'background-color:' . esc_attr($gridpress_bg_color) . ';';
}

?>

<div class="jc-cards<?php echo esc_attr($gridpress_terms_class); ?>"
    <?php echo !empty($gridpress_style_attr) ? 'style="' . esc_attr($gridpress_style_attr) . '"' : ''; ?>
>

    <?php
    $gridpress_card_url   = isset( $gridpress_card_data['url'] ) ? sanitize_url( (string) $gridpress_card_data['url'] ) : '';
    $gridpress_image_url  = isset( $gridpress_card_data['imag_url'] ) ? sanitize_url( (string) $gridpress_card_data['imag_url'] ) : '';
    $gridpress_card_title = isset( $gridpress_card_data['title'] ) ? sanitize_text_field( (string) $gridpress_card_data['title'] ) : '';
    ?>

    <div class="jc-cards-image">
        <a class="jc-cards-image-wrap" href="<?php echo esc_url( $gridpress_card_url ); ?>">
            <img
                src="<?php echo esc_url( $gridpress_image_url ); ?>"
                alt="<?php echo esc_attr( $gridpress_card_title ); ?>"
            />
        </a>
    </div>

    <div class="jc-cards-textbox">

        <?php
        if (
            isset($gridpress_card_data['type']) &&
            $gridpress_card_data['type'] === 'post' &&
            get_post_type($gridpress_card_data['id']) === 'product'
        ) {

            $gridpress_product_id = $gridpress_card_data['id'];
            $gridpress_rating_count = get_post_meta($gridpress_product_id, '_wc_review_count', true);
            ?>
            <div class="jc-cards-ratings">
                <ul>
                    <?php for ($gridpress_i = 1; $gridpress_i <= 5; $gridpress_i++) : ?>
                        <li>
                            <?php
                            /* translators: %d is the star number in a 5-star rating */
                            $gridpress_star_alt = sprintf( __( 'Star %d', 'gridpress-grid-layout-design' ), $gridpress_i );
                            ?>
                            <img
                                src="<?php echo esc_url(GRIDPRESS_URL . 'images/star.svg'); ?>"
                                alt="<?php echo esc_attr( $gridpress_star_alt ); ?>"
                            >
                        </li>
                    <?php endfor; ?>
                </ul>
                <strong class="jc-cards-ratings-count">
                    (<?php echo absint($gridpress_rating_count); ?>)
                </strong>
            </div>
        <?php } ?>

        <?php
        $gridpress_card_url   = isset( $gridpress_card_data['url'] ) ? sanitize_url( (string) $gridpress_card_data['url'] ) : '';
        $gridpress_card_title = isset( $gridpress_card_data['short_title'] ) ? sanitize_text_field( (string) $gridpress_card_data['short_title'] ) : '';
        ?>

        <strong class="jc-cards-title">
            <a href="<?php echo esc_url( $gridpress_card_url ); ?>">
                <?php echo esc_html( $gridpress_card_title ); ?>
            </a>
        </strong>

        <?php
        if (
            isset($gridpress_card_data['type']) &&
            $gridpress_card_data['type'] === 'post' &&
            get_post_type($gridpress_card_data['id']) === 'product'
        ) {

            $gridpress_product_id = $gridpress_card_data['id'];
            $gridpress_size_terms = wp_get_post_terms($gridpress_product_id, 'pa_size');

            if (!empty($gridpress_size_terms) && !is_wp_error($gridpress_size_terms)) {
                ?>
                <div class="jc-cards-sizes">
                    <?php foreach ($gridpress_size_terms as $gridpress_size_term) :

                        $gridpress_slug = isset($gridpress_size_term->slug) ? $gridpress_size_term->slug : '';
                        $gridpress_name = isset($gridpress_size_term->name) ? $gridpress_size_term->name : '';
                    ?>
                        <label class="jc-cards-option">
                            <input
                                type="radio"
                                name="pa_size"
                                value="<?php echo esc_attr($gridpress_slug); ?>"
                            >
                            <span class="jc-cards-option-text">
                                <?php echo esc_html(substr($gridpress_name, 0, 2)); ?>
                            </span>
                        </label>
                    <?php endforeach; ?>
                </div>
                <?php
            }

            $gridpress_price = get_post_meta($gridpress_product_id, '_price', true);
            $gridpress_sale_price = get_post_meta($gridpress_product_id, '_sale_price', true);

            if (!empty($gridpress_sale_price)) {

                $gridpress_regular_price = get_post_meta($gridpress_product_id, '_regular_price', true);
                ?>
                <div class="jc-cards-pricings">
                    <strong class="jc-cards-new-price">
                        <?php echo wp_kses_post(wc_price($gridpress_sale_price)); ?>
                    </strong>

                    <del class="jc-cards-old-price">
                        <?php echo wp_kses_post(wc_price($gridpress_regular_price)); ?>
                    </del>
                </div>
                <?php
            } else {
                ?>
                <div class="jc-cards-pricings">
                    <strong class="jc-cards-new-price">
                        <?php echo wp_kses_post(wc_price($gridpress_price)); ?>
                    </strong>
                </div>
                <?php
            }
        }
        ?>

        <div class="jc-cards-footer">
            <?php
            $gridpress_card_url = isset( $gridpress_card_data['url'] )
                ? sanitize_url( (string) $gridpress_card_data['url'] )
                : '';
            ?>

            <a href="<?php echo esc_url( $gridpress_card_url ); ?>" class="jc-cards-button">
                <?php esc_html_e( 'Add to Cart', 'gridpress-grid-layout-design' ); ?>
            </a>

            <label class="jc-cards-favourite">
                <span class="jc-cards-favourite-icon">
                    <img
                        src="<?php echo esc_url(GRIDPRESS_URL . 'images/heart.svg'); ?>"
                        alt="<?php echo esc_attr__('Favourite', 'gridpress-grid-layout-design'); ?>"
                    >
                </span>
            </label>
        </div>

    </div>
</div>