<?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">

    <!-- Book Cover and Content -->
    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700 overflow-hidden">
        <?php if ($page === 1 && $cover) : ?>
            <div class="flex flex-col lg:flex-row gap-6 p-6">
                <!-- Cover Image -->
                <div class="lg:w-1/3 flex-shrink-0">
                    <a href="../upload/library/images/orig/<?= $res['id'] ?>.png" class="block group">
                        <div class="relative overflow-hidden rounded-lg shadow-md group-hover:shadow-lg transition duration-300">
                            <img src="../upload/library/images/big/<?= $res['id'] ?>.png" 
                                 alt="Book cover" 
                                 class="w-full h-auto object-cover transform group-hover:scale-105 transition duration-300">
                            <div class="absolute inset-0 bg-black opacity-0 group-hover:opacity-10 transition duration-300"></div>
                        </div>
                    </a>
                </div>
                
                <!-- Content -->
                <div class="lg:w-2/3">
                    <div class="prose prose-lg dark:prose-invert max-w-none">
                        <?= $res['text'] ?>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <!-- Content without cover -->
            <div class="p-6">
                <div class="prose prose-lg dark:prose-invert max-w-none">
                    <?= $res['text'] ?>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <!-- Pagination -->
    <?php if ($count_pages > 1): ?>
        <div class="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
            <div class="flex justify-center"><?= $pagination ?></div>
        </div>
    <?php endif ?>

    <!-- Download Section -->
    <div class="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-5">
        <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4 flex items-center">
            <svg class="w-5 h-5 mr-2 text-blue-600 dark:text-blue-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 10v6m0 0l-3-3m3 3l3-3m2 8H7a2 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>
            <?= __('Download file') ?>
        </h3>
        <div class="flex flex-wrap gap-3">
            <a href="?act=download&amp;type=txt&amp;id=<?= $res['id'] ?>" 
               class="inline-flex items-center px-4 py-2 bg-blue-50 dark:bg-blue-900/30 border border-blue-200 dark:border-blue-800 text-blue-700 dark:text-blue-300 rounded-lg hover:bg-blue-100 dark:hover:bg-blue-900/50 transition duration-200 font-medium">
                <svg class="w-4 h-4 mr-2" 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>
                TXT
            </a>
            <a href="?act=download&amp;type=fb2&amp;id=<?= $res['id'] ?>" 
               class="inline-flex items-center px-4 py-2 bg-green-50 dark:bg-green-900/30 border border-green-200 dark:border-green-800 text-green-700 dark:text-green-300 rounded-lg hover:bg-green-100 dark:hover:bg-green-900/50 transition duration-200 font-medium">
                <svg class="w-4 h-4 mr-2" 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>
                FB2
            </a>
        </div>
    </div>

    <!-- Book Metadata -->
    <?php if ($page === 1) : ?>
        <div class="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-5">
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
                <!-- Tags -->
                <?php if ($tags) : ?>
                    <div class="flex items-start space-x-3">
                        <div class="flex-shrink-0 w-5 h-5 text-purple-600 dark:text-purple-400 mt-0.5">
                            <svg 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>
                        </div>
                        <div>
                            <div class="font-medium text-gray-700 dark:text-gray-300 mb-1"><?= __('The Tags:') ?></div>
                            <div class="flex flex-wrap gap-1">
                                <?php foreach(explode(',', $tags) as $tag): ?>
                                    <span class="inline-block bg-purple-100 dark:bg-purple-900/30 text-purple-700 dark:text-purple-300 px-2 py-1 rounded text-xs"><?= trim($tag) ?></span>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>

                <!-- Added by -->
                <div class="flex items-center space-x-3">
                    <div class="flex-shrink-0 w-5 h-5 text-blue-600 dark:text-blue-400">
                        <svg 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>
                    </div>
                    <div>
                        <div class="font-medium text-gray-700 dark:text-gray-300"><?= __('Who added:') ?></div>
                        <div class="text-gray-900 dark:text-white"><?= $who ?></div>
                    </div>
                </div>

                <!-- Rating -->
                <div class="flex items-center space-x-3">
                    <div class="flex-shrink-0 w-5 h-5 text-yellow-600 dark:text-yellow-400">
                        <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11.049 2.927c.3-.921 1.603-.921 1.902 0l1.519 4.674a1 1 0 00.95.69h4.915c.969 0 1.371 1.24.588 1.81l-3.976 2.888a1 1 0 00-.363 1.118l1.518 4.674c.3.922-.755 1.688-1.538 1.118l-3.976-2.888a1 1 0 00-1.176 0l-3.976 2.888c-.783.57-1.838-.197-1.538-1.118l1.518-4.674a1 1 0 00-.363-1.118l-3.976-2.888c-.784-.57-.38-1.81.588-1.81h4.914a1 1 0 00.951-.69l1.519-4.674z"/>
                        </svg>
                    </div>
                    <div>
                        <div class="font-medium text-gray-700 dark:text-gray-300"><?= __('Rating:') ?></div>
                        <div class="text-gray-900 dark:text-white"><?= $ratingView ?></div>
                    </div>
                </div>

                <!-- Read count -->
                <div class="flex items-center space-x-3">
                    <div class="flex-shrink-0 w-5 h-5 text-green-600 dark:text-green-400">
                        <svg 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>
                    </div>
                    <div>
                        <div class="font-medium text-gray-700 dark:text-gray-300"><?= __('Number of readings:') ?></div>
                        <div class="text-gray-900 dark:text-white"><?= $res['count_views'] ?></div>
                    </div>
                </div>

                <!-- Comments -->
                <?php if ($res['comments']) : ?>
                    <div class="flex items-center space-x-3">
                        <div class="flex-shrink-0 w-5 h-5 text-red-600 dark:text-red-400">
                            <svg 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>
                        </div>
                        <div>
                            <div class="font-medium text-gray-700 dark:text-gray-300">
                                <a href="?act=comments&amp;id=<?= $res['id'] ?>" class="hover:text-blue-600 dark:hover:text-blue-400 transition duration-200">
                                    <?= __('Comments:') ?>
                                </a>
                            </div>
                            <div class="text-gray-900 dark:text-white">(<?= $res['comm_count'] ?>)</div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    <?php endif; ?>

    <!-- Rating Vote -->
    <div class="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-5">
        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
            <div class="flex items-center space-x-3">
                <div class="w-8 h-8 bg-yellow-100 dark:bg-yellow-900/30 rounded-lg flex items-center justify-center">
                    <svg class="w-4 h-4 text-yellow-600 dark:text-yellow-400" fill="currentColor" viewBox="0 0 20 20">
                        <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"/>
                    </svg>
                </div>
                <div>
                    <h3 class="text-lg font-semibold text-gray-900 dark:text-white"><?= __('Your Rating') ?></h3>
                    <p class="text-sm text-gray-600 dark:text-gray-400"><?= __('Rate this book') ?></p>
                </div>
            </div>
            <div class="flex-shrink-0">
                <?= $ratingVote ?>
            </div>
        </div>
    </div>

    <!-- Moderator Actions -->
    <?php if ($moderMenu) : ?>
        <div class="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-5">
            <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4 flex items-center">
                <svg class="w-5 h-5 mr-2 text-red-600 dark:text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/>
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
                </svg>
                <?= __('Moderator Tools') ?>
            </h3>
            <div class="flex flex-wrap gap-3">
                <a href="?act=moder&amp;type=article&amp;id=<?= $res['id'] ?>" 
                   class="inline-flex items-center px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition duration-200 font-medium">
                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                    </svg>
                    <?= __('Edit') ?>
                </a>
                <a href="?act=del&amp;type=article&amp;id=<?= $res['id'] ?>" 
                   class="inline-flex items-center px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded-lg transition duration-200 font-medium">
                    <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                    </svg>
                    <?= __('Delete') ?>
                </a>
            </div>
        </div>
    <?php endif; ?>

</div>