<?php

  $Seccion          = 'Propiedades';
  $SubSeccion       = 'Propiedades';
  $NameSec          = 'Propiedades';
  $Position         = '../../';
  $UrlImg           = 'images/propiedades';
  $UrlImgs          = 'images/propiedades/galeria';
  $UrlImgItems      = 'images/propiedades/galeriaitems';
  $Pagina           = '1';
  $ShowTextFormats  = '0';

  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/resize-class.php');

  $SecLine = array('Propiedades','Propiedad');

  $Hoy      = date('Y-m-d');
  $HoyFecha = CambioFechaInversa($Hoy);

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

    //Datos de formulario
    $ImgNew             = $_POST['ImgNew'];
    $Tipo               = addslashes($_POST['Tipo']);
    $Nombre             = addslashes($_POST['Nombre']);
    $Ubicacion          = addslashes($_POST['Ubicacion']);
    $Descripcion        = addslashes($_POST['Descripcion']);
    $UrlTour            = addslashes($_POST['UrlTour']);
    $Activo             = addslashes($_POST['Activo']);

    //Inserta Nuevo Registro
    if( $_POST['New'] == 'Ok' ){

      //Inserta
      $Query = "
        INSERT INTO Propiedades
        (
          Nombre, Ubicacion,
          Fk_Tipo, Descripcion,
          UrlTour,
          Activo
        )
        VALUES (
          '$Nombre', '$Ubicacion',
          '$Tipo', '$Descripcion',
          '$UrlTour',
          '$Activo'
        )
      ";
      $Result = mysqli_query($mysqli, $Query);
      $Id = mysqli_insert_id($mysqli);

    }
    //End Inserta Nuevo Registro

    //Edita datos
    if( $_POST['Edit'] == 'Ok' ){

      $Id		= $_POST['Id'];

      //Update
      $Query = "
        UPDATE Propiedades
        SET
          Nombre = '$Nombre', Ubicacion = '$Ubicacion',
          Fk_Tipo = '$Tipo', Descripcion = '$Descripcion',
          UrlTour = '$UrlTour',
          Activo = '$Activo'
        WHERE Id = '$Id'
      ";
      $Result = mysqli_query($mysqli, $Query);

      $Saved = 'Ok';

    }
    //End Edita datos

    //Edita Items
    $Query = "
      UPDATE PorpiedadesItems
      SET
        Borrar = '1'
      WHERE Fk_Propiedad = '$Id'
    ";
    $Result = mysqli_query($mysqli, $Query);

    $Items  = $_POST[Items];
    $Item   = $_POST[Item];
    $Sort = 0;
    foreach ($Items AS $Ite) {

      $Sort = $Sort + 1;

      $IdItem = $Item[$Ite]['Id'];
      $Action = $Item[$Ite]['Action'];
      $Texto  = $Item[$Ite]['Texto'];
      $Texto2 = $Item[$Ite]['Texto2'];
      $ItemA  = $Item[$Ite]['Item'];

      //echo "<hr />";

      if( $Action == 'New' ){

        //Insert
        $Query = "
          INSERT INTO PorpiedadesItems
          (
            Fk_Propiedad, Fk_Item, Texto, Texto2, Orden
          )
          VALUES (
            '$Id', '$ItemA', '$Texto', '$Texto2', '$Sort'
          )
        ";
        $Result = mysqli_query($mysqli, $Query);

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

        //Update
        $Query = "
          UPDATE PorpiedadesItems
          SET
            Fk_Item = '$ItemA', Texto = '$Texto',
            Texto2 = '$Texto2',
            Orden = '$Sort', Borrar = '0'
          WHERE Id = '$IdItem' AND  Fk_Propiedad = '$Id'
        ";
        $Result = mysqli_query($mysqli, $Query);

        //Imagen
        $Imagen  = 'ItemImg'.$Ite;
        if(empty($_FILES[$Imagen]['name'])){}else{

          $_FILES[$Imagen]["name"];
          $allowedExts = array("JPG","jpg","JPEG","jpeg");
          $temp = explode(".", $_FILES[$Imagen]["name"]);
          $extension = end($temp);
          if((
          ($_FILES[$Imagen]["type"] == "image/jpg")
          || ($_FILES[$Imagen]["type"] == "image/JPG")
          || ($_FILES[$Imagen]["type"] == "image/jpeg")
          || ($_FILES[$Imagen]["type"] == "image/JPEG")
          )
          && ($_FILES[$Imagen]["size"] < 10000000)
          && in_array($extension, $allowedExts)) {
          if ($_FILES[$Imagen]["error"] > 0) {
          }else{

            //RootSite del Sitio Seleccionado
            $ImageUp = $RootSite.'/'.$UrlImgItems."/".$IdItem.".jpg";

            if ( move_uploaded_file($_FILES[$Imagen]["tmp_name"], $ImageUp) ){
            //if ( copy($_FILES["image"]["tmp_name"], $ImageUp) ){
              //echo "Img Up";
              chmod($ImageUp, 0777);

              //Ajuste de tamaño
              $width = getWidth($ImageUp);
              $height = getHeight($ImageUp);
              $max_width = "1800";
              if ($width > $max_width){
                $scale = $max_width/$width;
                $resizeObj = new resize($ImageUp);
                $resizeObj -> resizeImage(1200, 1200, 'portrait');
                $resizeObj -> saveImage($ImageUp, 75);
              }else{
                $scale = 1;
                $resizeObj = new resize($ImageUp);
                $resizeObj -> resizeImage(1200, 1200, 'portrait');
                $resizeObj -> saveImage($ImageUp, 75);
              }
            }else{
              //echo "Img Not<br>";
              print_r(error_get_last());
            }

          }}else{
          }
        }

      }

    }

    $mysqli->query("DELETE FROM PorpiedadesItems WHERE Borrar = '1' AND Fk_Propiedad = '$Id'");
    //End Items

    //Sube Imagen Principal
    $Imagen = "image";
    if(empty($_FILES[$Imagen]['name'])){}else{

      $_FILES[$Imagen]["name"];
      $allowedExts = array("JPG","jpg","JPEG","jpeg");
      $temp = explode(".", $_FILES[$Imagen]["name"]);
      $extension = end($temp);
      if((
      ($_FILES[$Imagen]["type"] == "image/jpg")
      || ($_FILES[$Imagen]["type"] == "image/JPG")
      || ($_FILES[$Imagen]["type"] == "image/jpeg")
      || ($_FILES[$Imagen]["type"] == "image/JPEG")
      )
      && ($_FILES[$Imagen]["size"] < 10000000)
      && in_array($extension, $allowedExts)) {
      if ($_FILES[$Imagen]["error"] > 0) {
      }else{

        //RootSite del Sitio Seleccionado
        $ImageUp = $RootSite.'/'.$UrlImg."/".$Id.".jpg";

        if ( move_uploaded_file($_FILES[$Imagen]["tmp_name"], $ImageUp) ){
        //if ( copy($_FILES["image"]["tmp_name"], $ImageUp) ){
          //echo "Img Up";
          chmod($ImageUp, 0777);

          //Ajuste de tamaño
          $width = getWidth($ImageUp);
          $height = getHeight($ImageUp);
          $max_width = "1800";
          if ($width > $max_width){
            $scale = $max_width/$width;
            $resizeObj = new resize($ImageUp);
            $resizeObj -> resizeImage(540, 760, 'crop');
            $resizeObj -> saveImage($ImageUp, 100);
          }else{
            $scale = 1;
            $resizeObj = new resize($ImageUp);
            $resizeObj -> resizeImage(540, 760, 'crop');
            $resizeObj -> saveImage($ImageUp, 100);
          }
        }else{
          //echo "Img Not<br>";
          print_r(error_get_last());
        }

      }}else{
      }
    }
    //End Sube Imagen Principal

    //Edita Imagenes
    $Query = "
      UPDATE PropiedadesGaleria
      SET
        Del = '1'
      WHERE Fk_Propiedad = '$Id'
    ";
    $Result = mysqli_query($mysqli, $Query);

    $Imagenes = $_POST['Imagenes'];
    $Imgs     = $_POST['Imgs'];
    $Sort     = 0;
    foreach ($Imagenes as $Img) {
      $Sort     = $Sort + 1;
      $IdImg    = $Imgs[$Img][Id];
      $Name     = $Imgs[$Img][Name];

      $Query = "
        UPDATE PropiedadesGaleria
        SET
          Del = '0', Orden  = '$Sort'
        WHERE Id = '$IdImg'
      ";
      $Result = mysqli_query($mysqli, $Query);

    }
    //End Edita Imagenes

    $Query = "
      SELECT
        img.Id
      FROM PropiedadesGaleria img
      WHERE img.Fk_Propiedad = '$Id' AND img.Del = '1'
      ORDER BY img.Orden ASC
    ";
    $Images = $mysqli->query($Query);
    mysqli_data_seek($Images, 0);
    while($Img = mysqli_fetch_array($Images)) {
      unlink($RootSite."/images/propiedades/galeria/".$Img[Id].".jpg");
      $mysqli->query("DELETE FROM PropiedadesGaleria WHERE Id = '$Img[Id]'");
    }
    //End Edita Imagenes

    //Sube Imagen
    $Imagen = "imagen";
    if(empty($_FILES[$Imagen]['name'])){}else{

      //Insert Image BD
      $NameImg = $_POST['NameImg'];
      $Query = "
        INSERT INTO PropiedadesGaleria
        (
          Fk_Propiedad
        )
        VALUES (
          '$Id'
        )
      ";
      $Result = mysqli_query($mysqli, $Query);
      $IdImg = mysqli_insert_id($mysqli);

      $_FILES[$Imagen]["name"];
      $allowedExts = array("JPG","jpg","JPEG","jpeg");
      $temp = explode(".", $_FILES[$Imagen]["name"]);
      $extension = end($temp);
      if((
      ($_FILES[$Imagen]["type"] == "image/jpg")
      || ($_FILES[$Imagen]["type"] == "image/JPG")
      || ($_FILES[$Imagen]["type"] == "image/jpeg")
      || ($_FILES[$Imagen]["type"] == "image/JPEG")
      )
      && ($_FILES[$Imagen]["size"] < 10000000)
      && in_array($extension, $allowedExts)) {
      if ($_FILES[$Imagen]["error"] > 0) {
      }else{

        //RootSite del Sitio Seleccionado
        $ImageUp = $RootSite.'/'.$UrlImgs."/".$IdImg.".jpg";

        if ( move_uploaded_file($_FILES[$Imagen]["tmp_name"], $ImageUp) ){
        //if ( copy($_FILES["image"]["tmp_name"], $ImageUp) ){
          //echo "Img Up";
          chmod($ImageUp, 0777);

          //Ajuste de tamaño
          $width = getWidth($ImageUp);
          $height = getHeight($ImageUp);
          $max_width = "1800";
          if ($width > $max_width){
            $scale = $max_width/$width;
            $resizeObj = new resize($ImageUp);
            $resizeObj -> resizeImage(1500, 750, 'crop');
            $resizeObj -> saveImage($ImageUp, 100);
          }else{
            $scale = 1;
            $resizeObj = new resize($ImageUp);
            $resizeObj -> resizeImage(1500, 750, 'crop');
            $resizeObj -> saveImage($ImageUp, 100);
          }
        }else{
          //echo "Img Not<br>";
          print_r(error_get_last());
        }

      }}else{
      }
    }
    //End Sube Imagen

    //Redirecciona
    if( $_POST['New'] == 'Ok' ){
      echo '<script type="text/javascript">document.location.href="?cat='.$Categoria.'&Id='.$Id.'&New=Ok"</script>';
    }
    //End Redirecciona

  }

  //Depa
  $Query = "
    SELECT
      pro.Id,
      pro.Nombre, pro.Ubicacion,
      pro.Fk_Tipo, pro.Descripcion,
      pro.UrlTour,
      pro.Activo
    FROM Propiedades pro
    WHERE pro.Id = '$_GET[Id]'
  ";
  $Result = mysqli_query($mysqli, $Query);
  $Prop = mysqli_fetch_assoc($Result);

