<?php
  $Seccion      = 'Clientes';
  $SubSeccion   = 'Clientes';
  $NameSec      = 'Catálogo de Clientes';
  $SecLine      = array('Facturación', 'Catálogo de Clientes');
  $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');

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

    $Id = addslashes(strip_tags( $_POST['Id'] ));
    //Elimina
    $Query = "DELETE FROM CatalogoClientes WHERE Id = '$Id'";
    $Result = mysqli_query($mysqli, $Query);

  }
  //End Borrar

  //Filtrar
  $Where = "WHERE cli.Id != '0' AND cli.Fk_Cuenta = '$Cuenta[Id]' ";
  if( $_POST[Filtrar] == 'Ok' ){
    //Variables
    $Seltrar     = 'Ok';
    $SelNom      = $_POST[Nom];
    $SelRfc      = $_POST[Rfc];
    $SelPai      = $_POST[Pai];
    $SelEst      = $_POST[Est];
    if( $SelNom ){ $Where .= "AND cli.Nombre LIKE '%$SelNom%' "; }
    if( $SelRfc ){ $Where .= "AND cli.Rfc LIKE '%$SelRfc%' "; }
    if( $SelPai ){ $Where .= "AND cli.Fk_Pais LIKE '$SelPai' "; }
    if( $SelEst ){ $Where .= "AND cli.Fk_Estado LIKE '$SelEst' "; }

  }

  // Catalogo Clientes
  $Query = "
    SELECT
      cli.Id, cli.Nombre, cli.Rfc, cli.Email
    FROM CatalogoClientes cli
    LEFT JOIN Estados est ON est.Id = cli.Fk_Estado
    LEFT JOIN Paises pai ON pai.Id = cli.Fk_Pais
    $Where
    GROUP BY cli.Id
    ORDER BY cli.Nombre DESC
  ";
  $Clis = $mysqli->query($Query);
?>

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

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

