<?php
	// запрет прямого доступа
	defined('_JEXEC') or die('Restricted access');
?>

<div id="tracking">
	<div class="right aligned">
		<button id="addStory" class="ui labeled icon orange button big">
			<i class="plus icon"></i>
			Добавить
		</button>
	</div>
	<table class="ui table item_table">
		<tr class="title">
			<td>Тип</td>
			<td>Условие отправки</td>
			<td>Исключение</td>
			<td style="min-width: 40%;">Текст сообщения</td>
			<td colspan="2">Статус</td>
		</tr>
	<?foreach($this->data as $row){ ?>
		<tr>
			<td>
				<?php
				$cond = json_decode($row->condition);
				if ($cond && json_last_error() == 0) {
					switch ($cond->type) {
					    case 'regular_wd':
					        echo '<div class="type">Еженедельное</div>';
					        break;
					    case 'regular_md':
					        echo '<div class="type">Ежемесячное</div>';
					        break;
					    case 'single':
					    	echo '<div class="type">Разовое</div>';
					        break;
					    case 'regular_date':
					        echo '<div class="type">Регулярная дата</div>';
					        break;	
						case 'birthday':
							echo '<div class="type">Напоминание про ДР</div>';
					       break;	
					}
				}
				?>
			</td>
			<td>
				<?php
				if ($cond && json_last_error() == 0) {

					switch ($cond->type) {
					    case 'regular_wd':
					        if (isset($cond->w_days)) {
					        	$days = array('Понедельник','Вторник','Среда','Четверг','Пятница','Суббота','Воскресенье');
					        	echo '<div class="cond_td">';
								if (isset($cond->w_days))
									foreach($cond->w_days as $day) {
										if ($day > 0)
											echo $days[$day-1].'<br>';
									};
								echo '</div>';
					        };
					        if (isset($cond->time)) {
								echo '<div class="cond_td">'."Время - ".print_r($cond->time, true).'</div>';
							}
					        break;
					    case 'regular_md':
					        if (isset($cond->m_days)) {
					        	echo '<div class="cond_td">Каждое ';
								foreach($cond->m_days as $day) {
									echo $day.', ';
								};
								echo 'число</div>';
					        };
					        if (isset($cond->time)) {
								echo '<div class="cond_td">'."Время - ".print_r($cond->time, true).'</div>';
							}
					        break;
					    case 'single':
					    case 'regular_date':
					        if (isset($cond->date)) {
					        	echo '<div class="cond_td">';
								echo $cond->date;
								echo '</div>';
					        };
					        if (isset($cond->time)) {
								echo '<div class="cond_td">'."Время - ".print_r($cond->time, true).'</div>';
							}
					        break;	
						 case 'birthday':
					        if (isset($cond->date)) {
					        	echo '<div class="cond_td">';
								echo $cond->date;
								echo '</div>';
					        };
					        if (isset($cond->time)) {
								echo '<div class="cond_td">'."Время - ".print_r($cond->time, true).'</div>';
							}
					        break;
					}
				} else {
					echo '<div class="cond_td"><pre>'.print_r($row->condition, true).'</pre></div>';
				}
				?>
			</td>
			<td style="word-break: break-all;word-wrap: break-word">
				<?=$row->exception;?>
			</td>
			<td class="comment_body">
				<div class="comment_body_title"><?=$row->mess;?></div>
			</td>
			<td>
				<?
				if ($row->state == 1)
					echo '<div class="state state1"><i class="icon check circle"></i></div>';
				else
					echo '<div class="state state0"><i class="icon remove circle"></i></div>';
				?>
			</td>
			<td class="issueedit">
				<div class="ui icon buttons">
					<button data-id="<?=$row->id;?>" data-sm_title="<?=$row->sm_title;?>" data-mess='<?=$row->mess;?>' data-exception='<?=$row->exception;?>' data-condition='<?=$row->condition;?>' data-state="<?=$row->state;?>" class="editComment ui button"><i class="edit icon"></i></button>
				</div>
				<div class="ui icon buttons">
					<button data-id="<?=$row->id;?>" class="deleteComment ui button"><i class="icon remove circle"></i></button>
				</div>
			</td>
		</tr>
		
	<?php } ?>
	</table>
	<div class="attachments" style="display: none;">
	<?php 
	foreach($this->data as $row){
		echo '<div id="attach'.$row->id.'">'.$row->attachment.'</div>';
	} 
	?>
	</div>
</div>