<?php
  $seccion      = 'Trivias';
  $position     = '../../';

  require_once($position.'../switch/access.php');
  require_once($position.'assets/global.php');
  require_once($position.'assets/session.php');
  require_once($position.'assets/funciones.php');

  //Buscador
  $Where = "
    WHERE
      tri.id != '0'
  ";

  if( $_GET['buscar'] == 'Ok' ){

    //Nombre
    if( $_GET['nombre'] ){
      $nom = $_GET['nombre'];
      $Where .= "
        AND tri.nombre LIKE '$nom%'
      ";
    }

    //Fecha Inicio
    if( $_GET['fechainicio'] && $_GET['fechalimite'] ){
      $fei = $_GET['fechainicio'];
      $Where .= "
        AND tri.fechainicio BETWEEN '$fei' AND '$fel'
      ";
    }



  }

  //Query's
  $qrTrivias = "
    SELECT
      tri.id,
      tri.fechainicio, tri.fechalimite,
      tri.puntos,
      tri.nombre, tri.activo,
      usrtip.nombre AS tipo_usr,
      cli.nombre AS cliente
    FROM trivias tri
    LEFT JOIN clientes cli ON cli.Id = tri.fk_cliente
    LEFT JOIN usuarios_tipos usrtip ON usrtip.Id = tri.fk_usuario_tipo
    $Where
      ORDER BY tri.fechainicio ASC
  ";
  $tris = mysqli_query($mysqli, $qrTrivias);

