<?php 
	$statusInfo = '';
	$signedUser = '';
	$accountantComment = '';
	$notInWork = '';
  if( $costsRequest->status == BudgetCostsRequests::STATUS_SIGNED){
		$statusInfo = '<p class="text-td-small">Подписано: '.$costsRequest->getDateTime('director_signed_at').'</p>';
		$signedUser = 'Подписано: '.$costsRequest->getDirectorName();
	}
	else if( $costsRequest->status == BudgetCostsRequests::STATUS_BACK_OFF){
		$statusInfo = '<p class="text-danger"><b>Возврат: '.$costsRequest->getDateTime('director_signed_at').'</b></p>';
		$signedUser = 'Возврат от: '.$costsRequest->getDirectorName();
	}
	else if( $costsRequest->status == BudgetCostsRequests::STATUS_SIGN_REJECT){
		$statusInfo = '<p class="text-td-small">Отказ в подписи: '.$costsRequest->getDateTime('director_signed_at').'</p>';
		$signedUser = 'Отказ в подписи: '.$costsRequest->getDirectorName();
	}
	else if( $costsRequest->status == BudgetCostsRequests::STATUS_APPROVED){
		$statusInfo = '<p class="text-td-small">Одобрено: '.$costsRequest->getDateTime('findirector_signed_at').'</p>';
		if($costsRequest->budget_upload_id && !$costsRequest->paid_at && Auth::user()->hasAnyRole( array(Role::ROLE_BUDGET_ACCOUNTANT, Role::ROLE_BUDGET_ACC_SECRETARY))){
		  $statusInfo .= '<p class="text-danger small-box"> <b>Ошибка автоматической оплаты.</b> Выгрузка №'.$costsRequest->budget_upload_id.' - отсутствует дата списания в п/п. При наличии в следующей выгрузке данного п/п с датой списания - отметка об оплате поставится автоматически (также можно оплатить в ручном режиме).</p>';
		}
		$signedUser = 'Подписано: '.$costsRequest->getDirectorName();
	}
	else if( $costsRequest->status == BudgetCostsRequests::STATUS_REJECT_DIR){
		$statusInfo = '<p class="text-td-small">Отказ фин.дир.: '.$costsRequest->getDateTime('findirector_signed_at').'</p>';
		$signedUser = 'Подписано: '.$costsRequest->getDirectorName();
	}
	else if( $costsRequest->status == BudgetCostsRequests::STATUS_PAID){
		$signedUser = 'Подписано: '.$costsRequest->getDirectorName().'. Оплачено: '.$costsRequest->getAccountantName();
		$statusInfo = '<p class="text-td-small">Оплачено: ';
		$statusInfo .= $costsRequest->getDate('paid_at').'</p>';
		
		if($costsRequest->accountant_comment){
			$accountantComment = '<br><b>Оплата:</b> '.$costsRequest->accountant_comment;
		}
	}else if( $costsRequest->status == BudgetCostsRequests::STATUS_CREATED && !$costsRequest->ispattern){
		$notInWork = '<b class="text-danger">Черновик. Не на подписи.</b><br>';
	}else if($costsRequest->ispattern){
		$notInWork = '<b class="text-info">Шаблон заявки</b><br>';
	}
?>
<tr class="<?php echo $costsRequest->getClassName(); ?> applicant-id-<?php echo $costsRequest->applicant_id; ?> request-id-<?php echo $costsRequest->id; ?>"
		data-id="<?php echo $costsRequest->id; ?>"
