<?php
session_start();
if(!isset($_SESSION['username'])){
  echo "<script> location.href='login.php' </script>";
  exit;
}

include "config/database.php";
include "inc/header.php";
include "inc/navbar.php";
include "inc/sidebar.php";
include "inc/alerts.php";

// ROUTING
if (isset($_GET['page'])) {
    $page = $_GET['page'];
    $file = "page/" . $page . ".php";

    if (file_exists($file)) {
        include $file;
    } else {
        include "page/dashboard.php";
    }
} else {
    include "page/dashboard.php";
}

include "inc/footer.php";
?>

</div>
<!-- ./wrapper -->

<!-- REQUIRED SCRIPTS -->

<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>

<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- DataTables & Plugins -->
<script src="plugins/datatables/jquery.dataTables.min.js"></script>
<script src="plugins/datatables-bs4/js/dataTables.bootstrap4.min.js"></script>
<script src="plugins/datatables-responsive/js/dataTables.responsive.min.js"></script>
<script src="plugins/datatables-responsive/js/responsive.bootstrap4.min.js"></script>
<script src="plugins/datatables-buttons/js/dataTables.buttons.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.bootstrap4.min.js"></script>
<script src="plugins/jszip/jszip.min.js"></script>
<script src="plugins/pdfmake/pdfmake.min.js"></script>
<script src="plugins/pdfmake/vfs_fonts.js"></script>
<script src="plugins/datatables-buttons/js/buttons.html5.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.print.min.js"></script>
<script src="plugins/datatables-buttons/js/buttons.colVis.min.js"></script>

<!-- ChartJS -->
<script src="plugins/chart.js/Chart.min.js"></script>

<!-- AdminLTE App -->
<script src="dist/js/adminlte.min.js"></script>

<!-- ✅ DATA TABLE (FIX: NO DOUBLE INIT) -->
<script>
  $(function () {

    if (!$.fn.DataTable.isDataTable("#example1")) {
      $("#example1").DataTable({
        responsive: true,
        lengthChange: false,
        autoWidth: false,
        buttons: ["copy", "csv", "excel", "pdf", "print", "colvis"]
      }).buttons().container().appendTo('#example1_wrapper .col-md-6:eq(0)');
    }

  });
</script>

<!-- CHART SUHU -->
<script>
$(function () {

  var areaChartCanvas = $('#areaChart').get(0)?.getContext('2d');
  if (!areaChartCanvas) return;

  new Chart(areaChartCanvas, {
    type: 'line',
    data: {
      labels  : ['18.00', '19.00', '20.00', '21.00', '22.00', '23.00', '24.00'],
      datasets: [{
        label               : 'Suhu',
        backgroundColor     : 'rgba(255,193,7,0.5)',
        borderColor         : 'rgba(255,193,7,1)',
        pointRadius         : false,
        data                : [36, 37, 38, 39, 37, 36, 38]
      }]
    },
    options: {
      maintainAspectRatio : false,
      responsive : true,
      legend: { display: false }
    }
  });

});
</script>

<!-- CHART HEART RATE -->
<!-- CHART SUHU -->
<script>
$(function () {

  var suhuChartCanvas = $('#areaChart').get(0);

  if (!suhuChartCanvas) return;

  const suhuLabels = [];
  const suhuData = [];

  window.suhuRealtimeChart = new Chart(suhuChartCanvas, {
    type: 'line',

    data: {
      labels: suhuLabels,

      datasets: [{
        label: 'Suhu Tubuh',

        backgroundColor: 'rgba(255,193,7,0.3)',
        borderColor: 'rgba(255,193,7,1)',

        pointRadius: 3,
        pointBackgroundColor: '#ffc107',

        borderWidth: 2,
        fill: true,
        tension: 0.4,

        data: suhuData
      }]
    },

    options: {
      maintainAspectRatio: false,
      responsive: true,

      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: false
          }
        }]
      },

      legend: {
        display: true
      }
    }
  });

});
</script>

<!-- CHART HEART RATE -->
<script>
$(function () {

  var heartChartCanvas = $('#heartChart').get(0);

  if (!heartChartCanvas) return;

  const heartLabels = [];
  const heartData = [];

  window.heartRealtimeChart = new Chart(heartChartCanvas, {
    type: 'line',

    data: {
      labels: heartLabels,

      datasets: [{
        label: 'Heart Rate',

        backgroundColor: 'rgba(220,53,69,0.3)',
        borderColor: 'rgba(220,53,69,1)',

        pointRadius: 3,
        pointBackgroundColor: '#dc3545',

        borderWidth: 2,
        fill: true,
        tension: 0.4,

        data: heartData
      }]
    },

    options: {
      maintainAspectRatio: false,
      responsive: true,

      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: false
          }
        }]
      },

      legend: {
        display: true
      }
    }
  });

});
</script>
</body>
</html>