<?php 
    @$msg = $_GET['msg'];
	require_once("vendor/autoload.php");
	$user = new \App\Models\User();
	$usuarios = new \App\Models\Usuarios();
	
	$getAllUsers    = $user->getAllUsers();
	$getAllUsuarios = $usuarios->getAllUsuarios();
?>
<!-- MOVE O CURSO DO MENU -->
<script> window.onload = function(e){ $('.nav-item').removeClass('active'); $('#user').addClass('active'); } </script>
<!-- MOVE O CURSO DO MENU -->
<div class="page-inner">
	<div class="page-header">
		<h4 class="page-title">Usuários</h4>
		<ul class="breadcrumbs">
			<li class="nav-home">
				<a href="#">
					<i class="flaticon-home"></i>
				</a>
			</li>
			<li class="separator">
				<i class="flaticon-right-arrow"></i>
			</li>
			<li class="nav-item">
				<a href="#">Usuários do sistema</a>
			</li>
		</ul>
	</div>
	<div class="row">
		<div class="col-md-12">
			<?php include './inc/alerts.php';?>
		</div>
	</div>
	<div class="row">
		<div class="col-md-12">
			
			<div class="card">
				<!--<div class="card-header">
					<div class="card-title">Usuários do sistema</div>
				</div>-->
				<div class="card-body">
					<!--<div class="card-sub">
					 Acessos do sistemas criados.
					</div>-->
					<div class="table-responsive">
						<table id="example" class="table table-bordered table-head-bg-default">
							<thead>
								<tr>
									<th>Nome</th>
									<th>Tipo</th>
									<th>Loja</th>
									<th>Email</th>
									<th>Criado Por</th>
									<th>Data Cadastro</th>
									<th style="text-align: center;width: 80px;">Status</th>
									<th style="text-align: center;width: 60px;">Ações</th>
								</tr>
							</thead>
							<tbody>
								<?php
									$contador = 1;
									if(!empty($getAllUsers)){
										foreach ($getAllUsers as $key => $lista) {
											$user_cadastro = @$user->getUser($lista['id_user'])[0];
								?>
								<tr>
									<td><?= $lista['user_name'];?></td>
									<td><?php
										switch($lista['id_user_type']){
											case 1:
												echo "Administrador";
												break;
											case 2:
												echo "Gestor";
												break;
											case 3:
												echo "Operador";
												break;
										}
									
									?></td>
									
									
									<td><?= $user->getAllLoja($lista['id_loja']); ?></td>
									<td><?= $lista['user_email'];?></td>
									<td><?= @$user_cadastro['user_name'] ?></td>
									<td><?= date("d-m-Y H:i:s", strtotime($lista['data_cadastro']));?></td>
									<td style="text-align: center;">
										<?php if($lista['status'] == 1){?>
										<button class="btn-success" data-toggle="modal" data-target="#modalStatus" style="cursor: pointer;" onClick="mudar_button('<?= $lista['id'];?>','<?= $lista['status'];?>','<?= $lista['user_name'];?>');" ><i class="fas fa-toggle-on"></i></button>
										<?php }else{?>
										<button class="btn-danger" data-toggle="modal" data-target="#modalStatus" style="cursor: pointer;" onClick="mudar_button('<?= $lista['id'];?>','<?= $lista['status'];?>','<?= $lista['user_name'];?>');" ><i class="fas fa-toggle-off"></i></button>
										<?php }?>
									</td>
									<td><div style="display: flex;justify-content: center;">
										<button type="button" class="btn btn-icon btn-round btn-info btn-sm" data-toggle="modal" data-target="#modalEdt" style="cursor: pointer;" onclick="popularEditar(
											'<?= $lista['id'];?>',
											'<?= $lista['user_email'];?>',
											'<?= $lista['user_name'];?>',
											'<?= $lista['id_user_type'];?>',
											'<?= $lista['id_loja'];?>',
											'<?= $lista['ext'];?>')" data-toggle2="tooltip" data-placement="top" title="Editar" ><i class="fas fa-pencil-alt"></i></button>										
										</div>
									</td>
								</tr>
								<?php $contador++;}}?>
							</tbody>
						</table>
					</div>
				</div>
			</div>
			
		</div>
	</div>
	<div class="row" style="padding-bottom: 10px;" >
		<div class="col-md-12">
			<a data-toggle="modal" data-target="#abrirModal"class="btn btn-success btn-sm" style="color: #fff" ><i class="fa fa-plus"></i> Adicionar novo</a>		
		</div>
	</div>
	<!-- COLABORADORES -->
		<div class="page-header">
			<h4 class="page-title">Colaboradores</h4>
			<ul class="breadcrumbs">
				<li class="nav-home">
					<a href="#">
						<i class="flaticon-home"></i>
					</a>
				</li>
				<li class="separator">
					<i class="flaticon-right-arrow"></i>
				</li>
				<li class="nav-item">
					<a href="#">Colaboradores cadastrados</a>
				</li>
			</ul>
		</div>
		<div class="row">
			<div class="col-md-12">
				
				<div class="card">
					<!--<div class="card-header">
						<div class="card-title">Usuários do sistema</div>
					</div>-->
					<div class="card-body">
						<!--<div class="card-sub">
						 Acessos do sistemas criados.
						</div>-->
						<div class="table-responsive">
							<table id="exampleb" class="table table-bordered table-head-bg-default">
								<thead>
									<tr>
										<th>Nome</th>
										<th>Data Cadastro</th>
										<th style="text-align: center;width: 60px;">Tipo</th>
										<th style="text-align: center;width: 60px;">Situação</th>
										<th style="text-align: center;width: 60px;">Ações</th>
									</tr>
								</thead>
								<tbody>
									<?php
										$contador = 1;
										if(!empty($getAllUsuarios)){
											foreach ($getAllUsuarios as $key => $lista) {
									?>
									<tr>
										<td><?= $lista['nome'];?></td>
										<td><?= date("d/m/Y H:i:s", strtotime($lista['date']));?></td>
										<td><?php if($lista['type'] == 0){ echo 'Separador'; }else{ echo 'Consultador'; };?></td>
										<td><?php if($lista['status'] == 0){ echo 'Inativo'; }else{ echo 'Ativo'; };?></td>
										<td>
											<div style="display: flex;justify-content: center;">
												<button type="button" data-toggle="modal" data-target="#modalEdtCOB" class="btn btn-icon btn-round btn-info btn-sm" style="cursor: pointer;" onclick="VALE('<?= $lista['id'];?>','<?= $lista['nome'];?>','<?= $lista['senha'];?>','<?= $lista['type'];?>','<?= $lista['status'];?>');" data-toggle2="tooltip" data-placement="top" title="Editar" ><i class="fas fa-pencil-alt"></i></button>										
												<button type="button" class="btn btn-icon btn-round btn-danger btn-sm" id="btt-ex" data-toggle="modal" data-target="#modalDelet" style="cursor: pointer;" onclick="excluir_padrao('uni_usuarios', '<?= $lista['id'];?>')" data-toggle2="tooltip" data-placement="top" title="Deletar" ><i class="fas fa-times"></i></button>										
											</div>
										</td>
									</tr>
									<?php $contador++;}}?>
								</tbody>
							</table>
						</div>
					</div>
				</div>
				
			</div>
		</div>
		<div class="row" style="padding-bottom: 10px;" >
			<div class="col-md-12">
				<a data-toggle="modal" data-target="#abrirModalColabo"class="btn btn-success btn-sm" style="color: #fff" ><i class="fa fa-plus"></i> Adicionar novo</a>		
			</div>
		</div>
		<?php include("./view/mods.php");?>
	<!-- Colaboradores -->
	
	
	    <!-- Modal -->
    <div class="modal fade" id="abrirModal" tabindex="-1" role="dialog" aria-labelledby="abrirModaldms" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" id="form_abrirModal" enctype="multipart/form-data" onSubmit="ChamadaPadrao(
                    this.id,
                    'btn_modalEdt',
                    'span_modalEdt',
                    'main?view=usuarios'
                    );return false;">
          <div class="modal-header">
            <h5 class="modal-title" id="abrirModaldms">Novo Usuário</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="caixa_form">
                <input type="hidden" name="cod" value="1">
                <label for="user_nome">Nome Completo</label>
                <input class="form-control" required id="user_nome" name="user_nome">
                <label for="user_email">Email</label>
                <input type="email" class="form-control" required id="user_email" name="user_email">
                <label for="user_type">Tipo do Usuário</label>
                <select required class="form-control tipo_user" name="user_type" id="user_type">
                </select>
				
				<label for="user_loja">Loja</label>
                <select required class="form-control tipo_loja" name="user_loja" id="user_loja">
                </select>
				
                <label for="user_senha">Senha</label>
                <input type="password" class="form-control" required id="user_senha" name="user_senha">
				<div class="row" style="padding-top: 20px;padding-bottom: 100px" >
					<div class="col-md-12">
						<label for="papel_type_edt">Perfil</label>
						<style>
							.image-preview-input {
								position: relative;
								overflow: hidden;
								margin: 0px;    
								color: #333;
								background-color: #fff;
								border-color: #ccc;    
							}
							.image-preview-input input[type=file] {
								position: absolute;
								top: 0;
								right: 0;
								margin: 0;
								padding: 0;
								font-size: 20px;
								cursor: pointer;
								opacity: 0;
								filter: alpha(opacity=0);
							}
							.image-preview-input-title {
								margin-left:2px;
							}
							.popover {
								max-width: fit-content !important;
							}
						</style>
					
						<!-- image-preview-filename input [CUT FROM HERE]-->
						<div class="input-group image-preview">
							<input type="text" class="form-control image-preview-filename" disabled="disabled"> <!-- don't give a name === doesn't send on POST/GET -->
							<span class="input-group-btn">
								<!-- image-preview-clear button -->
								<button type="button" class="btn btn-default image-preview-clear" style="display:none;">
									<span class="glyphicon glyphicon-remove"></span> Limpar
								</button>
								<!-- image-preview-input -->
								<div class="btn btn-default image-preview-input">
									<span class="glyphicon glyphicon-folder-open"></span>
									<span class="image-preview-input-title">Procurar</span>
									<input type="file" accept="image/png, image/jpeg, image/gif" name="input-file-preview" /> <!-- rename it -->
								</div>
							</span>
						</div><!-- /input-group image-preview [TO HERE]--> 
					</div>
				</div>
				
				
				
            </div>
          </div>
          <div class="modal-footer">
            <button class="btn btn-success" name="BTN_NEW" id="btn_abrirModal">+ Adicionar <span id="span_abrirModal"></span></button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
          </div>
          </form>
        </div>
      </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="modalEdt" tabindex="-1" role="dialog" aria-labelledby="modalEdtdms" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" id="form_modalEdt" enctype="multipart/form-data" onSubmit="ChamadaPadrao(
                    this.id,
                    'btn_modalEdt',
                    'span_modalEdt',
                    'main?view=usuarios'
                    );return false;">
          <div class="modal-header">
            <h5 class="modal-title" id="modalEdtdms">Editar Usuário</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="caixa_form">
                <input type="hidden" name="id_esc" id="id_esc">
                <input type="hidden" name="cod" value="3">
								<input type="hidden" name="ext_edt" id="ext_edt" >
                <label for="user_nome_edt">Nome Completo</label>
                <input class="form-control" required id="user_nome_edt" name="user_nome_edt">
                <label for="user_email_edt">Email</label>
                <input type="email" class="form-control" required id="user_email_edt" name="user_email_edt">
				
                <label for="user_type_edt">Tipo do Usuário</label>
                <select required class="form-control tipo_user" name="user_type_edt" id="user_type_edt">
                </select>	
				
                <label for="user_loja_edt">Loja</label>
                <select required class="form-control tipo_loja" name="user_loja_edt" id="user_loja_edt">
                </select>
				
                <p style="font-weight: bold;color: #666;font-size: 10px;text-transform: uppercase;margin: 0;margin-top: 10px;">A senha não pode ser mostrada aqui pois foi encriptografada. Caso deseje alterar a senha apenas digite uma nova senha no campo a baixo.</p>
                <label for="user_senha_edt">Senha</label>
                <input type="password" class="form-control" id="user_senha_edt" name="user_senha_edt" autocomplete="off" readonly onfocus="this.removeAttribute('readonly');" onBlur="this.setAttribute('readonly', true);">
				<div class="row" style="padding-top: 20px;padding-bottom: 100px" >
					<div class="col-md-12">
						<label for="papel_type_edt">Perfil</label>
						<style>
							.image-preview-input {
								position: relative;
								overflow: hidden;
								margin: 0px;    
								color: #333;
								background-color: #fff;
								border-color: #ccc;    
							}
							.image-preview-input input[type=file] {
								position: absolute;
								top: 0;
								right: 0;
								margin: 0;
								padding: 0;
								font-size: 20px;
								cursor: pointer;
								opacity: 0;
								filter: alpha(opacity=0);
							}
							.image-preview-input-title {
								margin-left:2px;
							}
							.popover {
								max-width: fit-content !important;
							}
						</style>
					
						<!-- image-preview-filename input [CUT FROM HERE]-->
						<div class="input-group image-preview">
							<input type="text" class="form-control image-preview-filename" disabled="disabled"> <!-- don't give a name === doesn't send on POST/GET -->
							<span class="input-group-btn">
								<!-- image-preview-clear button -->
								<button type="button" class="btn btn-default image-preview-clear" style="display:none;">
									<span class="glyphicon glyphicon-remove"></span> Limpar
								</button>
								<!-- image-preview-input -->
								<div class="btn btn-default image-preview-input">
									<span class="glyphicon glyphicon-folder-open"></span>
									<span class="image-preview-input-title">Procurar</span>
									<input type="file" accept="image/png, image/jpeg, image/gif" name="input-file-preview-2" /> <!-- rename it -->
								</div>
							</span>
						</div><!-- /input-group image-preview [TO HERE]--> 
					</div>
				</div>
		   
		   
		   </div>
          </div>
          <div class="modal-footer">
            <button class="btn btn-success" name="BTN_EDT" id="btn_modalEdt">&#10003; Salvar Alterações <span id="span_modalEdt"></span></button>
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
          </div>
          </form>
        </div>
      </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="modalStatus" tabindex="-1" role="dialog" aria-labelledby="modalEdtdms" aria-hidden="true">
		 <div class="modal-dialog">
			  <div class="modal-content">
				  <div class="modal-header">
					<h5 class="modal-title" id="modalEdtdms">Você deseja realmente modificar o status do usuario:<br> <span id="st-title"></span> ?</h5>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					  <span aria-hidden="true">&times;</span>
					</button>
				  </div>
				  <div class="modal-body">
						<button class="btn btn-success btn-block" id="btt-status" onClick="" >&#10003; Sim modificar o status</button>
				  </div>
				  <div class="modal-footer">
					<button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
				  </div>
			  </div>
		 </div>
    </div>
