<?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 $page
 * @var $settings_forum
 * @var \Johncms\Modules\Forum\Domain\Models\ForumTopic $topic
 * @var $write_access
 * @var $filter_by_author
 * @var \Johncms\Modules\Forum\Domain\Models\ForumMessage $messages
 * @var \Johncms\Modules\Forum\Domain\Models\ForumMessage $message
 * @var \Johncms\Modules\Forum\Domain\Models\ForumMessage $first_post
 * @var \Johncms\Modules\Forum\Domain\Models\ForumFile $file
 * @var \Johncms\Modules\Forum\Domain\Models\ForumVote $topic_vote
 * @var $csrf_token
 */

$this->layout('system::layout/default');
?>
<?= $this->fetch(
    'forum::header',
    [
        'unread_count' => $unread_count,
        'files_count'  => $files_count,
        'files_url'    => '/forum/files/?t=' . $id,
        'files_name'   => __('Topic Files'),
    ]
) ?>
<?php if ($filter_by_author): ?>
    <div class="alert alert-warning">
        <div>
            <?= __('Filter by author is activated') ?>
        </div>
        <form action="/forum/filter/<?= $id ?>/clear/<?= $page > 1 ? '?page=' . $page : '' ?>" method="post" class="mt-2">
            <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
            <button type="submit" class="btn btn-link p-0"><?= __('Cancel Filter') ?></button>
        </form>
    </div>
<?php endif; ?>

<?php if ($topic->closed): ?>
    <div class="alert alert-danger">
        <?= __('Topic closed') ?>
    </div>
<?php endif; ?>

<?php if ($topic->deleted): ?>
    <div class="alert alert-danger">
        <?= __('Topic deleted by') ?> <b><?= $topic->deleted_by ?></b>
    </div>
<?php elseif (! empty($topic->deleted_by) && $user->rights >= 7): ?>
    <div class="alert alert-success">
        <?= __('Undelete topic') ?> <b><?= $topic->deleted_by ?></b>
    </div>
<?php endif; ?>

<?php if (! empty($poll_data)): ?>
    <!-- Poll block -->
    <div class="h3"><?= $topic_vote->name ?></div>
    <?php if ($poll_data['show_form']): ?>
        <form action="/forum/poll-vote/<?= $id ?>/" method="post" class="mb-3">
            <?php foreach ($poll_data['results'] as $item): ?>
                <div class="custom-control custom-radio">
                    <input type="radio" id="vote_<?= $item['id'] ?>" name="vote" value="<?= $item['id'] ?>" required class="custom-control-input">
                    <label class="custom-control-label" for="vote_<?= $item['id'] ?>"><?= $item['name'] ?></label>
                </div>
            <?php endforeach; ?>
            <div class="mt-2">
                <button type="submit" name="submit" value="1" class="btn btn-success btn-sm"><?= __('Vote') ?></button>
                <a class="btn btn-outline-secondary btn-sm" href="<?= $topic->url ?>?vote_result=1<?= $poll_data['clip'] ?>"><?= __('Results') ?></a>
            </div>
        </form>
    <?php else: ?>
        <div class="row">
            <div class="col-sm-6">
                <?php foreach ($poll_data['results'] as $item): ?>
                    <div>
                        <?= $item['name'] ?> (<?= $item['count'] ?>)
                    </div>
                    <div class="progress mb-2 border">
                        <div class="progress-bar <?= $item['color_class'] ?? 'text-dark' ?>" style="width: <?= $item['vote_percent'] ?>%">
                            <span class="p-2"><?= $item['vote_percent'] ?>%</span>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
        <div class="mb-3">
            <div>
                <?= __('Total votes') ?>:
                <?php if ($user->rights > 6): ?>
                    <a href="/forum/poll-voters/<?= $id ?>/"><?= $topic_vote->count ?></a>
                <?php else: ?>
                    <?= $topic_vote->count ?>
                <?php endif; ?>
            </div>
            <?php if ($topic_vote->vote_user !== 1 && $user->isValid()): ?>
                <div>
                    <a href="<?= $topic->url ?>?clip=1"><?= __('Vote') ?></a>
                </div>
            <?php endif; ?>
        </div>
    <?php endif; ?>
<?php endif; ?>

