<?
foreach($deals as $deal) {
	$DealId 	= $deal->getAttribute('id');
	$Type 		= $deal->getAttribute('type');
	$Client 	= $deal->getAttribute('client');
	$DealNumber = $deal->getElementsByTagName("num")->item(0)->nodeValue;
	$Price 		= floatval($deal->getElementsByTagName("price")->item(0)->nodeValue);
	$Costs 		= floatval($deal->getElementsByTagName("costs")->item(0)->nodeValue);
	$Paid 		= floatval($deal->getElementsByTagName("paid")->item(0)->nodeValue);
	if(is_object($deal->getElementsByTagName("subject")->item(0))){
		$Subject 	= $deal->getElementsByTagName("subject")->item(0)->nodeValue;
	}
	if(is_object($deal->getElementsByTagName("comment")->item(0))){
		$Comment 	= $deal->getElementsByTagName("comment")->item(0)->nodeValue;
		$CommentSm 	= mb_substr(strip_tags($Comment), 0, 30, 'UTF-8') . '...';
	}
	$DateStart 	= $deal->getElementsByTagName("dates")->item(0)->nodeValue;
	$DateEnd 	= $deal->getElementsByTagName("datee")->item(0)->nodeValue;
	$UIP 		= $deal->getElementsByTagName("uip")->item(0)->nodeValue;
	
	$datetime 		= DateTime::createFromFormat( 'Y-m-d', date("Y-m-d") );
	$datetimeFrom 	= DateTime::createFromFormat( 'Y-m-d', $DateStart );
	$datetimeTo 	= DateTime::createFromFormat( 'Y-m-d', $DateEnd );
?>
<tr class="<?php if ($Price >= '500001') {echo 'vhigh';} elseif ($Price < '500000' and $Price >= '250000') {echo 'high';} elseif ($Price < '250000' and $Price >= '100000') {echo 'medium';} elseif ($Price < '100000' and $Price >= '25000') {echo 'low';} elseif ($Price < '25000' and $Price >= '5000') {echo 'vlow';} else {echo 'none';} ?>" id="<?php echo $DealId; ?>" type="<?php echo $Type; ?>">
	<td align="center">
		<?
		if ($Type == 'deal-c') {
			echo '<button class="btn btn-sm btn-link info-deal"><i class="fa fa-fw fa-briefcase" aria-hidden="true" title="Информация о сделке"></i></button>';
		} elseif ($Type == 'deal-a') {
		?>
		<form method="POST">
			<button class="btn btn-sm btn-link" type="submit" name="DealDone" onclick="return confirm('Завершить сделку <?php echo $DealNumber; ?>')" title="Завершить сделку"><i class="fa fa-fw fa-handshake-o" aria-hidden="true"></i></button>
			<input type="text" class="hidden" name="DealDoneId" value="<?php echo $DealId; ?>" />
		</form>
		<?
		} else {
		?>
		<form method="POST">
			<button class="btn btn-sm btn-link" type="submit" name="DealActive" onclick="return confirm('Добавить сделку <?php echo $DealNumber; ?>')" title="Перевести в сделку"><i class="fa fa-fw fa-file-text-o" aria-hidden="true"></i></button>
			<input type="text" class="hidden" name="DealActiveId" value="<?php echo $DealId; ?>" />
		</form>
		<?php } ?>
	</td>
	<td>
		<a href="<?php echo '/' . $folder.'/deals/?view='.$DealId; ?>"><?php if ($Type == 'deal-a' or $Type == 'deal-c') {echo $nCRMdeals . ' <span class="DealNumber">' . $DealNumber . '</span>';} else {echo $nCRMoffers . ' <span class="DealNumber">' . $DealNumber . '</span>';} ?></a>
		<?php if(glob('./upload/'.$DealId.'/*')){ echo '<i class="fa fa-fw fa-cloud-download" aria-hidden="true" title="Прикреплен файл"></i>';} ?>
		<small class="d-block">
			<i class="fa fa-fw fa-address-book" aria-hidden="true"></i> <?php if ($clients->length > 0) {echo '<a href="/' . $folder . '/clients/?view='.$Client.'" class="DealClient">' . get_client_name($Client) . '</a>';} else {echo 'Нет клиента';} ?>
			<div class="clear"></div>
			<i class="fa fa-fw fa-info-circle DealComment" aria-hidden="true"></i> <?php if(isset($Subject)){echo $Subject;}elseif(isset($Comment)){echo $CommentSm;}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 style="text-align:right!important;">
		<b class="DealPrice" title="Сумма сделки"><?php echo conv_price( $Price , 'long' , '<i class="fa fa-sm fa-rub"></i>'); ?></b><br/>
		<small>
			<div class="clr_red" title="Сумма расходов"><?php if($Costs > 0){echo '-' . conv_price( $Costs , 'long' , '<i class="fa fa-sm fa-rub"></i>');} else {echo '0 <i class="fa fa-sm fa-rub"></i>';} ?></div>
			<?php if($Type != 'deal-o' AND $Price > $Paid){ ?>
				<div class="clr_vlow" title="Сумма долга"><?php if($Price - $Paid > 0){echo conv_price( $Price - $Paid , 'long' , '<i class="fa fa-sm fa-rub"></i>');} else {echo conv_price( 0 , 'long' , '<i class="fa fa-sm fa-rub"></i>');};?></div>
			<?php } elseif($Paid != '0' AND $Paid != ''){ ?>
				<div class="clr_green" title="Сумма доходов"><?php echo conv_price( $Paid - $Costs , 'long' , '<i class="fa fa-sm fa-rub"></i>');?></div>
			<?php } else { ?>
				<div class="clr_green" title="Сумма доходов"><?php echo conv_price( $Price - $Costs , 'long' , '<i class="fa fa-sm fa-rub"></i>');?></div>
			<?php } ?>
		</small>
	</td>
	<td style="position:relative;">
		<div class="persent-bar">
			<div class="persent-bar-body" style="height:<?php if($Paid != '0' AND $Paid != ''){echo round(($Paid - $Costs) / $Price * 100 , 1);} else {echo '0';}?>%;max-height:100%;"></div>
			<div class="persent-bar-text"><?php if($Paid != '0' AND $Paid != ''){echo round(($Paid - $Costs) / $Price * 100 , 1);} else {echo '0';}?>%</div>
		</div>
	</td>
	<td>
		<div class="rem_days_body" >
			<div class="txt"><?php echo $DateStart; ?></div>
		</div>
	</td>
	<td>
		<div class="rem_days_body" title="<?php if($DateEnd != ''){if($datetimeTo < $datetime) {echo 'Срок сделки: ' . $datetimeFrom->diff( $datetimeTo )->format( '%a' ) . ' дн.';} else { echo 'До окончания: ' . $datetime->diff( $datetimeTo )->format( '%a' ) . ' дн.';}} else {echo 'Нет данных';} ?>" style="cursor:help;">
			<div class="txt"><?php if ($DateEnd != '') { echo '<span class="DateEnd">'.$DateEnd.'</span>'; } else {echo '- нет -';} ?></div>
			<?php if ($DateEnd != '') {?><?php if (timediff($DateStart, date("Y-m-d")) > 0) {?><div class="bar" style="width:<?php echo timediffpercent($DateStart, $DateEnd); ?>%;"></div><?php } ?><?php } ?>
		</div>
	</td>
	<?php if($aliases_arr[1] =='deals-archive'){ ?>
	<td class="daysleft">
		<?php if ($DateEnd != '') {if ($datetimeTo < $datetimeFrom) {echo '<b class="clr_red">- ' . $datetimeFrom->diff( $datetimeTo )->format( '%a' ) . '</b>';} else {echo $datetimeFrom->diff( $datetimeTo )->format( '%a' );} } else {echo '-';}
		?>
	</td>
	<?php } ?>
	<td>
		<a href="<?php echo '/' . $folder.'/deals/?edit='.$DealId; ?>" class="btn btn-sm btn-link edit-deal" title="Редактировать"><i class="fa fa-fw fa-pencil" aria-hidden="true"></i></a>
	</td>
	<td>
		<form method="POST">
			<button class="btn btn-sm btn-link" type="submit" name="DealDelete" onclick="return confirm('Удалить предложение <?php echo $DealNumber; ?>?')" title="Удалить"><i class="fa fa-fw fa-times" aria-hidden="true"></i></button>
			<input type="text" class="hidden" name="DealDelId" value="<?php echo $DealId; ?>" />
		</form>
	</td>
</tr>
<?php } ?>