<?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 $sections
 * @var $online
 * @var $files_count
 * @var $unread_count
 * @var $pagination
 * @var $settings_forum
 * @var \Forum\Models\ForumTopic $topic
 * @var $write_access
 * @var $filter_by_author
 * @var \Forum\Models\ForumMessage $messages
 * @var \Forum\Models\ForumMessage $message
 * @var \Forum\Models\ForumMessage $first_post
 * @var \Forum\Models\ForumFile $file
 * @var \Forum\Models\ForumVote $topic_vote
 */

$this->layout('system::layout/default');
?>
<?= $this->fetch(
    'forum::header',
    [
        'unread_count' => $unread_count,
        'files_count'  => $files_count,
        'files_url'    => '/forum/?act=files&t=' . $id,
        'files_name'   => __('Topic Files'),
    ]
) ?>

<!-- Alerts -->
<?php if ($filter_by_author): ?>
    <div class="bg-yellow-50 border border-yellow-200 rounded-lg p-4 mb-4">
        <div class="text-yellow-800 mb-2">
            <?= __('Filter by author is activated') ?>
        </div>
        <a href="/forum/?act=filter&amp;id=<?= $id ?>&amp;do=unset" class="text-yellow-700 hover:text-yellow-900 font-medium">
            <?= __('Cancel Filter') ?>
        </a>
    </div>
<?php endif; ?>

<?php if ($topic->closed): ?>
    <div class="bg-red-50 border border-red-200 rounded-lg p-4 mb-4 text-red-800">
        <?= __('Topic closed') ?>
    </div>
<?php endif; ?>

<?php if ($topic->deleted): ?>
    <div class="bg-red-50 border border-red-200 rounded-lg p-4 mb-4 text-red-800">
        <?= __('Topic deleted by') ?> <b><?= $topic->deleted_by ?></b>
    </div>
<?php elseif (! empty($topic->deleted_by) && $user->rights >= 7): ?>
    <div class="bg-green-50 border border-green-200 rounded-lg p-4 mb-4 text-green-800">
        <?= __('Undelete topic') ?> <b><?= $topic->deleted_by ?></b>
    </div>
<?php endif; ?>

<!-- Poll Block -->
<?php if (! empty($poll_data)): ?>
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
        <h3 class="text-xl font-bold text-gray-900 mb-4"><?= $topic_vote->name ?></h3>
        
        <?php if ($poll_data['show_form']): ?>
            <form action="/forum/?act=vote&amp;id=<?= $id ?>" method="post" class="mb-4">
                <div class="space-y-3 mb-4">
                    <?php foreach ($poll_data['results'] as $item): ?>
                        <div class="flex items-center">
                            <input type="radio" id="vote_<?= $item['id'] ?>" name="vote" value="<?= $item['id'] ?>" required 
                                   class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 focus:ring-blue-500 focus:ring-2">
                            <label for="vote_<?= $item['id'] ?>" class="ml-2 text-sm font-medium text-gray-900">
                                <?= $item['name'] ?>
                            </label>
                        </div>
                    <?php endforeach; ?>
                </div>
                <div class="flex space-x-3">
                    <button type="submit" name="submit" value="1" class="px-4 py-2 bg-green-600 text-white font-medium rounded-md hover:bg-green-700 transition-colors">
                        <?= __('Vote') ?>
                    </button>
                    <a class="px-4 py-2 bg-gray-200 text-gray-700 font-medium rounded-md hover:bg-gray-300 transition-colors" 
                       href="/forum/?type=topic&amp;id=<?= $id ?>&amp;start=<?= $start ?>&amp;vote_result<?= $poll_data['clip'] ?>">
                        <?= __('Results') ?>
                    </a>
                </div>
            </form>
        <?php else: ?>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="space-y-4">
                    <?php foreach ($poll_data['results'] as $item): ?>
                        <div>
                            <div class="flex justify-between text-sm text-gray-600 mb-1">
                                <span><?= $item['name'] ?></span>
                                <span><?= $item['count'] ?></span>
                            </div>
                            <div class="w-full bg-gray-200 rounded-full h-2">
                                <div class="<?= $item['color_class'] ?? 'bg-blue-600' ?> h-2 rounded-full" style="width: <?= $item['vote_percent'] ?>%"></div>
                            </div>
                            <div class="text-xs text-gray-500 mt-1 text-right"><?= $item['vote_percent'] ?>%</div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
            <div class="mt-6 pt-4 border-t border-gray-200">
                <div class="text-sm text-gray-600">
                    <?= __('Total votes') ?>:
                    <?php if ($user->rights > 6): ?>
                        <a href="?act=users&amp;id=<?= $id ?>" class="text-blue-600 hover:text-blue-800"><?= $topic_vote->count ?></a>
                    <?php else: ?>
                        <?= $topic_vote->count ?>
                    <?php endif; ?>
                </div>
                <?php if ($topic_vote->vote_user !== 1 && $user->isValid()): ?>
                    <div class="mt-2">
                        <a href="/forum/?type=topic&amp;id=<?= $id ?>&amp;start=<?= $start . $poll_data['clip'] ?>" class="text-blue-600 hover:text-blue-800 text-sm">
                            <?= __('Vote') ?>
                        </a>
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
<?php endif; ?>

