<?php

/**
 * This file is part of JohnCMS Content Management System.
 *
 * @copyright JohnCMS Community
 * @license   https://opensource.org/licenses/GPL-3.0 GPL-3.0
 * @link      https://johncms.com JohnCMS Project
 */

/**
 * @var $title
 * @var $page_title
 * @var $data
 */

$this->layout(
    'system::layout/default',
    [
        'title'      => $title,
        'page_title' => $page_title ?? $title,
    ]
);
?>

<!-- Main Container -->
<div class="max-w-4xl mx-auto p-4 space-y-6">

    <?php if ($total) : ?>
        <!-- Articles List -->
        <div class="space-y-6">
            <?php foreach ($list as $tags) : ?>
                <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 hover:shadow-md transition duration-200 overflow-hidden">
                    <div class="p-6">
                        <!-- Title -->
                        <a href="?id=<?= $tags['id'] ?>" class="block group mb-4">
                            <h3 class="text-2xl font-bold text-gray-900 dark:text-white group-hover:text-blue-600 dark:group-hover:text-blue-400 transition duration-200 leading-tight">
                                <?= $tags['name'] ?>
                            </h3>
                        </a>

                        <!-- Content Preview -->
                        <?php if (!empty($tags['text'])): ?>
                            <div class="mb-4">
                                <p class="text-gray-600 dark:text-gray-400 leading-relaxed line-clamp-3">
                                    <?= $tags['text'] ?>
                                </p>
                            </div>
                        <?php endif; ?>

                        <!-- Tags -->
                        <?php if (!empty($tags['tags'])): ?>
                            <div class="mb-4">
                                <div class="flex flex-wrap gap-2">
                                    <?php 
                                    // Assuming tags are stored as comma-separated string
                                    $tagArray = explode(',', $tags['tags']);
                                    foreach ($tagArray as $tag): 
                                        $trimmedTag = trim($tag);
                                        if (!empty($trimmedTag)):
                                    ?>
                                        <span class="inline-flex items-center px-3 py-1 bg-purple-100 dark:bg-purple-900/30 text-purple-700 dark:text-purple-300 rounded-full text-sm font-medium border border-purple-200 dark:border-purple-800">
                                            <svg class="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20">
                                                <path fill-rule="evenodd" d="M17.707 9.293a1 1 0 010 1.414l-7 7a1 1 0 01-1.414 0l-7-7A1 1 0 012.293 9.293L9 15.586V2a1 1 0 012 0v13.586l6.707-6.707a1 1 0 011.414 0z" clip-rule="evenodd"/>
                                            </svg>
                                            <?= $trimmedTag ?>
                                        </span>
                                    <?php 
                                        endif;
                                    endforeach; 
                                    ?>
                                </div>
                            </div>
                        <?php endif; ?>

                        <!-- Metadata -->
                        <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 pt-4 border-t border-gray-100 dark:border-gray-700">
                            <div class="flex flex-wrap items-center gap-4 text-sm text-gray-500 dark:text-gray-400">
                                <!-- Author -->
                                <div class="flex items-center space-x-2">
                                    <svg class="w-4 h-4 flex-shrink-0 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
                                    </svg>
                                    <span class="font-medium text-gray-700 dark:text-gray-300"><?= __('Author:') ?></span>
                                    <span class="text-gray-600 dark:text-gray-400"><?= $tags['who'] ?></span>
                                </div>

                                <!-- Views -->
                                <div class="flex items-center space-x-2">
                                    <svg class="w-4 h-4 flex-shrink-0 text-green-500" 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 class="font-medium text-gray-700 dark:text-gray-300"><?= __('Number of readings:') ?></span>
                                    <span class="text-gray-600 dark:text-gray-400"><?= $tags['count_views'] ?></span>
                                </div>
                            </div>

                            <!-- Comments -->
                            <?php if ($tags['comments']) : ?>
                                <div class="flex items-center space-x-2">
                                    <a href="?act=comments&amp;id=<?= $tags['id'] ?>" 
                                       class="inline-flex items-center px-3 py-1 bg-red-50 dark:bg-red-900/20 text-red-700 dark:text-red-400 rounded-lg hover:bg-red-100 dark:hover:bg-red-900/30 transition duration-200 font-medium text-sm">
                                        <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="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"/>
                                        </svg>
                                        <?= __('Comments:') ?>
                                        <span class="ml-1 bg-red-100 dark:bg-red-800 text-red-800 dark:text-red-200 text-xs font-medium px-2 py-0.5 rounded-full">
                                            <?= $tags['comm_count'] ?>
                                        </span>
                                    </a>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>

        <!-- Pagination and Stats -->
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 p-6">
            <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
                <!-- Total Count -->
                <div class="flex items-center space-x-2 text-gray-700 dark:text-gray-300">
                    <svg class="w-5 h-5 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                    </svg>
                    <span class="font-medium"><?= __('Total') ?>:</span>
                    <span class="text-lg font-bold text-gray-900 dark:text-white"><?= $total ?></span>
                </div>

                <!-- Pagination -->
                <?php if ($total > $user->config->kmess): ?>
                    <div class="flex items-center">
                        <?= $pagination ?>
                    </div>
                <?php endif ?>
            </div>
        </div>

        <!-- Back to Library -->
        <div class="text-center">
            <a href="?" class="inline-flex items-center px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white rounded-xl shadow-sm hover:shadow-md transition duration-200 font-medium">
                <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/>
                </svg>
                <?= __('To Library') ?>
            </a>
        </div>

    <?php else: ?>
        <!-- Empty State -->
        <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 p-12 text-center">
            <div class="w-20 h-20 mx-auto mb-6 text-gray-300 dark:text-gray-600">
                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.746 0 3.332.477 4.5 1.253v13C19.832 18.477 18.246 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"/>
                </svg>
            </div>
            <h3 class="text-2xl font-bold text-gray-900 dark:text-white mb-3"><?= __('The list is empty') ?></h3>
            <p class="text-gray-500 dark:text-gray-400 max-w-md mx-auto mb-6">
                <?= __('No articles found with the selected tags. Try browsing other categories or using different search terms.') ?>
            </p>
            <a href="?" class="inline-flex items-center px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white rounded-xl shadow-sm hover:shadow-md transition duration-200 font-medium">
                <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"/>
                </svg>
                <?= __('To Library') ?>
            </a>
        </div>
    <?php endif; ?>

</div>
