<?php
  $Seccion      = 'Desarrollos';
  $SubSeccion   = 'Desarrollos';
  $NameSec      = 'Propiedades';
  $Position     = '../';
  $Pagina       = '2';
  $SecLine      = array('Propiedades');
  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' ){
    $Id = $_POST['Id'];
    $Query = "
      SELECT
        des.Id
      FROM Desarrollos des
      WHERE des.Id = '$Id'
    ";
    $Result = mysqli_query($mysqli, $Query);
    $Des = mysqli_fetch_assoc($Result);
    $Image = $RootSite.'/images/desarrollos/'.$Id.'.jpg';
    unlink($RootSite."/images/desarrollos/".$Id.".jpg");
    //print_r(error_get_last());
    $mysqli->query("DELETE FROM Desarrollos WHERE Id = '$Id'");
  }

  //Guardar Orden
  if( $_POST['SaveOrder'] == 'Ok' ){
    $IdItems = $_POST['IdItems'];
    $Cont = 0;
    foreach ($IdItems as $IdItem){
      $Cont = $Cont + 1;
      $Query = "
        UPDATE Desarrollos
        SET
          Orden = '$Cont'
        WHERE Id = '$IdItem'
      ";
      $Result = mysqli_query($mysqli, $Query);
    }
    $Saved = 'Ok';
  }
  //End Guardar Orden

?>