?>

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

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

<body class="fix-sidebar fix-header card-no-border">
  <?php
    mysqli_data_seek($Fonts, 0);
    while($Font = mysqli_fetch_array($Fonts)) {
      if( $Font['Link'] ){
          echo '<link href="'.$Font['Link'].'" rel="stylesheet">';
      }
    }
  ?>

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

        <form method="post" enctype="multipart/form-data" data-validate="parsley" role="form" id="FormPer">
          <div class="container-fluid">

            <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-sm btn-secondary" onclick="window.location.href='../?Des=<?php if( $Prop['Fk_Desarrollo'] ){ echo $Prop['Fk_Desarrollo']; }else{ echo $_GET[des]; } ?>'">Regresar <i class="fa fa-arrow-left m-l-5"></i></button>
                    <button type="button" class="btn btn-sm btn-secondary" onclick="window.location.href='?des=<?php if( $Prop['Fk_Desarrollo'] ){ echo $Prop['Fk_Desarrollo']; }else{ echo $_GET[des]; } ?>'">Nuevo<i class="fa fa-plus m-l-5"></i></button>
                    <button type="submit" class="btn btn-sm btn-secondary">Guardar<i class="fa fa-check m-l-5"></i></button>
                    <input type="hidden" name="Save" value="Ok">
                    <?php if( !empty($Prop['Id']) ){ ?>
                      <input type="hidden" name="Id" value="<?php echo $Prop['Id']; ?>">
                      <input type="hidden" name="Edit" value="Ok">
                    <?php }else{ ?>
                      <input type="hidden" name="New" value="Ok">
                    <?php } ?>
                    <input type="hidden" name="Fk_Banner" value="<?php echo $Ban[Id]; ?>" />
                  </div>
                </div>
                <!-- End Botones -->

            </div>

            <!-- Información del Banner -->
            <div class="row inner-fixed-content">
              <div class="col-lg-12">

                <!-- Titulo -->
                <div class="card card-outline-info">
                  <div class="card-body">
                    <div class="row">

                      <div class="col-sm-12">
                        <h3>Información</h3>
                        <hr />
                      </div>

                      <!-- Tipo -->
                      <div class="col-sm-12 form-group">
                        <label>Tipo</label>
                        <select data-requirede="true" name="Tipo" class="form-control chosen-select FontSel">
                          <option value="0"></option>
                          <option
                            value="1"
                            <?php if( $Prop['Fk_Tipo'] == '1' ){ echo "selected"; } ?>
                          >
                            Desarrollo
                          </option>
                          <option
                            value="2"
                            <?php if( $Prop['Fk_Tipo'] == '2' ){ echo "selected"; } ?>
                          >
                            Experiencia
                          </option>
                        </select>
                      </div>
                      <!-- End Desarrollo -->

                      <!-- Nombre -->
                      <div class="col-sm-12 form-group">
                        <label>Nombre</label>
                        <input class="form-control" autocomplete="off" data-required="true" name="Nombre" value="<?php echo $Prop['Nombre']; ?>" />
                      </div>
                      <!-- End Nombre -->

                      <!-- Ubicacion -->
                      <div class="col-sm-12 form-group">
                        <label>Ubicación</label>
                        <input
                          class="form-control"
                          autocomplete="off"
                          data-required="true"
                          name="Ubicacion"
                          value="<?php echo $Prop['Ubicacion']; ?>"
                        />
                      </div>
                      <!-- End Ubicacion -->

                      <!-- Descripcion -->
                      <div class="col-sm-12 form-group">
                        <label>Descripción</label>
                        <input
                          class="form-control"
                          autocomplete="off"
                          name="Descripcion"
                          value="<?php echo $Prop['Descripcion']; ?>"
                        />
                      </div>
                      <!-- End Descripcion -->

                      <!-- UrlTour -->
                      <div class="col-sm-12 form-group">
                        <label>Url Tour</label>
                        <input
                          class="form-control"
                          autocomplete="off"
                          name="UrlTour"
                          value="<?php echo $Prop['UrlTour']; ?>"
                        />
                      </div>
                      <!-- End UrlTour -->

                    </div>
                  </div>
                </div>
                <!-- Titulo -->

                <!-- Imagen Principal -->
                <div class="card card-outline-info">
                  <div class="card-body">
                    <div class="row">
                      <div class="col-sm-12">
                        <h4 class="m-b-0">Imagen Principal</h4>
                        <hr />
                      </div>
                      <div class="col-sm-12 row">
                        <div class="col-sm-4 m-b-20">
                          <img class="img-responsive" src="<?php echo $UrlSite ?>/<?php echo $UrlImg ?>/<?php echo $Prop[Id]; ?>.jpg" />
                        </div>
                        <div class="col-sm-4 m-b-20">
                          <div style="width:100%;">
                            <label>
                                Agregar Imagen<br>
                                ( Formato .jpg, 540px de alto, 760px de alto minimo. )
                            </label>
                            <input name="image" type="file" id="input-file-now-custom-1" class="dropify"/>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
                <!-- End Imagen Principal -->

                <!-- Items -->
                <div class="card card-outline-info Areas">
                  <div class="card-body">
                    <div class="row">

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

                      <div class="AreaItems m-b-20 col-sm-12">
                        <?php
                          //AreaItems
                          $Query = "
                            SELECT
                              ite.Id, ite.Fk_Item, ite.Texto, ite.Texto2
                            FROM PorpiedadesItems ite
                            WHERE ite.Fk_Propiedad = '$Prop[Id]'
                            ORDER BY ite.Orden ASC
                          ";
                          $Items = $mysqli->query($Query);
                          mysqli_data_seek($Items, 0);
                          while($Item = mysqli_fetch_array($Items)) {
                        ?>
                          <div class="card Item">
                            <div class="card-body">
                              <button class="btn btn-danger BtnDelItem" type="button" style="position:absolute;top:0px;right:0px;">
                                <i class="fa fa-times"></i>
                              </button>
                              <div class="row">

                                <!-- Sel Item -->
                                <div class="col-sm-4 form-group">
                                  <label>Icono</label>
                                  <select data-requirede="true" name="Item[<?php echo $Item[Id]; ?>][Item]" class="form-control chosen-select FontSel">
                                    <option value="0"></option>
                                    <?php
                                      //Items
                                      $Query = "
                                        SELECT
                                          ite.Id, ite.Nombre
                                        FROM Items ite
                                        ORDER BY ite.Nombre ASC
                                      ";
                                      $Ites = $mysqli->query($Query);
                                      mysqli_data_seek($Ites, 0);
                                      while($Ite = mysqli_fetch_array($Ites)) {
                                    ?>
                                      <option
                                        value="<?php echo $Ite['Id']; ?>"
                                        <?php if( $Ite['Id'] == $Item['Fk_Item'] ){ echo "selected"; } ?>
                                      >
                                        <?php echo $Ite['Nombre']; ?>
                                      </option>
                                    <?php } ?>
                                  </select>
                                </div>
                                <!-- End Sel Item -->

                                <!-- Texto -->
                                <div class="col-sm-8 form-group">
                                  <label>Titulo</label>
                                  <input type="hidden" class="form-control" name="Items[]" value="<?php echo $Item[Id]; ?>" autocomplete="off" />
                                  <input type="hidden" class="form-control" name="Item[<?php echo $Item[Id]; ?>][Id]" value="<?php echo $Item[Id]; ?>" autocomplete="off" />
                                  <input type="hidden" class="form-control" name="Item[<?php echo $Item[Id]; ?>][Action]" value="Edit" autocomplete="off" />
                                  <input class="form-control" name="Item[<?php echo $Item[Id]; ?>][Texto]" value="<?php echo $Item[Texto]; ?>" autocomplete="off" />
                                </div>
                                <!-- End Texto -->

                                <!-- Texto -->
                                <div class="col-sm-12 form-group">
                                  <label>Texto</label>
                                  <input class="form-control" name="Item[<?php echo $Item[Id]; ?>][Texto2]" value="<?php echo $Item[Texto2]; ?>" autocomplete="off" />
                                </div>
                                <!-- End Texto -->

                                <!-- Imagen -->
                                <div class="col-sm-12 form-group">
                                  <label>Imagen ( Formato jpg, 1200px ancho por 1200px alto. 72 dpi  )</label>
                                  <input type="file" class="form-control" name="ItemImg<?php echo $Item[Id]; ?>" />

                                  <?php
                                    $Image = $RootSite.'/'.$UrlImgItems.'/'.$Item['Id'].'.jpg';
                                    if (file_exists($Image)) {
                                  ?>
                                    <img
                                      class="img-responsive"
                                      src="<?php echo $UrlSite ?>/<?php echo $UrlImgItems; ?>/<?php echo $Item[Id]; ?>.jpg"
                                      style="max-width: 350px;"
                                    />
                                  <?php } ?>
                                </div>
                                <!-- End Imagen -->

                              </div>

                            </div>
                          </div>
                        <?php } ?>
                      </div>

                      <div class="col-sm-12">
                        <button type="button" class="btn btn-secondary BtnAddArea" data-area="<?php echo $Area[Id]; ?>">
                          Agregar <i class="fa fa-plus m-l-10"></i>
                        </button>
                      </div>

                    </div>
                  </div>
                </div>
                <!-- End Items -->

              </div>
            </div>


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

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

    </div>

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

        //Areas de Texto
        tinymce.init({
            selector: ".IncEditor",
            theme: "modern",
            height: 300,
            force_br_newlines : true,
            force_p_newlines : false,
            forced_root_block : '',
            plugins: "paste",
            paste_as_text: true,
            plugins: [
                "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
                "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
                "save table contextmenu directionality emoticons template paste textcolor"
            ],
            toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | l      ink image | print preview media fullpage | forecolor backcolor emoticons",
        });
        //End Areas de Texto

        // Actualiza Imagenes
        $("img").each(function(){
          $(this).attr('src', $(this).attr('src')+'?'+Math.random());
        });

        //Date datepicker
        $('.datepicker').datepicker({
          autoclose: true,
          todayHighlight: true,
          format: "dd / MM / yyyy",
          startView: 2
        });
        //End Date datepicker

        //Seleccion de Fuente
        $(document.body).on('change', '.FontSel' ,function(){
          $("option:selected", this).each(function () {
            font = $( this ).data( "font" );
            console.log(font);
            $( this ).parents('.FontSel').css("font-family", font);
            $( this ).parents('.FontSel').parents('.form-group').find('.chosen-single').css("font-family", font);
          });
        });

        //Del Img
        $(document.body).on('click', '.BtnDelImg' ,function(){
          $(this).parents('.ImgBox').remove();
        });
        //End Del Img

        //Agregar Item
        <?php
          $Query = "
            SELECT
              ite.Id
            FROM PorpiedadesItems ite
            ORDER BY ite.Id
            LIMIT 1
          ";
          $Result = mysqli_query($mysqli, $Query);
          $ConsItem = mysqli_fetch_assoc($Result);
        ?>
        var Cons = <?php if( $ConsItem[Id] ){ echo $ConsItem[Id]; }else{ echo "0"; } ?>;
        $(document.body).on('click', '.BtnAddArea' ,function(){
          $('#FormPer').parsley().destroy();
          //var Area = $(this).data('area');

          Cons = Cons + 1;
          console.log(Cons);

          var Selection = '<select data-requirede="true" name="Item['+Cons+'][Item]" class="form-control chosen-select"><option value="0"></option><?php
              //Items
              $Query = "
                SELECT
                  ite.Id, ite.Nombre
                FROM Items ite
                ORDER BY ite.Nombre ASC
              ";
              $Ites = $mysqli->query($Query);
              mysqli_data_seek($Ites, 0);
              while($Ite = mysqli_fetch_array($Ites)) {
            ?><option value="<?php echo $Ite['Id']; ?>"><?php echo $Ite['Nombre']; ?></option><?php } ?></select>';

          $(this).parents('.Areas').find('.AreaItems').append('<div class="card Item"><div class="card-body"><button class="btn btn-danger BtnDelItem" type="button" style="position:absolute;top:0px;right:0px;"><i class="fa fa-times"></i></button>\
          <div class="row">\
            <div class="col-sm-6">\
              <label>Item</label>\
              '+Selection+'\
            </div>\
            <div class="col-sm-6">\
              <label>Texto</label>\
              <input type="hidden" class="form-control" name="Items[]" value="'+Cons+'" autocomplete="off" />\
              <input type="hidden" class="form-control" name="Item['+Cons+'][Id]" value="'+Cons+'" autocomplete="off" />\
              <input type="hidden" class="form-control" name="Item['+Cons+'][Action]" value="New" autocomplete="off" />\
              <input class="form-control" name="Item['+Cons+'][Texto]" value="" autocomplete="off" />\
            </div>\
          </div>\
          </div></div>');
          $('#FormPer').parsley();
        });
        //End Agregar Item

        //Del Img
        $(document.body).on('click', '.BtnDelItem' ,function(){
          $(this).parents('.Item').remove();
        });
        //End Del Img

      });
    </script>
    <!-- End Extra js -->

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

</body>

</html>