<?php if ($write_access && $settings_forum['upfp']): ?>
    <form name="new_message" action="/forum/new-message/<?= $id ?>/<?= $page > 1 ? '?page=' . $page : '' ?>" method="post" class="mb-3">
        <?php if (! empty($settings_forum['farea'])): ?>
            <input type="hidden" name="token" value="<?= $token ?>"/>
            <div class="form-group">
                <?= $this->fetch(
                    'system::app/ckeditor',
                    [
                        'label'        => __('Message'),
                        'errors'       => implode(', ', $errors['message'] ?? []),
                        'value'        => '',
                        'name'         => 'msg',
                        'allow_upload' => true,
                        'upload_url'   => '/forum/upload_file/',
                    ]
                ) ?>
            </div>
            <div class="custom-control custom-checkbox mb-3">
                <input type="checkbox" class="custom-control-input" name="addfiles" value="1" id="addfiles">
                <label class="custom-control-label" for="addfiles"><?= __('Add File') ?></label>
            </div>
            <div>
                <button type="submit" name="submit" value="1" class="btn btn-primary"><?= __('Write') ?></button>
            </div>
        <?php else: ?>
            <button type="submit" name="submit" value="1" class="btn btn-primary"><?= __('Write') ?></button>
        <?php endif; ?>
    </form>
<?php endif; ?>
<div class="d-flex flex-wrap justify-content-between align-items-center mt-1 mb-3 pagination-no-padding">
    <?php if ($total > $user->config->kmess): ?>
        <?= $pagination ?>
    <?php endif ?>
    <div class="text-muted forum-view-counter">
        <svg class="icon download-button-icon mt-n1">
            <use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#eye"></use>
        </svg>
        <?= $topic->formatted_view_count ?>
    </div>
</div>

<?php if ($first_post): ?>
    <div class="post-item mb-2 first-post" id="first_post_block">
        <div class="section-header post-header collapsed" data-bs-toggle="collapse" data-bs-target="#first_post" aria-expanded="false" aria-controls="first_post">
            <div class="d-flex align-items-center">
                <div class="topic-icons d-flex align-items-center me-2">
                    <svg class="icon">
                        <use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#pin"/>
                    </svg>
                </div>
                <?php if (! empty($first_post->user_profile_link)): ?>
                    <a href="<?= $first_post->user_profile_link ?>" class="user-login me-2"><?= $first_post->user_data->name ?></a>
                <?php else: ?>
                    <a class="user-login me-2"><?= $first_post->user_data->name ?></a>
                <?php endif; ?>
                <span class="post-date"><?= $first_post->post_time ?></span>
            </div>
            <?php if (! empty($first_post->post_preview)): ?>
                <div class="toggle d-flex align-items-center pe-2">
                    <svg class="icon icon-chevron-bottom text-dark-brown">
                        <use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#chevron-bottom"/>
                    </svg>
                </div>
            <?php endif; ?>
        </div>
        <?php if (! empty($first_post->post_preview)): ?>
            <div class="collapse" id="first_post">
                <div class="pt-2">
                    <?= $first_post->post_text ?>
                </div>
            </div>
            <div class="post-preview pt-2">
                <?= $first_post->post_preview ?>
            </div>
        <?php else: ?>
            <div class="pt-2">
                <?= $first_post->post_text ?>
            </div>
        <?php endif; ?>
    </div>
    <div class="border-bottom full-mobile-width mt-4 mb-3"></div>
<?php endif; ?>

