<?php //debug($this->session->getSessionValue('reqSearPatient')); ?>

<div class="table-overflow">
	<form id="table-search-requests">
		<table class="table table-hover">
			<thead class="thead-light">
				<tr>
					<th width="10%" nowrap><?php echo $this->helper->setSortableCol('#ID', $this->view->baseUri, 'id', 'desc'); ?></th>
					<th nowrap class="text-right" width="1%"></th>
					<th nowrap class="text-right" width="1%"></th>
					<th nowrap class="text-right"><?php echo $this->helper->setSortableCol(_('Pub. (GMT)'), $this->view->baseUri, 'created_at', 'desc'); ?></th>
					<th nowrap class="text-right"><?php echo $this->helper->setSortableCol(_('Offiline Pub. (GMT)'), $this->view->baseUri, 'desktop_created_at', 'desc'); ?></th>
					<?php if($this->view->scope != 'my'): ?>
						<th width="15%" nowrap><?php echo $this->helper->setSortableCol(_('Requester'), $this->view->baseUri, 'applicant_surname', 'desc'); ?></th>
					<?php endif; ?>
					<th width="25%" nowrap><?php echo $this->helper->setSortableCol(_('Patient'), $this->view->baseUri, 'patient_surname', 'desc'); ?></th>
					<th nowrap><?php echo $this->helper->setSortableCol(_('Status'), $this->view->baseUri, 'request_status_number', 'desc'); ?></th>
					<th width="15%" nowrap><?php echo $this->helper->setSortableCol(_('Med. Spec.'), $this->view->baseUri, 'med_specialties', 'desc'); ?></th>
					<th nowrap><?php echo $this->helper->setSortableCol(_('C. Center'), $this->view->baseUri, 'center_name', 'desc'); ?></th>
					<th class="text-center" nowrap><?php echo $this->helper->setSortableCol(_('Triage'), $this->view->baseUri, 'triage_number', 'desc'); ?></th>
					<th nowrap></th>
					<th width="1%"></th>
				</tr>
				<tr>
					<th class="request-search-id-wrapper">
						<input type="text" class="form-control form-control-sm request-search-ctrl" value="<?php echo $this->session->getSessionValue('reqSearStorId') !== false ? $this->session->getSessionValue('reqSearStorId') : ''; ?>" name="search[id]">
					</th>
					<th></th>
					<th></th>
					<th></th>
					<th></th>
					<?php if($this->view->scope != 'my'): ?>
						<th>
							<select class="form-control form-control-sm request-search-ctrl" name="search[requester]">
								<option value=""></option>
								<?php foreach($this->view->requesterSelectList as $userId => $userSurname): ?>
									<option <?php echo (int)$this->session->getSessionValue('reqSearStorUid') == $userId ? 'selected' : ''; ?> value="<?php echo $userId; ?>"><?php echo $userSurname; ?></option>
								<?php endforeach; ?>
							</select>
						</th>
					<?php endif ?>
					<th>
					<input type="text" class="form-control form-control-sm request-search-ctrl" value="<?php echo $this->session->getSessionValue('reqSearPatient') !== false ? $this->session->getSessionValue('reqSearPatient') : ''; ?>" name="search[patient]">
				</th>
					<th>
						<select class="form-control form-control-sm request-search-ctrl" name="search[status]" style="min-width: 100px;">
							<option value=""></option>
							<?php foreach($this->view->statusSelectList as $statusCode => $statusLabel): ?>
								<option <?php echo $this->session->getSessionValue('reqSearStorStat') == $statusCode ? 'selected' : ''; ?> value="<?php echo $statusCode; ?>"><?php echo $statusLabel; ?></option>
							<?php endforeach; ?>
						</select>
					</th>
					<th>
						<select class="form-control form-control-sm request-search-ctrl" name="search[ms]">
							<option value=""></option>
							<?php foreach($this->view->msSelectList as $msId => $msName): ?>
								<option <?php echo (int)$this->session->getSessionValue('reqSearStorMs') == $msId ? 'selected' : ''; ?> value="<?php echo $msId; ?>"><?php echo $msName; ?></option>
							<?php endforeach; ?>
						</select>
					</th>
					<th>
						<select class="form-control form-control-sm request-search-ctrl" name="search[cc]">
							<option value=""></option>
							<?php foreach($this->view->ccSelectList as $centerId => $centerInfo): ?>
								<option <?php echo (int)$this->session->getSessionValue('reqSearStorCc') == $centerId ? 'selected' : ''; ?> value="<?php echo $centerId; ?>"><?php echo $centerInfo['description']; ?></option>
							<?php endforeach; ?>
						</select>
					</th>
					<th></th>
					<th></th>
					<th>
						<a href="<?php echo $this->helper->setHash('requests/'.time().'/'.$this->view->scope.'/created_at/desc/1'); ?>" class="btn btn-info btn-sm"><span class="icon-search"></span></a>
					</th>
				</tr>
			</thead>
			<tbody>
				<?php if (is_array($this->view->requests) && !empty($this->view->requests)): ?>
					<?php foreach($this->view->requests as $request): ?>
						<tr>
							<td><button type="button" class="btn btn-outline-dark btn-sm btn-request-trigger-open" data-requestid="<?php echo $request['id']; ?>"><?php echo ucfirst($request['id']); ?></button></td>
								<td nowrap class="text-center">
									<?php echo $request['request_origin'] == 'web' ? '<span class="icon-earth" alt="Web"></span>' : '<span class="icon-display" alt="Off-line"></span>'; ?>
								</td>
								<td>
									<?php if ((int)$request['total_reports'] > 0): ?>
										<i class="fa fa-file-pdf-o" aria-hidden="true" title="Risposta inviata al paziente"></i>
									<?php endif; ?>
								</td>
								<td nowrap><small title="<?php echo strip_tags($this->helper->dateIntervalString($request['created_at'])); ?>"><?php echo $this->helper->getDateString($request['created_at'], true); ?></small></td>
								<td nowrap><small title="<?php echo $request['desktop_created_at'] != '' ? strip_tags($this->helper->dateIntervalString($request['desktop_created_at'])) : ''; ?>"><?php echo $request['desktop_created_at'] != '' ? $this->helper->getDateString($request['desktop_created_at'], true) : ''; ?></small></td>

							<?php if($this->view->scope != 'my'): ?>
								<td><small><?php echo $this->helper->setDottedFullname($request['applicant_name'], $request['applicant_surname'], false); ?></small></td>
							<?php endif; ?>

							<td>
								<?php echo $this->helper->requestSubject(['patientSurname'=>ucwords(strtolower($request['patient_surname'])), 'patientName'=>ucwords(strtolower($request['patient_name'])), 'ageYears'=>$request['patient_age_years'], 'ageMonths'=>$request['patient_age_months'], 'ageDays'=>$request['patient_age_days'], 'patientGender'=>$request['patient_gender'], 'anonymous'=>$request['request_anonymous']]); ?>
							</td>

							<td><?php echo $this->helper->setRequestStatus($request['request_status']); ?></td>

							<td><small>
								<?php if(is_array($request['specialties'])): ?>
									<ul class="list-unstyled">
										<?php foreach($request['specialties'] as $specialtyList): ?>
											<li>
												<b></span> <?php echo _($specialtyList['specialty_name']); ?></b>
												<?php if(is_array($specialtyList['referrals']) && !empty($specialtyList['referrals'])): ?>
													<ul class="list-unstyled">
														<?php foreach($specialtyList['referrals'] as $referral): ?>
															<li class="list-ident"><?php echo $referral; ?></li>
														<?php endforeach ?>
													</ul>
												<?php endif; ?>
											</li>
										<?php endforeach; ?>
									</ul>
								<?php endif; ?>
							</small></td>

							<td><span class="badge badge-light"><?php echo $request['center_name']; ?></span></td>
							<td class="text-center"><span class="triage-list triage-<?php echo $request['triage_color']; ?>"></span></td>
							<td class="text-center"><?php echo (int)$request['total_attach'] > 0 ? '<span class="icon-attachment" title="'.vsprintf(_('Attachments: %s'), [$request['total_attach']]).'"></span>' : '-'; ?></td>

							<td class="text-right">
								<?php if($request['request_status'] != 'draft'): ?>
									<a href="<?php echo $this->helper->setHash('requests-edit/'.time().'/'.$request['id'].'/view/'.rawurlencode($this->view->actionTitle).'/'.$this->view->scope.'/'.$this->view->parentBaseUri.'/'.$this->view->orderField.'/'.$this->view->orderDir.'/'.$this->view->currentPage); ?>" class="btn btn-outline-dark btn-sm" id="btn-open-request-<?php echo $request['id']; ?>"><span class="icon-eye"></span></a>
								<?php else: ?>
									<a href="<?php echo $this->helper->setHash('requests-edit/'.time().'/'.$request['id'].'/new-edit/'.rawurlencode($this->view->actionTitle).'/'.$this->view->scope.'/'.$this->view->parentBaseUri.'/'.$this->view->orderField.'/'.$this->view->orderDir.'/'.$this->view->currentPage); ?>" class="btn btn-outline-dark btn-sm" id="btn-open-request-<?php echo $request['id']; ?>"><span class="icon-pencil"></span></a>
								<?php endif; ?>
							</td>
						</tr>
					<?php endforeach; ?>
				<?php endif; ?>
			</tbody>
		</table>
	</form>
	<?php if (empty($this->view->requests)): ?>
		<div class="alert alert-warning" role="alert">
			<?php echo _('No data available in this list.'); ?>
		</div>

	<?php endif; ?>

</div>
