<?php
if (!isset($_SESSION['nombre'])) {
  header("Location: login");
}else{
  if ($_SESSION['escritorio']==1) {
    require_once "./modelos/Escritorio.php";
    
    $consulta = new Escritorio();
    $rsptac = $consulta->totalcomprahoy();
    $regc=$rsptac->fetch_object();
    $totalc=$regc->total_compra;

    $rsptav = $consulta->totalventahoy();
    $regv=$rsptav->fetch_object();
    $totalv=$regv->total_venta;
    //obtener valores para cargar al grafico de barras
    $compras10 = $consulta->comprasultimos_10dias();
    $fechasc='';
    $totalesc='';
    while ($regfechac=$compras10->fetch_object()) {
      $fechasc=$fechasc.'"'.$regfechac->fecha.'",';
      $totalesc=$totalesc.$regfechac->total.',';
    }
    //quitamos la ultima coma
    $fechasc=substr($fechasc, 0, -1);
    $totalesc=substr($totalesc, 0,-1);
    //obtener valores para cargar al grafico de barras
    $ventas12 = $consulta->ventasultimos_12meses ();
    $fechasv='';
    $totalesv='';
    while ($regfechav=$ventas12->fetch_object()) {
      $fechasv=$fechasv.'"'.$regfechav->fecha.'",';
      $totalesv=$totalesv.$regfechav->total.',';
    }
    //quitamos la ultima coma
    $fechasv=substr($fechasv, 0, -1);
    $totalesv=substr($totalesv, 0,-1);
    ?>
    <!-- Main content -->
    <section class="content">

                    <!-- Default box -->
                    <div class="row">
                      <div class="col-md-12">
                    <div class="box">
              <div class="box-header with-border">
                <h1 class="box-title"><?php echo $_SESSION['nombre_empresa'] ?></h1>
                <div class="box-tools pull-right">
                  
                </div>
              </div>
              <!--box-header-->
              <!--centro-->
              <div class="panel-body">
              <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="small-box bg-aqua">
                  <div class="inner">
                    <h4 style="font-size: 17px;">
                      <strong>Bs. <?php echo $totalc; ?> </strong>
                    </h4>
                    <p>Compras</p>
                  </div>
                  <div class="icon">
                    <i class="ion ion-bag"></i>
                  </div>
                  <a href="ingreso" class="small-box-footer">Compras <i class="fa fa-arrow-circle-right"></i></a>
                </div>
              </div>
              <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <div class="small-box bg-green">
                  <div class="inner">
                    <h4 style="font-size: 17px;">
                      <strong>Bs. <?php echo $totalv; ?> </strong>
                    </h4>
                    <p>Ventas</p>
                  </div>
                  <div class="icon">
                    <i class="ion ion-bag"></i>
                  </div>
                  <a href="ventaboleto" class="small-box-footer">Ventas <i class="fa fa-arrow-circle-right"></i></a>
                </div>
              </div>
              </div>
              <div class="panel-body">
              <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                <div class="box box-primary">
                  <div class="box-header with-border">
                    Compras de los últimos 10 dias
                  </div>
                  <div class="box-body">
                    <canvas id="compras" width="400" height="300"></canvas>
                  </div>
                </div>
              </div>
              <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
                <div class="box box-primary">
                  <div class="box-header with-border">
                    Ventas de los últimos 12 meses
                  </div>
                  <div class="box-body">
                    <canvas id="ventas" width="400" height="300"></canvas>
                  </div>
                </div>
              </div>
              </div>
              <!--fin centro-->
                    </div>
                    </div>
                    </div>
                    <!-- /.box -->

    </section>
    <!-- /.content -->
  </div>
  <?php 
  }else{
  require 'vistas/contenidos/noacceso-view.php'; 
  }
  ?>
      </div>
        <!-- /.content-wrapper -->
        <!-- Main Footer -->
        <?php include "vistas/modulos/mainfooter.php";?>
    </div>
    <!-- ./wrapper -->
    <!-- REQUIRED JS SCRIPTS -->
    <?php include "vistas/modulos/script.php";?>
    
</body>
</html>
  <script>
      var ctx = document.getElementById("compras").getContext('2d');
      var compras = new Chart(ctx, {
          type: 'bar',
          data: {
              labels: [<?php echo $fechasc ?>],
              datasets: [{
                  label: '# Compras en Bs. de los últimos 10 dias',
                  data: [<?php echo $totalesc ?>],
                  backgroundColor: [
                      'rgba(255, 99, 132, 0.2)',
                      'rgba(54, 162, 235, 0.2)',
                      'rgba(255, 206, 86, 0.2)',
                      'rgba(75, 192, 192, 0.2)',
                      'rgba(153, 102, 255, 0.2)',
                      'rgba(255, 159, 64, 0.2)',
                      'rgba(255, 99, 132, 0.2)',
                      'rgba(54, 162, 235, 0.2)',
                      'rgba(255, 206, 86, 0.2)',
                      'rgba(75, 192, 192, 0.2)'
                  ],
                  borderColor: [
                      'rgba(255,99,132,1)',
                      'rgba(54, 162, 235, 1)',
                      'rgba(255, 206, 86, 1)',
                      'rgba(75, 192, 192, 1)',
                      'rgba(153, 102, 255, 1)',
                      'rgba(255, 159, 64, 1)',
                      'rgba(255,99,132,1)',
                      'rgba(54, 162, 235, 1)',
                      'rgba(255, 206, 86, 1)',
                      'rgba(75, 192, 192, 1)'
                  ],
                  borderWidth: 1
              }]
          },
          options: {
              scales: {
                  yAxes: [{
                      ticks: {
                          beginAtZero:true
                      }
                  }]
              }
          }
      });
      var ctx = document.getElementById("ventas").getContext('2d');
      var ventas = new Chart(ctx, {
          type: 'bar',
          data: {
              labels: [<?php echo $fechasv ?>],
              datasets: [{
                  label: '# Ventas en Bs. de los últimos 12 meses',
                  data: [<?php echo $totalesv ?>],
                  backgroundColor: [
                      'rgba(255, 99, 132, 0.2)',
                      'rgba(54, 162, 235, 0.2)',
                      'rgba(255, 206, 86, 0.2)',
                      'rgba(75, 192, 192, 0.2)',
                      'rgba(153, 102, 255, 0.2)',
                      'rgba(255, 159, 64, 0.2)',
                      'rgba(255, 99, 132, 0.2)',
                      'rgba(54, 162, 235, 0.2)',
                      'rgba(255, 206, 86, 0.2)',
                      'rgba(75, 192, 192, 0.2)',
                      'rgba(153, 102, 255, 0.2)',
                      'rgba(255, 159, 64, 0.2)'
                  ],
                  borderColor: [
                      'rgba(255,99,132,1)',
                      'rgba(54, 162, 235, 1)',
                      'rgba(255, 206, 86, 1)',
                      'rgba(75, 192, 192, 1)',
                      'rgba(153, 102, 255, 1)',
                      'rgba(255, 159, 64, 1)',
                      'rgba(255,99,132,1)',
                      'rgba(54, 162, 235, 1)',
                      'rgba(255, 206, 86, 1)',
                      'rgba(75, 192, 192, 1)',
                      'rgba(153, 102, 255, 1)',
                      'rgba(255, 159, 64, 1)'
                  ],
                  borderWidth: 1
              }]
          },
          options: {
              scales: {
                  yAxes: [{
                      ticks: {
                          beginAtZero:true
                      }
                  }]
              }
          }
      });
  </script>
<?php 
}
ob_end_flush();
?>

