<?php
  $Seccion    = 'Vehiculos';
  $Position   = '../';
  require_once($Position.'config/config.php');
  require_once($Position.'config/conexion.php');
  require_once($Position.'config/user.php');
  require_once($Position.'funciones/acces.php');
  require_once($Position.'funciones/logout.php');
  require_once($Position.'funciones/funciones.php');
  // require_once($Position.'funciones/extras.php');
  require_once($Position.'funciones/paginador1.php');

  //Borrar
  if( $_POST['Borrar'] == 'Ok' ){

    $Id = $_POST['Id'];
    $Query = "
      DELETE FROM PA_Vehiculos
      WHERE Id = '$Id'
    ";
    $mysqli->query($Query);

  }

  //Filtrar
  $Where = "
    WHERE con.Id != '0'
  ";
  if( $_GET[Filtrar] == 'Ok' ){

    //Variables
    $Filtrar     = 'Ok';
    $Bus = "&Filtrar=Ok";

    //Nombre
    $FilId = $_GET[Id];
    if( $FilId ){
      $Where .= "AND con.Vehiculo_Id LIKE '$FilId%' ";
      $Bus .= "&Id=".$FilId;
    }

    //Nombre
    $FilNom = $_GET[Nom];
    if( $FilNom ){
      $Where .= "AND cli.Nombre LIKE '%$FilNom%' ";
      $Bus .= "&Nom=".$FilNom;
    }

    //Cliente
    $FilCon = $_GET[Con];
    if( $FilCon ){
      $Where .= "AND con.Id = '$FilCon' ";
      $Bus .= "&Con=".$FilCon;
    }

    //Rfc
    $FilRfc      = $_GET[Rfc];
    if( $FilRfc ){
      $Where .= "AND cli.Rfc LIKE '%$FilRfc%' ";
      $Bus .= "&Rfc=".$FilRfc;
    }

    //Email
    $FilEmail    = $_GET[Email];
    if( $FilEmail ){
      $Where .= "AND cli.Email LIKE '%$FilEmail%' ";
      $Bus .= "&Email=".$FilEmail;
    }

    //Eje
    $FilEje    = $_GET[Eje];
    if( $FilEje ){
      $Where .= "AND cli.Fk_Administrador = '$FilEje' ";
      $Bus .= "&Eje=".$FilEje;
    }

    //Status
    $FilSta      = $_GET[Sta];
    if( $FilSta ){
      $Where .= "AND cli.Fk_ProspectoStatus = '$FilSta' ";
      $Bus .= '&Sta='.$FilSta;
    }

    // $FilPai      = $_POST[Pai];
    // $FilEst      = $_POST[Est];
    // if( $FilPai ){ $Where .= "AND cli.Fk_Pais LIKE '$FilPai' "; }
    // if( $FilEst ){ $Where .= "AND cli.Fk_Estado LIKE '$FilEst' "; }



  }

  if (isset($_GET['pgn'])){
    $pgn =  $_GET['pgn']; // $pg es la pagina actual
  }else{
    $pgn = 0;
  }
  $cantidad = 20; // cantidad de resultados por página
  $PagerLap = $cantidad / 10;
  $inicial  = $pgn * $cantidad;

  //Total Vehiculos
  $Query = "
    SELECT
      COUNT(con.Id) AS Total
    FROM PA_Vehiculos usr
    $Where
  ";
  $Result = mysqli_query($mysqli, $Query);
  $ArtTotal = mysqli_fetch_assoc($Result);
  $total_records = $ArtTotal[Total];
  $pages = intval($total_records / $cantidad);

  //Vehiculos
  $Query = "
    SELECT
      con.Id,
      con.Vehiculo_Id,
      con.TipoVehiculo, con.Placas,
      con.Activo
    FROM PA_Vehiculos con
    $Where
    ORDER BY con.TipoVehiculo ASC, con.Placas ASC
    LIMIT $inicial, $cantidad
  ";
  $Vehiculos = $mysqli->query($Query);

?>

<!DOCTYPE html>
<html lang="en">

<?php require_once($Position.'head.php'); ?>

