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

	$id = "";
	$rut = "";
	$pasaporte = "";		
	$nombre = "";
	$apellido = "";	
	
    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">   
  <!-- JQUERY UI -->  
  <link rel="stylesheet" href="../plugins/jQueryUI/jquery-ui.css" />  
  <style>
 
	  #example2{
		  font-size:11px;
	  } 
 	  
      .dataTables_wrapper { font-size: 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>Listado Permisos Trabajador</h4>
          </div>
			<div class="col-md-3 d-none">
			  <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" id="txtRut" value="<?php echo $rut ?>" readonly >
				<input type="hidden" class="form-control" id="txtId" value="<?php echo $_POST["txtId"] ?>" readonly >
				<input type="hidden" class="form-control control-text" id="txtCargo" value="<?php echo $_SESSION['LOGIN']['CARGOUSUARIO'] ?>">
			  </div>						
			</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">
            <!-- Horizontal Form -->
            <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">Fecha Inicio</div>
							</div>
							<input type="text" class="form-control" id="txtFecha_inicio_permiso" value="<?php echo date("d-m-Y", strtotime("-3 month", strtotime(date("Y-m-d")))); ?>" readonly >
						  </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">Fecha T&eacute;rmino</div>
							</div>
							<input type="text" class="form-control" id="txtFecha_termino_permiso" value="<?php echo date("d-m-Y") ?>" readonly >
						  </div>
						</div>	
						<div class="col-md-6">
						  <div class="input-group mb-2 input-group-sm">
							<div class="input-group-prepend">
							  <div class="input-group-text">Trabajador</div>
							</div>
							<input type="text" class="form-control" value="<?php echo $nombre ." ". $apellido ?>" readonly >
						  </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">Tipo Permiso</div>
							</div>
        					<select class="form-control" id="txtTipo_permiso">
								<option value="0"> TODOS </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">Estado</div>
							</div>
        					<select class="form-control" id="txtEstado">
								<option value="0"> TODOS </option>
        					</select>
						  </div>
						</div>							
						<div class="col-md-1">
						  <button type="button" class="btn btn-info mb-2 btn-sm btn-block" id="btnBuscarPermiso">Buscar</button>
						</div>						
					</div> 				
				</div>
				<div class="card-body">	
				  <table id="example2" class="table table-bordered table-striped">
					<thead>
					<tr>			
					  <th>FECHA INICIO</th>				
					  <th>FECHA T&Eacute;RMINO</th>	
					  <th>D&Iacute;AS</th>						  
					  <th>TIPO DE PERMISO</th>	
					  <th>OBSERVACI&Oacute;N</th>						  
					</tr>
					</thead>			
				  </table>	
					<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">Cantidad D&iacute;as</div>
							</div>
							<input type="text" class="form-control" id="txtCantidad" value="0" readonly >
						  </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 Permisos</div>
							</div>
							<input type="text" class="form-control" id="txtCantidad_permiso" value="0" readonly >
						  </div>
						</div>							
					</div> 					  
				</div>
				<!-- /.card-body -->
				<div class="card-footer">
					<div class="row">			
						<div class="col-md-3">
						  <button type="button" class="btn btn-info mb-2 btn-sm btn-block" id="btnmprimir">Imprimir</button>
						</div>						
					</div>			  
				</div>
            </div>
          </div>
        </div>
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  
  <?php include_once("../inc/footer.php"); ?>

	<div class="modal" tabindex="-1" role="dialog"  id="modal-default">
	  <div class="modal-dialog modal-xl" role="document">
		<div class="modal-content">
		  <div class="modal-header">
			<h5 class="modal-title">Visor de Documentos</h5>
			<button type="button" class="close" data-dismiss="modal" aria-label="Close">
			  <span aria-hidden="true">&times;</span>
			</button>
		  </div>
		  <div class="modal-body modal-body-documento">
			<iframe name="iframe3" width="100%" height="100%" src="../inc/loading.php"></iframe>
		  </div>
		  <div class="modal-footer">
			<button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button>
		  </div>
		</div>
	  </div>
	</div>	
	
	<div class="div-response"></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>
  <!-- JQUERY UI --> 
<script src="../plugins/jQueryUI/jquery-ui.js"></script>
<script>

$(function () {
	
    $(".modal-body-documento").height(parseInt(screen.height) -400);
    
	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/buscarcargopermiso",
			data : JSON.stringify({
				id_cargo:$("#txtCargo").val(),			
			}),			
			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_tipo_permiso,
							text : dados[i][j].tipopermiso
						}));
					}
				}
			}
		});	
		$.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/permisotipoestado",
			success: function(dados)
			{
				for (i=0;i<dados.length;i++){
					for (j=0;j<dados[i].length;j++){
						$('#txtEstado').append($('<option>', {
							value: dados[i][j].id,
							text : dados[i][j].descripcion
						}));
					}
				}
			}
		});			
	}  
	  
    $(document).bind("contextmenu",function(e){
      return false;
    });	  
	
     $('#example2').DataTable({
		  "paging": true,
		  "lengthChange": false,
		  "searching": false,
		  "ordering": true,
		  "info": false,
		  "autoWidth": false,
		  "pageLength": 7,
		  "language": {
			"url": "//cdn.datatables.net/plug-ins/1.10.15/i18n/Spanish.json"
		  } 	
     });	
	 
	 $("#txtFecha_inicio_permiso").datepicker({
		dateFormat: "dd-mm-yy",
		changeMonth: true,
		changeYear: true,
		yearRange: "1900:2100",
		onSelect: function(dateText, inst) {
		var lockDate = new Date($('#txtFecha_inicio_permiso').datepicker('getDate'));
		$('#txtFecha_termino_permiso').datepicker('option', 'minDate', lockDate);
		}		
	 }) 
	
	 $("#txtFecha_termino_permiso").datepicker({
		dateFormat: "dd-mm-yy",
		changeMonth: true,
		changeYear: true,
		yearRange: "1900:2100",
		minDate: $("#txtFecha_inicio_permiso").val() 
	 }) 
	 
	 $("#txtTipo_permiso").change(function(){
	     listarpermisopersona();
	 });
	 
	 $("#txtEstado").change(function(){
	     listarpermisopersona();
	 });
	 
	listarpermisopersona();
	function listarpermisopersona(){
		$("#txtCantidad").val(0);
		$("#txtCantidad_permiso").val(0);
		
		$('#example2').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/listarpermisostrabajador",
			data : JSON.stringify({
				id_persona: $("#txtId").val(),	
				fecha_inicio: convertDateFormat($("#txtFecha_inicio_permiso").val()),
				fecha_termino: convertDateFormat($("#txtFecha_termino_permiso").val()),
				id_tipo_permiso: $("#txtTipo_permiso").val(),		
				id_estado: $("#txtEstado").val()						
			}),			
			success: function(dados)
			{
				$('#example2').dataTable().fnClearTable();
				var cantidad = 0;
				var cantidad_permiso = 0;
				 for (i=0;i<dados.length;i++){
					for (j=0;j<dados[i].length;j++){
						
					  var total = dados[i][j].total;
					  if(total == 0){ total = 1; }
					  $('#example2').dataTable().fnAddData( [
						convertDateFormat(dados[i][j].fecha_inicio),						  
						convertDateFormat(dados[i][j].fecha_termino),	
						total,							
						dados[i][j].tipo,							
						dados[i][j].observacion
					  ]);
					  cantidad_permiso++;
					  cantidad += parseInt(total); 
					}
				}
				$("#txtCantidad_permiso").val(cantidad_permiso);
				$("#txtCantidad").val(cantidad);
			}
		});				
	}

	$("#btnBuscarPermiso").click(function(){
		listarpermisopersona();
	});

	$("#btnmprimir").click(function(){
		$('.div-response').html('<form action="informe.permiso.pdf.inc.php"  target="iframe3" name="form" method="POST" style="display:none;"> '+
		'<input type="text" class="form-control" id="txtId_persona" name="txtId_persona" value="'+$("#txtId").val()+'" >'+
		'<input type="text" class="form-control" id="txtFecha_inicio_permiso" name="txtFecha_inicio_permiso" value="'+$("#txtFecha_inicio_permiso").val()+'" >'+
		'<input type="text" class="form-control" id="txtFecha_termino_permiso" name="txtFecha_termino_permiso" value="'+$("#txtFecha_termino_permiso").val()+'" >'+	
		'<input type="text" class="form-control" id="txtTipo_permiso" name="txtTipo_permiso" value="'+$("#txtTipo_permiso").val()+'" >'+			
		'<input type="text" class="form-control" id="txtEstado" name="txtEstado" value="'+$("#txtEstado").val()+'" >'+		
		'</form>');			
		document.forms['form'].submit();
		$('#modal-default').modal('show');			
	});	

	$('#modal-default').on('hidden.bs.modal', function () {
		$('.div-response').empty();
		$('.div-response').html('<form action="../inc/loading.php"  target="iframe3" name="form" method="POST" style="display:none;"> '+
		'</form>');			
		document.forms['form'].submit();		 
	});	
	
	function convertDateFormat(numeros) {
        return numeros.split('-').reverse().join('-');
    }		
			
	
});	 	
  
</script>

</body>
</html>