>
	<td><nobr><a href="#" class="show-history" data-action="<?php echo action('BudgetCostsRequestsController@showHistory', $costsRequest->id ); ?>"> <?php echo $costsRequest->id; ?> <i class="fa fa-info-circle"></i> </a></nobr>
    <!--<a href="#" class="requestMessages pull-right" title="Сообщения и комментарии по заявке"><i class="fa fa-envelope-o"></i> </a>-->
  </td>
	<td title="<?php echo $signedUser; ?>">
	 <?php echo $notInWork; ?>

  <?php echo $costsRequest->getDateTime('created_at'); ?>

	<?php if( $userType == 1 && $signRequest != 1): ?>
	<?php else: ?>
	  <span class="text-td-small">Автор: <?php echo $costsRequest->getApplicantName(); ?></span>
	<?php endif; ?>
	<?php echo $statusInfo; ?> 
	</td>
	<td title="<?php echo ($costsRequest->getRequestAccount())? 'Номер счета: '.$costsRequest->getRequestAccount()->current_account : ''; ?>"><?php echo $costsRequest->getCostName(); ?>

	<br><?php echo ($costsRequest->getRequestAccount())? '<b>Счет: </b>'.$costsRequest->getRequestAccount()->name : ''; ?>

	</td>
	<?php if( ($userType != 3 || ($statusType == 'my' && $userType == 3)) && $statusType != 'paid'): ?>
		<?php echo $costsRequest->checkCostsRequestTable($projectAmount); ?>

	<?php elseif( $userType == 3 && $statusType == 'agreed' ): ?>
	<td class="text-right"><span class="accountCode" data-pk="<?php echo $costsRequest->id; ?>"> <?php echo $costsRequest->payment_order_number?  $costsRequest->payment_order_number : ''; ?> </span></td>
	<?php elseif( $statusType == 'paid' ): ?>
	<td class="text-right"> <?php echo $costsRequest->payment_order_number?  $costsRequest->payment_order_number : ''; ?> </td>
	<?php endif; ?>
	<td>
		<?php
		  $links = '';
		  $files = BudgetCostsRequestsFiles::where('budget_cost_request_id', '=', $costsRequest->id)->get();
		  foreach($files as $file){
        $fileUserArr = explode('_', $file->name);
				$showName = $file->show_name;

				$showNameArr = explode('.', $showName);
				$ext = strtolower(array_pop($showNameArr));
				$fileType = '-o';
				if( ($ext === 'xls') || ($ext === 'xlsx') ){ $fileType = '-excel-o';
				}elseif( ($ext === 'doc') || ( $ext === 'docx') ){ $fileType = '-word-o';
				}elseif( $ext === 'pdf' ){ $fileType = '-pdf-o';
				}elseif( $ext === 'txt' ){ $fileType = '-text-o';
				}elseif( ($ext === 'jpeg') || ($ext === 'jpg') || ($ext === 'bmp') || ($ext === 'gif') || ($ext === 'tiff') ){ $fileType = '-image-o';
				}

				if( mb_strlen($showName) > 15 ){
					$showName = mb_substr($showName, 0, 15).'...';
				}
				$links .= '<a target="_blank" href="'.action('BudgetCostsRequestsUploadController@downloadFile', array($file->id, $fileUserArr[0], $file->name)).'" title="'.$file->show_name.'"><i class="fa fa-file'.$fileType.'"></i> '.$showName.'</a><br>';
			}
		?>
	  <?php echo $links; ?>

	</td>
	<td class="text-right"><span class="requestSum to-humanize" data-value="<?php echo $costsRequest->getAmountAttribute(); ?>"><?php echo $costsRequest->getAmountAttribute(); ?></span> <i class="fa fa-rub"></i></td>
	<td class="action-buttons table-comment">
		<?php echo $costsRequest->request_comment; ?>

		<?php if($costsRequest->parent_request_id): ?> <br>Создано из заявки <a  href="<?php echo URL::to('budget/costsrequests/search'); ?>?type=id&content=<?php echo $costsRequest->parent_request_id; ?>">#<?php echo $costsRequest->parent_request_id; ?></a> <?php endif; ?> 
			<!--  подпись заявок  -->
		<?php if( $signRequest == 1 && $costsRequest->status == BudgetCostsRequests::STATUS_WORK && !Auth::user()->isAdmin() && $costsRequest->director_id == Auth::user()->id): ?>
			<button class="cost-request-sign btn btn-success btn-xs" title="Подписать"><i class="fa fa-legal"></i> </button><!-- подпись заявки-->
			<button class="cost-request-no-sign btn btn-danger btn-xs" title="Окно возврата/отказа в подписи"><i class="fa fa-ban"></i> </button> <!-- Отказ в подписи-->
		<?php elseif( $signRequest == 1 && $costsRequest->status == BudgetCostsRequests::STATUS_SIGNED && $costsRequest->director_id == Auth::user()->id): ?>
			<button class="cost-request-sign remove-sign btn btn-warning btn-xs" title="Отменить подпись"><i class="fa fa-reply"></i> </button><!-- отмена подписи -->
		<?php endif; ?>
		<!--  пользователь заявок  -->
		<?php if( $costsRequest->status == BudgetCostsRequests::STATUS_REJECT_DIR): ?>
		<br><b> Отказано:  </b><?php echo $costsRequest->findirector_comment; ?>

		<?php elseif( $costsRequest->status == BudgetCostsRequests::STATUS_BACK_OFF): ?>
		<br><b class="text-danger"> Возврат: <span class="director-comment"><?php echo $costsRequest->director_comment; ?></span> </b>
		<?php elseif( $costsRequest->status == BudgetCostsRequests::STATUS_SIGN_REJECT): ?>
		<p class="text-danger"><b>Не подписано: </b> <span class="director-comment"><?php echo $costsRequest->director_comment; ?></span></p>
		<?php elseif($costsRequest->director_comment): ?>
		<br><b>Комментарий к подписи: </b><span class="director-comment"><?php echo $costsRequest->director_comment; ?></span>
		<?php endif; ?>
	<?php if($userType == 1): ?>
		<?php if( $costsRequest->applicant_id == Auth::user()->id && $costsRequest->status != BudgetCostsRequests::STATUS_PAID ): ?>
			<?php if( ($costsRequest->status == BudgetCostsRequests::STATUS_CREATED || $costsRequest->status == BudgetCostsRequests::STATUS_WORK || $costsRequest->status == BudgetCostsRequests::STATUS_BACK_OFF) && !$costsRequest->deleted_at): ?>
		  <button class="cost-request-edit btn btn-info btn-xs" title="Редактировать"><i class="fa fa-pencil"></i> </button> <!-- Редактировать-->
			  <?php if($costsRequest->ispattern == 1): ?>
			  <button class="cost-request-add-by-pattern btn btn-xs btn-success" title="Окно добавления по шаблону">Добавить по шаблону</button> <!-- Добавить по шаблону-->
				<?php endif; ?>
			<?php endif; ?>
		  <?php if($costsRequest->deleted_at): ?>
			  <button class="cost-request-restore btn btn-info btn-xs" data-action="<?php echo action('BudgetCostsRequestsController@restoreCostsRequest'); ?>" title="Восстановить"><i class="fa fa-undo"></i> </button> <!-- Восстановить-->
			<?php else: ?>
		    <button class="cost-request-delete btn btn-danger btn-xs" data-action="<?php echo action('BudgetCostsRequestsController@deleteCostsRequest'); ?>" title="Удалить"><i class="fa fa-remove"></i> </button> <!-- Удалить-->
		  <?php endif; ?>
		<?php endif; ?>
		<?php if( $signRequest == 1 && !$costsRequest->deleted_at && $costsRequest->status != BudgetCostsRequests::STATUS_APPROVED && $costsRequest->status != BudgetCostsRequests::STATUS_PAID ): ?>
		<button class="cost-request-edit btn btn-info btn-xs" title="Редактировать"><i class="fa fa-pencil"></i> </button> <!-- Редактировать-->
		<?php endif; ?>

	<!--  Бухгалтер, фин. директор  -->
	<?php elseif(($userType == 2) || ($userType == 3)): ?>
		<?php if( $userType == 2 and $costsRequest->status == BudgetCostsRequests::STATUS_SIGNED ): ?>
		  <button class="cost-request-allocate btn btn-success btn-xs" title="Разделить заявку на несколько / редактировать"><i class="fa fa-sitemap"></i> </button><!-- разбитие заявки -->
			<button class="cost-request-agree approve btn btn-success btn-xs" title="Одобрить"><i class="fa fa-thumbs-up"></i> </button><!-- Одобрение фин. директором-->
			<button class="cost-request-reject btn btn-danger btn-xs" title="Отказать"><i class="fa fa-ban"></i> </button> <!-- Отказ фин. директором-->
		<?php elseif( $userType == 2 and $costsRequest->status == BudgetCostsRequests::STATUS_APPROVED ): ?>
			<button class="cost-request-agree reject btn btn-warning btn-xs" title="Отменить одобрение"><i class="fa fa-reply"></i> </button><!-- Отмена одобрения-->
		<?php endif; ?>
		<?php if( $userType == 3  and $costsRequest->status == BudgetCostsRequests::STATUS_APPROVED): ?>
			<button class="cost-request-pay pay btn btn-success btn-xs" title="Открыть окно оплаты"><i class="fa fa-check"></i> </button> <!-- Оплата -->
		<?php elseif( $userType == 3 and $costsRequest->status == BudgetCostsRequests::STATUS_PAID ): ?>
			<button class="cost-request-pay remove-pay btn btn-warning btn-xs" title="Отменить оплату"><i class="fa fa-reply"></i> </button> <!-- Отмена оплаты -->
		<?php endif; ?>
		<?php if( $costsRequest->applicant_id == Auth::user()->id && $costsRequest->status != BudgetCostsRequests::STATUS_PAID): ?>
		  <?php if( ($costsRequest->status == BudgetCostsRequests::STATUS_CREATED || $costsRequest->status == BudgetCostsRequests::STATUS_WORK || $costsRequest->status == BudgetCostsRequests::STATUS_BACK_OFF) && !$costsRequest->deleted_at ): ?>
		  <button class="cost-request-edit btn btn-info btn-xs" title="Редактировать"><i class="fa fa-pencil"></i> </button> <!-- Редактировать-->
			<?php endif; ?>
			<?php if($costsRequest->deleted_at): ?>
			  <button class="cost-request-restore btn btn-info btn-xs" data-action="<?php echo action('BudgetCostsRequestsController@restoreCostsRequest'); ?>" title="Восстановить"><i class="fa fa-undo"></i> </button> <!-- Восстановить-->
			<?php else: ?>
		    <button class="cost-request-delete btn btn-danger btn-xs" data-action="<?php echo action('BudgetCostsRequestsController@deleteCostsRequest'); ?>" title="Удалить"><i class="fa fa-remove"></i> </button> <!-- Удалить-->
		  <?php endif; ?>
		<?php endif; ?>
		<?php if( $signRequest == 1 && !$costsRequest->deleted_at && $costsRequest->status != BudgetCostsRequests::STATUS_APPROVED && $costsRequest->status != BudgetCostsRequests::STATUS_PAID ): ?>
		<button class="cost-request-edit btn btn-info btn-xs" title="Редактировать"><i class="fa fa-pencil"></i> </button> <!-- Редактировать-->
		<?php endif; ?>
	<?php endif; ?>
		<?php echo $costsRequest->getChildrenRequests(); ?>

		<?php echo $accountantComment; ?>

	</td>
</tr>