<?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-4">
					<h1><?php echo $this->actionTitle; ?></h1>
				</div>
				<div class="col-lg-8 clearfix">
					<div class="float-right">

					</div>
				</div>
			</div>
			<hr>
			<?php //debug($this->view->pos); ?>
			
			<form id="users-advanced-search">
				<div class="form-row">
					
					<div class="form-group col-lg-6">
						<label class="label-main" for="search_first_language"><?php echo _('First language'); ?></label>
						<select class="form-control" id="search_first_language" name="data[default_lang]">
							<option value=""></option>
							<?php foreach($this->view->supportedLanguages as $language): ?>
								<option <?php echo isset($language['selected']) && $language['selected'] ? 'selected' : ''; ?> value="<?php echo $language['lang_code']; ?>"><?php echo $language['name_string']; ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					
					<div class="form-group col-lg-6">
						<label class="label-main" for="search_country"><?php echo _('Country'); ?></label>
						<select class="form-control chosen-select" name="data[country]" id="search_country">
							<option value="">...</option>
							<?php if(is_array($this->view->countries)): ?>
								<?php foreach($this->view->countries as $country): ?>
									<option value="<?php echo $country['country_iso2_code']; ?>" <?php echo isset($country['selected']) && $country['selected'] ? 'selected' : ''; ?>><?php echo $country['country_name']; ?></option>
								<?php endforeach; ?>
							<?php endif; ?>
						</select>
					</div>
				
					<div class="form-group col-lg-4">
						<label class="label-main"><?php echo _('Status'); ?></label>
						<div class="form-check form-check-inline">
							<input class="form-check-input" type="radio" name="data[status]" id="search_status_min1" value="-1" <?php echo isset($this->view->status) && $this->view->status == -1 ? 'checked' : ''; ?>>
							<label class="form-check-label" for="search_status_2"><?php echo _('Any'); ?></label>
						</div>
						<div class="form-check form-check-inline">
							<input class="form-check-input" type="radio" name="data[status]" id="search_status_1" value="1" <?php echo isset($this->view->status) && $this->view->status == 1 ? 'checked' : ''; ?>>
							<label class="form-check-label" for="search_status_0"><?php echo _('Active'); ?></label>
						</div>
						<div class="form-check form-check-inline">
							<input class="form-check-input" type="radio" name="data[status]" id="search_status_0" value="0" <?php echo isset($this->view->status) && $this->view->status == 0 ? 'checked' : ''; ?>>
							<label class="form-check-label" for="search_status_0"><?php echo _('Not active'); ?></label>
						</div>
						<div class="form-check form-check-inline">
							<input class="form-check-input" type="radio" name="data[status]" id="search_status_2" value="2" <?php echo isset($this->view->status) && $this->view->status == 2 ? 'checked' : ''; ?>>
							<label class="form-check-label" for="search_status_0"><?php echo STATUS_TECH_NAME; ?></label>
						</div>
					</div>
					
					<div class="form-group col-lg-4">
						<label class="label-main"><?php echo _('Roles'); ?></label>
						<div>
							<?php foreach($this->view->supportedRoles as $index => $role): ?>
									<div class="form-check form-check-inline">
										<input class="form-check-input" type="checkbox" id="alt-role-<?php echo $index; ?>" value="<?php echo $role['id']; ?>" <?php echo isset($role['checked']) && $role['checked'] ? 'checked' : ''; ?> name="data[role][<?php echo $role['id']; ?>]">
										<label class="form-check-label" for="alt-role-<?php echo $index; ?>"><?php echo $role['name']; ?></label>
									</div>
							<?php endforeach; ?>
						</div>
					</div>
					
					<div class="form-group col-lg-4">
						<label class="label-main"><?php echo _('Medical specialties'); ?></label>
						
						<select data-placeholder="<?php echo _('Select...'); ?>" class="form-control chosen-select" multiple="multiple" name="data[med_specialties][]" id="med_specialties">
							<?php if(is_array($this->view->medicalSpecialties)): ?>
								<?php foreach($this->view->medicalSpecialties as $specialty): ?>
									<option value="<?php echo $specialty['id']; ?>" <?php echo isset($specialty['selected']) && $specialty['selected'] ? 'selected' : ''; ?>><?php echo $specialty['description']; ?></option>
								<?php endforeach; ?>
							<?php endif; ?>
						</select>
					</div>
					
					<?php foreach($this->view->checkCcRoles as $roleId => $fieldName): ?>
						<div class="form-group col-lg-3">
							<label class="label-main"><?php echo vsprintf(_('Clinical Center (%s)'), [$this->view->checkCcRolesLabels[$roleId]]); ?></label>
							
							<select data-placeholder="<?php echo _('Select...'); ?>" class="form-control field-<?php echo $this->view->checkCcCountries[$roleId]; ?> chosen-select" multiple="multiple" id="<?php echo $this->view->checkCcCountries[$roleId]; ?>">
								<?php if(is_array($this->view->clinicalCenterCountries)): ?>
									<?php foreach($this->view->clinicalCenterCountries as $countryCode => $ccCountry): ?>
										<optgroup label="<?php echo $ccCountry['name']; ?>">
											<option value="all|<?php echo $countryCode; ?>"><?php echo vsprintf(_('All in %s'), [$ccCountry['name']]); ?> (<?php echo $ccCountry['count']; ?>)</option>
											<?php if(is_array($ccCountry['list'])): ?>
												<?php foreach($ccCountry['list'] as $ccList): ?>
													<option value="<?php echo $ccList['country_code']; ?>"><?php echo $ccList['country_name']; ?> (<?php echo $ccList['cc_count']; ?>)</option>
												<?php endforeach; ?>
											<?php endif; ?>
										</optgroup>
									<?php endforeach; ?>
								<?php endif; ?>
							</select>
							
							<div class="text-center margin-bottom-5 margin-top-5"><button type="button" data-lefthand="<?php echo $this->view->checkCcCountries[$roleId]; ?>" data-righthand="<?php echo $this->view->checkCcRoles[$roleId]; ?>" class="btn btn-outline-info btn-sm btn-countries-hand hand-no-margin"><span class="icon-point-down"></span></button></div>
							
							<select data-placeholder="<?php echo _('Select...'); ?>" class="form-control chosen-select" multiple="multiple" name="data[<?php echo $fieldName; ?>][]" id="<?php echo $fieldName; ?>">
								<?php if(is_array($this->view->clinicalCentersPerRole[$roleId])): ?>
									<?php foreach($this->view->clinicalCentersPerRole[$roleId] as $center): ?>
										<option value="<?php echo $center['id']; ?>" <?php echo isset($center['selected']) && $center['selected'] ? 'selected' : ''; ?>><?php echo $center['description']; ?></option>
									<?php endforeach; ?>
								<?php endif; ?>
							</select>
							
						</div>
					<?php endforeach; ?>
				</div>
				<hr>
				<div class="clearfix margin-bottom-20" id="search-scroll-pointer">
					
					<div class="float-left">
						<?php /*
						<div class="form-check">
							<input type="hidden" value="0" name="data[save_search]">
							<input class="form-check-input" type="checkbox" checked="checked" value="1" id="save-search" name="data[save_search]">
								<label class="form-check-label" for="defaultCheck1">
									<?php echo _('Save this search'); ?>
								</label>
						</div>
						*/ ?>
						<div style="display: inline-block;">
							<div><button type="button" class="btn btn-warning btn-search-clean"><?php echo _('Clean fields'); ?></button></div>
							<div><small class="text-muted">&nbsp;</small></div>
						</div> 
						
						<div style="display: inline-block;">
							<div><button type="button" class="btn btn-info btn-export-users"><?php echo _('Export selected'); ?></button></div>
							<div><small class="text-muted"><?php echo _('Only Requester users'); ?></small></div>
						</div>
					</div>
					
					<div class="float-right">
						<a href="<?php echo $this->helper->setHash('users-search/'.time().'/surname/asc/1'); ?>" class="btn btn-primary btn-search-users"><?php echo _('Search'); ?></a>
					</div>
				</div>
			</form>
			
			<form id="users-advanced-search-list">
				<div class="table-overflow">
				<?php if($this->view->isSearch): ?>
					<?php if (is_array($this->view->results) && !empty($this->view->results)): ?>

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

						<table class="table table-striped table-hover">
							<thead class="thead-light">
								<tr>
									<th width="1%">
										<div class="form-check form-check-inline">
											<input type="checkbox" class="form-check-input" id="checkall">
										</div>
									</th>
									<th width="1%"></th>
									<th nowrap><?php echo _('Surname'); ?></th>
									<th nowrap><?php echo _('Name'); ?></th>
									<th nowrap><?php echo _('Roles'); ?></th>
									<?php if($this->view->tableHasCcColumn[APPLICANT_ROLE_ID]): ?>
										<th nowrap><?php echo _('C.C. Requester'); ?></th>
									<?php endif; ?>
									<?php if($this->view->tableHasCcColumn[REFERRER_ROLE_ID]): ?>
										<th nowrap><?php echo _('C.C. Responder'); ?></th>
									<?php endif; ?>
									<?php if($this->view->tableHasCcColumn[MODERATOR_ROLE_ID]): ?>
										<th nowrap><?php echo _('C.C. Moderator'); ?></th>
									<?php endif; ?>
									<?php if($this->view->tableHasCcColumn[GUEST_ROLE_ID]): ?>
										<th nowrap><?php echo _('C.C. Guest'); ?></th>
									<?php endif; ?>
									<?php if($this->view->tableHasSpecialties): ?>
										<th nowrap><?php echo _('M. Specialties'); ?></th>
									<?php endif; ?>
									<th width="1%"></th>
								</tr>
							</thead>
							
							<tbody>
								<?php foreach($this->view->results as $result): ?>
									<tr>
										<td>
											<div class="form-check form-check-inline">
												<input type="checkbox" class="form-check-input check-list-item" value="<?php echo $result['id']; ?>" name="export_user_list[]">
											</div>
										</td>
										<td><?php echo $this->helper->setAvatarView($this->user->getAvatar($result['id']), ['timestamp'=>md5($result['updated_at']), 'class'=>'list-avatar']); ?></td>
										<td><?php echo $result['surname']; ?></td>
										<td><?php echo $result['name']; ?></td>
										<td><?php echo $result['role_list']; /*$this->helper->getRoleNameInList($result['role_list'], $this->view->roleSelectList);*/ ?></td>
										<?php if($this->view->tableHasCcColumn[APPLICANT_ROLE_ID]): ?>
											<td><?php echo $result['cc_applicant_list']; ?></td>
										<?php endif; ?>
										
										<?php if($this->view->tableHasCcColumn[REFERRER_ROLE_ID]): ?>
											<td><?php echo $result['cc_referrer_list']; ?></td>
										<?php endif; ?>
										
										<?php if($this->view->tableHasCcColumn[MODERATOR_ROLE_ID]): ?>
											<td><?php echo $result['cc_moderator_list']; ?></td>
										<?php endif; ?>
										
										<?php if($this->view->tableHasCcColumn[GUEST_ROLE_ID]): ?>
											<td><?php echo $result['cc_guest_list']; ?></td>
										<?php endif; ?>
										
										<?php if($this->view->tableHasSpecialties): ?>
											<td><?php echo $result['specialty_list']; ?></td>
										<?php endif; ?>
										<td><a href="<?php echo $this->helper->setHash('users-edit/'.time().'/'.$result['id'].'/surname/desc/1'); ?>" title="<?php echo _('Edit user'); ?>" class="btn btn-outline-dark btn-sm"><span class="icon-pencil"></span></a></td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
						<?php echo $this->partial('Layout/pagination'); ?>
					<?php else: ?>
						<div class="alert alert-warning"><?php echo _('There are no results according to the provided parameters. Please try again better defining your search.'); ?></div>
					<?php endif; ?>
				<?php endif; ?>
			</div>
			</form>
			
			
				
		</div>
	</div>
</div>