<!-- Quick Reply Form -->
<?php if ($write_access && $settings_forum['upfp']): ?>
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mb-6">
        <form name="new_message" action="/forum/?act=say&amp;type=post&amp;id=<?= $id ?>" method="post">
            <?php if (! empty($settings_forum['farea'])): ?>
                <input type="hidden" name="token" value="<?= $token ?>"/>
                <div class="mb-4">
                    <label for="msg" class="block text-sm font-medium text-gray-700 mb-2"><?= __('Message') ?></label>
                    <div class="mb-2"><?= $bbcode ?></div>
                    <textarea rows="<?= $user->config->fieldHeight ?>" name="msg" id="msg" required 
                              class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"></textarea>
                </div>
                <div class="flex items-center mb-4">
                    <input type="checkbox" name="addfiles" value="1" id="addfiles" 
                           class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 focus:ring-2">
                    <label for="addfiles" class="ml-2 text-sm text-gray-700"><?= __('Add File') ?></label>
                </div>
                <div class="flex space-x-3">
                    <button type="submit" name="submit" value="1" class="px-4 py-2 bg-blue-600 text-white font-medium rounded-md hover:bg-blue-700 transition-colors">
                        <?= __('Write') ?>
                    </button>
                    <?php if (! empty($settings_forum['preview'])): ?>
                        <button type="submit" class="px-4 py-2 bg-gray-200 text-gray-700 font-medium rounded-md hover:bg-gray-300 transition-colors">
                            <?= __('Preview') ?>
                        </button>
                    <?php endif; ?>
                </div>
            <?php else: ?>
                <button type="submit" name="submit" value="1" class="px-4 py-2 bg-blue-600 text-white font-medium rounded-md hover:bg-blue-700 transition-colors">
                    <?= __('Write') ?>
                </button>
            <?php endif; ?>
        </form>
    </div>
<?php endif; ?>

<!-- Pagination and View Counter -->
<div class="flex flex-wrap justify-between items-center mb-6">
    <?php if ($total > $user->config->kmess): ?>
        <div class="mb-3 sm:mb-0"><?= $pagination ?></div>
    <?php endif ?>
    <div class="text-gray-500 text-sm flex items-center">
        <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="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>
        <?= $topic->formatted_view_count ?>
    </div>
</div>

<!-- First Post -->
<?php if ($first_post): ?>
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 mb-6" id="first_post_block">
        <div class="border-b border-gray-200 p-4 cursor-pointer hover:bg-gray-50 transition-colors" 
             onclick="toggleFirstPost()">
            <div class="flex items-center justify-between">
                <div class="flex items-center">
                    <svg class="w-5 h-5 text-gray-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 5a2 2 0 012-2h10a2 2 0 012 2v16l-7-3.5L5 21V5z"/>
                    </svg>
                    <?php if (! empty($first_post->user_profile_link)): ?>
                        <a href="<?= $first_post->user_profile_link ?>" class="font-medium text-gray-900 hover:text-blue-600 mr-2"><?= $first_post->user_data->name ?></a>
                    <?php else: ?>
                        <span class="font-medium text-gray-900 mr-2"><?= $first_post->user_data->name ?></span>
                    <?php endif; ?>
                    <span class="text-sm text-gray-500"><?= $first_post->post_time ?></span>
                </div>
                <?php if (! empty($first_post->post_preview)): ?>
                    <svg id="firstPostToggleIcon" class="w-5 h-5 text-gray-400 transform transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
                    </svg>
                <?php endif; ?>
            </div>
        </div>
        <?php if (! empty($first_post->post_preview)): ?>
            <div id="firstPostContent" class="hidden">
                <div class="p-4 pt-2">
                    <?= $first_post->post_text ?>
                </div>
            </div>
            <div class="p-4 pt-2 border-t border-gray-200">
                <?= $first_post->post_preview ?>
            </div>
        <?php else: ?>
            <div class="p-4">
                <?= $first_post->post_text ?>
            </div>
        <?php endif; ?>
    </div>
