<?
foreach ($events as $event) {
	$eventId 		= $event->getAttribute('id');
	$eventClient 	= $event->getAttribute('client');
	$eventUser 		= $event->getAttribute('user');
	$eventPriority 	= $event->getAttribute('priority');
	$eventName		= $event->getElementsByTagName("name")->item(0)->nodeValue;
	$eventDateYear	= $event->getElementsByTagName("dateyear")->item(0)->nodeValue;
	$eventDateMonth	= $event->getElementsByTagName("datemonth")->item(0)->nodeValue;
	$eventDateDay	= $event->getElementsByTagName("dateday")->item(0)->nodeValue;
	$eventRepeat 	= $event->getElementsByTagName("repeat")->item(0)->nodeValue;
	
	if($eventDateYear 	!= ''){ $eventDateYear 	= $eventDateYear; 	} else { $eventDateYear 	= date("Y"); }
	if($eventDateMonth 	!= ''){ $eventDateMonth = $eventDateMonth; 	} else { $eventDateMonth 	= date("m"); $eventDateYear 	= date("Y"); }
	if($eventDateDay 	!= ''){ $eventDateDay 	= $eventDateDay;	} else { $eventDateDay 		= date("d"); $eventDateMonth 	= date("m"); $eventDateYear 	= date("Y"); }
	
	if($eventRepeat == 'none')		{ $eventRepeatText = 'Единовременно';}
	if($eventRepeat == 'year')	{ $eventRepeatText = 'Ежегодно';}
	if($eventRepeat == 'month')	{ $eventRepeatText = 'Ежемесячно';}
	if($eventRepeat == 'day')	{ $eventRepeatText = 'Ежедневно';}
	
	$eventDate		= $eventDateYear . '-' . $eventDateMonth . '-' . $eventDateDay;
	$UIP 			= $event->getElementsByTagName("uip")->item(0)->nodeValue;
	
	$date				= date("Y-m-d");
	$datetimeFrom 		= DateTime::createFromFormat( 'Y-m-d', $date );
	$datetimeTo 		= DateTime::createFromFormat( 'Y-m-d', $eventDate );
	
	$daysY = date('L')?366:365;
	$daysM = date('t');
	
	if (file_exists($baseC) and file_exists($fileC)) {
		$doc = new DOMDocument;
		$doc->load($baseC);
		$xpath = new DOMXPath($doc);
		$clients = $xpath->query('/clients/client[@id="'. $eventClient .'"]');
		for ($i = 0; $i < $clients->length; $i++) {
			$ClientID 	= $xpath->query('/clients/client[@id="'. $eventClient .'"]')->item($i)->getAttribute("client");
			$ClientName	= $xpath->query('/clients/client[@id="'. $eventClient .'"]')->item($i)->getElementsByTagName("clientname")->item(0)->nodeValue;
		}
	}
	if (file_exists($baseU) and file_exists($fileU)) {
		$doc = new DOMDocument;
		$doc->load($baseU);
		$xpath = new DOMXPath($doc);
		$users = $xpath->query('/users/user[@login="'. $eventUser .'"]');
		for ($i = 0; $i < $users->length; $i++) {
			$UserID 		= $xpath->query('/users/user[@login="'. $eventUser .'"]')->item($i)->getAttribute("id");
			$UserName 		= $xpath->query('/users/user[@login="'. $eventUser .'"]')->item($i)->getElementsByTagName("username")->item(0)->nodeValue;
			$UserSurName 	= $xpath->query('/users/user[@login="'. $eventUser .'"]')->item($i)->getElementsByTagName("usersurname")->item(0)->nodeValue;
		}
	}
?>
<tr class="<?php if ($eventDate != '' and $datetimeTo < $datetimeFrom){echo 'hidden_event';} ?>" id="<?php echo $eventId; ?>" priority="<?php echo $eventPriority; ?>">
	<td>
		<button class="btn btn-sm btn-link" style="cursor:default;"><i class="fa fa-fw <?php if ($eventDate != '' and $datetimeTo < $datetimeFrom){echo 'fa-calendar-check-o clr_none" title="Прошедшее событие';}else{echo 'fa-calendar-o clr_green" title="Предстоящее событие';} ?>" aria-hidden="true"></i></button>
	</td>
	<td>
		<a href="/<?php echo $folder; ?>/events/?view=<?php echo $eventId; ?>" class="eventName"><?php echo $eventName; ?></a>
		<?php if(glob('./crmdata/upload/'.$eventId.'/*')){ echo '<i class="fa fa-fw fa-cloud-download" aria-hidden="true" title="Прикреплен файл"></i>';} ?>
		<div class="clear"></div>
		<small class="d-block">
			<i class="fa fa-fw fa-address-book" aria-hidden="true"></i> <?php if (file_exists($fileC) AND $eventClient != '') {
				echo '<a href="/' . $folder . '/clients/?view='.$eventClient.'" class="EventClient">'.$ClientName.'</a>';
			} else {
				echo '<a href="javascript:;">Клиент отсутствует</a>';
			} 
			?>
			<div class="clear"></div>
			<i class="fa fa-fw fa-history" aria-hidden="true"></i> <?php if ($eventRepeat != '') { echo '<span class="event">'.$eventRepeatText.'</span>'; } else {echo '- нет -';} ?>
			<?php if ($UIP != '' and $GroupCheck == 'admin') { ?><div class="clear"></div><span class="clr_red" title="IP пользователя, изменившего материал"><i class="fa fa-fw fa-lock" aria-hidden="true"></i> <?php echo $UIP; ?></span><?php } ?>
		</small>
	</td>
	<td>
		<div class="rem_days_body" >
			<div class="txt"><?php echo $eventDate; ?></div>
		</div>
	</td>
	<td class="daysleft">
	<?php if ($eventDate != '') {
			if ($datetimeTo < $datetimeFrom) {
				if 		($eventRepeat == 'year') { echo '<b class="clr_none" title="До следующего события, дн.">' . floatval($daysY - floatval(timediff($eventDate, $date))) . '</b>';} 
				elseif	($eventRepeat == 'month'){echo '<b class="clr_none" title="До следующего события, дн.">' . floatval($daysM - floatval(timediff($eventDate, $date))) . '</b>';}
				else	{echo '<b class="clr_none" title="Событие завершено, дн.">- ' . floatval(timediff($date, $eventDate)) . '</b>';}
			} else { echo '<span title="До текущего события, дн.">' . floatval(timediff($date, $eventDate)) . '</span>';}
		} else {echo '-';} 
	?>
	</td>
	<td>
		<?
		if ($GroupCheck == 'admin' or $GroupCheck == 'manager') {
			echo '<a href="/'.$folder.'/events/?edit='.$eventId.'" class="btn btn-sm btn-link" title="Редактировать"><i class="fa fa-fw fa-pencil" aria-hidden="true"></i></a>';
		} else {
			echo '<a href="javascript:;" class="btn btn-sm btn-link btn-disabled"><i class="fa fa-fw fa-pencil" aria-hidden="true"></i></a>';
		}
		?>
	</td>
	<td>
		<?php if ($GroupCheck != 'user') { ?>
			<form method="POST">
				<button class="btn btn-sm btn-link" type="submit" name="eventDelete" onclick="return confirm('Удалить событие # <?php echo $eventName; ?>?')" title="Удалить"><i class="fa fa-fw fa-times" aria-hidden="true"></i></button>
				<input type="text" class="hidden" name="eventDelId" value="<?php echo $eventId; ?>" />
			</form>
		<?php } else { echo '<a href="javascript:;" class="btn btn-sm btn-link btn-disabled"><i class="fa fa-fw fa-times" aria-hidden="true"></i></a>'; } ?>
	</td>
</tr>
<?php } ?>