<?php if (file_exists($baseLT) and file_exists($fileLT)) { ?>
	<?php if ( isset($_GET['edit']) OR isset($_GET['new']) ) {?>
			<div class="panel panel-default" style="margin-top:15px;">
				<div class="panel-heading">
					<h3 class="panel-title"><i class="fa fa-fw fa-clipboard"></i> Шаблоны писем</h3>
				</div>
				<div class="panel-body">
					<?php if(count_templs($module) > 0){ ?>
					<div class="page_block2">
						<ul class="templs_list">
							<?php 
							$dom = new DOMDocument;
							$dom->load($baseTMPL);
							$xpath = new DOMXPath($dom);
							$templs = $xpath->query('/templs/templ[mod="'.$module.'"]');
							foreach ($templs as $templ) {
								$ID		= $templ->getAttribute('id');
								$Name 	= $templ->getElementsByTagName("name")->item(0)->nodeValue;
								$Mod 	= $templ->getElementsByTagName("mod")->item(0)->nodeValue;
								echo '<li id="templ_'.$ID.'">';
								if ($GroupCheck == 'admin' or $GroupCheck == 'manager') { echo '<i class="fa fa-times-circle clr_high" title="Удалить шаблон" onclick="del_templ('.$ID.');"></i>';}
								echo '<span onclick="get_templ('.$ID.');">'.$Name.'</span></li>';
							}
							?>
						</ul>
					</div>
					<?php } else { ?>
					<center>- нет шаблонов -</center>
					<?php } ?>
				</div>
			</div>
			<p style="line-height:15px;margin:5px 0 0;color:#999;text-align:justify;"><small>Для сохранения нового шаблона обязательно заполните: Тему, Текст письма, включите "Сохранить как шаблон"</small></p>
	<?php } else { ?>
	<div class="btn-group btn-group-justified" style="margin-bottom: 20px;">
		<?php if (file_exists($baseLT) and file_exists($fileLT)) { ?>
		<a href="/<?php echo $folder; ?>/letters/?new" class="btn btn-md btn-info add-letter" style="color:#fff;">Написать</a>
		<?php } ?>
		<?php if (file_exists($fileN)) { ?>
		<a href="javascript:;" onclick="location.reload();" class="btn btn-md btn-default">Проверить</a>
		<?php } ?>
	</div>

	<div class="panel panel-default">
		<div class="panel-body">
			<ul class="filter-list">
				<li><a href="/<?php echo $folder; ?>/letters"><i class="fa fa-fw fa-envelope-open"></i> Все письма<b></a><span class="filter-list-num"><?php echo count_letters('all');?></span></b></li>
				<li><a href="/<?php echo $folder; ?>/letters/?folder=inbox"><i class="fa fa-fw fa-envelope-open-o"></i> Входящие<b></a><span class="filter-list-num"><?php echo count_letters('inbox');?></span></b></li>
				<li><a href="/<?php echo $folder; ?>/letters/?folder=notread"><i class="fa fa-fw fa-envelope-o"></i> Непрочитанные<b></a><span class="filter-list-num"><?php echo count_letters('notread');?></span></b></li>
				<li><a href="/<?php echo $folder; ?>/letters/?folder=outbox"><i class="fa fa-fw fa-envelope"></i> Отправленные<b></a><span class="filter-list-num"><?php echo count_letters('outbox');?></span></b></li>
				<hr/>
				<li><a href="/<?php echo $folder; ?>/letters/?folder=important"><i class="fa fa-fw fa-exclamation-triangle"></i> Важные<b></a><span class="filter-list-num"><?php echo count_letters('important');?></span></b></li>
			</ul>
		</div>
	</div>


	<form method="POST">
	
	<div class="btn-group btn-group-justified" style="margin-bottom:20px;">
		<div class="btn-group" role="group">
			<button type="submit" name="ReadAllLetters" class="btn btn-default">Прочитать все</button>
		</div>
		<div class="btn-group" role="group">
			<button type="submit" name="DelAllNot" class="btn btn-default" title="Очистить системыне уведомления">Очистить</button>
		</div>	
	</div>
	
	<div class="filters">
		<div class="panel panel-default">
			<div class="panel-heading">
				<h3 class="panel-title"><i class="fa fa-fw fa-tags"></i> Фильтр по меткам</h3>
			</div>
			<div class="panel-body">
				
					<div class="input-group">
						<input class="form-control" type="text" placeholder="Выберите метку" value="<?php if(isset($_POST['selectLetterTags'])){echo $_POST['selectLetterTags'];} ?>" name="selectLetterTags" list="all-lettertags" autocomplete="off" />
						<div class="input-group-btn">
							<button class="btn btn-default" type="submit" name="searchTags"><i class="fa fa-fw fa-search"></i></button>
						</div>
					</div>
				
				<datalist id="all-lettertags">
					<?php
					$doc = new DOMDocument;
					$doc->load($baseLT);
					$xpathc = new DOMXPath($doc);
					$tletters = $xpathc->query('/letters/letter');
					$all_tags_arr = array();
					$i = 0;
					foreach ($tletters as $letter) {
						$tags = $letter->getElementsByTagName("tags")->item(0)->nodeValue;
						
						$tags_arr = explode(", ", $tags);
						$all_tags_arr = array_merge($all_tags_arr, $tags_arr);
					}
					$all_tags_arr = array_unique($all_tags_arr);
					$all_tags_arr = array_values($all_tags_arr);
					foreach ($all_tags_arr as $tag => $value) {
						$tagName	= $value;
						$tquery	= mb_strtolower(rawurldecode($tagName), 'UTF-8');
						$tagCount 	= count($xmlletters->xpath('/letters/letter[contains(translate(., "ABCDEFGHJIKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИКЛМНОПРСТУФХЦЧШЩЭЮЯ", "abcdefghjiklmnopqrstuvwxyzабвгдеёжзиклмнопрстуфхцчшщэюя"),"'.$tquery.'")]'));
						echo '<option value="' . $tagName . '">'.$tagCount.'</option>';
					}
					?>
				</datalist>
			</div>
		</div>
	</div>
	</form>
	<?php } ?>
<?php } ?>