<?php endif; ?>

<!-- Messages -->
<?php if ($user->rights === 3 || $user->rights >= 6): ?>
<form action="?act=massdel" method="post" id="massDeleteForm">
<?php endif; ?>

<?php foreach ($messages as $message): ?>
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 mb-4" id="post_<?= $message->id ?>">
        <!-- Post Header -->
        <div class="border-b border-gray-200 p-4">
            <div class="flex items-start justify-between">
                <div class="flex items-start space-x-3 flex-1">
                    <!-- Avatar -->
                    <div class="relative">
                        <?php if (! empty($message->user_profile_link)): ?>
                            <a href="<?= $message->user_profile_link ?>">
                                <img src="<?= $this->avatar($message->user_id) ?>" class="w-12 h-12 rounded-full" alt="Avatar">
                            </a>
                        <?php else: ?>
                            <img src="<?= $this->avatar($message->user_id) ?>" class="w-12 h-12 rounded-full" alt="Avatar">
                        <?php endif; ?>
                        <span class="absolute -bottom-1 -right-1 w-3 h-3 rounded-full border-2 border-white <?= $message->user_data->is_online ? 'bg-green-500' : 'bg-gray-400' ?>"></span>
                        <?php if (! empty($message->user_data->rights_name)): ?>
                            <div class="absolute -top-1 -left-1 w-5 h-5 bg-blue-500 rounded-full flex items-center justify-center" 
                                 title="<?= $message->user_data->rights_name ?>">
                                <svg class="w-3 h-3 text-white" fill="currentColor" viewBox="0 0 20 20">
                                    <path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
                                </svg>
                            </div>
                        <?php endif ?>
                    </div>

                    <!-- User Info -->
                    <div class="flex-1 min-w-0">
                        <div class="flex items-center flex-wrap">
                            <?php if (! empty($message->user_profile_link)): ?>
                                <a href="<?= $message->user_profile_link ?>" class="font-medium text-gray-900 hover:text-blue-600 mr-2"><?= $message->user_data->name ?></a>
                            <?php else: ?>
                                <span class="font-medium text-gray-900 mr-2"><?= $message->user_name ?></span>
                            <?php endif; ?>
                            
                            <a href="<?= $message->url ?>" class="text-sm text-gray-500 hover:text-gray-700 mr-2" title="<?= __('Link to post') ?>">
                                <?= $message->post_time ?>
                            </a>
                            
                            <?php if (! empty($message->edit_count)): ?>
                                <span class="text-sm text-gray-500 cursor-help" title="<?= __('Edited:') ?> <?= $message->editor_name ?> (<?= $message->edit_time ?>) [<?= $message->edit_count ?>]">
                                    <?= __('Edited') ?>
                                </span>
                            <?php endif; ?>
                            
                            <?php if ($message->can_edit): ?>
                                <?php if (! empty($message->deleted)): ?>
                                    <span class="ml-2 px-2 py-1 text-xs bg-red-100 text-red-800 rounded cursor-help" title="<?= __('Post deleted:') ?> <?= $message->deleted_by ?>">
                                        <?= __('Deleted') ?>
                                    </span>
                                <?php elseif (! empty($message->deleted_by)): ?>
                                    <span class="text-sm text-gray-500 cursor-help" title="<?= __('Post restored by:') ?> <?= $message->deleted_by ?>">
                                        <?= __('Restored') ?>
                                    </span>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>
                        
                        <?php if (! empty($message->user_data->status)): ?>
                            <div class="text-sm text-gray-600 mt-1"><?= $message->user_data->status ?></div>
                        <?php endif ?>
                    </div>
                </div>

                <?php if ($user->rights === 3 || $user->rights >= 6): ?>
                    <div class="flex items-center">
                        <input type="checkbox" name="delch[]" value="<?= $message->id ?>" id="check_<?= $message->id ?>"
                               class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 focus:ring-2">
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <!-- Post Body -->
        <div class="p-4">
            <?= $message->post_text ?>
        </div>

        <!-- Files -->
        <?php if ($message->files->count() > 0): ?>
            <div class="border-t border-gray-200 p-4">
                <div class="font-medium text-gray-900 mb-3"><?= __('Attached files') ?>:</div>
                <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 gap-4">
                    <?php foreach ($message->files as $file): ?>
                        <div class="border border-gray-200 rounded-lg p-3 text-center">
                            <div class="mb-2">
                                <?php if (! empty($file->file_preview)): ?>
                                    <a href="<?= $file->file_url ?>" class="gallery-item inline-block" data-source="<?= $file->file_url ?>" title="<?= $file->filename ?>">
                                        <img src="<?= $file->file_preview ?>" class="w-full h-20 object-cover rounded" alt="<?= $file->filename ?>">
                                    </a>
                                <?php else: ?>
                                    <a href="<?= $file->file_url ?>" class="inline-block">
                                        <svg class="w-12 h-12 text-gray-400 mx-auto" 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>
                                    </a>
                                <?php endif; ?>
                            </div>
                            <div class="flex items-center justify-center space-x-2 text-sm text-gray-500 mb-1">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                                </svg>
                                <span><?= $file->dlcount ?></span>
                                <span>(<?= $file->file_size ?>)</span>
                            </div>
                            <div class="text-xs font-medium truncate" title="<?= $file->filename ?>">
                                <a href="<?= $file->file_url ?>" class="text-blue-600 hover:text-blue-800"><?= $file->filename ?></a>
                            </div>
                            <?php if ($message->can_edit): ?>
                                <div class="mt-1">
                                    <a href="<?= $file->delete_url ?>" class="text-xs text-red-600 hover:text-red-800"><?= __('Delete') ?></a>
                                </div>
                            <?php endif; ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>

        <!-- Post Footer -->
        <div class="border-t border-gray-200 px-4 py-3 bg-gray-50">
            <div class="flex justify-between items-center">
                <div class="text-xs text-gray-500">
                    <?php if ($user->rights): ?>
                        <div class="flex items-center space-x-4">
                            <span>
                                <a href="<?= $message->user_data->search_ip_url ?>" class="hover:text-gray-700"><?= $message->user_data->ip ?></a>
                                <?php if (! empty($message->user_data->ip_via_proxy)): ?>
                                    / <a href="<?= $message->user_data->search_ip_via_proxy_url ?>" class="hover:text-gray-700"><?= $message->user_data->ip_via_proxy ?></a>
                                <?php endif; ?>
                            </span>
                            <span><?= $message->user_data->browser ?></span>
                        </div>
                    <?php endif; ?>
                </div>
                <div class="flex items-center space-x-3">
                    <?php if (! empty($message->reply_url)): ?>
                        <a href="<?= $message->reply_url ?>" class="text-sm text-blue-600 hover:text-blue-800"><?= __('Reply') ?></a>
                    <?php endif; ?>
                    <?php if (! empty($message->quote_url)): ?>
                        <a href="<?= $message->quote_url ?>" class="text-sm text-blue-600 hover:text-blue-800"><?= __('Quote') ?></a>
                    <?php endif; ?>
                    <?php if ($message->can_edit): ?>
                        <div class="relative">
                            <button type="button" class="post-actions-btn text-gray-400 hover:text-gray-600 p-1" data-post-id="<?= $message->id ?>">
                                <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="M5 12h.01M12 12h.01M19 12h.01M6 12a1 1 0 11-2 0 1 1 0 012 0zm7 0a1 1 0 11-2 0 1 1 0 012 0zm7 0a1 1 0 11-2 0 1 1 0 012 0z"/>
                                </svg>
                            </button>
                            <div id="postMenu_<?= $message->id ?>" class="post-actions-menu absolute right-0 mt-1 w-48 bg-white rounded-md shadow-lg border border-gray-200 py-1 z-10 hidden">
                                <a href="<?= $message->edit_url ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Edit') ?></a>
                                <a href="<?= $message->delete_url ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Delete') ?></a>
                                <?php if (! empty($message->restore_url)): ?>
                                    <a href="<?= $message->restore_url ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Restore') ?></a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>