?>
<!DOCTYPE html>
<html lang="es_MX">
  <?php require_once($position.'head.php'); ?>

  <body class="loading" data-layout='{"mode": "light", "width": "fluid", "menuPosition": "fixed", "sidebar": { "color": "dark", "size": "default", "showuser": false}, "topbar": {"color": "light"}, "showRightSidebarOnPageLoad": false}'>

    <!-- Pagina de inicio -->
    <div id="wrapper">

      <!-- ========== Inicio nav-izq ========== -->
      <?php include_once($position.'assets/nav.php'); ?>
      <!-- Fin nav-izq -->

      <!-- ============================================================== -->
      <!-- Incio de Contenido -->
      <!-- ============================================================== -->

      <div class="content-page">
        <div class="content">

          <!-- Topbar -->
          <?php include_once($position.'assets/topbar.php'); ?>
          <!-- /Topbar -->

            <!-- Content-->
            <div class="container-fluid">

              <!-- page title -->
              <div class="row">
                <div class="col-12">
                  <div class="page-title-box">
                    <h4 class="page-title"><?php echo $seccion; ?></h4>
                    <div class="page-title-right">
                      <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="<?php echo $dir; ?>inicio/">Inicio</a></li>
                        <li class="breadcrumb-item active"><?php echo $seccion; ?></li>
                      </ol>
                    </div>
                  </div>
                </div>
              </div>
              <!-- /page title -->

              <!-- Botones -->
  						<div class="row">
  							<div class="col-12 text-right">
  								<button type="button" class="btn btn-sp btn-sm" onclick="window.location.href='trivia'">
                    <i class="fe-plus"></i>&nbsp; Nueva Trivia</button>
  							</div>
  							<div class="col-12" style="height: 5px;">&nbsp;</div>
  						</div>
              <!-- End Botones -->

  						<!-- Trivias -->
              <div class="row">
                <div class="col-12">
                  <div class="card">
                      <div class="card-body">
                        <h4 class="header-title">Lista de <?php echo $seccion; ?></h4>
                        <p class="text-muted font-13 mb-4">
                          Trivias
                        </p>

                        <!-- Tabla -->
                        <table id="" class="table dt-responsive nowrap w-100">
                          <thead>
                            <tr>
                              <th>Cliente</th>
                              <th>Nombre de trivia</th>
                              <th>Disigido a</th>
                              <th>Fechas</th>
                              <th>Respuestas</th>
                              <th style="width:20px;"></th>
                              <th style="width:100px;"></th>
                              <th style="width:20px;"></th>
                            </tr>
                          </thead>
                          <tbody>
						              <?php
                            mysqli_data_seek($tris, 0);
                            while($tri = mysqli_fetch_array($tris)) {
                          ?>
                            <tr>

                              <!-- cliente -->
                              <td>
                                <?php echo $tri['cliente']; ?>
                              </td>
                              <!-- End cliente -->

                              <!-- Nombre -->
                              <td>
                                <div>
                                  <?php echo $tri['nombre']; ?>
                                </div>
                                <div>
                                  Puntos: <?php echo $tri['puntos']; ?>
                                </div>
                              </td>
                              <!-- End Nombre -->

                              <!-- Dirigido -->
                              <td>
                                <i class="fa fa-user text-success m-r-5"></i>
                                <?php echo $tri['tipo_usr']; ?>
                              </td>
                              <!-- End Dirigido -->

                              <!-- Fechas -->
                              <td>
                                <div>
                                  <i class="fa fa-calendar text-success"></i>
                                  <?php echo cambio_fecha_numerica_texto($tri['fechainicio']); ?>
                                </div>
                                <div>
                                  <i class="fa fa-calendar text-danger"></i>
                                  <?php echo cambio_fecha_numerica_texto($tri['fechalimite']); ?>
                                </div>
                              </td>
                              <!-- End Fechas -->

                              <!-- Respuestas -->
                              <td>
                                <?php
                                  //Cantidad de Respuestas
                                  $query = "
                              			SELECT
                              				COUNT(res.id) AS total
                              			FROM trivias_respuestas res
                              			WHERE
                                      res.fk_trivia = '$tri[id]'
                              		";
                                  $result  = mysqli_query($mysqli, $query);
                                  $resp   = mysqli_fetch_assoc($result);
                                  if( $resp[total] >= 1 ){
                                ?>
                                  <a href="trivia/respuestas?id=<?php echo $tri[id]; ?>">
                                    <button class="btn btn-outline-secondary btn-xs">
                                      <i class="fa fa-file m-r-5"></i>
                                      Ver Respuestas
                                      (<?php echo $resp[total]; ?>)
                                    </button>
                                  </a>
                                <?php } ?>
                              </td>
                              <!-- End Respuestas -->

                              <!-- Btn Activo-->
                              <td>
    														<?php if ($tri['activo'] == 1){$btnClass = "btn-success"; $btnStatus = "Activo";}else{$btnClass = "btn-primary"; $btnStatus = "Inactivo";}?>
    														<button class="btn <?php echo $btnClass ?> btn-xs"><?php echo $btnStatus ?></button>
                              </td>
                              <!-- End Btn Activo-->

                              <!-- Btn Editar -->
                              <td>
                                <a class="btn  btn-outline-secondary waves-effect btn-xs" href="trivia?id=<?php echo $tri['id'];?>">
                                  <i class="fe-edit-1"></i>
                                  Editar
                                </a>
                              </td>
                              <!-- End Btn Editar -->

                              <!-- Btn Borrar -->
                              <td>
                                <a class="btn btn-primary waves-effect waves-light btn-xs" href="#?id=<?php echo $tri['id'];?>">
                                  <i class="fa fa-times"></i>
                                </a>
                              </td>
                              <!-- End Btn Borrar -->

                            </tr>
						               <?php } ?>
                        </tbody>
                      </table>
                      <!-- End Tabla -->

                    </div> <!-- end card body-->
                  </div> <!-- end card -->
                </div><!-- end col-->
              </div>
              <!-- End Trivias -->

            </div> <!-- /container -->

        </div> <!-- /content -->

        <!-- Footer -->
        <?php include_once($position.'assets/footer.php'); ?>
        <!-- /Footer -->

      </div>

      <!-- ============================================================== -->
      <!-- Fin de Contenido -->
      <!-- ============================================================== -->


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

    <?php require_once($position.'scripts.php') ?>

  </body>

</html>
