<?php
  $seccion      = 'Premios';
  $position     = '../../';
  $UrlImg = "sandbox/switchpoint-app/premios";

  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
      pre.id != 0
  ";

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

    //Cliente
    if( $_GET['cliente'] ){
      $cli = $_GET['cliente'];
      $Where .= "
        AND pre.fk_cliente = '$cli'
      ";
    }

    //Evento
    if( $_GET['evento'] ){
      $eve = $_GET['evento'];
      $Where .= "
        AND pre.fk_evento = '$eve'
      ";
    }

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

  }

  //Premios
  $Query = "
    SELECT
      pre.id,
      pre.nombre,
      pre.existencia, pre.disponible, pre.disponible,
      pre.puntos,
      pre.activo,
      eve.nombre AS evento,
      eve.fecha_inicio, eve.fecha_limite
    FROM eventos_premios pre
    LEFT JOIN eventos eve ON eve.id = pre.fk_evento
    LEFT JOIN clientes cli ON cli.id = eve.fk_cliente
    $Where
      ORDER BY pre.nombre ASC
  ";
  $Prems = mysqli_query($mysqli, $Query);

?>
<!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='premio'">
                    <i class="fe-plus"></i>&nbsp; Nuevo Premio</button>
  							</div>
  							<div class="col-12" style="height: 5px;">&nbsp;</div>
  						</div>

              <div class="row">

                <!-- Buscador -->
                <div class="col-12">

                  <div class="card">
                    <div class="card-body">
                      <form method="get">
                        <div class="row">

                          <!-- Cliente -->
                          <div class="col-sm-3">
                            <div class="form-group">
                              <label class="control-label">Cliente</label>
                              <select name="cliente" class="Cliente form-control chosen-select">
                                <option></option>
                                <?php
                                  //Clientees
                                  $Query = "
                                    SELECT
                                      cli.id,
                                      cli.nombre
                                    FROM clientes cli
                                    ORDER BY cli.nombre ASC
                                  ";
                                  $Clis = $mysqli->query($Query);
                                  mysqli_data_seek($Clis, 0);
                                  while($Cli = mysqli_fetch_array($Clis)) {
                                ?>
                                  <option
                                    <?php
                                      if( $Cli['id'] == $cli ){
                                        echo 'selected="selected"';
                                      }
                                    ?>
                                    value="<?php echo $Cli['id']; ?>"
                                  >
                                  <?php echo $Cli['nombre']; ?>
                                  </option>
                                <?php } ?>
                              </select>
                            </div>
                          </div>
                          <!-- End Cliente -->

                          <!-- Evento -->
                          <div class="col-sm-3">
                            <div class="form-group">
                              <label class="control-label">Evento</label>
                              <select name="evento" class="Evento form-control chosen-select">
                                <option></option>
                                <?php
                                  //Eventos
                                  $Query = "
                                    SELECT
                                      eve.id,
                                      eve.nombre,
                                      eve.fecha_inicio,
                                      eve.fecha_limite
                                    FROM eventos eve
                                    WHERE
                                      eve.fk_cliente = '$cli'
                                    ORDER BY eve.nombre ASC
                                  ";
                                  $Eventos = $mysqli->query($Query);
                                  mysqli_data_seek($ClEventosis, 0);
                                  while($Eve = mysqli_fetch_array($Eventos)) {
                                ?>
                                  <option
                                    <?php
                                      if( $Eve['id'] == $eve ){
                                        echo 'selected="selected"';
                                      }
                                    ?>
                                    value="<?php echo $Eve['id']; ?>"
                                  >
                                  <?php echo $Eve['nombre']; ?>
                                  (
                                  <?php echo $Eve['fecha_inicio']; ?>
                                  -
                                  <?php echo $Eve['fecha_limite']; ?>
                                  )
                                  </option>
                                <?php } ?>
                              </select>
                            </div>
                          </div>
                          <!-- End Evento -->

                          <!-- Nombre -->
                          <div class="col-sm-3">
                            <div class="form-group">
                              <label class="control-label">Nombre</label>
                              <input name="nom" value="<?php echo $fil_nom; ?>" type="text"
                                class="form-control form-control-sm"
                              >
                            </div>
                          </div>
                          <!-- End Nombre -->

                          <!-- Filtrar -->
                          <div class="col-sm-3">
                            <div class="form-group">
                              <label class="control-label">.</label>
                              <button class="btn btn-success btn-sm btn-block" type="submit" name="button">Filtrar <i class="fa fa-search mar-l-5"></i></button>
                              <input type="hidden" name="buscar" value="Ok" />
                            </div>
                          </div>
                          <!-- End Filtrar -->

                        </div>
                      </form>
                    </div>
                  </div>

                </div>
                <!-- End Buscador -->

              </div>

  						<!-- usuarios -->
              <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">
                          Promociones
                        </p>

                        <!-- Tabla -->
                        <table id="" class="table dt-responsive nowrap w-100">
                          <thead>
                            <tr>
                              <th>Img</th>
                              <th>Nombre</th>
                              <th>Existencia</th>
                              <th>Puntos</th>
                              <th>Evento</th>
                              <th>Estatus</th>
    													<th></th>
                              <th></th>
                            </tr>
                          </thead>
                          <tbody>
						              <?php
                            mysqli_data_seek($Prems, 0);
                            while($Prem = mysqli_fetch_array($Prems)) {
                          ?>
                            <tr>

                              <!-- Img -->
                              <td>
                                <?php
																	$Imageirm = $RootSite.'/'.$UrlImg.'/'.$Prem[id].'.jpg';
																	//if (file_exists($ImageFirm)) {
																?>
																	<img
																		class="ImgLogo img-responsive"
																		style="max-width: 150px;"
																		src="<?php echo $position; ?>../premios/<?php echo $Prem[id]; ?>.jpg?<?php echo $Timer = strtotime("now"); ?>"
																	/>
																<?php //} ?>
                              </td>
                              <!-- End Img -->

                              <!-- Nombre -->
                              <td>
                                <?php echo $Prem['nombre']; ?>
                              </td>
                              <!-- End Nombre -->

                              <!-- existencia -->
                              <td>
                                <div>
                                  Existencia:
                                  <?php echo $Prem['existencia']; ?>
                                </div>
                                <div>
                                  Disponible:
                                  <?php echo $Prem['disponible']; ?>
                                </div>
                                <div>
                                  Entregados:
                                  <?php echo $Prem['entregados']; ?>
                                </div>
                              </td>
                              <!-- End existencia -->

                              <!-- Puntos -->
                              <td>
                                <?php echo number_format($Prem['puntos'],0,'.',','); ?>
                              </td>
                              <!-- End Puntos -->

                              <!-- Evento -->
                              <td>
                                <div>
                                  <?php echo $Prem['evento']; ?>
                                </div>
                                <div>
                                  <i class="fa fa-calendar text-success m-r-5"></i>
                                  <?php echo cambio_fecha_numerica_texto($Prem['fecha_inicio']); ?>
                                </div>
                                <div>
                                  <i class="fa fa-calendar text-danger m-r-5"></i>
                                  <?php echo cambio_fecha_numerica_texto($Prem['fecha_limite']); ?>
                                </div>
                              </td>
                              <!-- End Evento -->

                              <!-- Btn Activo-->
                              <td>
    														<?php if ($Prem['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="premio?id=<?php echo $Prem['id'];?>">
                                  <i class="fe-edit-1"></i>
                                  Editar
                                </a>
                              </td>
                              <!-- End Btn Editar -->

                              <!-- Btn Borrar -->
                              <td>
                                <a class="btn btn-danger waves-effect waves-light btn-xs" href="#?id=<?php echo $Prem['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>
              <!-- /secciones -->

          </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') ?>

    <script>
      //Cliente-Evento
      $(document.body).on('change', '.Cliente' ,function(){
        val 	= $(this).val();
        console.log('test: '+val);
        Rute 	= "premio/eventos.php";
        $.post(Rute, { val: val }, function(data){
          $('.Evento').html(data);
          // $(".chosen-select").chosen();
          // $('.chosen-select').trigger("chosen:updated");
          // $('#FormPer').parsley();
        });
      });
      //End Cliente-Evento
    </script>

  </body>
</html>