<?php if ($user->rights === 3 || $user->rights >= 6): ?>
<form action="/forum/bulk-delete-posts/<?= $id ?>/" method="post">
    <?php endif; ?>

    <?php foreach ($messages as $message): ?>
        <div class="new_post-item border-bottom shadow mb-2" id="post_<?= $message->id ?>">
            <div class="new_post-header d-flex justify-content-between">
                <div class="post-user">
                    <?php if (! empty($message->user_profile_link)): ?>
                        <a href="<?= $message->user_profile_link ?>">
                            <div class="avatar">
                                <img src="<?= $this->avatar($message->user_id) ?>" class="img-fluid" alt=".">
                            </div>
                        </a>
                    <?php else: ?>
                        <div class="avatar">
                            <img src="<?= $this->avatar($message->user_id) ?>" class="img-fluid" alt=".">
                        </div>
                    <?php endif; ?>
                    <span class="user-status <?= $message->user_data->is_online ? 'online' : 'offline' ?> shadow"></span>
                    <?php if (! empty($message->user_data->rights_name)): ?>
                        <div class="post-of-user"
                             data-bs-toggle="tooltip"
                             data-bs-placement="top"
                             data-bs-html="true"
                             title="<?= $message->user_data->rights_name ?>">
                            <svg class="icon-post">
                                <use xlink:href="<?= $this->asset('icons/sprite.svg') ?>?4#check"/>
                            </svg>
                        </div>
                    <?php endif ?>
                </div>
                <div class="flex-grow-1 post-user d-flex flex-wrap overflow-hidden d-flex align-items-center">
                    <div class="w-100">
                        <?php if (! empty($message->user_profile_link)): ?>
                            <a href="<?= $message->user_profile_link ?>"><span class="user-name d-inline me-2"><?= $message->user_data->name ?></span></a>
                        <?php else: ?>
                            <div class="user-name d-inline me-2"><?= $message->user_name ?></div>
                        <?php endif; ?>
                        <a href="<?= $message->url ?>"
                           class="post-meta d-inline me-2"
                           data-bs-toggle="tooltip"
                           data-bs-placement="top"
                           title="<?= __('Link to post') ?>">
                            <?= $message->post_time ?>
                        </a>
                        <?php if (! empty($message->edit_count)): ?>
                            <div class="post-meta d-inline me-2"
                                 data-bs-toggle="tooltip"
                                 data-bs-placement="top"
                                 data-bs-html="true"
                                 title="<?= __('Edited:') ?> <?= $message->editor_name ?> <br> (<?= $message->edit_time ?>) [<?= $message->edit_count ?>]">
                                <?= __('Edited') ?>
                            </div>
                        <?php endif; ?>
                        <?php if ($message->can_edit): ?>
                            <?php if (! empty($message->deleted)): ?>
                                <div class="d-inline me-2 badge bg-danger"
                                     data-bs-toggle="tooltip"
                                     data-bs-placement="top"
                                     title="<?= __('Post deleted:') ?> <?= $message->deleted_by ?>">
                                    <?= __('Deleted') ?>
                                </div>
                            <?php elseif (! empty($message->deleted_by)): ?>
                                <div class="post-meta d-inline me-2"
                                     data-bs-toggle="tooltip"
                                     data-bs-placement="top"
                                     title="<?= __('Post restored by:') ?> <?= $message->deleted_by ?>">
                                    <?= __('Restored') ?>
                                </div>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                    <?php if (! empty($message->user_data->status)): ?>
                        <div class="overflow-hidden text-nowrap text-dark-brown overflow-ellipsis small">
                            <span class="fw-bold"><?= $message->user_data->status ?></span>
                        </div>
                    <?php endif ?>
                </div>

                <?php if ($user->rights === 3 || $user->rights >= 6): ?>
                    <div class="custom-control custom-checkbox d-inline float-right">
                        <input type="checkbox" class="custom-control-input" name="post_ids[]" value="<?= $message->id ?>" id="check_<?= $message->id ?>">
                        <label class="custom-control-label" for="check_<?= $message->id ?>"></label>
                    </div>
                <?php endif; ?>
            </div>
            <div class="post-body">
                <?= $message->post_text ?>
            </div>
            <?php if ($message->files->count() > 0): ?>
                <!-- Files block -->
                <div class="pb-1">
                    <div class="fw-bold pb-1"><?= __('Attached files') ?>:</div>
                    <div class="d-flex flex-wrap image-gallery">
                        <?php foreach ($message->files as $file): ?>
                            <div class="attached-file pe-3 d-flex flex-column justify-content-between">
                                <div class="d-flex justify-content-center align-items-center">
                                    <?php if (! empty($file->file_preview)): ?>
                                        <a href="<?= $file->file_url ?>" class="gallery-item" data-source="<?= $file->file_url ?>" title="<?= $file->filename ?>">
                                            <img src="<?= $file->file_preview ?>" class="img-thumbnail thumb-limit-height" alt="<?= $file->filename ?>">
                                        </a>
                                    <?php else: ?>
                                        <a href="<?= $file->file_url ?>">
                                            <svg class="icon attachment-icon">
                                                <use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#file"/>
                                            </svg>
                                        </a>
                                    <?php endif; ?>
                                </div>
                                <div class="pt-1">
                                    <div class="text-muted small d-flex flex-wrap justify-content-center">
                                        <div class="text-nowrap pe-1">
                                            <svg class="icon downloads-icon">
                                                <use xlink:href="<?= $this->asset('icons/sprite.svg') ?>#download"/>
                                            </svg>
                                            <?= $file->dlcount ?>
                                        </div>
                                        <div>(<?= $file->file_size ?>)</div>
                                    </div>
                                    <div class="file-name">
                                        <a href="<?= $file->file_url ?>" title="<?= $file->filename ?>"><?= $file->filename ?></a>
                                    </div>
                                    <?php if ($message->can_edit): ?>
                                        <div class="delete-link">
                                            <a href="<?= $file->delete_url ?>"><?= __('Delete') ?></a>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            <?php endif; ?>
            <div class="post-footer d-flex justify-content-between mt-2">
                <div class="overflow-hidden">
                    <?php if ($user->rights): ?>
                        <div class="post-meta d-flex">
                            <div class="user-ip me-2">
                                <a href="<?= $message->user_data->search_ip_url ?>"><?= $message->user_data->ip ?></a>
                                <?php if (! empty($message->user_data->ip_via_proxy)): ?>
                                    / <a href="<?= $message->user_data->search_ip_via_proxy_url ?>"><?= $message->user_data->ip_via_proxy ?></a>
                                <?php endif; ?>
                            </div>
                            <div class="useragent">
                                <span><?= $message->user_data->browser ?></span>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
                <div class="d-flex">
                    <?php if (! empty($message->reply_url)): ?>
                        <div class="ms-3">
                            <a href="<?= $message->reply_url ?>"><?= __('Reply') ?></a>
                        </div>
                    <?php endif; ?>
                    <?php if (! empty($message->quote_url)): ?>
                        <div class="ms-3">
                            <a href="<?= $message->quote_url ?>"><?= __('Quote') ?></a>
                        </div>
                    <?php endif; ?>
                    <?php if ($message->can_edit): ?>
                        <div class="dropdown ms-3">
                            <div class="cursor-pointer" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                <svg class="icon text-primary">
                                    <use xlink:href="<?= $this->asset('icons/sprite.svg') ?>?1#more_horizontal"/>
                                </svg>
                            </div>
                            <div class="dropdown-menu dropdown-menu-right">
                                <a class="dropdown-item" href="<?= $message->edit_url ?>" class="ms-2"><?= __('Edit') ?></a>
                                <a class="dropdown-item" href="<?= $message->delete_url ?>" class="ms-2"><?= __('Delete') ?></a>
                                <?php if (! empty($message->restore_url)): ?>
                                    <a class="dropdown-item" href="<?= $message->restore_url ?>" class="ms-2"><?= __('Restore') ?></a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    <?php endforeach; ?>

    <?php if ($user->rights === 3 || $user->rights >= 6): ?>
    <div class="d-flex align-items-center mt-3 mb-3">
        <button type="submit" name="submit" value="submit" class="btn btn-danger btn-sm me-1"><?= __('Delete selected') ?></button>

        <div class="dropdown">
            <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" id="actions" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <?= __('Actions with topic') ?>
            </button>
            <div class="dropdown-menu" aria-labelledby="actions">
                <?php if ($topic_vote): ?>
                    <a class="dropdown-item" href="/forum/editvote/<?= $id ?>/"><?= __('Edit Poll') ?></a>
                    <a class="dropdown-item" href="/forum/delvote/<?= $id ?>/"><?= __('Delete Poll') ?></a>
                <?php else: ?>
                    <a class="dropdown-item" href="/forum/addvote/<?= $id ?>/"><?= __('Add Poll') ?></a>
                <?php endif; ?>

                <?php if ($user->rights >= 7): ?>
                    <a class="dropdown-item" href="/forum/curators/<?= $id ?>/<?= $page > 1 ? '?page=' . $page : '' ?>"><?= __('Curators of the Topic') ?></a>
                <?php endif; ?>

                <a class="dropdown-item" href="/forum/change-topic/<?= $id ?>/"><?= __('Change the topic') ?></a>

                <?php if ($topic->closed): ?>
                    <a class="dropdown-item" href="/forum/close/<?= $id ?>/"><?= __('Open Topic') ?></a>
                <?php else: ?>
                    <a class="dropdown-item" href="/forum/close/<?= $id ?>/?closed"><?= __('Close Topic') ?></a>
                <?php endif; ?>

                <?php if ($topic->deleted): ?>
                    <a class="dropdown-item" href="/forum/restore-topic/<?= $id ?>/"><?= __('Restore Topic') ?></a>
                <?php endif; ?>

                <a class="dropdown-item" href="/forum/delete-topic/<?= $id ?>/"><?= __('Delete Topic') ?></a>

                <?php if ($topic->pinned): ?>
                    <a class="dropdown-item" href="/forum/pin-topic/<?= $id ?>/"><?= __('Unfix Topic') ?></a>
                <?php else: ?>
                    <a class="dropdown-item" href="/forum/pin-topic/<?= $id ?>/?pin"><?= __('Pin Topic') ?></a>
                <?php endif; ?>

                <a class="dropdown-item" href="/forum/move-topic/<?= $id ?>/"><?= __('Move Topic') ?></a>
            </div>
        </div>
    </div>
