<?
foreach ($tasks as $task) {
	$TaskId 		= $task->getAttribute('id');
	$TaskName 		= $task->getElementsByTagName("taskname")->item(0)->nodeValue;
	$TaskStatus 	= $task->getAttribute('status');
	$TaskType 		= $task->getAttribute('type');
	$TaskPriority 	= $task->getAttribute('priority');
	$TaskPercent 	= $task->getAttribute('percent');
	$TaskUser 		= $task->getAttribute('deligated');
	$TaskDeal 		= $task->getAttribute('deal');
	$TaskComment 	= $task->getElementsByTagName("comment")->item(0)->nodeValue;
	$TaskLink 		= $task->getElementsByTagName("link")->item(0)->nodeValue;
	$TaskDateStart 	= $task->getElementsByTagName("datestart")->item(0)->nodeValue;
	$TaskDateEnd 	= $task->getElementsByTagName("dateend")->item(0)->nodeValue;
	$TaskTimeInWork = $task->getElementsByTagName("timeinwork")->item(0)->nodeValue;
	$UIP 			= $task->getElementsByTagName("uip")->item(0)->nodeValue;
	
	$date				= date("Y-m-d");
	$datetimeFrom 		= DateTime::createFromFormat( 'Y-m-d', $date );
	$datetimeTo 		= DateTime::createFromFormat( 'Y-m-d', $TaskDateEnd );
	
	if (file_exists($baseD) and file_exists($fileD)) {
		$doc = new DOMDocument;
		$doc->load(ROOT . '/crmdata/database/deals.xml');
		$xpath = new DOMXPath($doc);
		$deals = $xpath->query('/deals/deal[@id="'. $TaskDeal .'"]');
		for ($i = 0; $i < $deals->length; $i++) {
			$DealNumber 	= $xpath->query('/deals/deal[@id="'. $TaskDeal .'"]')->item($i)->getElementsByTagName("num")->item(0)->nodeValue;
			$DealClient		= $xpath->query('/deals/deal[@id="'. $TaskDeal .'"]')->item($i)->getAttribute("client");
			$DealType		= $xpath->query('/deals/deal[@id="'. $TaskDeal .'"]')->item($i)->getAttribute("type");
			$doс = new DOMDocument;
			$doс->load(ROOT . '/crmdata/database/clients.xml');
			$xpathc = new DOMXPath($doс);
			$clients = $xpathc->query('/clients/client[@id="'. $DealClient .'"]');
			foreach ($clients as $client) {
				$ClientName 	= $client->getElementsByTagName("clientname")->item(0)->nodeValue;
			}
		}
	}
	if (file_exists($baseU) and file_exists($fileU)) {
		$doc = new DOMDocument;
		$doc->load(ROOT . '/crmdata/database/users.xml');
		$xpath = new DOMXPath($doc);
		$users = $xpath->query('/users/user[@login="'. $TaskUser .'"]');
		for ($i = 0; $i < $users->length; $i++) {
			$UserID 		= $xpath->query('/users/user[@login="'. $TaskUser .'"]')->item($i)->getAttribute("id");
			$UserName 		= $xpath->query('/users/user[@login="'. $TaskUser .'"]')->item($i)->getElementsByTagName("username")->item(0)->nodeValue;
			$UserSurName 	= $xpath->query('/users/user[@login="'. $TaskUser .'"]')->item($i)->getElementsByTagName("usersurname")->item(0)->nodeValue;
		}
	}
	if ( $TaskType == 'call' ) {
		$TaskTypeIco = '<i class="fa fa-fw fa-volume-control-phone" aria-hidden="true"></i>';
	} elseif( $TaskType == 'mail' ) {
		$TaskTypeIco = '<i class="fa fa-fw fa-envelope" aria-hidden="true"></i></button>';
	} elseif( $TaskType == 'visit' ) {
		$TaskTypeIco = '<i class="fa fa-fw fa-users" aria-hidden="true"></i></button>';
	} else {
		$TaskTypeIco = '<i class="fa fa-fw fa-check" aria-hidden="true"></i></button>';
	}
?>
<tr id="<?php echo $TaskId; ?>" priority="<?php echo $TaskPriority; ?>">
	<td>
		<?php if ($TaskUser == $LoginCheck or $GroupCheck == 'admin' or $GroupCheck == 'manager' or $TaskUserS == $LoginCheck) { if ( $TaskStatus == 'inwork' ) { ?>
			<form method="POST">
				<button class="btn btn-sm btn-link  <?php if ($TaskDateEnd != '' and $datetimeTo < $datetimeFrom){echo 'clr_red';} ?>" type="submit" name="TaskDone" onclick="return confirm('Завершить задачу # <?php echo $TaskName; ?>?')" title="Завершить задачу"><? echo $TaskTypeIco;?></button>
				<input type="text" class="hidden" name="TaskDoneId" value="<?php echo $TaskId; ?>" />
			</form>
			<?php } else { ?>
			<form method="POST">
				<button class="btn btn-sm btn-link" type="submit" name="TaskRestore" onclick="return confirm('Возобновить задачу # <?php echo $TaskName; ?>?')" title="Возобновить задачу"><i class="fa fa-fw fa-refresh" aria-hidden="true"></i></button>
				<input type="text" class="hidden" name="TaskRestoreId" value="<?php echo $TaskId; ?>" />
			</form>
		<?php } } else { echo '<button class="btn btn-sm btn-link btn-disabled">'.$TaskTypeIco.'</button>'; } ?>
	</td>
	<td>
		<a href="/<?php echo $folder; ?>/tasks/?view=<?php echo $TaskId; ?>" class="TaskName"><?php echo $TaskName; ?></a>
		<?php if(glob('./crmdata/upload/'.$TaskId.'/*')){ echo '<i class="fa fa-fw fa-cloud-download" aria-hidden="true" title="Прикреплен файл"></i>';} ?>
		<?php if ($TaskLink != '') { ?><a class="TaskLink" href="<?php echo $TaskLink; ?>" target="_blank"><i class="fa fa-fw fa-link" aria-hidden="true" title="Перейти по ссылке"></i></a><?php } ?>
		<div class="clear"></div>
		<small class="d-block">
			<i class="fa fa-fw fa-briefcase" aria-hidden="true"></i> <?php if (file_exists($fileD) AND $TaskDeal != '') {
				echo '<a href="/' . $folder . '/deals/?view='.$TaskDeal.'" class="TaskDeal">'; if($DealType == 'offer'){echo $nCRMoffers;}else{echo $nCRMdeals;} echo ' '.$DealNumber.' - '.$ClientName.'</a>';
			} else {
				echo '<a href="#">Сделка отсутствует</a>';
			} 
			?>
			<div class="clear"></div>
			<i class="fa fa-fw fa-user" aria-hidden="true"></i> <?php if ($TaskUser != '') {
				echo '<a href="/' . $folder . '/users/?view='.$UserID.'" class="TaskUser" user="'.$TaskUser.'">'.$UserSurName . ' ' . $UserName.'</a>';
			} else {
				echo '<a href="/' . $folder . '/search/?user='.$TaskUser.'">Не делегировано</a>';
			} 
			?>
			<?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>
	<?php if ($TaskStatus == 'inwork' or $aliases_arrX[0] != '') {?>
	<td style="position:relative;">
		<div class="persent-bar">
			<div class="persent-bar-body" style="height:<?php echo $TaskPercent;?>%;max-height:100%;"></div>
			<div class="persent-bar-text"><?php echo $TaskPercent;?>%</div>
		</div>
	</td>
	<?php } ?>
	<td>
		<div class="rem_days_body" >
			<div class="txt"><?php echo $TaskDateStart;?></div>
		</div>
	</td>
	<td>
		<div class="rem_days_body">
			<div class="txt"><?php if ($TaskDateEnd != '') { echo '<span class="TaskDateEnd">'.$TaskDateEnd.'</span>'; } else {echo '- нет -';} ?></div>
			<?php if ($TaskDateEnd != '') {?><?php if (timediff($TaskDateStart, date("Y-m-d")) > 0) {?><div class="bar" style="width:<?php echo timediffpercent($TaskDateStart, $TaskDateEnd); ?>%;"></div><?php } ?><?php } ?>
		</div>
	</td>
	<td>
		<?
		if ($TaskUser == $LoginCheck or $GroupCheck == 'admin' or $GroupCheck == 'manager') {
			echo '<a href="/'.$folder.'/tasks/?edit='.$TaskId.'" class="btn btn-sm btn-link" title="Редактировать"><i class="fa fa-fw fa-pencil" aria-hidden="true"></i></a>';
		} else {
			echo '<a href="#" class="btn btn-sm btn-link btn-disabled"><i class="fa fa-fw fa-pencil" aria-hidden="true"></i></a>';
		}
		?>
	</td>
</tr>
<?php } ?>