<?php
  if( $_POST['action'] == 'New' ){

    $IdArt  = $_POST['art'];
    $Val    = $_POST['val'];
    $Action = 'New';

    $Position     = '../../';
    require_once($Position.'config/config.php');
    require_once($Position.'config/conexion.php');

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

    $Val    = $Precio[Id];
    $Action = 'Edit';
    $IdArt  = $Art[Id];

  }
?>

<div class="ItemOpc">
  <div
    class="card"
    style="
      position: relative;
    "
  >

    <!-- Btn -->
    <button
      class="btn btn-danger DelItemOpc"
      type="button"
      style="
        position: absolute;
        top: 0px;
        right: 0px;
      "
    >
      <i class="fa fa-times"></i>
    </button>
    <!-- End Btn -->

    <div class="card-body">
      <div class="row">

        <input
          type="hidden"
          name="Precios[]"
          value="<?php echo $Val; ?>"
        />

        <!-- Id -->
        <input
          type="hidden"
          name="Precio[<?php echo $Val; ?>][Id]"
          value="<?php echo $Val; ?>"
        />
        <!-- End Id -->

        <!-- Action -->
        <input
          type="hidden"
          name="Precio[<?php echo $Val; ?>][Action]"
          value="<?php echo $Action; ?>"
        />
        <!-- End Action -->

        <!-- Variante -->
        <div class="col-sm-3 form-group">
          <label>Variante</label>
          <select
            name="Precio[<?php echo $Val; ?>][Variante]"
            class="form-control chosen-select"
          >
            <option value="0">Sin variante</option>
            <?php
              //Variantes
              $Query = "
                SELECT
                  va.Id, va.Clave,
                  va.Fk_Medida,
                  va.Ancho, va.Largo, va.Alto,
                  va.Fk_Talla, va.Fk_Color,
                  me.Clave AS ClaveMedida, me.Nombre AS Medida,
                  ta.Clave AS ClaveTalla, ta.Nombre AS Talla,
                  co.Clave AS ClaveColor, co.Nombre AS Color
                FROM ArticulosVariantes va
                LEFT JOIN CatalogoMedidas me ON me.Id = va.Fk_Medida
                LEFT JOIN CatalogoTallas ta ON ta.Id = va.Fk_Talla
                LEFT JOIN Colores co ON co.Id = va.Fk_Color
                WHERE va.Fk_Articulo = '$IdArt'
                ORDER BY va.Clave ASC
              ";
              $Vars = $mysqli->query($Query);
              mysqli_data_seek($Vars, 0);
              while($Var = mysqli_fetch_array($Vars)) {
            ?>
              <option
                <?php
                  if( $Precio[Fk_Variante] == $Var[Clave] ){
                    echo "selected='selected'";
                  }
                ?>
                value="<?php echo $Var[Clave]; ?>"
              >
                <?php
                  echo $Var[Clave].' ( ';

                  //Alto
                  if( $Var[Alto] && $Var[Alto] != '0' ){
                    echo $Var[Alto].', ';
                  }
                  //End Alto

                  //Ancho
                  if( $Var[Ancho] && $Var[Ancho] != '0' ){
                    echo $Var[Ancho].', ';
                  }
                  //End Ancho

                  //Largo
                  if( $Var[Largo] && $Var[Largo] != '0' ){
                    echo $Var[Largo].', ';
                  }
                  //End Largo

                  //Unidad de Medida
                  if( $Var[Fk_Medida] ){
                    echo $Var[ClaveMedida];
                  }
                  //End Unidad de Medida

                  //Talla
                  if( $Var[Fk_Talla] ){
                    echo $Var[ClaveTalla].' ('.$Var[Talla].')';
                  }
                  //End Talla

                  echo " )";

                ?>
              </option>
            <?php } ?>
          </select>
        </div>
        <!-- End Variante -->

        <!-- Precio -->
        <div class="col-sm-3">
          <label>Precio</label>
          <input
            name="Precio[<?php echo $Val; ?>][Precio]"
            class="form-control text-right"
            data-required="true"
            dara-type="number"
            value="<?php echo $Precio[Precio]; ?>"
            autocomplete="off"
          />
        </div>
        <!-- End Precio -->

        <!-- RangoA -->
        <div class="col-sm-3">
          <label>RangoA</label>
          <input
            name="Precio[<?php echo $Val; ?>][RangoA]"
            class="form-control text-right"
            data-required="true"
            dara-type="number"
            value="<?php echo $Precio[RangoA]; ?>"
            autocomplete="off"
          />
        </div>
        <!-- End RangoA -->

        <!-- RangoB -->
        <div class="col-sm-3">
          <label>RangoB</label>
          <input
            name="Precio[<?php echo $Val; ?>][RangoB]"
            class="form-control text-right"
            data-required="true"
            dara-type="number"
            value="<?php echo $Precio[RangoB]; ?>"
            autocomplete="off"
          />
        </div>
        <!-- End RangoB -->


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