<body class="fix-sidebar fix-header card-no-border">
    <div class="preloader">
        <svg class="circular" viewBox="25 25 50 50">
            <circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10" /> </svg>
    </div>
    <div id="main-wrapper">
      <?php require_once($Position.'header.php'); ?>
      <?php require_once($Position.'menu.php'); ?>
      <div class="page-wrapper">
        <div class="container-fluid">
          <div class="row page-titles">

            <!-- Titulo -->
            <div class="col-md-5 col-8 align-self-center">
              <h3 class="text-themecolor m-b-0 m-t-0">Vehiculos</h3>
              <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="javascript:void(0)">Lista de Vehiculos</a></li>
              </ol>
            </div>
            <!-- End Titulo -->

            <!-- Botones -->
            <div class="col-md-7 align-self-center text-right">
              <div class="btn-group" role="group" aria-label="Basic example">
                <?php //if( $Perms[2] ){ ?>
                  <a href="edit.php">
                    <button type="button" class="btn btn-sm btn-secondary">Nuevo<i class="fa fa-plus mar-l-10"></i></button>
                  </a>
                <?php //} ?>
              </div>
            </div>
            <!-- End Botones -->

          </div>
          <div class="row">
              <div class="col-12">

                <!-- Buscador -->
                <div id="Filtrar">

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

                          <!-- <div class="col-sm-12">
                            <h4>Filtar</h4>
                            <hr />
                          </div> -->

                          <!-- ID -->
                          <div class="col-sm-4">
                            <div class="form-group">
                              <label class="control-label">ID</label>
                              <input
                                name="Id"
                                type="text"
                                class="form-control form-control-sm"
                                autocomplete="off"
                                placeholder=""
                                value="<?php echo $FilId; ?>"
                              >
                            </div>
                          </div>
                          <!-- End ID -->

                          <!-- Vehiculos -->
                          <div class="col-sm-4">
                            <div class="form-group">
                              <label class="control-label">Vehiculos</label>
                              <div class="controls">
                                <select name="Con" class="form-control form-condtrol-sm chosen-select">
                                  <option value="">Todos</option>
                                  <?php
                                    //Vehiculos
                                    $Query = "
                                      SELECT
                                        con.Id,
                                        con.Vehiculo_Id,
                                        con.TipoVehiculo, con.Placas
                                      FROM PA_Vehiculos con
                                      ORDER BY con.TipoVehiculo ASC, con.Placas ASC
                                    ";
                                    $ConductorsFil = $mysqli->query($Query);
                                    mysqli_data_seek($ConductorsFil, 0);
                                    while($ConductorFil = mysqli_fetch_array($ConductorsFil)) {
                                  ?>
                                    <option
                                      <?php if( $ConductorFil['Id'] == $FilCon ){ echo 'selected="selected"'; } ?>
                                      value="<?php echo $ConductorFil['Id']; ?>"
                                    ><?php echo $ConductorFil['Nombre'].' '.$ConductorFil['Apellidos']; ?></option>
                                  <?php } ?>
                                </select>
                              </div>
                            </div>
                          </div>
                          <!-- End Vehiculos -->

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

                          <!-- Btn Descargar Excel --
                          <div class="col-sm-3">
                            <div class="form-group">
                              <label class="control-label">.</label>
                              <a href="excel.php?pgn=0<?php echo $Bus; ?>" target="_blank">
                                <button class="btn btn-secondary btn-block" type="button">
                                  Exportar Excel
                                  <i class="fa fa-file mar-l-5"></i>
                                </button>
                              </a>
                            </div>
                          </div>
                          <!-- End Btn Descargar Excel -->

                          <!-- Total --
                          <div class="col-sm-3">
                            <div class="form-group">
                              <label class="control-label">Vehiculos</label>
                              <input
                                readonly
                                type="text"
                                class="form-control form-control-sm"
                                autocomplete="off"
                                placeholder=""
                                value="<?php echo $total_records; ?>"
                              >
                            </div>
                          </div>
                          <!-- End Total -->

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

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

                <!-- Card -->
                <div class="card">
                  <div class="card-body">
                    <h4 class="card-title">Listado de Vehiculos</h4>

                    <!-- Table -->
                    <div class="table-responsive m-t-20">
                      <table id="example23" class="display nowrap table table-hover table-striped table-bordered" cellspacing="0" width="100%">
                        <thead>
                          <tr>
                            <th>Id</th>
                            <th>Tipo</th>
                            <th>Placas</th>
                            <th width="20"></th>
                            <th width="20"></th>
                          </tr>
                        </thead>
                        <tbody>
                            <?php
                              mysqli_data_seek($Vehiculos, 0);
                              while($Conductor = mysqli_fetch_array($Vehiculos)) {
                            ?>
                              <tr>
                                <!-- Id -->
                                <td width="10">
                                  <?php echo $Conductor['Vehiculo_Id'];?>
                                </td>
                                <!-- End Id -->

                                <!-- TipoVehiculo -->
                                <td>
                                  <div><?php echo $Conductor['TipoVehiculo']; ?></div>
                                </td>
                                <!-- End TipoVehiculo -->

                                <!-- Placas -->
                                <td>
                                  <div><?php echo $Conductor['Placas']; ?></div>
                                </td>
                                <!-- End Placas -->

                                <!-- Status --
                                <td>
                                  <span
                                    class="
                                      label
                                      <?php
                                        if( $Conductor[Activo] == '1' ){
                                          echo "label-success";
                                        }elseif( $Conductor[Activo] == '0' ){
                                          echo "label-warning";
                                        }
                                      ?>
                                    "
                                  >
                                    <?php
                                      if( $Conductor[Activo] == '1' ){
                                        echo "Activo";
                                      }elseif( $Conductor[Activo] == '0' ){
                                        echo "Inactivo";
                                      }
                                    ?>
                                  </span>
                                </td>
                                <!-- End Status -->

                                <!-- Editar Btn -->
                                <td>
                                  <?php //if( $Perms[3] ){ ?>
                                    <a href="edit.php?Id=<?php echo $Conductor['Id']; ?>">
                                      <button class="btn btn-secondary btn-sm" data-toggle="tooltip" data-placement="top" title="" data-original-title="Editar">
                                        Editar<i class="fa fa-pencil m-l-10"></i>
                                      </button>
                                    </a>
                                  <?php //} ?>
                                </td>
                                <!-- End Editar Btn -->

                                <!-- Btn Eliminar -->
                                <td width="20">
                                  <?php //if( $Perms[4] ){ ?>

                                    <!-- Btn Eliminar -->
                                    <a data-toggle="modal" href="#Borrar<?php echo $Conductor[Id];?>">
                                      <button class="btn btn-danger btn-sm ml-2" data-toggle="tooltip" data-placement="top"  title="Eliminar" data-original-title="Eliminar">
                                      <i class="fa fa-times"></i></button>
                                    </a>
                                    <!-- End Btn Eliminar -->

                                    <!--Modal Eliminar-->
                                    <form method="POST">
                                      <div id="Borrar<?php echo $Conductor[Id];?>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
                                        <div class="modal-dialog">
                                          <div class="modal-content">
                                            <div class="modal-header">
                                              <h4 class="modal-title">Eliminar</h4>
                                              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                            </div>
                                            <div class="modal-body p-4">
                                              <div class="row">
                                                <div class="col-md-12">
                                                  <p>¿Desea eliminar esté conductor?</p>
                                                  <p>
                                                    <?php echo $Conductor['Nombre'].' '.$Conductor['Apellidos'];?><br>
                                                    <?php echo $Conductor['Email']; ?><br>
                                                    <?php echo $Conductor['Celular']; ?>
                                                  </p>
                                                </div>
                                              </div>
                                            </div>
                                            <div class="modal-footer">
                                                <input name="Borrar" type="hidden" value="Ok">
                                                <input name="Id" type="hidden" value="<?php echo $Conductor[Id];?>">
                                                <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">Cancelar</button>
                                                <button type="submit" class="btn btn-danger waves-effect waves-light">Eliminar</button>
                                              </div>
                                          </div>
                                        </div>
                                      </div>
                                    </form>
                                    <!-- End Modal Eliminar-->
                                  <?php //} ?>
                                </td>
                                <!-- End Btn Eliminar -->

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

                    <!-- Paginador -->
                    <div class="m-t-40 text-right">
                      <!-- <button class="btn btn-secondary">< Primera</button> -->

                      <?php
                        $cantRegistros  = $total_records / $PagerLap;
                        $pgnActual      = $_GET['pgn'];
                        $pag            = new Paginador();
                        $datos          = $pag->paginar($pgnActual, $cantRegistros);
                        if ($datos) {
                          'Pagina: ' . ($_GET['pgn'] + 1) . ' de ' . $pag->getCantidadPaginas() . '<br />';
                      ?>
                        <?php
                          foreach( $datos as $enlace ) {
                        ?>
                          <a
                            href="?pgn=<?php
                              echo $enlace['numero'];
                              echo $Bus;
                            ?>"
                          >
                            <button type="button" class="btn btn-secondary"><?php echo $enlace['vista']; ?></button>
                          </a>
                        <?php } ?>
                      <?php } ?>

                    </div>
                    <!-- End Paginador -->

                  </div>
                </div>
                <!-- End Card -->
              </div>
            </div>
        </div>
        <?php //require_once($Position.'footer.php'); ?>
      </div>
    </div>
    <?php require_once($Position.'scripts.php'); ?>
    <script>

    </script>
    <script src="<?php echo $Position; ?>assets/plugins/styleswitcher/jQuery.style.switcher.js"></script>

</body>
</html>