<body class="fix-sidebar fix-header card-no-border">
    <!-- ============================================================== -->
    <!-- Preloader - style you can find in spinners.css -->
    <!-- ============================================================== -->
    <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>
    <!-- ============================================================== -->
    <!-- Main wrapper - style you can find in pages.scss -->
    <!-- ============================================================== -->
    <div id="main-wrapper">
        <?php require_once($Position.'header.php'); ?>
        <?php require_once($Position.'menu.php'); ?>

        <!-- ============================================================== -->
        <!-- Page wrapper  -->
        <!-- ============================================================== -->
        <div class="page-wrapper">
            <!-- ============================================================== -->
            <!-- Container fluid  -->
            <!-- ============================================================== -->
            <div class="container-fluid">
                <!-- ============================================================== -->
                <!-- Bread crumb and right sidebar toggle -->
                <!-- ============================================================== -->
                <div class="row page-titles">

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

                    <!-- Botones -->
                    <div class="col-md-7 align-self-center text-right">
                      <div class="btn-group" role="group" aria-label="Basic example">
                        <!--<button type="button" class="btn btn-secondary BtnFiltrar">Filtrar <i class="fa fa-search m-l-5"></i></button>-->

                        <!-- Btn Nuevo -->
                        <button
                          type="button"
                          class="btn btn-sm btn-secondary"
                          onclick="window.location.href='edit.php'"
                        >
                          Nuevo
                          <i class="fa fa-plus m-l-5"></i>
                        </button>
                        <!-- End Btn Nuevo -->

                      </div>
                    </div>
                    <!-- End Botones -->

                </div>
                <!-- ============================================================== -->
                <!-- End Bread crumb and right sidebar toggle -->
                <!-- ============================================================== -->
                <!-- ============================================================== -->
                <!-- Page Content -->
                <!-- ============================================================== -->
                <div class="row">
                    <div class="col-12">

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

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

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

                                <!-- Nombre -->
                                <div class="col-sm-5">
                                  <div class="form-group">
                                    <label class="control-label">Nombre</label>
                                    <!-- Input Nombre -->
                                    <input
                                      name="Nom"
                                      type="text"
                                      class="form-control form-control-sm"
                                      autocomplete="off"
                                      placeholder=""
                                      value="<?php echo $SelNom; ?>"
                                    >
                                    <!-- End Input Nombre -->
                                  </div>
                                </div>
                                <!-- End Nombre -->

                                <!-- RFC -->
                                <div class="col-sm-3">
                                  <div class="form-group">
                                    <label class="control-label">RFC</label>
                                    <!-- Input Rfc -->
                                    <input
                                      name="Rfc"
                                      type="text"
                                      class="form-control form-control-sm"
                                      autocomplete="off"
                                      placeholder=""
                                      value="<?php echo $SelRfc; ?>"
                                    >
                                    <!-- End Input Nombre -->
                                  </div>
                                </div>
                                <!-- End RFC -->

                                <!-- 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 -->

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

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

                      <!-- Tabla -->
                      <div class="card">
                        <div class="card-body">
                          <h4 class="card-title"><?php echo $NameSec; ?></h4>
                          <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>Nombre / Razón Social</th>
                                    <th>RFC</th>
                                    <th>Email</th>
                                    <th style="width:50px;"></th>
                                    <th style="width:50px;"></th>
                                  </tr>
                                </thead>
                                <?php
                                  mysqli_data_seek($Clis, 0);
                                  while($Cli = mysqli_fetch_array($Clis)) {
                                ?>
                                  <tbody>
                                    <tr>
                                      <td><?php echo $Cli['Nombre']; ?></td>
                                      <td><?php echo $Cli['Rfc']; ?></td>
                                      <td><?php echo $Cli['Email']; ?></td>
                                      <td>
                                        <!-- Btn Editar -->
                                        <button
                                          onclick="window.location.href='edit.php?Id=<?php echo $Cli['Id']; ?>'"
                                          class="btn btn-success btn-sm"
                                          data-toggle="tooltip"
                                          data-placement="top"
                                          title=""
                                          data-original-title="Editar"
                                        >
                                          <i class="fa fa-pencil"></i>
                                        </button>
                                        <!-- End Btn Editar -->
                                      </td>
                                      <td>

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

                                        <!-- Modal Borrar -->
                                        <form method="post">
                                          <div id="DelCat<?php echo $Cli['Id']; ?>" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                                            <div class="modal-dialog">
                                              <div class="modal-content">
                                                <div class="modal-header">
                                                  <h4 class="modal-title" id="myModalLabel">Eliminar</h4>
                                                  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                                </div>
                                                <div class="modal-body">
                                                  <h5>¿Estás seguro de eliminar este cliente?</h5>
                                                  <p>
                                                    <?php echo $Cli[Nombre]; ?><br />
                                                    <?php echo $Cli[Rfc]; ?>
                                                  </p>
                                                </div>
                                                <div class="modal-footer">
                                                  <button type="submit" class="btn btn-danger">Eliminar</button>
                                                  <button type="button" class="btn btn-secondary waves-effect" data-dismiss="modal">No</button>
                                                  <input type="hidden" name="Borrar" value="Ok">
                                                  <input type="hidden" name="Tipo" value="1">
                                                  <input type="hidden" name="Id" value="<?php echo $Cli['Id']; ?>">
                                                </div>
                                              </div>
                                              <!-- /.modal-content -->
                                            </div>
                                            <!-- /.modal-dialog -->

                                          </div>
                                        </form>
                                        <!-- End Modal Borrar -->

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

                    </div>
                </div>
                <!-- ============================================================== -->
                <!-- End Page Content -->
                <!-- ============================================================== -->
            </div>
            <!-- ============================================================== -->
            <!-- End Container fluid  -->
            <!-- ============================================================== -->

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

        </div>
        <!-- ============================================================== -->
        <!-- End Page wrapper  -->
        <!-- ============================================================== -->
    </div>

    <?php require_once($Position.'scripts.php'); ?>
    <script>
    $(document).ready(function() {

        //Ver Subcategorias
        $('.Btn-Subs').click(function(){
     	   //alert($(this).val());
     	   $("#SubsOpcs" + $(this).val()).toggle();
        });

        $('#myTable').DataTable();
        $(document).ready(function() {
            var table = $('#example').DataTable({
                "columnDefs": [{
                    "visible": false,
                    "targets": 2
                }],
                "order": [
                    [2, 'asc']
                ],
                "displayLength": 25,
                "drawCallback": function(settings) {
                    var api = this.api();
                    var rows = api.rows({
                        page: 'current'
                    }).nodes();
                    var last = null;
                    api.column(2, {
                        page: 'current'
                    }).data().each(function(group, i) {
                        if (last !== group) {
                            $(rows).eq(i).before('<tr class="group"><td colspan="5">' + group + '</td></tr>');
                            last = group;
                        }
                    });
                }
            });
            // Order by the grouping
            $('#example tbody').on('click', 'tr.group', function() {
                var currentOrder = table.order()[0];
                if (currentOrder[0] === 2 && currentOrder[1] === 'asc') {
                    table.order([2, 'desc']).draw();
                } else {
                    table.order([2, 'asc']).draw();
                }
            });
        });
    });
    $('#example23').DataTable({
        dom: 'Bfrtip',
        searching: false,
        /*buttons: [
            'copy', 'csv', 'excel', 'pdf', 'print'
        ]*/
    });
    </script>

</body>

</html>
