<?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;
	$id_turno = 0;
	$id_dia = -1;	
	$hora_inicio = "08:00";
	$hora_termino = "08:00";
	$descanso = "00:00";
	
    if($_POST){

		$hide = "";
		$url = 'http://200.24.13.60/~program1/power-time-api/public/obtenerhorarioturno';
		$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;
		$id_turno = $array[0]->id_turno;
		$id_dia = $array[0]->id_dia;
		$hora_inicio = date("H:i", strtotime($array[0]->hora_inicio));
		$hora_termino = date("H:i", strtotime($array[0]->hora_termino));	
		$descanso = date("H:i", strtotime($array[0]->descanso));			
		
		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">
  <!-- DataTables -->
  <link rel="stylesheet" href="../plugins/datatables-bs4/css/dataTables.bootstrap4.css">
  
  <link href="../plugins/toastr/build/toastr.css" rel="stylesheet" type="text/css" />
  
  <!-- JQUERY TIMEPICKER -->  
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.css">
 
  
  <style>
  #example{
	  font-size:10px;
  }
  #example2{
	  font-size:10px;
  }  
  #example3{
	  font-size:10px;
  }   
   .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 mb-2">
          <div class="col-sm-6">
            <h4>Horario Turno</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">
            <!-- Horizontal Form -->
            <div class="card">
				<div class="card-header">
					<h3 class="card-title">Formulario Horario Turno</h3>
				</div>
				<div class="card-body">
				  <div class="form-group input-group-sm">
					<label>Turno:</label>
					<select class="form-control" id="txtTurno" >
						<option value="0">Seleccione...</option>
					</select>
					<small class="form-text text-muted">Turno  asignar horario.</small>
				  </div>	
				  
				  <div class="form-group input-group-sm">
					<label>Hora Inicio :</label>
					<input type="text" class="form-control control-number" id="txtHora_inicio" value="<?php echo $hora_inicio ?>" readonly>
					<small class="form-text text-muted">Hora de inicio de Turno Diario.</small>
				  </div>				
				  <div class="form-group input-group-sm">
					<label>Hora T&eacute;rmino:</label>
					<input type="text" class="form-control control-text" id="txtHora_termino" value="<?php echo $hora_termino ?>" readonly>
					<small class="form-text text-muted">Hora de t&eacute;rmino Turno Diario.</small>
				  </div>		
				</div>
				<!-- /.card-body -->
				<div class="card-footer">
					<?php echo $boton ?>
					<input type="hidden" class="form-control control-text" id="txtId" value="<?php echo $id ?>" readonly>						
					<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-header">
					<h3 class="card-title">D&iacute;a Laboral</h3>
				</div>
				<div class="card-body">	
				  <div class="form-group input-group-sm">
					<label>Descanso:</label>
					<input type="text" class="form-control control-text" id="txtDescanso" value="<?php echo $descanso ?>" readonly>
					<small class="form-text text-muted">Hora de Descanso Turno Diario.</small>
				  </div>					  
				  <div class="form-group input-group-sm">
					<label>D&iacute;a:</label>
					<select class="form-control" id="txtDia">
						<option value="-1">Seleccione...</option>
							<option value="2">Lunes</option>
							<option value="3">Martes</option>
							<option value="4">Mi&eacute;rcoles</option>
							<option value="5">Jueves</option>
							<option value="6">Viernes</option>
							<option value="7">Sabado</option>
							<option value="1">Domingo</option>						
					</select>
					<small class="form-text text-muted">D&iacute;a semana asignar horario.</small>
				  </div>	
				</div>
				<!-- /.card-body -->
				<div class="card-footer">
				  <table id="example" class="table table-bordered table-striped">
					<thead>
					<tr>							
					  <th>DIA</th>	
					  <th>HORA INICIO</th>				
					  <th>HORA TERMINO</th>	
					  <th class="col-md-2"></th>					  
					</tr>
					</thead>			
				  </table>				
				</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>
<!-- DataTables -->
<script src="../plugins/datatables/jquery.dataTables.js"></script>
<script src="../plugins/datatables-bs4/js/dataTables.bootstrap4.js"></script>

<script src="../plugins/toastr/toastr.js"></script>

  <!-- JQUERY TIMEPICKER -->  
<script src="//cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.js"></script>

<script>

  $(function () {	  
  
    $('#example').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": false,
      "autoWidth": false,
	  "language": {
		"url": "//cdn.datatables.net/plug-ins/1.10.15/i18n/Spanish.json"
      }	 	  
    });
	  
	  
	$('#txtHora_inicio').timepicker({ 
		timeFormat: 'HH:mm',
		interval: 30,
		minTime: '08:00',
		maxTime: '23:00',
		dynamic: false,
		dropdown: true,
		scrollbar: true		
	});
	$('#txtHora_termino').timepicker({
		timeFormat: 'HH:mm',
		interval: 30,
		minTime: '08:00',
		maxTime: '23:00',
		dynamic: false,
		dropdown: true,
		scrollbar: true
	});	  	
	
	$('#txtDescanso').timepicker({
		timeFormat: 'HH:mm',
		interval: 30,
		minTime: '00:00',
		maxTime: '02:00',
		dynamic: false,
		dropdown: true,
		scrollbar: true
	});	 	
	
	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/turno",
			success: function(dados)
			{
				for (i=0;i<dados.length;i++){
					for (j=0;j<dados[i].length;j++){
						$('#txtTurno').append($('<option>', {
							value: dados[i][j].id,
							text : dados[i][j].descripcion
						}));
					}
				}
				$('#txtTurno').val(<?php echo $id_turno ?>);
			}
		});		
	}	
	 
	$('#txtDia').val(<?php echo $id_dia ?>); 
	  
	$("#btnEnviar").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/ingresarhorarioturno",
			data : JSON.stringify({
				id_turno:$("#txtTurno").val(),
				id_dia:$("#txtDia").val(),				
				hora_inicio:$("#txtHora_inicio").val(),
				hora_termino:$("#txtHora_termino").val(),
				descanso:$("#txtDescanso").val()				
			}),
			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(){

		$.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/actualizarhorarioturno",
			data : JSON.stringify({
				id:$("#txtId").val(),
				id_turno:$("#txtTurno").val(),
				id_dia:$("#txtDia").val(),				
				hora_inicio:$("#txtHora_inicio").val(),
				hora_termino:$("#txtHora_termino").val(),
				descanso:$("#txtDescanso").val()				
			}),
			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 Editado Exitosamente", "POWERTIME");
				}
			}
		})
		
	});

	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/obtenerlistadohorarioturno",
			data : JSON.stringify({
				id_turno: $("#txtTurno").val()			
			}),				
			success: function(dados)
			{
				 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-sm btn-editar'>Editar</button>";
					  var boton2 = "<button type='button' class='btn btn-info btn-sm btn-eliminar'>Eliminar</button>";
					  $('#example').dataTable().fnAddData( [
						dados[i][j].dia,
						formatearHora(dados[i][j].hora_inicio),	
						formatearHora(dados[i][j].hora_termino),
						boton +" "+boton2 + "<input type='hidden' value ='"+dados[i][j].id+"' />"
					  ]);
					}
				}
			}
		});
	} 
	
	function formatearHora(hora){		
		if(hora != null){ hora = hora.replace('-', '');  hora =  hora.substring(0, 5); return hora; } else { return hora; }	
	}	

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

</body>
</html>
