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

	$boton = "<button type='button' class='btn btn-info btn-sm' id='btnEnviar' name='btnEnviar'>INGRESAR</button>";
	
	$id = 0;
	$descripcion = "";
	$congoce = "";
	$hora = "";
	$ley = "";
	$observacion = "";	
    
	if($_POST){
        
		$hide = "";
		$url = 'http://200.24.13.60/~program1/power-time-api/public/obtenertipopermiso';
		$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;
	    $descripcion = $array[0]->descripcion;	
		$ley = $array[0]->ley;	
		$observacion = $array[0]->observacion;			

		if($array[0]->congoce) { $congoce = "checked"; }		
		if($array[0]->hora) { $hora = "checked"; }

		curl_close($ch); 


		$boton = "<button type='button' class='btn btn-info btn-sm' id='btnEditar' name='btnEditar'>EDITAR</button>";
	}

?>
<!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" />
    <!-- iCheck for checkboxes and radio inputs -->
	<link rel="stylesheet" href="../plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <style>
  #example{
	  font-size:11px;
  }
  label{
	  font-size:13px;	  
  }
  </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 mb-2">
          <div class="col-sm-6">
            <h1>Tipo Permiso</h1>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content" id="app">
      <div class="container-fluid">
        <div class="row">
          <!-- left column -->
          <div class="col-md-6">
            <!-- Horizontal Form -->
            <div class="card">
				<div class="card-body">
					  <div class="form-group input-group-sm">
						<label>Descripci&oacute;n :</label>
						<input type="text" class="form-control control-text txtToUpper" id="txtDescripcion" value="<?php echo $descripcion ?>">
					  </div>
					  <div class="form-group input-group-sm">
						<label>Goce de Sueldo :</label><br />
						<input type="checkbox" <?php echo $congoce; ?> id="chkCongoce" >
					  </div>
					  <div class="form-group">
						<label>P&eacute;rmiso por Rango Horario :</label><br />
                        <input type="checkbox" <?php echo $hora; ?> id="chkHora" >
					  </div>						  					  
				</div>
				<!-- /.card-body -->
				<div class="card-footer">
				    <input type="hidden" class="form-control control-text" id="txtId" value="<?php echo $id ?>" readonly>
					<?php echo $boton; ?>
					<button type="button" class="btn btn-default float-right" id="btnLimpiar">Limpiar</button>
				</div>
            </div>
          </div>
          <div class="col-md-6">
            <!-- Horizontal Form -->
            <div class="card">
				<div class="card-body">
					  <div class="form-group input-group-sm">
						<label>N&uacute;mero de Ley :</label>
						<input type="text" class="form-control control-text txtToUpper" id="txtLey" value="<?php echo $ley ?>">
					  </div>	
					  <div class="form-group input-group-sm">
						<label>Observaci&oacute;n :</label>
						<textarea class="form-control control-text txtToUpper" id="txtObservacion" rows="5"><?php echo $observacion ?></textarea>
					  </div>						  
				</div>
				<!-- /.card-body -->
				<div class="card-footer">
				</div>
            </div>
          </div>		  
        </div>
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
  
  <?php include_once("../inc/footer.php"); ?>

 
</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>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<script>
	
  $(function () {
	  
    $('#chkCongoce').bootstrapToggle({
      on: 'Con Goce',
      off: 'Sin Goce',
	  onstyle: "info",
	  offstyle: "default",
	  style: "slow",
	  size: "small",
	  width: "200"
    });	  
    $('#chkHora').bootstrapToggle({
      on: 'Permite',
      off: 'No Permite',
	  onstyle: "info",
	  offstyle: "default",
	  style: "slow",
	  size: "small",
	  width: "200"
    });
	
	/*
	$(".control-text").bind('keypress', function(event) {
	  var regex = new RegExp("^[a-zA-Z 0-9 .,-]+$");
	  var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
	  if (!regex.test(key)) {
		event.preventDefault();
		return false;
	  }
	});	  
	
    $(document).bind("contextmenu",function(e){
      return false;
    });	 
    */
	  
    $('.txtToUpper').keyup(function(){
        $(this).val($(this).val().toUpperCase());
    });	 	 	

	  
	$("#btnEnviar").click(function(){

		if($("#txtDescripcion").val().length == 0){
			$("#mensajeUsuario").html("Debe Ingresar Descripci&oacute;n");
			$("#modal-default2").modal('show');
            return false;
		}else{
			var goceSueldo = false;
			if( $('#chkCongoce').is(':checked') ) { goceSueldo = true; }				
			var rangoHora = false;
			if( $('#chkHora').is(':checked') ) { rangoHora = true; }	
			
			$.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/ingresartipopermiso",
				data : JSON.stringify({
					id:$("#txtId").val(),
					descripcion:$("#txtDescripcion").val(),	
					ley:$("#txtLey").val(),	
					observacion:$("#txtObservacion").val(),						
					congoce:goceSueldo,
					hora:rangoHora					
				}),
				beforeSend : function()
				{
					//$("#mensajeUsuario").html("<div class='alert alert-info'>Enviando datos</div>");
				},
				success: function(data, textStatus, xhr) {
					if(parseInt(xhr.status) == 201){
						toastr.success("Registro Ingresado Exitosamente", "POWERTIME");
					}
				}
			})
		}

	});

	$("#btnEditar").click(function(){

		if($("#txtDescripcion").val().length == 0){
			$("#mensajeUsuario").html("Debe Ingresar Descripci&oacute;n");
			$("#modal-default2").modal('show');
            return false;
		}else{
			
			var goceSueldo = false;
			if( $('#chkCongoce').is(':checked') ) { goceSueldo = true; }				
			var rangoHora = false;
			if( $('#chkHora').is(':checked') ) { rangoHora = true; }	
						
			$.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/actualizartipopermiso",
				data : JSON.stringify({
					id:$("#txtId").val(),
					descripcion:$("#txtDescripcion").val(),	
					ley:$("#txtLey").val(),	
					observacion:$("#txtObservacion").val(),						
					congoce:goceSueldo,
					hora:rangoHora					
				}),
				beforeSend : function()
				{
					//$("#mensajeUsuario").html("<div class='alert alert-info'>Enviando datos</div>");
				},
				success: function(data, textStatus, xhr) {
					if(parseInt(xhr.status) == 201){
						toastr.success("Registro Ingresado Exitosamente", "POWERTIME");
					}
				}
			})
		}

	});	 

 	$("#btnLimpiar").click(function(){
		limpiarFormulario();
	});
	
	function limpiarFormulario(){
		$("#txtDescripcion").val("");
		$("#txtId").val(0);
		$("#txtLey").val("");
		$("#txtObservacion").val("");				
		$("#chkCongoce").attr('checked', false);
		$("#chkHora").attr('checked', false);		
	}
	
});	 	
  
</script>

</body>
</html>
