<?php
	Assets::add( 'select2' );
	// Assets::add( 'users.min.js' );
	// Assets::add( 'amib-datepicker' );
	// Assets::add( 'momentjs' );
	// Assets::add( 'users.css' );
	Assets::add( 'exam-users.min.js' );

	AMIB::add_js_translation( trans( 'exam.js-msgs' ) );
?>
<?php $__env->startSection( 'head' ); ?>
	<script>
		var companiesAjaxUrl = "<?php echo route( 'get-companies-ajax' ); ?>";
		var clearExamUsersAjaxUrl = "<?php echo route( 'clear-exam-users-ajax' ); ?>";
		var searchedCompany = <?php echo AMIB::json_encode( $searched_company ); ?>;
		var restartExamUrl = '<?php echo route( 'restart-exam-user-ajax' ); ?>';
	</script>
<?php $__env->stopSection(); ?>

<?php $__env->startSection( 'content' ); ?>
	<h1><?php echo $exam_type == 'exam' ?
			'فهرست شرکت کنندگان در آزمون' :
			'نظرسنجی‌های آزمون'; ?>

		&laquo;<?php echo e($exam->name); ?>&raquo;
	</h1>

	<?php echo Form::model( $old_search_data, array( 'method' => 'get', 'id' => 'search_form' ) ); ?>

		<?php echo Form::label( 's_name', trans( 'user.full_name' ) ); ?>

		<?php echo Form::text( 's_name' ); ?>

		<?php echo Form::label( 's_company', trans( 'user.company_id' ) ); ?>

		<?php echo Form::select( 's_company', array(), null, array( 'class' => 'company-select' ) ); ?>

		<?php echo Form::select( 's_status', $statuses_select ); ?>

		<?php echo Form::submit( trans( 'main.search' ) ); ?>

	<?php echo Form::close(); ?>


	<?php if( count( $users ) ): ?>
		<table id="users-table" class="default">
			<thead>
				<tr>
					<th><?php echo trans( 'main.row' ); ?></th>
					<th><?php echo trans( 'user.fname' ); ?></th>
					<th><?php echo trans( 'user.lname' ); ?></th>
					<th><?php echo trans( 'user.province_id' ); ?></th>
					<th><?php echo trans( 'user.national_code' ); ?></th>
					<th><?php echo trans( 'user.company_name' ); ?></th>
					<th><?php echo trans( 'begin.allowed_timestamp' ); ?></th>
					<th><?php echo trans( 'begin.start_timestamp' ); ?></th>
					<th><?php echo trans( 'begin.finalized' ); ?></th>
					<th><?php echo trans( 'begin.result' ); ?></th>
					<th></th>
					<th><?php echo trans( 'begin.passed' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php $row = $users->getFrom(); ?>
				<?php foreach( $users as $user ): ?>
					<?php $passed = ( $user->pivot->finalized ? ( $user->pivot->mark >= $exam->pass_mark ? 1 : 0 ) : -1 ); ?>

					<tr data-exam-user-id="<?php echo $user->pivot->id; ?>" data-user-id="<?php echo $user->userable_id; ?>" class="finalized-<?php echo $user->pivot->finalized; ?> passed-<?php echo $passed; ?>">
						<td><?php echo en2fa( $row++ ); ?></td>
						<td><?php echo e($user->fname); ?></td>
						<td><?php echo e($user->lname); ?></td>
						<td><?php echo e($user->province_name); ?></td>
						<td><?php echo e(en2fa( $user->national_code )); ?></td>
						<td><?php echo e($user->company_name); ?></td>
						<td><?php echo unix_to_pdate_time( $user->pivot->allowed_timestamp ); ?></td>
						<td><?php echo unix_to_pdate_time( $user->pivot->start_timestamp ); ?></td>
						<td>
							<?php if( $user->pivot->finalized ): ?>
								<i class="fa fa-check"></i>
							<?php else: ?>
								<i class="fa fa-times"></i>
							<?php endif; ?>
						</td>
						<td><?php echo $exam_type != 'poll' && $user->pivot->finalized ? en2fa( $user->pivot->mark ) : ''; ?></td>
						<td>
							<?php if( $user->needs_admin_score ): ?>
								<?php echo HTML::linkRoute( 'admin-score', trans( 'exam.set-score' ), array( $user->pivot->id ), array( 'target' => '_BLANK', 'class' => 'button admin-score' ) ); ?>

							<?php elseif( $user->pivot->finalized ): ?>
								
								<?php if( $exam_type != 'poll' ): ?>

									<?php if( raw_results_has_ex_workbook( $user->pivot->raw_results ) ): ?>
										<?php echo HTML::linkRoute( 'exworkbook', trans( 'user.exworkbook' ), array( $user->pivot->id ), array( 'class' => 'button workbook' ) ); ?>

									<?php else: ?>
										<?php echo HTML::linkRoute( 'workbook', trans( 'user.workbook' ), array( $user->pivot->id ), array( 'class' => 'button workbook' ) ); ?>

									<?php endif; ?>

								<?php endif; ?>

								<?php echo HTML::linkRoute( 'answer_sheet', trans( 'user.answer-sheet' ), array( $user->pivot->id ), array( 'target' => '_BLANK', 'class' => 'button answer-sheet' ) ); ?>

							<?php endif; ?>
							
							<?php if( $exam_type != 'poll' && $user->pivot->finalized ): ?>
								<?php echo HTML::link( '#', 'ادامه آزمون', array( 'class' => 'button restart-exam-user' ) ); ?>

							<?php endif; ?>
						</td>
						<td>
							<?php if( $exam_type != 'poll' && $user->pivot->finalized ): ?>
								<?php if( $user->pivot->mark >= $exam->pass_mark ): ?>
									<i class="fa fa-check"></i>
								<?php else: ?>
									<i class="fa fa-times"></i>
								<?php endif; ?>
							<?php endif; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
		<div>
			<?php echo $users->appends( $old_search_data )->links(); ?>

		</div>
	<?php else: ?>
		<div class="info-message"><?php echo trans( 'main.empty' ); ?></div>
	<?php endif; ?>

	<?php if( Auth::user()->isAdmin() ): ?>
		<?php echo HTML::link( '#', trans( 'exam.clear-all-users' ), array( 'data-exam-id' => $exam->id, 'class' => 'button clear-all-users' ) ); ?>

	<?php endif; ?>

	<?php echo HTML::linkRoute( 'exam-users', trans( 'exam.export-exam-users' ), array( $exam->id, $exam_type, 'excel' ), array( 'class' => 'button excel' ) ); ?>


	<?php if( $exam->type == 'pdf' || ( in_array( $exam->type, array( 'generic', 'konkoor' ) ) && $exam->random_questions == 0 ) ): ?>
		<?php echo HTML::linkRoute( 'exam-get-text-results', 'text پاسخ‌ها', array( $exam->id ), array( 'class' => 'button' ) ); ?>

	<?php endif; ?>
<?php $__env->stopSection(); ?>