<?php

/**
 * @var Config $ccdnConf
 * @var array $categories
 * @var CollectionInterface[] $collections
 * @var array $collectionsBundle
 * @var array $customFields
 */

use CCDN\Helpers\Api\Response\CollectionInterface;
use CCDN\Helpers\Enqueue;
use CCDN\Helpers\Entities\Config;
use CCDN\Helpers\HTML;
use CCDN\Helpers\Facade\Http\Url;
use CCDN\Helpers\MenuBuilder;
use CCDN\Helpers\Settings;
use CCDN\Helpers\CCDNFunctions;

CCDNFunctions::echoHeader([
    Url::to('main') => 'Главная ' . Settings::PLUGIN_NAME,
    '' => 'Подборки',
]);

global $config;

?>
	<link href="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/css/select2.min.css" rel="stylesheet">
	<link href="<?php echo Enqueue::assets('css/main.css') ?>" rel="stylesheet">

	<link href="<?php echo Enqueue::assets('css/theme-dark.css') ?>" rel="stylesheet">
<?php if ((float)$config['version_id'] < 14.0): ?>
	<link href="<?php echo Enqueue::assets('css/theme-dark-fix.css') ?>" rel="stylesheet">
<?php endif; ?>
<?php echo MenuBuilder::build() ?>
	<div class="panel panel-flat">
	<div class="panel-body">
		<h3>Подборки</h3>
		<div class="row">
			<div class="col-md-12">
				<button
						data-chunk-count-url="<?php echo Url::to('collections-chunk-count') ?>"
						data-url="<?php echo Url::to('collections-update') ?>"
						class="btn btn-gray bg-slate-600 btn-raised collections-js">Проставить подборки
				</button>
			</div>
		</div>
		<form action="<?php echo Url::to('collections-save-config') ?>" method="POST">
			<div class="row mt-20">
				<div class="form-group col-md-6">
					<label for="collection_field">Доп. поле для вставок подборок</label>
					<select class="form-control custom-select" name="collection_field"
					        id="collection_field">
						<option selected value="">Выбрать поле...</option>
                        <?php foreach ($customFields as $customField) : ?>
							<option <?php echo HTML::selected(
                                $ccdnConf->collection_field,
                                $customField['key']
                            ) ?> value="<?php echo $customField['key'] ?>"><?php echo $customField['name'] ?></option>
                        <?php endforeach; ?>
					</select>
				</div>
				<div class="clearfix"></div>
				<h3 class="col-md-12">Связки категорий и подборок</h3>
                <?php foreach ($categories as $category) : ?>
					<div class="form-group col-md-6">
						<label for="<?php echo $category['alt_name'] ?>">Категория: <?php echo CCDNFunctions::implodeCategories($category) ?></label>
						<select class="form-control custom-select" id="<?php echo $category['alt_name'] ?>"
						        name="collections_bundle[<?php echo $category['id'] ?>]">
							<option selected value="">Выбрать...</option>
                            <?php foreach ($collections as $collection) : ?>
								<option <?php echo HTML::selected($collectionsBundle[$category['id']],
                                    $collection->getName())
                                ?> value="<?php echo $collection->getValue() ?>"><?php echo $collection->getName() ?></option>
                            <?php endforeach; ?>
						</select>
					</div>
                <?php endforeach; ?>
			</div>
			<button class="btn btn-green bg-teal btn-raised" type="submit">Сохранить настройки</button>
		</form>
	</div>


	<div class="modal fade" tabindex="-1" id="progressUpdate" role="dialog" aria-hidden="true">
		<div class="modal-dialog modal-lg">
			<div class="modal-content">
				<div class="modal-header ui-dialog-titlebar">
					<button type="button" class="close ui-dialog-titlebar-close ui-corner-all" data-dismiss="modal" aria-label="Close"><span
								aria-hidden="true">&times;</span></button>
					<span class="modal-title ui-dialog-title" id="exampleModalCenterTitle">Обновление подборок</span>
				</div>
				<div class="modal-body">
					<h5>Не закрывайте окно и страницу до завершения процесса</h5>

					<div class="progress" style="height: 20px">
						<div class="progress-bar" role="progressbar" style="width: 0%;height: 20px;" aria-valuemin="0"
						     aria-valuemax="100">0%
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
	<script src="<?php echo Enqueue::assets('js/collections.js') ?>"></script>
	<script src="<?php echo Enqueue::assets('js/main.js') ?>"></script>
	<script>
		$(document).ready(function () {
			$('.custom-select').select2({
				width: '100%',
				placeholder: 'Выбрать...',
				allowClear: true,
				multiple: false,
			});
		});
	</script>

<?php
echofooter();
