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

/*
error_reporting(E_ALL);
ini_set('display_errors', '1');

	$user = "program1_pow_tim";
	$pass = "Sw7+mgcphfYkp9-";

    $pdo = new PDO("mysql:host=127.0.0.1;dbname=program1_powertime;charset=utf8", $user, $pass);
     # Para que genere excepciones a la hora de reportar errores.
    $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

	$sql = "SELECT count(rut) FROM 
					personas p 
					INNER JOIN persona_turnos pt ON p.id = pt.id_persona
					INNER JOIN horario_turnos ht ON pt.id_turno = ht.id_turno
					WHERE DAYOFWEEK('2019-12-19') = ht.id_dia
					AND ht.hora_inicio >= '00:00:00'
					AND ht.hora_inicio <= '10:00:00'";
	$sth = $pdo->prepare($sql);
	$sth->execute();
	$rsd = $sth->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);
	$todos=$rsd[0]*1;

	$sql = "SELECT COUNT(DISTINCT(m.rut))
					FROM marcajes m
					INNER JOIN personas p ON m.rut = p.rut
					INNER JOIN persona_turnos pt ON p.id = pt.id_persona
					INNER JOIN horario_turnos ht ON pt.id_turno = ht.id_turno
					WHERE m.fecha = '2019-12-19' AND DAYOFWEEK('2019-12-19') = ht.id_dia
					AND m.hora > ht.hora_inicio AND m.hora <= '10:00:00'
					
					AND m.rut IN (
					SELECT rut FROM 
					personas p 
					INNER JOIN persona_turnos pt ON p.id = pt.id_persona
					INNER JOIN horario_turnos ht ON pt.id_turno = ht.id_turno
					WHERE DAYOFWEEK('2019-12-19') = ht.id_dia
					AND ht.hora_inicio >= '00:00:00'
					AND ht.hora_inicio <= '10:00:00'
					)";
	$sth2 = $pdo->prepare($sql);
	$sth2->execute();
	$rsd2 = $sth2->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);
	$atrasados=$rsd2[0]*1;

	$sql = "SELECT COUNT(DISTINCT(m.rut))
					FROM marcajes m
					INNER JOIN personas p ON m.rut = p.rut
					INNER JOIN persona_turnos pt ON p.id = pt.id_persona
					INNER JOIN horario_turnos ht ON pt.id_turno = ht.id_turno
					WHERE m.fecha = '2019-12-19' AND DAYOFWEEK('2019-12-19') = ht.id_dia
					AND m.hora <= ht.hora_inicio AND m.hora <= '10:00:00'
					
					AND m.rut IN (
					SELECT rut FROM 
					personas p 
					INNER JOIN persona_turnos pt ON p.id = pt.id_persona
					INNER JOIN horario_turnos ht ON pt.id_turno = ht.id_turno
					WHERE DAYOFWEEK('2019-12-19') = ht.id_dia
					AND ht.hora_inicio >= '00:00:00'
					AND ht.hora_inicio <= '10:00:00'
					)";
	$sth3 = $pdo->prepare($sql);
	$sth3->execute();
	$rsd3 = $sth3->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT);
	$enhora=$rsd3[0]*1;
	
	$ausentes = $todos - $atrasados - $enhora;
	*/
	
$enhora = 0;
$atrasados = 0;  
$ausentes = 0; 	

?>	
<!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">  
  <style>
	  #example{
		  font-size:10px;
	  }
     .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 mb-2">
        </div>
      </div><!-- /.container-fluid -->
    </section>

    <section class="content">
        <div class="row">
            <div class="col-md-12">		
				<div class="card">
					<div class="card-header">
						<img src="../img/002.png" />
					</div>
					<div class="card-body">
					    <div class="row">
							<div class="col-md-6">	
								<div id="canvas-holder">
									<canvas id="chart-area"></canvas>
								</div>								
							</div>
							<div class="col-md-6">	
							  <table id="example" class="table table-bordered table-striped">
								<thead>
								<tr>			
								  <th>SUCURSAL</th>	
								  <th>PERSONA</th>		
								  <th>HORA</th>		
								  <th>HORA</th>										  
								</tr> 
								</thead>			
							  </table>							
							</div>							
						</div>			  
					</div>
					<!-- /.card-body -->
					<div class="card-footer">
					</div>
				</div>	
            </div>				
        </div>
    </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/chart.js/Chart.min.js"></script>
<script>

  $(function () { 
    $('#example').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": true,
      "ordering": true,
      "info": true,
      "autoWidth": false,
	  "language": {
		"url": "//cdn.datatables.net/plug-ins/1.10.15/i18n/Spanish.json"
      } 		  
    });
	
    var config = {
			type: 'pie',
			data: {
				datasets: [{
					data: [
						<?php echo $enhora;?>,
						<?php echo $atrasados ;?>,
						<?php echo $ausentes ;?>
					],
					backgroundColor: ['#008000','#ffff00','#ff0000'],
					label: 'Dataset 1'
				}],
				labels: [
					'En hora',
					'Atrasados',
					'Ausentes'
				]
			},
			options: {
				responsive: true
			}
		};	
	
		var canvas = document.getElementById("chart-area");
		var ctx = canvas.getContext("2d");
		var myNewChart = new Chart(ctx, config);
		
		canvas.onclick = function(evt) {
			var activePoints = myNewChart.getElementsAtEvent(evt);
			if (activePoints[0]) {
			var chartData = activePoints[0]['_chart'].config.data;
			var idx = activePoints[0]['_index'];
			
			var label = chartData.labels[idx];
			var value = chartData.datasets[0].data[idx];
			listadoPersona(label);
			}
		};

	function listadoPersona(label){
		var urlMarcaje = "http://200.24.13.60/~program1/power-time-api/public/graficopresente";
		if(label == "Atrasados") { urlMarcaje = "http://200.24.13.60/~program1/power-time-api/public/graficoatrasado"; }
		if(label == "Ausentes") { urlMarcaje = "http://200.24.13.60/~program1/power-time-api/public/graficoausente"; }	
	

		$('#example').dataTable().fnClearTable();

		$.ajax({
			type:'POST',
			contentType: 'application/json',
			dataType: "json",
            crossDomain: true,
			headers: {
				'Content-Type': 'application/json'
			},
			url: urlMarcaje,		
			success: function(dados)
			{
				 for (i=0;i<dados.length;i++){
					for (j=0;j<dados[i].length;j++){
												
					  $('#example').dataTable().fnAddData( [						  
						dados[i][j].sucursal,	
						dados[i][j].nombre +" "+dados[i][j].apellido,	
						dados[i][j].hora,	
						dados[i][j].hora_inicio,							
					  ]);
					}
				}
			}
		});		
	}
	
});

 
</script>

</body>
</html>