<?php
  $Position     = '../';
  $Seccion      = 'Menus';
  //$SubSeccion   = 'Principal';
  $NameSec      = 'Menus';
  $SecLine      = array('Menus');

  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');

  if( $_POST[Save] == 'Ok' ){

    //Varaibles
    $Items      = $_POST[Items];
    $Item       = $_POST[Item];
    $Sorting    = 0;

    $Query = "UPDATE LandingMenusItems
    SET
    Del = '1'
    ";
    $Result = mysqli_query($mysqli, $Query);

    foreach ($Items AS $Ite) {
      $ItemAction   = $Item[$Ite][Action];
      $IdItem       = $Item[$Ite][Id];
      $ItemMenu     = $Item[$Ite][Menu];
      $ItemTitle    = $Item[$Ite][Title];
      $ItemUrl      = $Item[$Ite][Url];

      if( $ItemAction == 'New' ){

        $Sorting = $Sorting + 1;
        //Insert Items in Page
        $Query = "
        INSERT INTO LandingMenusItems
        (
          Fk_Menu, Title, Url, Sort
        ) VALUES (
          '$ItemMenu', '$ItemTitle', '$ItemUrl', '$Sorting'
        )";
        $Result = mysqli_query($mysqli, $Query);

      }elseif( $ItemAction == 'Edit' ){

        $Sorting = $Sorting + 1;
        $Query = "UPDATE LandingMenusItems
        SET
        Title = '$ItemTitle', Url = '$ItemUrl',
        Sort = '$Sorting', Del = '0'
        WHERE Id = '$IdItem'";
        $Result = mysqli_query($mysqli, $Query);
        //echo "<hr />";

      }

    }

    //Delete Items
    $Query = "DELETE FROM LandingMenusItems WHERE Del = '1'";
    $Result = mysqli_query($mysqli, $Query);
    //End Delete Items

  }

?>

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

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

<style>
  .OrdenarBoxes { list-style-type: none; margin: 0; padding: 0; width: 100%; }
  .OrdenarBoxes li { float: left; }
  .sortable-placeholder{
    height: 37px !important;
    width: 100%;
    border: 1px dotted #333333 !important;
    background: #dbdbdb !important;
  }
  .BoxesSort li{
    background: #ffffff;
    border: 1px solid rgba(120, 130, 140, 0.13);
    padding: 8px 16px;
    margin: 4px 0px;
    height: auto;
    width: 100%;
    cursor:move;
    position: relative;
  }
</style>

<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">
            <form method="post" enctype="multipart/form-data" data-validate="parsley" role="form" id="FormPer">

              <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="submit" class="btn btn-sm btn-success">Guardar <i class="fa fa-check m-l-5"></i></button>
                      <input type="hidden" name="Save" value="Ok" />
                    </div>
                  </div>
                  <!-- End Botones -->

              </div>

              <!-- Inner -->
              <div class="row inner-fixed-content">

                <!-- Menus -->
                <?php
                  //Menu
                  $Query = "
                  SELECT
                  men.Id, men.Name
                  FROM LandingMenus men
                  WHERE men.Id != '0'
                  ORDER BY men.Sort ASC
                  ";
                  $Menus = $mysqli->query($Query);
                  mysqli_data_seek($Menus, 0);
                  while($Men = mysqli_fetch_array($Menus)) {
                ?>
                  <div class="col-lg-12 col-md-12 Pag">
                    <div class="card">
                      <div class="card-body">

                        <div class="row">

                          <div class="col-sm-12">
                            <h4 class="card-title"><?php echo $Men[Name]; ?></h4>
                          </div>

                          <div class="col-sm-12">
                            <ul class="OrdenarBoxes BoxesSort">
                              <?php

                                //Items
                                $Query = "
                                SELECT
                                ite.Id, ite.Fk_Menu, ite.Title, ite.Url
                                FROM LandingMenusItems ite
                                WHERE ite.Id != '0' AND ite.Fk_Menu = '$Men[Id]'
                                ORDER BY ite.Sort ASC
                                ";
                                $ItemsIn = $mysqli->query($Query);
                                mysqli_data_seek($ItemsIn, 0);
                                while($ItemIn = mysqli_fetch_array($ItemsIn)) {
                                  $Action = 'Edit';
                                  require('item.php');
                                }
                              ?>
                            </ul>
                          </div>

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

                          <div class="col-sm-12">
                            <div class="">
                              <button type="button" class="btn btn-success btn-block BtnAddItem" data-menu="<?php echo $Men[Id]; ?>">Agregar Item</button>
                            </div>
                          </div>

                        </div>

                      </div>
                    </div>
                  </div>
                <?php } ?>
                <!-- End Menus -->

              </div>
              <!-- End Inner -->

            </form>
          </div>

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

        </div>

    </div>

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

    <script>
      $(document).ready(function() {

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

        //DelSec
        $(document.body).on('click', '.DelSec' ,function(){
          $(this).parents('.ItemBox').remove();
        });
        //End DelSec

        //Add Item
        Next = 0;
        $(document.body).on('click', '.BtnAddItem' ,function(){
          Action = 'New';
          Pag = $(this).parents('.Pag');
          Menu = $(this).data('menu');
          Next = Next+1;
          console.log(Menu);
          $.post("item.php", { Action: Action, Menu: Menu, Next: Next }, function(data){
            console.log(data);
            Pag.find('.BoxesSort').append(data);
            $( ".OrdenarBoxes" ).sortable({});
          });
        });
        //End Add Item

      });
    </script>

</body>

</html>
