<?php
// $symbol = ConfigurationData::getByPreffix("currency")->val;
$iva_name = ConfigurationData::getByPreffix("imp-name")->val;
$iva_val = ConfigurationData::getByPreffix("imp-val")->val;

?>

<section class="content">

<div class="row">
  <div class="col-md-12">
  <h1>Productos Favoritos</h1>


  <?php
$products_fav = ProductData::getFavorites();
$filas = 0;
if(count($products_fav)>0){

$nproducts = count($products_fav);
$filas = $nproducts/10;
$extra = $nproducts%10;
if($filas>1&& $extra>0){ $filas++; }


  ?>
<div class="box box-primary">
<div class="box-body">
<form method="post" action="./?view=addtocartquick">
<table class="table table-bordered table-hover">
  <thead>
  </thead>
  <?php
  $cnt=0;
$products_in_cero=0;
   for($f=0;$f<$filas;$f++):?>
  <tr class="<?php if( ($product->use_inventary && $q<=5) ){ echo "danger"; }?>">
   <?php
   for($i=0;$i<10;$i++):
    if($cnt<count($products_fav)):
      $product = $products_fav[$cnt];
$q= OperationData::getQByStock($product->id,StockData::getPrincipal()->id);
  ?>
  <?php 
  if( ($product->use_inventory && $q>0) || !$product->use_inventory ):?>
    <td style="width:50px;">
<h4>    <?php echo $product->name; ?></h4>
      <?php if($product->image!=""):?>
        <img src="storage/products/<?php echo $product->image;?>" style="width:64px;">
      <?php endif;?>
    <input type="hidden" name="product_id" value="<?php echo $product->id; ?>">
    <input type="number" style="width:80px;" value="0" min="0" class="form-control" required name="q-<?php echo $product->id; ?>" placeholder="Cantidad de producto ...">

<div><b>$<?php echo $product->price_out; ?></b></div>
    </td>
<?php else:$products_in_cero++;
?>
<?php  endif; ?>
<?php else:?>
  <td></td>
<?php  endif; ?>
  <?php $cnt++;endfor;?>
  </tr>
  <?php endfor;?>
</table>
<br>
<input type="submit" value="Agregar a la venta" class="btn btn-primary">
</form>
</div>
</div>
<?php if($products_in_cero>0){ echo "<p class='alert alert-warning'>Se omitieron <b>$products_in_cero productos</b> que no tienen existencias en el inventario. <a href='index.php?view=inventary&stock=".StockData::getPrincipal()->id."'>Ir al Inventario</a>"; }?>

  <?php
}else { echo "<p class='alert alert-warning'>No hay resultados en la busqueda.</p>"; }
?>
<br><hr>




<?php if(isset($_SESSION["errors"])):?>
<h2>Errores</h2>
<p></p>
<table class="table table-bordered table-hover">
<tr class="danger">
  <th>Codigo</th>
  <th>Producto</th>
  <th>Mensaje</th>
</tr>
<?php foreach ($_SESSION["errors"]  as $error):
$product = ProductData::getById($error["product_id"]);
?>
<tr class="danger">
  <td><?php echo $product->id; ?></td>
  <td><?php echo $product->name; ?></td>
  <td><b><?php echo $error["message"]; ?></b></td>
</tr>

<?php endforeach; ?>
</table>
<?php
unset($_SESSION["errors"]);
 endif; ?>


<!--- Carrito de compras :) -->
<?php if(isset($_SESSION["cart"])):
$total = 0;
?>
<h2>Lista de venta</h2>
<a href="./?view=sell" class="btn btn-default btn-lg">Continuar <i class="fa fa-chevron-right"></i></a>
<br><br>
<div class="box box-primary">
<table class="table table-bordered table-hover">
<thead>
  <th style="width:30px;">Codigo</th>
  <th style="width:30px;">Cantidad</th>
  <th style="width:30px;">Unidad</th>
  <th>Producto</th>
  <th style="width:30px;">Precio Unitario</th>
  <th style="width:30px;">Precio Total</th>
  <th ></th>
</thead>
<?php foreach($_SESSION["cart"] as $p):
$product = ProductData::getById($p["product_id"]);
?>
<tr >
  <td><?php echo $product->barcode; ?></td>
  <td ><?php echo $p["q"]; ?></td>
  <td><?php echo $product->unit; ?></td>
  <td><?php echo $product->name; ?></td>
  <td><b>$ <?php echo number_format($product->price_out); ?></b></td>
  <td><b>$ <?php  $pt = $product->price_out*$p["q"]; $total +=$pt; echo number_format($pt); ?></b></td>
  <td style="width:30px;"><a href="index.php?view=clearcart&product_id=<?php echo $product->id; ?>" class="btn btn-danger"><i class="glyphicon glyphicon-remove"></i> Cancelar</a></td>
</tr>

<?php endforeach; ?>
</table>
</div>


</div>
</div>

<?php endif; ?>

</div>
</section>