</form>
<?php endif; ?>

<?php if ($write_access && ! $settings_forum['upfp']): ?>
    <form name="new_message" action="/forum/new-message/<?= $id ?>/<?= $page > 1 ? '?page=' . $page : '' ?>" method="post" class="mt-1 mb-2">
        <?php if (! empty($settings_forum['farea'])): ?>
            <input type="hidden" name="token" value="<?= $token ?>"/>
            <div class="form-group">
                <?= $this->fetch(
                    'system::app/ckeditor',
                    [
                        'label'        => __('Message'),
                        'errors'       => implode(', ', $errors['message'] ?? []),
                        'value'        => '',
                        'name'         => 'msg',
                        'allow_upload' => true,
                        'upload_url'   => '/forum/upload_file/',
                    ]
                ) ?>
            </div>
            <div class="custom-control custom-checkbox mb-3">
                <input type="checkbox" class="custom-control-input" name="addfiles" value="1" id="addfiles">
                <label class="custom-control-label" for="addfiles"><?= __('Add File') ?></label>
            </div>
            <div>
                <button type="submit" name="submit" value="1" class="btn btn-primary"><?= __('Write') ?></button>
            </div>
        <?php else: ?>
            <button type="submit" name="submit" value="1" class="btn btn-primary"><?= __('Write') ?></button>
        <?php endif; ?>
    </form>
