<?php

/**
 * @var $user
 * @var $query
 * @var $articles \News\Models\NewsArticle[]
 */

$this->layout('system::layout/default');
?>

<div class="max-w-4xl mx-auto">
    <!-- Search by Tag Form -->
    <div class="mb-6">
        <form action="/news/search_tags/" method="get" class="max-w-lg">
            <div class="relative">
                <input type="text" 
                       name="tag" 
                       minlength="4" 
                       placeholder="<?= __('Enter a tag') ?>" 
                       value="<?= $this->e($query) ?>" 
                       class="w-full pl-4 pr-12 py-3 border border-gray-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors duration-200">
                <button type="submit" 
                        name="submit" 
                        value="1" 
                        class="absolute right-2 top-1/2 transform -translate-y-1/2 p-2 text-gray-400 hover:text-blue-600 transition-colors duration-200">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                    </svg>
                </button>
            </div>
            <div class="mt-2 text-sm text-gray-500">
                <?= __('Enter tag to search articles') ?>
            </div>
        </form>
    </div>

    <!-- Search Results -->
    <div class="space-y-4">
        <?php if ($articles !== null && $articles->count() > 0): ?>
            <!-- Results Header -->
            <div class="flex items-center justify-between mb-4">
                <div class="text-sm text-gray-600">
                    <?= sprintf(__('Found: %d'), $articles->count()) ?>
                </div>
                <?php if ($query): ?>
                    <div class="flex items-center bg-blue-100 text-blue-800 px-3 py-1 rounded-full text-sm font-medium">
                        <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"/>
                        </svg>
                        <?= $this->e($query) ?>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Articles List -->
            <?php foreach ($articles as $article): ?>
                <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 hover:shadow-md transition-all duration-300">
                    <!-- Article Title -->
                    <a href="<?= $article->url ?>" class="block group mb-3">
                        <h3 class="text-xl font-bold text-gray-900 group-hover:text-blue-600 transition-colors duration-200 leading-tight">
                            <?= $article->name ?>
                        </h3>
                    </a>

                    <!-- Preview Text -->
                    <?php if ($article->preview_text_safe): ?>
                        <div class="text-gray-600 leading-relaxed mb-4 line-clamp-3">
                            <?= $article->preview_text_safe ?>
                        </div>
                    <?php endif ?>

                    <!-- Article Stats -->
                    <div class="flex items-center space-x-6 text-sm text-gray-500">
                        <!-- Rating -->
                        <div class="flex items-center">
                            <svg class="w-4 h-4 mr-1 text-yellow-500" fill="currentColor" viewBox="0 0 24 24">
                                <path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"/>
                            </svg>
                            <?php if ($article->rating > 0): ?>
                                <span class="text-green-600 font-semibold">+<?= $article->rating ?></span>
                            <?php elseif ($article->rating < 0): ?>
                                <span class="text-red-600 font-semibold"><?= $article->rating ?></span>
                            <?php else: ?>
                                <span class="font-semibold"><?= $article->rating ?></span>
                            <?php endif; ?>
                        </div>

                        <!-- Views -->
                        <div class="flex items-center">
                            <svg class="w-4 h-4 mr-1 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/>
                            </svg>
                            <span><?= (int) $article->view_count ?></span>
                        </div>

                        <!-- Comments -->
                        <div class="flex items-center">
                            <svg class="w-4 h-4 mr-1 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z"/>
                            </svg>
                            <span><?= $article->comments_count ?></span>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>

            <!-- Pagination -->
            <div class="mt-6">
                <?= $articles->render() ?>
            </div>

        <?php else: ?>
            <!-- No Results -->
            <div class="bg-blue-50 border border-blue-200 rounded-xl p-8 text-center">
                <svg class="w-16 h-16 text-blue-400 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"/>
                </svg>
                <h3 class="text-lg font-semibold text-gray-900 mb-2"><?= __('No articles found') ?></h3>
                <p class="text-gray-600 mb-4"><?= __('The search didn\'t yield any results.') ?></p>
                <div class="text-sm text-gray-500 space-y-1">
                    <p><?= __('Try searching for a different tag.') ?></p>
                    <p><?= __('Make sure the tag is at least 4 characters long.') ?></p>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

<style>
.line-clamp-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
</style>