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

<div class="container-fluid">
	<div class="container-page-wrapper shodowed bordered">
		<div class="container-page-container">
			<div class="row">
				<div class="col-lg-6">
					<h1><?php echo $this->actionTitle; ?></h1>
				</div>
				<div class="col-lg-6 clearfix">
					<div class="float-right">
						<button type="button" class="btn btn-info btn-refresh-list"><span class="icon-loop2"></span></button>
					</div>
				</div>
			</div>
			
			<?php //debug($this->view->testt); ?>

			<?php if(is_array($this->view->survey)): ?>
				<?php echo $this->partial('Layout/pagination'); ?>
				
				<form id="form-survey-list">
					<div class="table-overflow">
						<table class="table table-striped">
							<thead>
								<th width="1%"></th>
								<th width="1%"></th>
								<th width="1%"></th>
								<th width="1%"></th>
								<th class="text-center" width="1%">#ID</th>
								<th class="text-center">Visionato</th>
								<th class="text-center" nowrap><?php echo $this->helper->setSortableCol('Compilato', 'survey-list/'.time(), 'sy.aswered_date', 'desc'); ?></th>
								<th nowrap><?php echo $this->helper->setSortableCol('Ricezione', 'survey-list/'.time(), 'sq.msg_delay_at', 'desc'); ?></th>
								<th>Tipo</th>
								
								<?php if($this->view->isGlobal): ?>
									<th>Gestore</th>
								<?php endif; ?>
								
								<th>Paziente</th>
								<th>E-mail</th>
								<th>Tel.</th>
								<th nowrap><?php echo $this->helper->setSortableCol('Richiesta', 'survey-list/'.time(), 'sy.created_at', 'desc'); ?></th>
								<th width="1%"></th>
								<th width="1%"></th>
								<th width="1%"></th>
							</thead>
							
							<tbody>
									<tr>
										<td></td>
										<td>
											<select class="form-control request-search-ctrl" name="searchData[tlc_linked]" style="min-width: 32px;">
												<option value="-1"></option>
												<option value="0" <?php echo $this->session->getSessionValue('search_survey_tlc_linked') === 0 ? 'selected' : ''; ?>>Senza televisita</option>
												<option value="1" <?php echo $this->session->getSessionValue('search_survey_tlc_linked') == 1 ? 'selected' : ''; ?>>Con televisita</option>
											</select>
										</td>
										<td></td>
										<td></td>
										<td></td>
										<td class="text-center">
											<select class="form-control request-search-ctrl" name="searchData[mail_opened]" style="min-width: 32px;">
												<option value="-1"></option>
												<option value="0" <?php echo $this->session->getSessionValue('search_survey_email_opened') === 0 ? 'selected' : ''; ?>>Non visionato</option>
												<option value="1" <?php echo $this->session->getSessionValue('search_survey_email_opened') == 1 ? 'selected' : ''; ?>>Visionato</option>
											</select>
										</td>
										<td class="text-center">
											<select class="form-control request-search-ctrl" name="searchData[survey_filled]" style="min-width: 32px;">
												<option value="-1"></option>
												<option value="0" <?php echo $this->session->getSessionValue('search_survey_filled') === 0 ? 'selected' : ''; ?>>Non compilato</option>
												<option value="1" <?php echo $this->session->getSessionValue('search_survey_filled') == 1 ? 'selected' : ''; ?>>Compilato</option>
											</select>
										</td>
										<td>
											<select class="form-control request-search-ctrl" name="searchData[email_delivered]" style="min-width: 32px;">
												<option value="-1"></option>
												<option value="0" <?php echo $this->session->getSessionValue('search_survey_email_delivered') === 0 ? 'selected' : ''; ?>>Non ricevuto</option>
												<option value="1" <?php echo $this->session->getSessionValue('search_survey_email_delivered') == 1 ? 'selected' : ''; ?>>Ricevuto</option>
											</select>
										</td>
										<td>
											<select class="form-control request-search-ctrl" name="searchData[survey_type]">
												<option value=""></option>
												<?php foreach($this->view->typeList as $id => $label): ?>
													<option value="<?php echo $id; ?>" <?php echo $this->session->getSessionValue('search_survey_type') == $id ? 'selected' : ''; ?> ><?php echo $label; ?></option>
												<?php endforeach; ?>
											</select>
										</td>
										<?php if($this->view->isGlobal): ?>
											<td></td>
										<?php endif; ?>
										<td>
											<input type="text" class="form-control request-search-ctrl" name="searchData[survey_patient]" value="<?php echo trim($this->session->getSessionValue('search_survey_patient')) != '' ? trim($this->session->getSessionValue('search_survey_patient')) : ''; ?>" style="width: 180px;">
										</td>
										<td></td>
										<td></td>
										<td></td>
										<td></td>
										<td></td>
										<td>
											<button type="button" class="btn btn-outline-dark btn-sm btn-search-refresh-list"><i class="fa fa-search" aria-hidden="true"></i></button>
										</td>
									</tr>
								<?php foreach($this->view->survey as $item): ?>
									<tr class="<?php if((bool)$item['email_failure']) echo 'cell-danger'; ?>">
										<td>
											<?php if((bool)$item['aswered_recipient']): ?>
												<button type="button" class="btn btn-outline-dark btn-sm btn-survey-preview" data-surveyid="<?php echo $item['survey_id']; ?>"><i class="fa fa-eye" aria-hidden="true"></i></button>
											<?php endif; ?>
										</td>
										
										<td class="text-center" nowrap>
											<?php if((int)$item['remote_added'] == 1): ?>
												<i class="fa fa-upload" aria-hidden="true" title="Teleconsulto creato"></i>
											<?php else: ?>
												<i class="fa fa-ellipsis-h text-muted" aria-hidden="true" title="Teleconsulto in creazione o aggiornamento"></i>
											<?php endif; ?>
											<?php if($item['remote_auto_tlc_id'] > 0): ?>
												<div class="text-muted"><small><i class="fa fa-external-link" aria-hidden="true"></i> <i>#<?php echo $item['remote_auto_tlc_id']; ?></i></small></div>
											<?php endif; ?>
										</td>
										<td class="text-center">
											<?php if($item['attach_count'] > 0): ?>
												<i class="fa fa-paperclip" aria-hidden="true" title="Allegati: <?php echo $item['attach_count']; ?>"></i>
											<?php endif; ?>
										</td>
										<td class="text-center">
											<?php if((bool)$item['email_failure']): ?>
												<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>
											<?php endif; ?>
										</td>
										
										
										<td class="text-center"><?php echo $item['survey_id']; ?></td>
										
										
										<td class="text-center">
											<?php if((int)$item['opened_recipient'] == 0): ?>
												<i class="fa fa-envelope" aria-hidden="true"></i>
											<?php else: ?>
												<i class="fa fa-envelope-open-o" aria-hidden="true"></i>
											<?php endif; ?>
										</td>
										<td class="text-center">
											<?php if((int)$item['aswered_recipient'] == 0): ?>
												<i class="fa fa-clock-o" aria-hidden="true"></i>
											<?php else: ?>
												<div><i class="fa fa-check" aria-hidden="true"></i></div>
												<div><small><?php echo $this->helper->getDateString($item['aswered_date']); ?></small></div>
											<?php endif; ?>
										</td>
										
										
										<td>
											<?php
												if (!is_null($item['msg_delay_at'])) {
													if ($this->utility->isDateInThePast($item['msg_delay_at']) && $item['msg_sent'] == 0) {
														$badge_class = 'badge-danger';
														$badge_icon = '<i class="fa fa-clock-o" aria-hidden="true"></i>';
													} else {
														if ($this->utility->isDateInThePast($item['msg_delay_at'])) {
															$badge_class = 'badge-success';
															$badge_icon = '<i class="fa fa-check" aria-hidden="true"></i>';
														} else {
															$badge_class = 'badge-warning';
															$badge_icon = '<i class="fa fa-clock-o" aria-hidden="true"></i>';
														}
													}

												} else {
													$badge_class = '';
													$badge_icon = '';
												}
											?>
											<span class="badge <?php echo $badge_class; ?>"><?php echo $badge_icon; ?> <?php echo $this->helper->getDateString($item['msg_delay_at'], false); ?></span>
										</td>
										<td><?php echo $item['survey_label']; ?></td>
										
										<?php if($this->view->isGlobal): ?>
											<td><?php echo $item['manager_surname'], ' ', $item['manager_name']; ?></td>
										<?php endif; ?>
										
										<td><?php echo $item['surname'], ' ', $item['name']; ?></td>
										<td><span class="<?php if((bool)$item['email_failure']) echo 'string-wave'; ?>"><?php echo $item['email']; ?></span></td>
										<td><?php echo $item['phone']; ?></td>
										<td><?php echo $this->helper->getDateString($item['survey_created_at']); ?></td>
										<td>
											<?php if($item['extra_buttons']): ?>
												<button type="button" title="Reinvia" class="btn btn-outline-dark btn-sm btn-survey-resend" data-surveyuuid="<?php echo $item['code']; ?>" data-surveyid="<?php echo $item['survey_id']; ?>" data-recipient="<?php echo $item['email']; ?>"><i class="fa fa-repeat" aria-hidden="true"></i></button>
											<?php endif; ?>
										</td>
										<td>
											<?php if($item['extra_buttons']): ?>
												<a title="Modifica" class="btn btn-outline-dark btn-sm" href="/survey-edit/<?php echo time(); ?>/<?php echo $item['survey_id']; ?>"><i class="fa fa-pencil" aria-hidden="true"></i></a>
											<?php endif; ?>
										</td>
										<td>
											<?php if($item['extra_buttons']): ?>
												<button type="button" title="Elimina" class="btn btn-outline-danger btn-sm btn-survey-delete" data-surveyuuid="<?php echo $item['code']; ?>" data-surveyid="<?php echo $item['survey_id']; ?>" data-recipient="<?php echo $item['email']; ?>"><i class="fa fa-times" aria-hidden="true"></i></button>
											<?php endif; ?>
										</td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				</form>
				<hr>
				<?php echo $this->partial('Layout/pagination'); ?>
			<?php else: ?>
				<div class="alert alert-warning" role="alert">
					<?php echo _('No data available in this list.'); ?>
				</div>
			<?php endif; ?>
		</div>
	</div>
</div>

<div class="modal" id="dialog-survey-preview" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title"><?php echo _('Survey preview'); ?></h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        	<div class="text-center" id="dialog-loader"><img src="<?php echo $this->layout->getPublicUri(); ?>images/ajaxloader.svg" width="50"></div>
			<div class="modal-body-survey-preview"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal"><?php echo _('Close'); ?></button>
      </div>
    </div>
  </div>
</div>