<?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 $categories
 * @var $urls
 * @var $pagination
 * @var $can_upload
 * @var $total_new
 */

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

<?php if (! $config['mod_down']): ?>
    <div class="alert alert-danger border-0 shadow">
        <?= __('Downloads are closed') ?>
    </div>
<?php endif; ?>

<?php if (empty($id)): ?>
    <div class="mb-3 mt-n1">
        <?php if (! empty($urls['new'])): ?>
            <a href="<?= $urls['new'] ?>" class="btn btn-outline-primary btn-sm mr-1 mt-1">
                <?= __('New Files') ?> <span class="badge badge-secondary badge-pill ml-1"><?= $total_new ?></span>
            </a>
        <?php endif; ?>
        <a href="?act=search" class="btn btn-outline-primary btn-sm mr-1 mt-1"><?= __('Search') ?></a>
        <a href="?act=top_files&amp;id=0" class="btn btn-outline-primary btn-sm mr-1 mt-1"><?= __('Top Files') ?></a>
        <a href="?act=top_users" class="btn btn-outline-primary btn-sm mt-1"><?= __('Top Users') ?></a>
    </div>
<?php endif; ?>

<?php foreach ($categories as $category): ?>
    <div class="forum-section">
        <div class="section-header d-flex justify-content-between <?= (empty($category['desc']) ? 'border-0 pb-0' : '') ?>">
            <div class="d-flex align-items-center">
                <a href="<?= $category['url'] ?>" class="text-dark-brown"><?= $category['rus_name'] ?></a>
                <span class="badge badge-pill badge-light border ml-3"><?= $category['total'] ?></span>
            </div>
        </div>
        <?php if (! empty($category['desc']) || ! empty($category['field'])): ?>
            <div class="small pt-2 text-muted">
                <?php if (! empty($category['desc'])): ?>
                    <div><?= $category['desc'] ?></div>
                <?php endif; ?>
                <?php if (! empty($category['text'])): ?>
                    <div><?= __('Allowed extensions') ?>: <?= $category['text'] ?></div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
        <?php if ($category['has_edit']): ?>
            <div class="pt-2 small">
                <?php if ($total_cat > 1): ?>
                    <a href="<?= $category['up_url'] ?>" class="mr-2"><?= __('Up') ?></a>
                    <a href="<?= $category['down_url'] ?>" class="mr-2"><?= __('Down') ?></a>
                <?php endif; ?>
                <a href="<?= $category['edit_url'] ?>" class="mr-2"><?= __('Edit') ?></a>
                <a href="<?= $category['delete_url'] ?>"><?= __('Delete') ?></a>
            </div>
        <?php endif; ?>
    </div>
<?php endforeach; ?>

<?php if ($total_cat > 0 && $total_files > 0): ?>
    <h4 class="mt-3"><?= __('List of files') ?></h4>
<?php endif; ?>

<?php if (! empty($urls['sort_action'])): ?>
    <form class="form-inline mb-2" method="post" action="<?= $urls['sort_action'] ?>">
        <label class="my-1 mr-2" for="sort_down"><?= __('Sorting') ?></label>
        <select class="custom-select my-1 mr-sm-2" name="sort_down" id="sort_down">
            <option value="0" <?= ! $_SESSION['sort_down'] ? ' selected="selected"' : '' ?>><?= __('by time') ?></option>
            <option value="1" <?= $_SESSION['sort_down'] ? ' selected="selected"' : '' ?>><?= __('by name') ?></option>
        </select>
        <label for="sort_down2" class="mr-2 d-none d-sm-inline-block">&mdash;</label>
        <select class="custom-select my-1 mr-sm-2" name="sort_down2" id="sort_down2">
            <option value="0" <?= ! $_SESSION['sort_down2'] ? ' selected="selected"' : '' ?>><?= __('descending') ?></option>
            <option value="1" <?= $_SESSION['sort_down2'] ? ' selected="selected"' : '' ?>><?= __('ascending') ?></option>
        </select>
        <button type="submit" class="btn btn-primary my-1"><?= __('Sort') ?></button>
    </form>
<?php endif; ?>

<?php foreach ($files as $file): ?>
    <?= $this->fetch('downloads::file_row', ['file' => $file]) ?>
<?php endforeach; ?>

<?php if ($total_files): ?>
    <div class="mt-2">
        <div><?= __('Total') ?>: <?= $total_files ?></div>
        <!-- Page switching -->
        <?php if ($total_files > $user->config->kmess): ?>
            <div class="mt-2"><?= $pagination ?></div>
        <?php endif ?>
    </div>
<?php endif; ?>

<?php if (empty($total_cat) && empty($total_files)): ?>
    <div class="row">
        <div class="col-md-5">
            <div class="alert alert-info shadow border-0"><?= __('List is empty') ?></div>
        </div>
    </div>
<?php endif; ?>

<div class="mt-3">
    <?php if ($user->rights === 4 || $user->rights >= 6): ?>
        <div class="btn-group">
            <?php if (! empty($id)): ?>
                <div class="dropdown mr-2">
                    <button class="btn btn-secondary dropdown-toggle btn-sm" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <?= __('Add file') ?>
                    </button>
                    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                        <a class="dropdown-item" href="?act=files_upload&amp;id=<?= $id ?>"><?= __('Upload File') ?></a>
                        <a class="dropdown-item" href="?act=import&amp;id=<?= $id ?>"><?= __('Import File') ?></a>
                    </div>
                </div>
            <?php endif; ?>
            <div class="dropdown">
                <button class="btn btn-danger dropdown-toggle btn-sm" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                    <?= __('Section actions') ?>
                </button>
                <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                    <a class="dropdown-item" href="?act=folder_add&amp;id=<?= $id ?? '' ?>"><?= __('Create Folder') ?></a>
                    <a class="dropdown-item" href="?act=scan_dir&amp;id=<?= $id ?? '' ?>"><?= __('Update Files') ?></a>
                    <?php if (! empty($id)): ?>
                        <a class="dropdown-item" href="?act=folder_edit&amp;id=<?= $id ?>"><?= __('Change Folder') ?></a>
                        <a class="dropdown-item" href="?act=folder_delete&amp;id=<?= $id ?>"><?= __('Delete Folder') ?></a>
                        <a class="dropdown-item" href="?act=scan_dir&amp;do=clean&amp;id=<?= $id ?>"><?= __('Remove missing files') ?></a>
                        <a class="dropdown-item" href="?act=recount&amp;id=<?= $id ?>"><?= __('Update counters') ?></a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    <?php elseif ($can_upload): ?>
        <div><a href="?act=files_upload&amp;id=<?= $id ?>" class="btn btn-primary"><?= __('Upload File') ?></a></div>
    <?php endif; ?>

    <div class="mt-2">
        <?php if ($id): ?>
            <a href="<?= $urls['downloads'] ?>"><?= __('Downloads') ?></a>
        <?php else: ?>
            <?php if ($user->rights >= 7 || (isset($config['mod_down_comm']) && $config['mod_down_comm'])): ?>
                <a href="?act=review_comments"><?= __('Review comments') ?></a><br>
            <?php endif; ?>
            <a href="?act=bookmark"><?= __('Favorites') ?></a>
        <?php endif; ?>
    </div>
</div>
