<?
foreach ($letters as $letter) {
	$id 			= $letter->getAttribute('id');
	$status 		= $letter->getAttribute('status');
	
	$subject 		= $letter->getElementsByTagName("subject")->item(0)->nodeValue;
	
	$text			= $letter->getElementsByTagName("text")->item(0)->nodeValue;
	$textSm 		= mb_substr(strip_tags($text), 0, 70, 'UTF-8') . '...';
	
	$fromid			= $letter->getElementsByTagName("fromid")->item(0)->nodeValue;
	$fromname		= $letter->getElementsByTagName("fromname")->item(0)->nodeValue;
	$toid			= $letter->getElementsByTagName("toid")->item(0)->nodeValue;
	$toname			= $letter->getElementsByTagName("toname")->item(0)->nodeValue;
	$coppyid		= $letter->getElementsByTagName("coppyid")->item(0)->nodeValue;
	$coppyname		= $letter->getElementsByTagName("coppyname")->item(0)->nodeValue;
	
	$priority		= $letter->getElementsByTagName("priority")->item(0)->nodeValue;
	$date			= $letter->getElementsByTagName("date")->item(0)->nodeValue;
	$tags			= $letter->getElementsByTagName("tags")->item(0)->nodeValue;
?>
<tr id="<?php echo $id; ?>" status="<?php echo $status; ?>" priority="<?php echo $priority; ?>" <?php if($status == 'notread' and $toid == $_SESSION["userid"]){echo 'style="background-color:rgba(255, 191, 0,0.05);"';} ?> >
	<td>
		<a href="/<?php echo $folder; ?>/letters/?view=<?php echo $id; ?>" class="info-user">
			<?php 
			if($fromid == ''){echo '<div class="user-photo empty" style="background:#5995db;background-image:none!important;"><i class="fa fa-fw fa-exclamation-circle" aria-hidden="true" ></i></div>';}elseif(file_exists('./crmdata/upload/_user-photo/' . get_user_photo($fromid)) and get_user_photo($fromid) != false){
				echo '<div class="user-photo" style="background:url(\'/'. $folder . '/crmdata/upload/_user-photo/' . get_user_photo($fromid) . '?'.rand(1,1000).'        \') center center;"></div>';
			} else {echo '<div class="user-photo empty" style="background:' . get_color2(get_user_login($fromid)) . ';">' . get_user_name($fromid, 'short') . '</div>';}
			?>
		</a>
	</td>
	<td>
		<a href="/<?php echo $folder; ?>/letters/?view=<?php echo $id; ?>">
		<span <?php if($status == 'notread' and $toid == $_SESSION["userid"]){echo 'style="font-weight:bold;"';} ?> href="/<?php echo $folder; ?>/letters/?view=<?php echo $id; ?>"><?php if($fromid == ''){echo 'WORKBox CRM';}else {echo get_user_name($fromid, 'full');} ?></span><?php if($fromid == $_SESSION["userid"]){?> <i class="fa fa-angle-right" title="Отправлено" aria-hidden="true"></i> <?php echo get_user_name($toid, 'full'); ?><?php } ?>
		<small class="d-block">
			<?php if($subject != ''){echo $subject;} else {echo 'Без темы';} ?>
			<?php if ($text != '') {echo '<br/>' . $textSm;} ?>
		</small>
		</a>
	</td>
	<td>
		<?php if($status == 'read' and $toid != $_SESSION["userid"]){?><span class="letter_read" title="Прочитано"><i class="fa fa-check-circle"></i></span><?}?><?php if($coppyid == $_SESSION["userid"]){?><span class="letter_read" title="Копия: <?php echo $coppyname; ?>"><i class="fa fa-files-o"></i></span><?}?>
	</td>
	<td>
		<div class="rem_days_body" >
			<div class="txt"><?php echo date("Y-m-d H:i",strtotime($date)); ?></div>
		</div>
	</td>
	<td>
		<?php if ($fromid == $_SESSION["userid"] or $toid == $_SESSION["userid"] and $fromid == '') { ?>
		<form method="POST">
			<button class="btn btn-sm btn-link" type="submit" name="LetterDelete" onclick="return confirm('Удалить письмо?')" title="Удалить"><i class="fa fa-fw fa-times" aria-hidden="true"></i></button>
			<input type="text" class="hidden" name="LetterDelId" value="<?php echo $id; ?>" />
		</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 } ?>