<?php if ($user->rights === 3 || $user->rights >= 6): ?>
    <div class="flex items-center space-x-3 my-6">
        <button type="submit" name="submit" value="submit" class="px-4 py-2 bg-red-600 text-white font-medium rounded-md hover:bg-red-700 transition-colors">
            <?= __('Delete selected') ?>
        </button>

        <div class="relative">
            <button type="button" id="topicActionsBtn" class="px-4 py-2 bg-gray-600 text-white font-medium rounded-md hover:bg-gray-700 transition-colors flex items-center">
                <?= __('Actions with topic') ?>
                <svg class="w-4 h-4 ml-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/>
                </svg>
            </button>
            <div id="topicActionsMenu" class="absolute left-0 mt-1 w-56 bg-white rounded-md shadow-lg border border-gray-200 py-1 z-10 hidden">
                <?php if ($topic_vote): ?>
                    <a href="/forum/?act=editvote&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Edit Poll') ?></a>
                    <a href="/forum/?act=delvote&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Delete Poll') ?></a>
                <?php else: ?>
                    <a href="/forum/?act=addvote&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Add Poll') ?></a>
                <?php endif; ?>

                <?php if ($user->rights >= 7): ?>
                    <a href="/forum/?act=curators&amp;id=<?= $id ?>&amp;start=<?= $start ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Curators of the Topic') ?></a>
                <?php endif; ?>

                <a href="/forum/?act=change_topic&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Change the topic') ?></a>

                <?php if ($topic->closed): ?>
                    <a href="/forum/?act=close&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Open Topic') ?></a>
                <?php else: ?>
                    <a href="/forum/?act=close&amp;id=<?= $id ?>&amp;closed" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Close Topic') ?></a>
                <?php endif; ?>

                <?php if ($topic->deleted): ?>
                    <a href="/forum/?act=restore&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Restore Topic') ?></a>
                <?php endif; ?>

                <a href="/forum/?act=deltema&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Delete Topic') ?></a>

                <?php if ($topic->pinned): ?>
                    <a href="/forum/?act=vip&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Unfix Topic') ?></a>
                <?php else: ?>
                    <a href="/forum/?act=vip&amp;id=<?= $id ?>&amp;vip" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Pin Topic') ?></a>
                <?php endif; ?>

                <a href="/forum/?act=per&amp;id=<?= $id ?>" class="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"><?= __('Move Topic') ?></a>
            </div>
        </div>
    </div>