<?php endif; ?>
<div>
    <div><?= __('Total') ?>: <?= $total ?></div>
    <!-- Page switching -->
    <?php if ($total > $user->config->kmess): ?>
        <div class="mt-1"><?= $pagination ?></div>
    <?php endif ?>
</div>
<?php if (! empty($curators_array)): ?>
    <div class="alert alert-secondary">
        <?= __('Curators') ?>: <?= implode(', ', $curators_array) ?>
    </div>
<?php endif; ?>
<div>
    <div>
        <?php if ($filter_by_author): ?>
            <form action="/forum/filter/<?= $id ?>/clear/<?= $page > 1 ? '?page=' . $page : '' ?>" method="post">
                <input type="hidden" name="csrf_token" value="<?= $csrf_token ?>">
                <button type="submit" class="btn btn-link p-0"><?= __('Cancel Filter') ?></button>
            </form>
        <?php else: ?>
            <a href="/forum/filter/<?= $id ?>/<?= $page > 1 ? '?page=' . $page : '' ?>"><?= __('Filter by author') ?></a>
        <?php endif; ?>
    </div>
</div>

<?php $this->push('scripts') ?>
<script>
    if (window.location.hash !== '') {
        var post = $(window.location.hash);
        if (post.offset() !== undefined) {
            $('body,html').animate({scrollTop: post.offset().top - 50}, 500);
        }
    }
</script>
<?php $this->stop(); ?>

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