<?php

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

$gridpress_terms_class = '';
$gridpress_comments_count = 0;
$gridpress_terms_str = '';
$gridpress_card_data        = $gridpress_card_data ?? [];

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

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

    if (
        isset($gridpress_display_settings['top_filter_tax']) &&
        !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'])
        : '';
}

/**
 * Format comments safely
 */
$gridpress_comments_count = ($gridpress_comments_count <= 9)
    ? '0' . $gridpress_comments_count
    : $gridpress_comments_count;

/**
 * Safe date handling
 */
$gridpress_post_date = !empty($gridpress_card_data['post_date'])
    ? sanitize_text_field(str_replace('/', '-', $gridpress_card_data['post_date']))
    : '';

?>

<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) &&
                isset($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))); ?>">

                    <strong class="jc-cards-day">
                        <?php echo esc_html(wp_date('d', strtotime($gridpress_post_date))); ?>
                    </strong>

                    <?php echo esc_html(wp_date('M, y', strtotime($gridpress_post_date))); ?>
                </time>
            <?php endif; ?>

            <?php if (
                        isset($gridpress_display_settings['show_comments']) &&
                    $gridpress_display_settings['show_comments'] === 'on'
                ) :
                $gridpress_card_url = isset( $gridpress_card_data['url'] ) ? sanitize_url( (string) $gridpress_card_data['url'] ) : '';
                $gridpress_comments_count = isset( $gridpress_comments_count ) ? (int) $gridpress_comments_count : 0;

                ?>
                <div class="jc-cards-info">
                    <strong class="jc-cards-subtext">
                        <a href="<?php echo esc_url($gridpress_card_data['url']); ?>#comments"
                           class="gridpress-dynm-carditm-comments">
                            <?php echo intval($gridpress_comments_count); ?>
                        </a>
                    </strong>
                </div>
            <?php endif; ?>
        </div>
        <div class="jc-cards-image">
            <a href="<?php echo esc_url($gridpress_card_data['url']); ?>">
                <img
                    src="<?php echo esc_url($gridpress_card_data['imag_url']); ?>"
                    alt="<?php echo esc_attr($gridpress_card_data['title']); ?>"
                >
            </a>
        </div>
        <div class="jc-cards-textbox">
            <?php if (
                !empty($gridpress_terms_str) &&
                isset($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; ?>
            <h2>
                <a href="<?php echo esc_url($gridpress_card_data['url']); ?>">
                    <?php echo esc_html($gridpress_card_data['short_title']); ?>
                </a>
            </h2>
        </div>
    </div>
    <?php if (
        isset($gridpress_display_settings['show_button']) &&
        $gridpress_display_settings['show_button'] === 'on'
    ) : ?>
        <div class="jc-cards-footer">
            <a href="<?php echo esc_url($gridpress_card_data['url']); ?>"
               class="jc-cards-link">
                <?php esc_html_e('Read More', 'gridpress-grid-layout-design'); ?>
            </a>
        </div>
    <?php endif; ?>
</div>