</div>
<!-- inclusao do footer -->
<?php
		$ff    = "<script src='utils/js/users.js'></script>";
		$jspage  = "<script>";
		$jspage .= "			$(document).ready(function() {";
		$jspage .= "				$(\"[data-toggle2='tooltip']\").tooltip();";
		
		$jspage .= "				$(\"#example\").DataTable( {";
		$jspage .= "					'order': [],";
		$jspage .= "					'pagingType': 'full_numbers',";
		$jspage .= "					'language': {";
		$jspage .= "						'lengthMenu': 'Exibição _MENU_ registros por página',";
		$jspage .= "						'zeroRecords': 'Nada encontrado - desculpe',";
		$jspage .= "						'info': 'Mostrando página _PAGE_ de _PAGES_',";
		$jspage .= "						'infoEmpty': 'Não há registros acessível',";
		$jspage .= "						'infoFiltered': '(filtrado de _MAX_ total registros)',";
		$jspage .= "							'sSearch':       	'Procurar',";
		$jspage .= "						'sEmptyTable': 'Nenhum dado disponível na tabela', ";
		$jspage .= "						'sLoadingRecords':'Carregando...',";
		$jspage .= "						'sProcessing':'Aguarde...', ";
		$jspage .= "						'sZeroRecords':'Nenhuma entrada.', ";
		$jspage .= "					'oPaginate': {";
		$jspage .= "							'sFirst':    	'Primeira',";
		$jspage .= "							'sPrevious': 	'Voltar',";
		$jspage .= "							'sNext':     	'Próximo',";
		$jspage .= "							'sLast':     	'Último'";
		$jspage .= "						}";
		$jspage .= "					}";
		$jspage .= "				} );";
		$jspage .= "				$(\"#exampleb\").DataTable( {";
		$jspage .= "					'order': [],";
		$jspage .= "					'pagingType': 'full_numbers',";
		$jspage .= "					'language': {";
		$jspage .= "						'lengthMenu': 'Exibição _MENU_ registros por página',";
		$jspage .= "						'zeroRecords': 'Nada encontrado - desculpe',";
		$jspage .= "						'info': 'Mostrando página _PAGE_ de _PAGES_',";
		$jspage .= "						'infoEmpty': 'Não há registros acessível',";
		$jspage .= "						'infoFiltered': '(filtrado de _MAX_ total registros)',";
		$jspage .= "							'sSearch':       	'Procurar',";
		$jspage .= "						'sEmptyTable': 'Nenhum dado disponível na tabela', ";
		$jspage .= "						'sLoadingRecords':'Carregando...',";
		$jspage .= "						'sProcessing':'Aguarde...', ";
		$jspage .= "						'sZeroRecords':'Nenhuma entrada.', ";
		$jspage .= "					'oPaginate': {";
		$jspage .= "							'sFirst':    	'Primeira',";
		$jspage .= "							'sPrevious': 	'Voltar',";
		$jspage .= "							'sNext':     	'Próximo',";
		$jspage .= "							'sLast':     	'Último'";
		$jspage .= "						}";
		$jspage .= "					}";
		$jspage .= "				} );";
		
		
		$jspage .= "				let msg = localStorage.getItem('msg');";
		$jspage .= "				if(msg == 'ativa'){";
		$jspage .= "					func(localStorage.getItem('status'),localStorage.getItem('texto'));";
		$jspage .= "				}";
		$jspage .= "			});";
		
		
		$jspage .= "		</script>";
?>