</form>
<?php endif; ?>

<!-- Bottom Reply Form -->
<?php if ($write_access && ! $settings_forum['upfp']): ?>
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6 mt-6">
        <form name="new_message" action="/forum/?act=say&amp;type=post&amp;id=<?= $id ?>" method="post">
            <?php if (! empty($settings_forum['farea'])): ?>
                <input type="hidden" name="token" value="<?= $token ?>"/>
                <div class="mb-4">
                    <label for="msg" class="block text-sm font-medium text-gray-700 mb-2"><?= __('Message') ?></label>
                    <div class="mb-2"><?= $bbcode ?></div>
                    <textarea rows="<?= $user->config->fieldHeight ?>" name="msg" id="msg" required 
                              class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"></textarea>
                </div>
                <div class="flex items-center mb-4">
                    <input type="checkbox" name="addfiles" value="1" id="addfiles" 
                           class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 focus:ring-2">
                    <label for="addfiles" class="ml-2 text-sm text-gray-700"><?= __('Add File') ?></label>
                </div>
                <div class="flex space-x-3">
                    <button type="submit" name="submit" value="1" class="px-4 py-2 bg-blue-600 text-white font-medium rounded-md hover:bg-blue-700 transition-colors">
                        <?= __('Write') ?>
                    </button>
                    <?php if (! empty($settings_forum['preview'])): ?>
                        <button type="submit" class="px-4 py-2 bg-gray-200 text-gray-700 font-medium rounded-md hover:bg-gray-300 transition-colors">
                            <?= __('Preview') ?>
                        </button>
                    <?php endif; ?>
                </div>
            <?php else: ?>
                <button type="submit" name="submit" value="1" class="px-4 py-2 bg-blue-600 text-white font-medium rounded-md hover:bg-blue-700 transition-colors">
                    <?= __('Write') ?>
                </button>
            <?php endif; ?>
        </form>
    </div>
