<?php
$d=array("tituloApp"=>"Usuarios","menu"=>"");
$this->parser->parse("html/page/v2/header",$d);
$this->parser->parse("html/page/v2/bar",$d);
$parse["menu"]=$this->load->view("html/page/v2/php/menuHClinica",$d,true);
$parse["tituloApp"]=$d["tituloApp"];
$this->parser->parse("html/page/v2/navigation",$parse);
$nombreTmp=isset($_SESSION["nombreTmp"])?$_SESSION["nombreTmp"]:"";
unset($_SESSION["nombreTmp"]);
?>
<script type="text/javascript">
var nombre = "<?php echo $nombreTmp?>";
</script>
<div class="modal fade" id="modal-usuario" role="dialog" aria-hidden="true">
	<div class="modal-dialog modal-md" role="document">
		<div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title" id="">Nuevo Usuario</h3>
<!--                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"> -->
<!--                   <span aria-hidden="true">&times;</span> -->
<!--                 </button> -->
            </div>
            <form id="formUsuario" action="<?=base_url("clinica/usuario/guardarUsuario")?>" method="post">
            	<div class="modal-body">
                	<div class="form-group">
                		<label>Nombre de usuario</label>
                		<?php 
                		$user=array("name"=>"user","id"=>"user","value"=>"","class"=>"form-control form-control-sm","placeholder"=>"Ingrese username");
                		echo form_input($user);
                		?>
                	</div>
                	<div class="form-group">
                		<label>Contraseña</label>
                		<?php 
                		$pass=array("name"=>"pass","id"=>"pass","type"=>"password","value"=>"","class"=>"form-control form-control-sm","placeholder"=>"Contraseña");
                		echo form_input($pass);
                		?>
                	</div>
                	<div class="form-group">
                		<label>Repita contraseña</label>
                		<?php 
                		$repass=array("name"=>"repass","id"=>"repass","type"=>"password","value"=>"","class"=>"form-control form-control-sm","placeholder"=>"Repita Contraseña");
                		echo form_input($repass);
                		?>
                	</div>
                	<?php
                	?>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Cerrar</button>
                	<button type="" class="btn btn-primary">Guardar</button>
                </div>
            </form>
        </div>
  	</div>
</div>
<div class="row">
	<div class="col-md-12 col-lg-12">
		<div class="box box-primary">
			<div class="box-header">
				<button data-toggle="modal" data-target="#modal-usuario" type="button" id="btnNuevo" class="btn btn-success"><i class="fa fa-user fa-lg">&nbsp;</i>Nuevo Usuario</button>
    		</div>
    		<div class="box-body">
    			<table id="listUsuario" class="table table-sm table-bordered table-striped dt-responsive table-hover table-active">
        			<thead class="thead-dark">
        				<tr>
        					<th>Usuario</th>
        					<th>Acciones</th>
        				</tr>				
        			</thead>
        			<tbody>
        			</tbody>
        		</table>
    		</div>
		</div>
	</div>
</div>
<?php
$this->load->view("html/page/v2/prefooter");?>
<!--<script src="<?php echo base_url()?>assets/js/paciente/listPaciente.js"></script>-->
<script type="text/javascript">
$(document).ready(function(){
	$("#formUsuario").submit(function(event){
	    event.preventDefault();
	    $.ajax({url:$(this).attr("action"),
	        data:$(this).serialize(),
	        method:"POST",
	        dataType:"JSON",
	        cache:false,
	        success:function(res){
	          if(res.res)
	          {
	          	  alert(res.message);
	          	  addRegistro(res.usuario);
	          	  $("#modal-usuario").modal("hide");
	          }
	          else
	          {
	              alert(res.message);
	          } 
	          
	        },error:function(res){
	          alert(res.responseText);
	        }});
	});
	$.ajax({url:path+"/clinica/usuario/getListUsuario",
		data:{},
		method:"POST",
		dataType:"JSON",
		success:function(res){
			console.log(res);
			
			var listUsuario=$("#listUsuario").DataTable({
				"language":{
					"url":path+"assets/js/Spanish.json"
				},
				"paging":true,
				"searching":false,
				"info":false,
				"destroy":true,
				"data":res.datos,
				"columns":[
					{"data":"user"},
					{data:function(data){
						var editar="editarUsuario('"+data.idUsuario+"')";
						return "<button title='Editar usuario' onclick="+editar+" class='btn btn-warning btn-sm'><i class='fa fa-pencil'></i></button> "+
						"<button title='Borrar usuario' class='del-usuario btn btn-danger btn-sm'><i class='glyphicon glyphicon-remove-circle'></i></button";
					}}
				],
				"columnDefs":[{
					"targets": 1,
					"createdCell":function(td,cellData,rowData,row,col){
						$(td).on("click",".del-usuario",function(){
							Swal.fire({
								title: 'Eliminar Usuario',
								text: "Confirme para eliminar este usuario!",
								icon: 'warning',
								showCancelButton: true,
								confirmButtonColor: '#3085d6',
								cancelButtonColor: '#d33',
								cancelButtonText: "Cancelar",
								confirmButtonText: 'Eliminar'
							}).then((result) => {
								if (result.isConfirmed) {
								Swal.fire({
									icon:'success',
									title:"Eliminado",
									showConfirmButton: false,
									timer: 1500}
									
									/*'Deleted!',
									'Your file has been deleted.',
									'success',1500*/)
								}
							});
						});
					}}]
			});
			//listUsuario.search(nombre).draw();
	  },error:function(res){
		  console.log(res.responseText);
	  }});
});
function editarUsuario(idUsuario)
{
	//alert(idUsuario);
	$("#modal-usuario").modal();
}
function addRegistro(data)
{
	var table = $('#listUsuario').DataTable();
	table.row.add(data).draw();
}
</script>
<?php
$this->load->view("html/page/v2/footer");