<?php $this->partial('aside_subscription'); ?>

<div class="post drop-section">
	<a href="<?php echo _url('index', 'index'); ?>"><?php echo _t('gen.action.back_to_rss_feeds'); ?></a>

	<h2><?php echo _t('sub.title'); ?></h2>

	<form id="add_rss" method="post" action="<?php echo _url('feed', 'add'); ?>" autocomplete="off">
		<div class="stick">
			<input type="url" name="url_rss" class="long" placeholder="<?php echo _t('sub.feed.add'); ?>" />
			<div class="dropdown">
				<div id="dropdown-cat" class="dropdown-target"></div>

				<a class="dropdown-toggle btn" href="#dropdown-cat"><?php echo _i('down'); ?></a>
				<ul class="dropdown-menu">
					<li class="dropdown-close"><a href="#close">❌</a></li>

					<li class="dropdown-header"><?php echo _t('sub.category'); ?></li>

					<li class="input">
						<select name="category" id="category">
						<?php foreach ($this->categories as $cat) { ?>
						<option value="<?php echo $cat->id(); ?>"<?php echo $cat->id() == 1 ? ' selected="selected"' : ''; ?>>
							<?php echo $cat->name(); ?>
						</option>
						<?php } ?>
						<option value="nc"><?php echo _t('sub.category.new'); ?></option>
						</select>
					</li>

					<li class="input" style="display:none">
						<input type="text" name="new_category[name]" id="new_category_name" autocomplete="off" placeholder="<?php echo _t('sub.category.new'); ?>" />
					</li>

					<li class="separator"></li>

					<li class="dropdown-header"><?php echo _t('sub.feed.auth.http'); ?></li>
					<li class="input">
						<input type="text" name="http_user" id="http_user_feed" value=" " autocomplete="off" placeholder="<?php echo _t('sub.feed.auth.username'); ?>" />
					</li>
					<li class="input">
						<input type="password" name="http_pass" id="http_pass_feed" autocomplete="off" placeholder="<?php echo _t('sub.feed.auth.password'); ?>" />
					</li>
				</ul>
			</div>
			<button class="btn" type="submit"><?php echo _i('add'); ?></button>
		</div>
	</form>

	<p class="alert alert-warn">
		<?php echo _t('sub.feed.moved_category_deleted', $this->default_category->name()); ?>
	</p>

	<div class="box">
		<div class="box-title"><label for="new-category"><?php echo _t('sub.category.add'); ?></label></div>

		<ul class="box-content box-content-centered">
			<form action="<?php echo _url('category', 'create'); ?>" method="post">
				<li class="item"><input type="text" id="new-category" name="new-category" placeholder="<?php echo _t('sub.category.new'); ?>" /></li>
				<li class="item"><button class="btn btn-important" type="submit"><?php echo _t('gen.action.submit'); ?></button></li>
			</form>
		</ul>
	</div>

	<form id="controller-category" method="post" style="display: none;"></form>

	<?php
		foreach ($this->categories as $cat) {
			$feeds = $cat->feeds();
	?>
	<div class="box">
		<div class="box-title">
			<form action="<?php echo _url('category', 'update', 'id', $cat->id()); ?>" method="post">
				<input type="text" name="name" value="<?php echo $cat->name(); ?>" />

				<div class="dropdown">
					<div id="dropdown-cat-<?php echo $cat->id(); ?>" class="dropdown-target"></div>

					<a class="dropdown-toggle btn" href="#dropdown-cat-<?php echo $cat->id(); ?>"><?php echo _i('down'); ?></a>
					<ul class="dropdown-menu">
						<li class="dropdown-close"><a href="#close">❌</a></li>

						<li class="item"><a href="<?php echo _url('index', 'index', 'get', 'c_' . $cat->id()); ?>"><?php echo _t('gen.action.filter'); ?></a></li>

						<?php
							$no_feed = empty($feeds);
							$is_default = ($cat->id() === $this->default_category->id());

							if (!$no_feed || !$is_default) {
						?>
						<li class="separator"></li>
						<?php } if (!$no_feed) { ?>
						<li class="item">
							<button class="as-link confirm"
							        data-str-confirm="<?php echo _t('gen.js.confirm_action_feed_cat'); ?>"
							        type="submit"
							        form="controller-category"
							        formaction="<?php echo _url('category', 'empty', 'id', $cat->id()); ?>">
							        <?php echo _t('gen.action.empty'); ?></button>
						</li>
						<?php } if (!$is_default) { ?>
						<li class="item">
							<button class="as-link confirm"
							        data-str-confirm="<?php echo _t('gen.js.confirm_action_feed_cat'); ?>"
							        type="submit"
							        form="controller-category"
							        formaction="<?php echo _url('category', 'delete', 'id', $cat->id()); ?>">
							        <?php echo _t('gen.action.remove'); ?></button>
						</li>
						<?php } ?>
					</ul>
				</div>
			</form>
		</div>

		<ul class="box-content" data-cat-id="<?php echo $cat->id(); ?>">
			<?php if (!empty($feeds)) { ?>
			<?php
					foreach ($feeds as $feed) {
						$error = $feed->inError() ? ' error' : '';
						$empty = $feed->nbEntries() == 0 ? ' empty' : '';
			?>
			<li class="item feed<?php echo $error, $empty; ?>"
			    draggable="true"
			    data-feed-id="<?php echo $feed->id(); ?>"
			    dropzone="move">
				<a class="configure open-slider" href="<?php echo _url('subscription', 'feed', 'id', $feed->id()); ?>"><?php echo _i('configure'); ?></a>
				<img class="favicon" src="<?php echo $feed->favicon(); ?>" alt="✇" /> <?php echo $feed->name(); ?>
			</li>
			<?php 	}
				} else {
			?>
			<li class="item disabled" dropzone="move"><?php echo _t('sub.category.empty'); ?></li>
			<?php } ?>
		</ul>
	</div>
	<?php } ?>
</div>

<?php $class = isset($this->feed) ? ' class="active"' : ''; ?>
<a href="#" id="close-slider"<?php echo $class; ?>></a>
<div id="slider"<?php echo $class; ?>>
<?php
	if (isset($this->feed)) {
		$this->renderHelper('feed/update');
	}
?>
</div>
