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

	$boton = "<button type='button' class='btn btn-info btn-sm' id='btnEnviar' name='btnEnviar'>INGRESAR</button>";
	
	$id = "";
	$descripcion = "";
 
    if($_POST){
 
		$url = $apiUrl . '/public/obtenermensaje';
		$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;			
		
		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" />
  <!-- DataTables -->
  <link rel="stylesheet" href="../plugins/datatables-bs4/css/dataTables.bootstrap4.css">    
   <style>
	  label{
		  font-size:11px;
	  }   
	  #example{
		  font-size:10px;
	  }
	  .content-header h4{ 
		  font-size: 20px
	  }
     .dataTables_wrapper { font-size: 10px; }	
	.dataTables_filter input { height: 25px; }	   
  </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>Mensaje</h4>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <!-- Main content -->
    <section class="content" id="app">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-6">
            <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 " id="txtDescripcion" value="<?php echo $descripcion; ?>">
				  </div>					  
				</div>
				<div class="card-footer">
					<input type="hidden" class="form-control " 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>
        <!-- /.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>
<!-- DataTables -->
<script src="../plugins/datatables/jquery.dataTables.js"></script>
<script src="../plugins/datatables-bs4/js/dataTables.bootstrap4.js"></script>
<script>

  $(function () {
	  
    $(".modal-body-documento").height(parseInt(screen.height) -400);		  
	    
    $('#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;
    });	  
 
	$("#btnEnviar").click(function(){

		if( $("#txtDescripcion").val() == null || $("#txtDescripcion").val().length == 0 || /^\s+$/.test($("#txtDescripcion").val()) ){
			toastr.warning("Debe ingresar Descripci&oacute;n", "POWERTIME");
            return false;
		}else{
			
	 
			$.ajax({
				type:'POST',
				contentType: 'application/json',
				dataType: "json",
				crossDomain: true,
				headers: {
					'Content-Type': 'application/json'
				},
				url: "<?php include_once('../inc/api_cred.php'); echo $apiUrl; ?>/public/ingresarmensaje",
				data : JSON.stringify({
					descripcion:$("#txtDescripcion").val()				
				}),
				beforeSend : function()
				{
					//$("#mensajeUsuario").html("<div class='alert alert-info'>Enviando datos</div>");
				},
				success: function(data) {
					if(parseInt(data[0]) == 0){
						toastr.warning(data.error, "POWERTIME");
					} else {
						toastr.success("Registro Ingresado Exitosamente", "POWERTIME");
					}
				}
			})
		}

	});

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

		if( $("#txtDescripcion").val() == null || $("#txtDescripcion").val().length == 0 || /^\s+$/.test($("#txtDescripcion").val()) ){
			toastr.warning("Debe ingresar Descripci&oacute;n", "POWERTIME");
            return false;
		}else{
			$.ajax({
				type:'POST',
				contentType: 'application/json',
				dataType: "json",
				crossDomain: true,
				headers: {
					'Content-Type': 'application/json'
				},
				url: "<?php include_once('../inc/api_cred.php'); echo $apiUrl; ?>/public/actualizarmensaje",
				data : JSON.stringify({
					id:$("#txtId").val(),
					descripcion:$("#txtDescripcion").val()					
				}),
				beforeSend : function()
				{
					//$("#mensajeUsuario").html("<div class='alert alert-info'>Enviando datos</div>");
				},
				success: function(data) {
					if(parseInt(data[0]) > 0){
						toastr.success("Registro Ingresado Exitosamente", "POWERTIME");
					}
				}
			})
		}

	});	 
	
	
 	$("#btnLimpiar").click(function(){
		limpiarFormulario();
	});
	
	function limpiarFormulario(){
		$("#txtDescripcion").val("");
	}
	
});	 	
  
</script>

</body>
</html>