<!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">
              <form method="post" enctype="multipart/form-data">
                <!-- ============================================================== -->
                <!-- Bread crumb and right sidebar toggle -->
                <!-- ============================================================== -->
                <div class="row page-titles page-titles-fixed">

                    <?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</button>-->
                        <!-- <button
                          type="button"
                          class="btn btn-sm btn-secondary"
                          onclick="window.location.href='item'"
                        >
                          Nuevo
                          <i class="fa fa-plus m-l-5"></i>
                        </button> -->
                        <!-- <button type="submit" class="btn btn-sm btn-secondary">Guardar Orden <i class="fa fa-sort m-l-5"></i></button> -->
                        <input type="hidden" name="SaveOrder" value="Ok" />
                      </div>
                    </div>
                    <!-- End Botones -->

                </div>
                <!-- ============================================================== -->
                <!-- End Bread crumb and right sidebar toggle -->
                <!-- ============================================================== -->
                <!-- ============================================================== -->
                <!-- Page Content -->
                <!-- ============================================================== -->
                <div class="row inner-fixed-content">

                  <style>
                  .OrdenarBoxes { list-style-type: none; margin: 0; padding: 0; width: 100%; }
                  .OrdenarBoxes li { float: left; }
                  .sortable-placeholder{
                    height: 150px;
                    width: 33.333333%;
                    border: 1px dotted #333333;
                  }
                  </style>

                  <!-- Banners -->
                  <div class="col-sm-12">
                    <div class="card">
                      <div class="card-body">

                        <div class="row">
                          <div class="col-sm-6">
                            <h4>Propiedades</h4>
                          </div>
                          <div class="col-sm-6 text-right">

                          </div>
                        </div>
                        <hr />

                        <ul class="OrdenarBoxes">
                          <?php
                            //Banner Items
                            $Query = "
                              SELECT
                                des.Id, des.Nombre, des.Ubicacion
                              FROM Desarrollos des
                              WHERE des.Id != '0'
                              ORDER BY des.Orden ASC
                            ";
                            $Desarrollos = $mysqli->query($Query);
                            mysqli_data_seek($Desarrollos, 0);
                            while($Des = mysqli_fetch_array($Desarrollos)) {
                          ?>
                          <li class="ui-state-default col-md-4">
                            <!-- Card -->
                            <div class="card">
                              <div class="Box BoxImg" style="background-image:url( <?php echo $UrlSite."/images/desarrollos/".$Des['Id'].".jpg"; ?> );">

                              </div>
                                <div class="card-body">
                                    <h5 class="card-title"><?php echo $Des['Nombre']; ?>&nbsp;</h5>
                                    <!--<p class="card-text"></p>-->
                                    <div class="text-left">
                                      <button type="button" class="btn btn-sm btn-secondary" onclick="window.location.href='../propiedades?Des=<?php echo $Des['Id']; ?>'">
                                        Propiedades<i class="fa fa-pencil m-l-10"></i>
                                      </button>
                                      <button type="button" class="btn btn-sm btn-success" onclick="window.location.href='item?Id=<?php echo $Des['Id']; ?>'">
                                        Editar<i class="fa fa-pencil m-l-10"></i>
                                      </button>
                                      <!-- <button type="button" class="btn btn-sm btn-danger Del-Btn" data-id="<?php echo $Des['Id']; ?>" data-img="<?php
                                      $Image = $RootSite.'/images/desarrollos/'.$Des['Id'].'.jpg';
                                      if (file_exists($Image)) {
                                        echo $UrlSite."/images/desarrollos/".$Des['Id'].".jpg";
                                      }else{
                                        echo $UrlSite."/images/desarrollos/0.jpg";
                                      }
                                      ?>" data-title="<?php echo $Des['Nombre']; ?>"><i class="fa fa-times"></i></button> -->
                                      <input type="hidden" name="IdItems[]" value="<?php echo $Des['Id']; ?>" />
                                    </div>
                                </div>
                            </div>
                            <!-- End Card -->
                          </li>
                        <?php } ?>
                        </ul>

                      </div>
                    </div>
                  </div>
                  <!-- End Banners -->


                </div>
                <!-- ============================================================== -->
                <!-- End Page Content -->
                <!-- ============================================================== -->
              </form>

              <!-- Modal Borrar -->
              <form method="post">
                <div id="Del-Modal" 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">Borrar</h4>
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            </div>
                            <div class="modal-body">
                                <p>¿Estas seguro de borrar este elemento?</p>
                                <p><strong id="DelTitle"></strong></p>
                                <img id="DelIng" class="card-img-top img-responsive" src="" alt="Card image cap">
                            </div>
                            <div class="modal-footer">
                                <button type="submit" class="btn btn-sm btn-danger waves-effect">Borrar<i class="fa fa-times m-l-10"></i></button>
                                <button type="button" class="btn btn-sm btn-secondary waves-effect" data-dismiss="modal">No</button>
                                <input id="DelId" type="hidden" name="Id" value="" />
                                <input type="hidden" name="Borrar" value="Ok" />
                            </div>
                        </div>
                        <!-- /.modal-content -->
                    </div>
                    <!-- /.modal-dialog -->
                </div>
              </form>
              <!-- End Modal Borrar -->

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

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

        </div>
        <!-- ============================================================== -->
        <!-- End Page wrapper  -->
        <!-- ============================================================== -->
    </div>
    <!-- ============================================================== -->
    <!-- End Wrapper -->
    <!-- ============================================================== -->
    <!-- ============================================================== -->
    <!-- All Jquery -->
    <!-- ============================================================== -->

    <?php require_once($Position.'scripts.php'); ?>
    <script src="<?php echo $Position; ?>js/extra.js"></script>

    <script>

      $(document).ready(function() {

        //Box
        $(".Box").each(function(){
          alto = $(this).width();
          halfalto = alto / 2;
          $(this).height(halfalto);
        });
        //End Box

        //Sortable
        $( ".OrdenarBoxes" ).sortable({
          //forcePlaceholderSize: true
        });
        //End Sortable

        //Modal Borrar
        $( ".Del-Btn" ).click(function() {
          id    = $( this ).data( "id" );
          title = $( this ).data( "title" );
          img   = $( this ).data( "img" );
          $('#DelId').val(id);
          $('#DelTitle').html(title);
          $("#DelIng").attr("src", img);
          $('#Del-Modal').modal('show');
        });
        //End Modal Borrar

      });
    </script>

    <!-- Cerrar Conexion -->
    <?php require_once($Position.'cerrarconexion.php'); ?>
    <!-- End Cerrar Conexion -->
</body>

</html>
