<?php include_once("../lib/control.seguridad.php"); ?>
<?php

	$id = "";
	$rut = "";
	$pasaporte = "";		
	$nombre = "";
	$apellido = "";		
	$hide = "d-none";
	
    if($_POST){
        
		$hide = "";
		$url = 'http://200.24.13.60/~program1/power-time-api/public/obtenerpersona';
		$ch = curl_init($url); //create a new cURL resource
		$payload = json_encode(array('id' => $_POST["txtId"]));
		curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);//attach encoded JSON string to the POST fields
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); //set the content type to application/json
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //return response instead of outputting
		$result = curl_exec($ch); //execute the POST request
        $array = json_decode($result);

		$id = $array[0]->id;
		$rut = $array[0]->rut;
		$pasaporte = $array[0]->pasaporte;		
		$nombre = $array[0]->nombre;	
		$apellido = $array[0]->apellido;			

		curl_close($ch); 

	}

?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>POWERTIME</title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">
 
  <link href="../plugins/toastr/build/toastr.css" rel="stylesheet" type="text/css" />
  <!-- DataTables -->
  <link rel="stylesheet" href="../plugins/datatables-bs4/css/dataTables.bootstrap4.css">   
  <style>
	  .content-header h4{ 
		  font-size: 20px
	  }
      .badge-success{
		  font-size:11px;		  
	  }	  
	  #example{
		  font-size:11px;
	  }
      label{
		  font-size:11px;		  
	  } 	  
      .dataTables_wrapper { font-size: 10px }

	.nav-item .nav-link{
		padding:5px 10px 5px 10px;
	}	  
	
 	
  </style>    
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
 
  <?php include_once("../inc/header.php"); ?>

  <?php include_once("../inc/menu.php"); ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row">
          <div class="col-sm-6">
            <h4>Configuraci&oacute;n de Permisos de Usuarios</h4>
			<input type="hidden" class="form-control control-text" id="txtId_autoriza" value="<?php echo $_SESSION['LOGIN']['CODIGO'] ?>">
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content" id="app">
      <div class="container-fluid">
	  
        <div class="row">		
			<div class="col-md-12">
				<div class="card">
				    <div class="card-header">
					  <div class="row">
						<div class="col-md-3">
						  <div class="input-group mb-2 input-group-sm">
							<div class="input-group-prepend">
							  <div class="input-group-text">Rut :</div>
							</div>
							<input type="text" class="form-control control-rut" id="txtRut" value="<?php echo $rut ?>" readonly >
						  </div>
						</div>	
						<div class="col-md-5">
						  <div class="input-group mb-2 input-group-sm">
							<div class="input-group-prepend">
							  <div class="input-group-text">Nombres :</div>
							</div>
							<input type="text" class="form-control" id="txtRut" value="<?php echo $nombre ." ".$apellido ?>" readonly >
							<input type="hidden" class="form-control" id="txtId" value="<?php echo $id ?>" readonly>
						  </div>
						</div>						  
					  </div>					
					</div>
					<div class="card-body">
						 <div class="row">
							<div class="col-md-3">
							  <div class="input-group mb-2 input-group-sm">
								<div class="input-group-prepend">
								  <div class="input-group-text">Tipo Permiso :</div>
								</div>
								<select class="form-control" id="txtTipo_permiso">
									<option value="0">Seleccione...</option>
								</select>								
							  </div>
							</div>							 
							<div class="col-md-3">
							  <div class="input-group mb-2 input-group-sm">
								<div class="input-group-prepend">
								  <div class="input-group-text">Cantidad D&iacute;as :</div>
								</div>
								<input type="text" class="form-control" id="txtCantidad" value="0"  >
							  </div>
							</div>							 
							<div class="col-md-1">
								<button type='button' class='btn btn-info btn-sm' id='btnEnviar' name='btnEnviar'>INGRESAR</button>
							</div>
						 </div>
						 <div class="row">
							<div class="col-md-12">
							  <table id="example" class="table table-bordered table-striped">
								<thead>
								<tr>			
								  <th>TIPO PERMISO</th>				
								  <th>CANTIDAD DIAS</th>				  
								  <th></th>					  
								</tr>
								</thead>			
							  </table>					  
							  <div class="div-response"></div>		 
							</div>							 					 
						 </div>	
					</div>
					<!-- /.card-body -->
					<div class="card-footer">
						<div class="row">
							<div class="col-md-3">
							  <div class="input-group mb-2 input-group-sm">
								<div class="input-group-prepend">
								  <div class="input-group-text">Permisos Con goce :</div>
								</div>
								<input type="text" class="form-control" readonly id="txtCantidad_congoce" name="txtCantidad_congoce" value="0" >						
							  </div>
							</div>		
							<div class="col-md-3">
							  <div class="input-group mb-2 input-group-sm">
								<div class="input-group-prepend">
								  <div class="input-group-text">Permisos Sin goce :</div>
								</div>
								<input type="text" class="form-control" readonly id="txtCantidad_singoce" name="txtCantidad_singoce" value="0" >								
							  </div>
							</div>									
						</div>
					</div>
				</div>		
            </div>					
        </div>
		
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  
  <?php include_once("../inc/footer.php"); ?>
  
	<div id="modal-delete" class="modal" tabindex="-1" role="dialog">
	  <div class="modal-dialog" role="document">
		<div class="modal-content">
		  <div class="modal-header">
			<h5 class="modal-title">POWERTIME</h5>
			<button type="button" class="close" data-dismiss="modal" aria-label="Close">
			  <span aria-hidden="true">&times;</span>
			</button>
		  </div>
		  <div class="modal-body text-center">
			<p>&iquest; Desea Eliminar Configuraci&oacute;n de Permiso de Usuario ? </p>
		  </div>
		  <div class="modal-footer">
			<button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
			<button type="button" class="btn btn-info" id="btn-confirmar">Confirmar</button>
		  </div>
		</div>
	  </div>
	</div>    

	
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/adminlte.min.js"></script>

