<?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'),
    ]
) ?>
<?php if ($filter_by_author): ?>
    <div class="alert alert-warning">
        <div>
            <?= __('Filter by author is activated') ?>
        </div>
        <a href="/forum/?act=filter&amp;id=<?= $id ?>&amp;do=unset"><?= __('Cancel Filter') ?></a>
    </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/?act=vote&amp;id=<?= $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="/forum/?type=topic&amp;id=<?= $id ?>&amp;start=<?= $start ?>&amp;vote_result<?= $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="?act=users&amp;id=<?= $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="/forum/?type=topic&amp;id=<?= $id ?>&amp;start=<?= $start . $poll_data['clip'] ?>"><?= __('Vote') ?></a>
                </div>
            <?php endif; ?>
        </div>
    <?php endif; ?>
<?php endif; ?>

<?php if ($write_access && $settings_forum['upfp']): ?>
    <form name="new_message" action="/forum/?act=say&amp;type=post&amp;id=<?= $id ?>" method="post" class="mb-3">
        <?php if (! empty($settings_forum['farea'])): ?>
            <input type="hidden" name="token" value="<?= $token ?>"/>
            <div class="form-group">
                <label for="msg"><?= __('Message') ?></label>
                <div><?= $bbcode ?></div>
                <textarea rows="<?= $user->config->fieldHeight ?>" name="msg" id="msg" required class="form-control"></textarea>
            </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>
                <?php if (! empty($settings_forum['preview'])): ?>
                    <button type="submit" class="btn btn-secondary"><?= __('Preview') ?></button>
                <?php endif; ?>
            </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-toggle="collapse" data-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 mr-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 mr-2"><?= $first_post->user_data->name ?></a>
                <?php else: ?>
                    <a class="user-login mr-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 pr-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="?act=massdel" 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-toggle="tooltip"
                             data-placement="top"
                             data-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 mr-2"><?= $message->user_data->name ?></span></a>
                        <?php else: ?>
                            <div class="user-name d-inline mr-2"><?= $message->user_name ?></div>
                        <?php endif; ?>
                        <a href="<?= $message->url ?>"
                           class="post-meta d-inline mr-2"
                           data-toggle="tooltip"
                           data-placement="top"
                           title="<?= __('Link to post') ?>">
                            <?= $message->post_time ?>
                        </a>
                        <?php if (! empty($message->edit_count)): ?>
                            <div class="post-meta d-inline mr-2"
                                 data-toggle="tooltip"
                                 data-placement="top"
                                 data-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 mr-2 badge badge-danger"
                                     data-toggle="tooltip"
                                     data-placement="top"
                                     title="<?= __('Post deleted:') ?> <?= $message->deleted_by ?>">
                                    <?= __('Deleted') ?>
                                </div>
                            <?php elseif (! empty($message->deleted_by)): ?>
                                <div class="post-meta d-inline mr-2"
                                     data-toggle="tooltip"
                                     data-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="font-weight-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="delch[]" 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="font-weight-bold pb-1"><?= __('Attached files') ?>:</div>
                    <div class="d-flex flex-wrap image-gallery">
                        <?php foreach ($message->files as $file): ?>
                            <div class="attached-file pr-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 pr-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 mr-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="ml-3">
                            <a href="<?= $message->reply_url ?>"><?= __('Reply') ?></a>
                        </div>
                    <?php endif; ?>
                    <?php if (! empty($message->quote_url)): ?>
                        <div class="ml-3">
                            <a href="<?= $message->quote_url ?>"><?= __('Quote') ?></a>
                        </div>
                    <?php endif; ?>
                    <?php if ($message->can_edit): ?>
                        <div class="dropdown ml-3">
                            <div class="cursor-pointer" data-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="ml-2"><?= __('Edit') ?></a>
                                <a class="dropdown-item" href="<?= $message->delete_url ?>" class="ml-2"><?= __('Delete') ?></a>
                                <?php if (! empty($message->restore_url)): ?>
                                    <a class="dropdown-item" href="<?= $message->restore_url ?>" class="ml-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 mr-1"><?= __('Delete selected') ?></button>

        <div class="dropdown">
            <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" id="actions" data-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/?act=editvote&amp;id=<?= $id ?>"><?= __('Edit Poll') ?></a>
                    <a class="dropdown-item" href="/forum/?act=delvote&amp;id=<?= $id ?>"><?= __('Delete Poll') ?></a>
                <?php else: ?>
                    <a class="dropdown-item" href="/forum/?act=addvote&amp;id=<?= $id ?>"><?= __('Add Poll') ?></a>
                <?php endif; ?>

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

                <a class="dropdown-item" href="/forum/?act=ren&amp;id=<?= $id ?>"><?= __('Rename Topic') ?></a>

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

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

                <a class="dropdown-item" href="/forum/?act=deltema&amp;id=<?= $id ?>"><?= __('Delete Topic') ?></a>

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

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

<?php if ($write_access && ! $settings_forum['upfp']): ?>
    <form name="new_message" action="/forum/?act=say&amp;type=post&amp;id=<?= $id ?>" method="post" class="mt-1 mb-2">
        <?php if (! empty($settings_forum['farea'])): ?>
            <input type="hidden" name="token" value="<?= $token ?>"/>
            <div class="form-group">
                <label for="msg"><?= __('Message') ?></label>
                <div><?= $bbcode ?></div>
                <textarea rows="<?= $user->config->fieldHeight ?>" name="msg" id="msg" required class="form-control"></textarea>
            </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>
                <?php if (! empty($settings_forum['preview'])): ?>
                    <button type="submit" class="btn btn-secondary"><?= __('Preview') ?></button>
                <?php endif; ?>
            </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): ?>
            <a href="/forum/?act=filter&amp;id=<?= $id ?>&amp;do=unset"><?= __('Cancel Filter') ?></a>
        <?php else: ?>
            <a href="/forum/?act=filter&amp;id=<?= $id ?>&amp;start=<?= $start ?>"><?= __('Filter by author') ?></a>
        <?php endif; ?>
    </div>
    <div>
        <a href="?act=tema&amp;id=<?= $id ?>"><?= __('Download Topic') ?></a>
    </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/?act=who&id=' . $id,
        'who_name' => __('Who is here'),
    ]
) ?>
