<?php

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

$gridpress_terms_class   = '';
$gridpress_terms_str     = '';
$gridpress_comments_count = 0;

/**
 * DATA PREPARATION
 */
if (isset($gridpress_card_data['type']) && $gridpress_card_data['type'] === 'post') {

    $gridpress_comments_count = get_comments_number($gridpress_card_data['id']);

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

        $gridpress_tax_terms = wp_get_post_terms(
            $gridpress_card_data['id'],
            $gridpress_taxonomy,
            ['fields' => 'names']
        );

        if (!is_wp_error($gridpress_tax_terms) && !empty($gridpress_tax_terms)) {
            $gridpress_terms_str = implode(', ', array_map('sanitize_text_field', $gridpress_tax_terms));
        }

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

} else {
    $gridpress_comments_count = isset($gridpress_card_data['comments'])
        ? absint($gridpress_card_data['comments'])
        : 0;

    $gridpress_terms_str = isset($gridpress_card_data['category'])
        ? sanitize_text_field($gridpress_card_data['category'])
        : '';
}

/**
 * COMMENTS LABEL (FIX: avoids inline translation output issues)
 */
$gridpress_comments_label = sprintf(
    /* translators: %s: number of comments */
    _n('%s comment', '%s comments', $gridpress_comments_count, 'gridpress-grid-layout-design'),
    number_format_i18n($gridpress_comments_count)
);
/**
 * DATE SAFE
 */
$gridpress_post_date = !empty($gridpress_card_data['post_date'])
    ? sanitize_text_field(str_replace('/', '-', $gridpress_card_data['post_date']))
    : '';
/**
 * COMMENT LINK PART (FIX: removes inline echo ' / ')
 */
$gridpress_show_comments = !empty($gridpress_display_settings['show_comments']) &&
                            $gridpress_display_settings['show_comments'] === 'on';
?>
<div class="jc-cards<?php echo esc_attr($gridpress_terms_class); ?>">
    <div class="jc-cards-holder">
        <div class="jc-cards-meta">
            <?php if (
                !empty($gridpress_post_date) &&
                !empty($gridpress_display_settings['show_date']) &&
                $gridpress_display_settings['show_date'] === 'on'
            ) : ?>
                <time class="jc-cards-date"
                      datetime="<?php echo esc_attr(wp_date('Y-m-d', strtotime($gridpress_post_date))); ?>">
                    <?php echo esc_html(wp_date(get_option('date_format'), strtotime($gridpress_post_date))); ?>
                </time>
                <?php if ($gridpress_show_comments) : ?>
                    <span class="jc-cards-separator"> / </span>
                <?php endif; ?>

            <?php endif; ?>
           <?php
            $gridpress_card_url = isset( $gridpress_card_data['url'] )
                ? esc_url_raw( (string) $gridpress_card_data['url'] )
                : '';
            ?>
            <?php if ( $gridpress_show_comments ) : ?>
                <a href="<?php echo esc_url( $gridpress_card_url . '#comments' ); ?>"
                class="gridpress-dynm-carditm-comments">
                    <?php echo esc_html( $gridpress_comments_label ); ?>
                </a>
            <?php endif; ?>
        </div>
        <?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 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 (
                !empty($gridpress_terms_str) &&
                !empty($gridpress_display_settings['show_terms']) &&
                $gridpress_display_settings['show_terms'] === 'on'
            ) : ?>
                <strong class="jc-cards-category">
                    <?php echo esc_html($gridpress_terms_str); ?>
                </strong>
            <?php endif; ?>

            <?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'] ) : '';
            ?>
            <h2>
                <a href="<?php echo esc_url( $gridpress_card_url ); ?>">
                    <?php echo esc_html( $gridpress_card_title ); ?>
                </a>
            </h2>
        </div>
    </div>
    <?php if (!empty($gridpress_display_settings['show_button']) &&
              $gridpress_display_settings['show_button'] === 'on') : ?>
        <?php
        $gridpress_card_url = isset( $gridpress_card_data['url'] )
            ? sanitize_url( (string) $gridpress_card_data['url'] )
            : '';
        $gridpress_icon_url = trailingslashit( GRIDPRESS_URL ) . 'images/arrow-white.svg';
        ?>
        <div class="jc-cards-foot">
            <a href="<?php echo esc_url( $gridpress_card_url ); ?>"
            class="jc-cards-link">
                <img
                    src="<?php echo esc_url( $gridpress_icon_url ); ?>"
                    alt="<?php echo esc_attr__( 'Read More', 'gridpress-grid-layout-design' ); ?>"
                />
            </a>
        </div>
    <?php endif; ?>
</div>