<?php endif; ?>

<!-- Footer Section -->
<div class="mt-8">
    <div class="text-gray-600 mb-4"><?= __('Total') ?>: <?= $total ?></div>
    
    <?php if ($total > $user->config->kmess): ?>
        <div class="mb-6"><?= $pagination ?></div>
    <?php endif ?>
</div>

<?php if (! empty($curators_array)): ?>
    <div class="bg-gray-50 border border-gray-200 rounded-lg p-4 mb-6">
        <span class="font-medium text-gray-700"><?= __('Curators') ?>:</span>
        <span class="text-gray-600"><?= implode(', ', $curators_array) ?></span>
    </div>
<?php endif; ?>

<div class="flex space-x-6 text-sm text-blue-600 mb-8">
    <div>
        <?php if ($filter_by_author): ?>
            <a href="/forum/?act=filter&amp;id=<?= $id ?>&amp;do=unset" class="hover:text-blue-800"><?= __('Cancel Filter') ?></a>
        <?php else: ?>
            <a href="/forum/?act=filter&amp;id=<?= $id ?>&amp;start=<?= $start ?>" class="hover:text-blue-800"><?= __('Filter by author') ?></a>
        <?php endif; ?>
    </div>
    <div>
        <a href="?act=tema&amp;id=<?= $id ?>" class="hover:text-blue-800"><?= __('Download Topic') ?></a>
    </div>
</div>

<?php $this->push('scripts') ?>
<script>
    // Toggle first post content
    function toggleFirstPost() {
        const content = document.getElementById('firstPostContent');
        const icon = document.getElementById('firstPostToggleIcon');
        
        if (content && icon) {
            content.classList.toggle('hidden');
            icon.classList.toggle('rotate-180');
        }
    }

    // Handle post actions dropdown
    document.addEventListener('DOMContentLoaded', function() {
        // Post actions dropdown
        document.querySelectorAll('.post-actions-btn').forEach(button => {
            button.addEventListener('click', function(e) {
                e.stopPropagation();
                const postId = this.getAttribute('data-post-id');
                const menu = document.getElementById('postMenu_' + postId);
                
                // Close all other menus
                document.querySelectorAll('.post-actions-menu').forEach(m => {
                    if (m !== menu) {
                        m.classList.add('hidden');
                    }
                });
                
                // Toggle current menu
                menu.classList.toggle('hidden');
            });
        });

        // Topic actions dropdown
        const topicActionsBtn = document.getElementById('topicActionsBtn');
        const topicActionsMenu = document.getElementById('topicActionsMenu');
        
        if (topicActionsBtn && topicActionsMenu) {
            topicActionsBtn.addEventListener('click', function(e) {
                e.stopPropagation();
                topicActionsMenu.classList.toggle('hidden');
            });
        }

        // Close dropdowns when clicking outside
        document.addEventListener('click', function() {
            document.querySelectorAll('.post-actions-menu').forEach(menu => {
                menu.classList.add('hidden');
            });
            
            if (topicActionsMenu) {
                topicActionsMenu.classList.add('hidden');
            }
        });

        // Handle mass delete form
        const massDeleteForm = document.getElementById('massDeleteForm');
        if (massDeleteForm) {
            massDeleteForm.addEventListener('submit', function(e) {
                const checkedBoxes = this.querySelectorAll('input[type="checkbox"]:checked');
                if (checkedBoxes.length === 0) {
                    e.preventDefault();
                    alert('<?= __('Please select at least one post to delete') ?>');
                }
            });
        }

        // Scroll to post if hash exists
        if (window.location.hash) {
            const post = document.querySelector(window.location.hash);
            if (post) {
                setTimeout(() => {
                    post.scrollIntoView({ behavior: 'smooth', block: 'start' });
                }, 100);
            }
        }
    });

    // Add rotate utility class if not present
    const style = document.createElement('style');
    style.textContent = `
        .rotate-180 {
            transform: rotate(180deg);
        }
        .transition-transform {
            transition: transform 0.2s ease-in-out;
        }
    `;
    document.head.appendChild(style);
</script>
<?php $this->stop(); ?>

<?= $this->fetch(
    'forum::footer',
    [
        'online'   => $online,
        'who_url'  => '/forum/?act=who&id=' . $id,
        'who_name' => __('Who is here'),
    ]
) ?>