<?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">
					<span class="mr-3">
							<?php echo _('Users').": ".$this->view->getTotUsers;?>
							<?php if($this->config['settings']['free'] == 0){
								echo " - "._('Roles'); ?>: <span style="color:#7f8c8d"><?php echo strtolower(_('Used')); ?></span> <?php echo $this->view->getTotRoles; ?> / <span style="color:#7f8c8d"><?php echo strtolower(_('Total')); ?></span> <?php echo $this->view->getMaxAddRoles;
							} ?>
					</span>

					<?php if($this->config['settings']['free'] == 0){
					 	if($this->view->getTotRoles >= $this->view->getMaxAddRoles){
						?>
						<a class="btn btn-success disabled" href="#"><?php echo _('New user'); ?></a>
						<?php
					}else{
						?>
						<a class="btn btn-success" href="#" data-toggle="modal" data-target="#newuser-dialog"><?php echo _('New user'); ?></a>

						<?php
					}
				}else{
						?>
					<a class="btn btn-success" href="<?php echo $this->helper->setHash(vsprintf($this->view->editLinkHash, [0])); ?>"><?php echo _('New user'); ?></a>
					<?php
				}
					?>

						<button type="button" class="btn btn-danger btn-recycle-bin"><?php echo _('Deactivate'); ?> <span class="bin-label"></span></button>
					</div>
				</div>
			</div>

			<?php //debug($this->view->lastQuery); ?>

			<div class="table-overflow">
				<form id="table-search-users">
					<table class="table table-hover">
						<thead class="thead-light">
							<tr>
								<th width="1%"></th>
								<!--<th nowrap><?php echo $this->helper->setSortableCol(_('Username'), 'users/'.time(), 'username', 'desc'); ?></th>-->
								<th nowrap><?php echo $this->helper->setSortableCol(_('Created'), 'users/'.time(), 'created_at', 'desc'); ?></th>
								<th nowrap><?php echo $this->helper->setSortableCol(_('Updated'), 'users/'.time(), 'updated_at', 'desc'); ?></th>
								<th nowrap><?php echo $this->helper->setSortableCol(_('Surname'), 'users/'.time(), 'surname', 'desc'); ?></th>
								<th nowrap><?php echo $this->helper->setSortableCol(_('Name'), 'users/'.time(), 'name', 'desc'); ?></th>
								<th nowrap><?php echo $this->helper->setSortableCol(_('E-mail'), 'users/'.time(), 'email', 'desc'); ?></th>
								<th nowrap><?php echo $this->helper->setSortableCol(_('Roles'), 'users/'.time(), 'roles', 'desc'); ?></th>
								<th nowrap><?php echo $this->helper->setSortableCol(_('Status'), 'users/'.time(), 'status', 'desc'); ?></th>
								<th nowrap></th>
							</tr>

							<tr>
								<th></th>
								<!--<th><input type="text" class="form-control form-control-sm user-search-ctrl" value="<?php echo isset($this->view->strOutput['search']['username']) ? $this->view->strOutput['search']['username'] : ''; ?>" name="search[username]"></th>-->
								<th></th>
								<th></th>
								<th><input type="text" class="form-control form-control-sm user-search-ctrl" value="<?php echo isset($this->view->strOutput['search']['surname']) ? $this->view->strOutput['search']['surname'] : ''; ?>" name="search[surname]"></th>
								<th><input type="text" class="form-control form-control-sm user-search-ctrl" value="<?php echo isset($this->view->strOutput['search']['name']) ? $this->view->strOutput['search']['name'] : ''; ?>" name="search[name]"></th>
								<th><input type="text" class="form-control form-control-sm user-search-ctrl" value="<?php echo isset($this->view->strOutput['search']['email']) ? $this->view->strOutput['search']['email'] : ''; ?>" name="search[email]"></th>
								<th>
									<select name="search[roles]" class="form-control form-control-sm user-search-ctrl">
										<option value=""></option>
										<?php if(is_array($this->view->roleSelectList)): ?>
											<?php foreach($this->view->roleSelectList as $roleItem): ?>
												<option value="<?php echo $roleItem['value']; ?>" <?php echo $roleItem['selected'] ? 'selected' : ''; ?>><?php echo $roleItem['name']; ?></option>
											<?php endforeach; ?>
										<?php endif; ?>
									</select>
								</th>
								<th></th>
								<th><a href="<?php echo $this->helper->setHash('users/'.time().'/surname/desc/1'); ?>" class="btn btn-info btn-sm float-right"><span class="icon-search"></span></a></th>
							</tr>

						</thead>

						<tbody>
							<?php if (is_array($this->view->usersList) && !empty($this->view->usersList)): ?>
								<?php foreach($this->view->usersList as $user): ?>
									<tr>
										<?php /*<td><input type="checkbox" class="recycle-bin-checkbox" name="trash_user[]" value="<?php echo $user['id']; ?>"></td>*/ ?>
										<td>
											<?php echo $this->helper->setAvatarView($this->user->getAvatar($user['id']), ['timestamp'=>md5($user['updated_at']), 'class'=>'list-avatar']); ?>
										</td>
										<!--<td><?php echo $user['username']; ?></td>-->
										<td><?php echo $this->helper->getDateString($user['created_at'], false); ?></td>
										<td><?php echo $this->helper->getDateString($user['updated_at'], false); ?></td>
										<td><?php echo $user['surname']; ?></td>
										<td><?php echo $user['name']; ?></td>
										<td><?php echo $user['email']; ?></td>
										<td><?php echo $user['roles']; ?></td>
										<?php
											switch((int)$user['status']) {
												case 0:
													$iconTitle = _('Not active');
													$iconCode = '<span class="icon-sad color-yellow"></span>';
												break;

												case 1:
													$iconTitle = _('Active');
													$iconCode = '<span class="icon-smile2 color-green"></span>';
												break;

												case 2:
													$iconTitle = STATUS_TECH_NAME;
													$iconCode = '<span class="icon-wrench"></span>';
												break;
											}
										?>
										<td class="text-center" title="<?php echo $iconTitle; ?>"><?php echo $iconCode; ?></td>
										<td class="text-right"><a href="<?php echo $this->helper->setHash(vsprintf($this->view->editLinkHash, [$user['id']])); ?>" title="<?php echo _('Edit user'); ?>" class="btn btn-outline-dark btn-sm"><span class="icon-pencil"></span></a></td>
									</tr>
								<?php endforeach; ?>
							<?php endif; ?>
						</tbody>
					</table>
					<?php if (empty($this->view->usersList)): ?>
						<div class="alert alert-warning" role="alert">
							<?php echo _('No data available in this list.'); ?>
						</div>
					<?php endif; ?>
				</form>
			</div>
			<?php echo $this->partial('Layout/pagination'); ?>
		</div>
	</div>

	<?php echo $this->partial('User/new-user-dialog'); ?>
</div>
