<?php echo $this->partial('Layout/nav'); ?>

<div class="container">
	<div class="container-page-wrapper shodowed bordered">
		<div class="container-page-container">
			<div class="row">
				<div class="col-lg-6">
					<h2><?php echo $this->requestOrigin == 'web' ? '<span class="icon-earth"></span>' : '<span class="icon-display"></span>'; ?> <?php echo $this->actionTitle; ?></h2>

					<?php //debug($this->view->survey); ?>
				</div>
				<div class="col-lg-6 clearfix">
					<div class="float-right text-right">
						<div title="<?php echo $this->helper->getDateString($this->view->requestData['updated_at']); ?>">
							<small><?php echo vsprintf(_('Last update: %s'), [$this->helper->dateIntervalString($this->view->requestData['updated_at'])]); ?></small>
						</div>
						<div class="request-delete-wrapper">
							
								<?php if($this->view->requestData['request_status'] == 'referted' && !empty($this->view->survey)): ?>
									<button type="button" class="btn btn-info btn-send-pdf" data-filetitle="<?php echo vsprintf(_('Request-%s'), $this->view->requestData['id']); ?>" data-attachments="<?php echo htmlspecialchars(json_encode($this->view->attachments)); ?>" data-hasattachments="<?php echo !empty($this->view->attachments) ? 1 : 0; ?>">Invia PDF</button> 
								<?php endif; ?>
								
								<button type="button" class="btn btn-info btn-print-request" data-filetitle="<?php echo vsprintf(_('Request-%s'), $this->view->requestData['id']); ?>" data-op="print" data-context="requestview" data-requestid="<?php echo $this->view->requestData['id']; ?>"><?php echo _('Print'); ?></button>


							<?php if($this->view->userIsModerator): ?>
								<button type="button" class="btn btn-danger btn-delete-request" data-requestid="<?php echo $this->view->requestData['id']; ?>"><?php echo _('Delete request'); ?></button>
							<?php endif; ?>
						</div>
					</div>
				</div>
			</div>
			<div class="row margin-top-20">
				<div class="col-lg-6">
					<table class="table">
						<tr>
							<td width="50%"><?php echo _('Patient'); ?></td>
							<td><?php echo $this->helper->requestSubject(['anonymous'=>$this->view->requestData['request_anonymous'], 'patientSurname'=>$this->view->requestData['patient_surname'], 'patientName'=>$this->view->requestData['patient_name'], 'ageYears'=>$this->view->requestData['age_years'], 'ageMonths'=>$this->view->requestData['age_months'], 'ageDays'=>$this->view->requestData['age_days'], 'patientGender'=>$this->view->requestData['gender']]); ?></td>
						</tr>
						<tr>
							<td><?php echo _('Medical specialties'); ?></td>
							<td>
								<?php if(is_array($this->view->requestMedicalSpecialties)): ?>
									<?php foreach($this->view->requestMedicalSpecialties as $item): ?>
										<span class="badge badge-primary"><?php echo $item['description']; ?></span>
									<?php endforeach; ?>
								<?php endif; ?>
							</td>
						</tr>
					</table>

				</div>
				<div class="col-lg-6">

					<table class="table">
						<tbody>
							<tr>
								<td width="50%"><?php echo _('Health Center'); ?></td>
								<td><span class="icon-location"></span> <a href="#" class="btn-cc-dialog" data-cclat="<?php echo $this->view->requestData['lat']; ?>" data-cclng="<?php echo $this->view->requestData['lng']; ?>"><?php echo $this->view->requestData['center_description']; ?></a></td>
							</tr>
							<tr>
								<td><?php echo _('Requester'); ?></td>
								<!-- <td><?php echo $this->helper->setDottedFullname($this->view->requestData['sender_name'], $this->view->requestData['sender_surname'], false); ?></td> -->
								<td><?php echo $this->view->requestData['sender_name']." ".$this->view->requestData['sender_surname'] ?></td>
							</tr>
							<?php if($this->view->userIsModerator): ?>
								<tr>
									<td><?php echo _('Responders'); ?></td>
									<td><button class="btn btn-sm btn-outline-dark btn-show-responders" data-requestid="<?php echo $this->view->requestData['id']; ?>"><?php echo _('Show'); ?></button></td>
								</tr>
							<?php endif; ?>
						</tbody>
					</table>
				</div>
			</div>
			<hr>
			<div class="row">
				<div class="col-lg-4">

					<div class="row">
						<div class="col-lg-6"><?php echo $this->helper->setRequestStatus($this->view->requestData['request_status']); ?></div>
						<div class="col-lg-6 text-right"><small><?php echo _('Triage'); ?></small> <span class="triage-list triage-<?php echo $this->view->requestData['triage_color']; ?>"></span></div>
					</div>
					<hr>

					<?php if(!empty($this->view->dreamData)): ?>
						<button type="button" class="btn btn btn-info btn-block btn-show-dream" data-requestid="<?php echo $this->view->requestData['id']; ?>"><?php echo _('DREAM Information'); ?></button>
					<?php endif; ?>
					
					<?php if (!empty($this->view->survey)): ?>
						<button type="button" class="btn btn btn-info btn-block btn-show-sportellocura">Scheda paziente</button>
						<div class="text-center"><small>Scheda #<?php echo $this->view->survey['survey_id']; ?> inviata dal paziente <b>Sportello Cura</b></small></div>
						
						<?php if(is_array($this->view->pdfLog) && !empty($this->view->pdfLog)): ?>
							<div class="pdf-log-wrapper"><small><ul class="list-no-type">
								<?php foreach($this->view->pdfLog as $log): ?>
									<li>Risposta inviata: <?php echo $this->helper->getDateString($log['created_at']); ?></li>
								<?php endforeach; ?>
							</ul></small></div>
						<?php endif; ?>
					<?php endif; ?>

					<?php if(is_array($this->view->groupedAttachments) && !empty($this->view->groupedAttachments)): ?>
					<div class="card margin-top-10">
						<div class="card-body">
							<h5 class="card-title"><?php echo _('Attachments'); ?></h5>

								<?php foreach($this->view->groupedAttachments as $created_at => $attachments): ?>
									<label class="label-main"><?php echo $this->helper->getDateString($created_at, false); ?></label>
									<ul class="list-no-type margin-bottom-10">
										<?php foreach($attachments as $attach): ?>
											<li class="list-no-type padding-left-10">
												<?php echo $this->helper->getPreviewLink($attach); ?>
											</li>
										<?php endforeach; ?>
									</ul>
								<?php endforeach; ?>
						</div>
					</div>
					<?php endif; ?>

					<?php if(is_array($this->view->symptoms) && !empty($this->view->symptoms)): ?>
						<div class="card margin-top-10">
							<div class="card-body">
								<h5 class="card-title"><?php echo _('ICD-10'); ?></h5>
								<ol class="list-no-margin-bottom">
									<?php foreach($this->view->symptoms as $symptom): ?>
										<li><?php echo ucfirst($symptom['symptom_label']); ?></li>
									<?php endforeach; ?>
								</ol>
							</div>
						</div>
					<?php endif; ?>

					<?php if(is_array($this->view->wizards)): ?>
						<?php foreach($this->view->wizards as $type => $items): ?>
							<div class="card margin-top-10">
								<div class="card-body">
									<h5 class="card-title"><?php echo isset($this->view->wizardTitles[$type]) ? $this->view->wizardTitles[$type] : '-'; ?></h5>
									<ol class="list-no-margin-bottom">
										<?php foreach($items as $item): ?>
											<li><?php echo $item['description']; ?></li>
										<?php endforeach; ?>

									</ol>

									<?php if($type == 'derma'): ?>
										<?php if(is_array($this->view->dermaPositions)): ?>
											<div><b><?php echo _('Position'); ?></b></div>
											<ol>
												<?php foreach($this->view->dermaPositions as $positionType => $positionData): ?>
													<li><?php echo isset($this->view->dermaPositionLabels[$positionData['position_type']]) ? $this->view->dermaPositionLabels[$positionData['position_type']] : '-'; ?></li>
												<?php endforeach; ?>
											</ol>
										<?php endif; ?>
									<?php endif; ?>
								</div>
							</div>
						<?php endforeach; ?>
					<?php endif; ?>

				</div>
				<div class="col-lg-8 mobile-margin-top">

					<div class="card margin-bottom-20">
						<div class="card-body">
							<h5 class="card-title"><?php echo _('Medical Remarks'); ?></h5>

							<?php if((float)$this->view->requestData['max_arterial_pressure'] || (float)$this->view->requestData['heart_rate'] > 0 || (float)$this->view->requestData['saturation'] > 0): ?>
								<hr>
								<div class="row">

									<?php if((float)$this->view->requestData['max_arterial_pressure'] > 0 && (float)$this->view->requestData['min_arterial_pressure']): ?>
										<div class="col text-center">
											<div><img src="/images/blood-pressure-max.svg" width="35"></div>
											<div class="text-muted"><small><?php echo _('Blood Pressure'); ?></small></div>
											<div><b><?php echo $this->view->requestData['max_arterial_pressure']; ?>/<?php echo $this->view->requestData['min_arterial_pressure']; ?></b> <sup>mmHg</sup></div>
										</div>
									<?php endif; ?>

									<?php if((float)$this->view->requestData['heart_rate'] > 0): ?>
										<div class="col text-center">
											<div><img src="/images/heart-rate.svg" width="35"></div>
											<div class="text-muted"><small><?php echo _('Heart Rate'); ?></small></div>
											<div><b><?php echo $this->view->requestData['heart_rate']; ?></b> <sup>bpm</sup></div>
										</div>
									<?php endif; ?>

									<?php if((float)$this->view->requestData['saturation'] > 0): ?>
										<div class="col text-center">
											<div><img src="/images/oxygen-saturation.svg" width="35"></div>
											<div class="text-muted"><small><?php echo vsprintf(_('%s Sat.'), 'O<sub>2</sub>'); ?></small></div>
											<div><b><?php echo $this->view->requestData['saturation']; ?></b> <sup>%</sup></div>
										</div>
									<?php endif; ?>
								</div>
							<?php endif; ?>

							<hr>
							<?php echo nl2br(trim(strip_tags($this->view->requestData['clinical_remarks']))); ?>

							<?php if(trim($this->view->requestData['medical_history']) != ''): ?>
								<hr>
								<h5><?php echo _('Medical History'); ?></h5>
								<?php echo $this->helper->hashtag(nl2br(trim(strip_tags($this->view->requestData['medical_history'])))); ?>
							<?php endif; ?>
						</div>
					</div>

					<?php echo $this->partial('Request/comment-element', ['commentPosition'=>'right', 'show_meta'=>true, 'is_moderator'=>false, 'style'=>'', 'userId'=>$this->view->requestData['user_id'], 'userUpdatedAt'=>$this->view->requestData['user_updated_at'], 'userName'=>ucwords($this->view->requestData['sender_name']), 'userSurname'=>ucwords($this->view->requestData['sender_surname']), 'userExtra'=>'', 'commentCreatedAt'=>$this->view->requestData['created_at'], 'commentText'=>$this->helper->hashtag($this->view->requestData['request_question'])]); ?>

					<hr class="request-main-question">

					<div id="request-comment-wrapper">
						<?php echo $this->partial('Request/comment-list', ['comments'=>$this->view->comments]); ?>
					</div>

					<?php if(!$this->user->hasOneRole(GUEST_ROLE_ID)): ?>
						<hr>
						<form>
							<input type="hidden" id="request-id" name="data[request_id][value]" value="<?php echo $this->view->requestId; ?>">
							<input type="hidden" id="center-id" name="data[center_id][value]" value="<?php echo $this->view->requestData['center_id']; ?>">
							<input type="hidden" id="request-unique-code" name="data[request_unique_code][value]" value="<?php echo $this->view->requestUniqueCode; ?>">
							<input type="hidden" id="request-comment-id" name="data[request_comment_id][value]" value="0">
							<input type="hidden" id="request-page-type" value="request-view">


							<div class="form-group text-center">
								<h4><span class="badge badge-pill badge-secondary badge-yeswrap"><span class="icon-bubble2"></span> <?php echo vsprintf(_("%s's new message"), [$this->user->setDisplayName()]); ?></span></h4>
								<hr>
							</div>

							<?php if($this->view->userIsReferrer || $this->view->userIsModerator): ?>
								<div class="form-group request-referral-ms-list">
									<div class="row">
										<div class="col-lg-1">
											<?php echo _('To'); ?>:
										</div>
										<div class="col-lg-11">
											<?php //Medical specialties added by a referral ?>
											<select class="form-control chosen-select" id="request-fw-spec-list" multiple="multiple">
												<?php foreach($this->view->forwardMedicalSpecialties as $ms): ?>
													<option value="<?php echo $ms['id']; ?>" <?php //echo $ms['selected'] ? 'selected' : ''; ?>><?php echo $ms['description']; ?></option>
												<?php endforeach; ?>
											</select>
											<small class="text-muted"><?php echo _('Add medical specialties if needed'); ?></small>
										</div>
										<?php //Comma separated id of the request medical specialties ?>
										<input type="hidden" id="request-spec-list" value="<?php echo implode(',', $this->view->requestMedicalSpecialtiesIdList); ?>">
									</div>
								</div>
								<div class="clearfix form-group">
									<button type="button" class="btn btn-outline-info btn-sm btn-show-referral-list float-right"><?php echo _('Add Medical Specialties'); ?> <span class="icon-arrow-right"></span></button>
								</div>
							<?php endif; ?>

							<div class="form-group">
								<textarea class="form-control" rows="8" id="request-message-text"></textarea>
							</div>

							<div class="row">
								<div class="col">

									<div class="clearfix">
											<!--<button type="button" class="btn btn-light" data-toggle="tooltip" data-placement="top" title="<?php echo _('Forward to other specialists'); ?>..."><span class="icon-arrow-right"></span> <?php echo _('Forward'); ?></button>-->
											<button type="button" class="btn btn-light btn-show-tranfer"><span class="icon-attachment"></span> <?php echo _('Load files'); ?></button>
									</div>
								</div>
								<div class="col clearfix">

									<button type="button" class="btn btn-primary btn-send-message float-right"><?php echo _('Send'); ?></button>
									<?php if($this->view->userIsModerator): ?>
										<select class="form-control  float-right" id="request-new-status" style="width: auto; display: inline-block; margin-right: 5px;">
											<option value=""></option>
											<?php if(is_array($this->view->statuses)): ?>
												<?php foreach($this->view->statuses as $status): ?>
													<option value="<?php echo $status['code']; ?>"><?php echo _($status['default_label']); ?></option>
												<?php endforeach; ?>
											<?php endif; ?>
										</select>
									<?php endif; ?>
								</div>
							</div>
						</form>
					<?php endif; ?>
					
					<?php if ($this->view->hasMoreDetails): ?>
					
						<p><hr><a class="prevent-default" data-toggle="collapse" href="#collapse-more-details" role="button" aria-expanded="false" aria-controls="collapseExample"><?php echo _('More details'), ' (Caregiver - MMG)'; ?></a></p>
					
						<div class="collapse" id="collapse-more-details">
							<div class="card margin-bottom-20">
								<div class="card-body">
									<!--<h6 class="card-title"><?php echo _('More details'), ' (Caregiver - MMG)'; ?></h6>
									<hr>-->
									<small>
										<?php if (trim($this->view->requestData['cgrole']) != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b><?php echo _('Role'); ?></b></div></div>
												<div class="col"><?php echo _($this->view->requestData['cgrole']); ?></div>
											</div>
											<hr>
										<?php endif; ?>
										<?php if (trim($this->view->requestData['cgnamesurname']) != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b><?php echo _('Name'); ?></b></div></div>
												<div class="col"><?php echo $this->view->requestData['cgnamesurname']; ?></div>
											</div>
											<hr>
										<?php endif; ?>
										<?php if (trim($this->view->requestData['cgphone']) != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b><?php echo _('Phone'); ?></b></div></div>
												<div class="col"><?php echo $this->view->requestData['cgphone']; ?></div>
											</div>
											<hr>
										<?php endif; ?>
										<?php if (trim($this->view->requestData['cgemail']) != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b>E-mail</b></div></div>
												<div class="col"><?php echo $this->view->requestData['cgemail']; ?></div>
											</div>
											<hr>
										<?php endif; ?>
										<?php if ($this->view->cgProvince != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b><?php echo _('Province'); ?></b></div></div>
												<div class="col"><?php echo $this->view->cgProvince; ?></div>
											</div>
											<hr>
										<?php endif; ?>
										<?php if ($this->view->cgCity != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b><?php echo _('City'); ?></b></div></div>
												<div class="col"><?php echo $this->view->cgCity; ?></div>
											</div>
											<hr>
										<?php endif; ?>
										<?php if (trim($this->view->requestData['cgmmgnamesurname']) != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b><?php echo _('MMG (name)'); ?></b></div></div>
												<div class="col"><?php echo $this->view->requestData['cgmmgnamesurname']; ?></div>
											</div>
											<hr>
										<?php endif; ?>
										<?php if (trim($this->view->requestData['cgmmgphone']) != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b><?php echo _('MMG (phone)'); ?></b></div></div>
												<div class="col"><?php echo $this->view->requestData['cgmmgphone']; ?></div>
											</div>
											<hr>
										<?php endif; ?>
										<?php if (trim($this->view->requestData['cgmmgemail']) != ''): ?>
											<div class="row">
												<div class="col"><div class="dashed-row"><b><?php echo _('MMG (e-mail)'); ?></b></div></div>
												<div class="col"><?php echo $this->view->requestData['cgmmgemail']; ?></div>
											</div>
											<hr>
										<?php endif; ?>
									</small>
								</div>
							</div>
						</div>
					<?php endif; ?>
				</div>
			</div>
			<?php //debug($this->view->specialties); ?>
	</div>

	<?php echo $this->partial('Request/show-responders'); ?>
	<?php echo $this->partial('Request/dream-dialog'); ?>
	<?php echo $this->partial('Request/clinical-center-dialog'); ?>
	<?php echo $this->partial('Request/attach-preview'); ?>
	<?php echo $this->partial('Survey/survey-dialog'); ?>
	<?php echo $this->partial('Survey/survey-sendpdf-dialog'); ?>
</div>
