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

<?php if (!$is_configured): ?>
<div class="lm-empty-state">
    <h2><?php esc_html_e('Connect your Launchmind account', 'launchmind-blog'); ?></h2>
    <p><?php esc_html_e('Enter your Launchmind API key to start displaying blog articles on your site.', 'launchmind-blog'); ?></p>
    <div class="lm-empty-state__actions">
        <a href="?page=launchmind-dashboard&tab=settings" class="button button-primary"><?php esc_html_e('Enter API Key', 'launchmind-blog'); ?></a>
        <a href="https://launchmind.io/backlinks/dashboard" target="_blank" rel="noopener noreferrer" class="button"><?php esc_html_e('Get API Key', 'launchmind-blog'); ?></a>
    </div>
</div>
<?php else: ?>
    <div class="lm-search">
        <span class="dashicons dashicons-search"></span>
        <input type="text" id="lm-search-input" placeholder="<?php esc_attr_e('Search articles...', 'launchmind-blog'); ?>">
    </div>

    <?php if (empty($posts)): ?>
        <div class="lm-empty-state">
            <h3><?php esc_html_e('No articles found', 'launchmind-blog'); ?></h3>
            <p><?php esc_html_e('Your blog articles will appear here once published.', 'launchmind-blog'); ?></p>
        </div>
    <?php else: ?>
        <div class="lm-articles-grid" id="lm-articles-grid">
            <?php foreach ($posts as $post): ?>
                <article class="lm-article-card" data-slug="<?php echo esc_attr($post['slug']); ?>" data-lang="<?php echo esc_attr(get_locale() ? substr(get_locale(), 0, 2) : 'en'); ?>">
                    <div class="lm-article-card__image">
                        <?php if (!empty($post['coverImage'])): ?>
                            <img src="<?php echo esc_url($post['coverImage']); ?>" alt="<?php echo esc_attr($post['title']); ?>" loading="lazy">
                        <?php else: ?>
                            <div class="lm-article-card__placeholder">
                                <span class="dashicons dashicons-format-image"></span>
                            </div>
                        <?php endif; ?>
                        <span class="lm-article-card__badge"><?php esc_html_e('Published', 'launchmind-blog'); ?></span>
                        <?php if (!empty($post['publishedAt'])): ?>
                            <span class="lm-article-card__date"><?php echo esc_html(date_i18n('M j', strtotime($post['publishedAt']))); ?></span>
                        <?php endif; ?>
                    </div>
                    <div class="lm-article-card__content">
                        <h3 class="lm-article-card__title"><?php echo esc_html($post['title']); ?></h3>
                        <p class="lm-article-card__excerpt"><?php echo esc_html(wp_trim_words($post['excerpt'], 22)); ?></p>
                        <?php if (!empty($post['tags'])): ?>
                            <div class="lm-article-card__tags">
                                <?php foreach (array_slice($post['tags'], 0, 2) as $tag): ?>
                                    <span class="lm-tag"><?php echo esc_html($tag); ?></span>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                        <button type="button" class="lm-article-card__view">
                            <?php esc_html_e('View article', 'launchmind-blog'); ?>
                            <span class="dashicons dashicons-arrow-right-alt2"></span>
                        </button>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
<?php endif; ?>
