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

$gridpress_terms_class = '';

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

    if ( ! 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']
        );
    }
}
?>

<div class="jc-cards<?php echo esc_attr( $gridpress_terms_class ); ?>">
    <?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['id'] ) &&
            $gridpress_card_data['type'] === 'post' &&
            get_post_type( $gridpress_card_data['id'] ) === 'product'
        ) {
            $gridpress_product_id = (int) $gridpress_card_data['id'];
            $gridpress_rating_count = (int) get_post_meta( $gridpress_product_id, '_wc_review_count', true );
            ?>
            <div class="jc-cards-ratings">
                <ul>
                    <?php for ( $gridpress_i = 0; $gridpress_i < 5; $gridpress_i++ ) : ?>
                        <li>
                            <img src="<?php echo esc_url( GRIDPRESS_URL . 'images/star.svg' ); ?>"
                                 alt="<?php echo esc_attr__( 'Star', 'gridpress-grid-layout-design' ); ?>">
                        </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>
        <div class="jc-cards-footer">
            <?php
            if (
                isset( $gridpress_card_data['type'], $gridpress_card_data['id'] ) &&
                $gridpress_card_data['type'] === 'post' &&
                get_post_type( $gridpress_card_data['id'] ) === 'product'
            ) {

                $gridpress_product_id = (int) $gridpress_card_data['id'];

                $gridpress_price      = get_post_meta( $gridpress_product_id, '_price', true );
                $gridpress_sale_price = get_post_meta( $gridpress_product_id, '_sale_price', true );
                $gridpress_regular    = get_post_meta( $gridpress_product_id, '_regular_price', true );
                echo '<div class="jc-cards-pricings">';
                if ( ! empty( $gridpress_sale_price ) ) {
                    echo '<strong class="jc-cards-new-price">' . wp_kses_post( wc_price( $gridpress_sale_price ) ) . '</strong>';
                    echo '<del class="jc-cards-old-price">' . wp_kses_post( wc_price( $gridpress_regular ) ) . '</del>';
                } else {
                    echo '<strong class="jc-cards-new-price">' . wp_kses_post( wc_price( $gridpress_price ) ) . '</strong>';
                }
                echo '</div>';
            }
            ?>
            <a class="jc-cards-cart"
               href="<?php echo esc_url( $gridpress_card_data['url'] ?? '' ); ?>">
                <img src="<?php echo esc_url( GRIDPRESS_URL . 'images/shopping-cart.svg' ); ?>"
                     alt="<?php echo esc_attr__( 'Add to Cart', 'gridpress-grid-layout-design' ); ?>">
            </a>
        </div>
    </div>
</div>