<script src="../plugins/toastr/toastr.js"></script>
<!-- DataTables -->
<script src="../plugins/datatables/jquery.dataTables.js"></script>
<script src="../plugins/datatables-bs4/js/dataTables.bootstrap4.js"></script>

<script>

  $(function () {
	  
    $('#example').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": false,
      "info": true,
      "autoWidth": false,
	  "language": {
		"url": "//cdn.datatables.net/plug-ins/1.10.15/i18n/Spanish.json"
      } 		  
    });	  
	  
    $(document).bind("contextmenu",function(e){
      return false;
    });	  
	  
	combo();
	function combo(){
		$.ajax({
			type:'POST',
			contentType: 'application/json',
			dataType: "json",
            crossDomain: true,
			headers: {
				'Content-Type': 'application/json'
			},
			url: "http://200.24.13.60/~program1/power-time-api/public/tipopermiso",	
			success: function(dados)
			{
				for (i=0;i<dados.length;i++){
					for (j=0;j<dados[i].length;j++){
						$('#txtTipo_permiso').append($('<option>', {
							value: dados[i][j].id,
							text : dados[i][j].descripcion
						}));
					}
				}
			}
		});		
	}	
	
	$("#btnEnviar").click(function(){
		if(parseInt($("#txtTipo_permiso").val()) == 0){
			toastr.warning("Debe Seleccionar Tipo de Permiso", "POWERTIME");
			return false;
		}		
		else if( $("#txtCantidad").val() == null || $("#txtCantidad").val().length == 0 || /^\s+$/.test($("#txtCantidad").val()) ){
			toastr.warning("Debe ingresar Cantidad de D&iacute;as", "POWERTIME");
            return false;
		}
		else{
			$.ajax({
				type:'POST',
				contentType: 'application/json',
				dataType: "json",
				crossDomain: true,
				ifModified:true,
				cache: false,
				headers: {
					'Content-Type': 'application/json'
				},
				url: "http://200.24.13.60/~program1/power-time-api/public/ingresarconfiguracionpermisopersona",
				data : JSON.stringify({
					id_tipo_permiso:$("#txtTipo_permiso").val(),	
					id_persona:$("#txtId").val(),	
					cantidad:$("#txtCantidad").val()				
				}),
				success: function(data, textStatus, xhr) {					
					if(parseInt(xhr.status) == 201){
						toastr.success("Registro ingresado Exitosamente", "POWERTIME");
						listarRegistro();
					}
				}			
			})			
		}
	});	
	listarRegistro();
	function listarRegistro(){

		$('#example').dataTable().fnClearTable();
		$.ajax({
			type:'POST',
			contentType: 'application/json',
			dataType: "json",
            crossDomain: true,
			headers: {
				'Content-Type': 'application/json'
			},
			url: "http://200.24.13.60/~program1/power-time-api/public/configuracionpermisopersona",
			data : JSON.stringify({
				id_persona:$("#txtId").val()				
			}),		
			success: function(dados)
			{
				$('#example').dataTable().fnClearTable();
				 for (i=0;i<dados.length;i++){
					for (j=0;j<dados[i].length;j++){	

					  var boton = "<button type='button' class='btn btn-info btn-xs btn-Agregar'><i class='fas fa-arrow-up'></i></button>";
					  var boton2 = "<button type='button' class='btn btn-info btn-xs btn-Disminuir'><i class='fas fa-arrow-down'></i></button>";
					  var boton3 = "<button type='button' class='btn btn-info btn-xs btn-eliminar'>Eliminar</button>";
					
					  $('#example').dataTable().fnAddData( [
						dados[i][j].tipo,	
						dados[i][j].cantidad,	
                        boton+" "+boton2+" "+boton3 + "<input type='hidden' value ='"+dados[i][j].id+"' />" 						
					  ]);
					}
				}
			}
		});
		
		$('#txtCantidad_congoce').val(0);		
		$('#txtCantidad_singoce').val(0);
		var cantidadCongoce = 0;
		var cantidadSingoce = 0;
		
		$.ajax({
			type:'POST',
			contentType: 'application/json',
			dataType: "json",
            crossDomain: true,
			headers: {
				'Content-Type': 'application/json'
			},
			url: "http://200.24.13.60/~program1/power-time-api/public/totaldiastipopermiso",
			data : JSON.stringify({
				id_persona:$("#txtId").val()				
			}),		
			success: function(dados)
			{
				 for (i=0;i<dados.length;i++){
					for (j=0;j<dados[i].length;j++){								
						if(dados[i][j].congoce == 0)  {
							cantidadSingoce += parseInt(dados[i][j].total); 
						}else{
							cantidadCongoce += parseInt(dados[i][j].total); 
						}
					}
				}
				$('#txtCantidad_congoce').val(cantidadCongoce);		
				$('#txtCantidad_singoce').val(cantidadSingoce);				
			}
		});		
	}

	var idEliminar;	
	$("#example").on('click','.btn-eliminar', function() {			
		$('#modal-delete').modal("show");
	    idEliminar = $(this).parents("tr").find("td:eq(2)").find("input").val();
	});	
		
	$("#btn-confirmar").click(function(){
		$.ajax({
			type:'POST',
			contentType: 'application/json',
			dataType: "json",
			crossDomain: true,
			headers: {
				'Content-Type': 'application/json'
			},
			url: "http://200.24.13.60/~program1/power-time-api/public/eliminarconfiguracionpermisopersona",
			data : JSON.stringify({
				id:idEliminar		
			}),
			success: function(data, textStatus, xhr) {
				if(parseInt(xhr.status) == 201){
					$('#modal-delete').modal("toggle");					
                     toastr.success("Eliminaci&oacute;n exitosa", "POWERTIME");
					listarRegistro();
				}
			}
		})		
	});

	$("#example").on('click','.btn-Agregar', function() {
alert($(this).parents("tr").find("td:eq(2)").find("input").val());		
		$.ajax({
			type:'POST',
			contentType: 'application/json',
			dataType: "json",
			crossDomain: true,
			headers: {
				'Content-Type': 'application/json'
			},
			url: "http://200.24.13.60/~program1/power-time-api/public/agregardiaspermisopersona",
			data : JSON.stringify({
				id:$(this).parents("tr").find("td:eq(2)").find("input").val()		
			}),
			success: function(data, textStatus, xhr) {
				if(parseInt(xhr.status) == 201){			
                    toastr.success("Cantidad de d&iacute;as Aumentado", "POWERTIME");
					listarRegistro();
				}
			}
		})			
	});	

	$("#example").on('click','.btn-Disminuir', function() {			
		$.ajax({
			type:'POST',
			contentType: 'application/json',
			dataType: "json",
			crossDomain: true,
			headers: {
				'Content-Type': 'application/json'
			},
			url: "http://200.24.13.60/~program1/power-time-api/public/disminuirdiaspermisopersona",
			data : JSON.stringify({
				id:$(this).parents("tr").find("td:eq(2)").find("input").val()		
			}),
			success: function(data, textStatus, xhr) {
				if(parseInt(xhr.status) == 201){			
                    toastr.success("Cantidad de d&iacute;as Disminuido", "POWERTIME");
					listarRegistro();
				}
			}
		})			
	});		
	
	
});	 	
  
</script>

</body>
</html>
