<?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>Venta</h1>
  <p><b>Buscar producto por nombre o por codigo:</b></p>
    <form id="searchp">
    <div class="row">
      <div class="col-md-3">
        <input type="hidden" name="view" value="sell">
        <input type="text" id="product_name" name="product_name" class="form-control" placeholder="Nombre del Producto">
      </div>

      <div class="col-md-3">
        <input type="hidden" name="view" value="sell">
        <input type="text" id="product_code" name="product_code" class="form-control" placeholder="Codigo de Barra">
      </div>


      <div class="col-md-3">
      <button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-search"></i> Buscar</button>
      </div>
    </div>
    </form>
<div id="show_search_results">
  


<script>
//jQuery.noConflict();

$(document).ready(function(){
  $("#searchp").on("submit",function(e){
    e.preventDefault();

    code = $("#product_code").val();
    name = $("#product_name").val();
    if(name!=""){
    $.get("./?action=searchproduct",$("#searchp").serialize()+"&go=name",function(data){
      $("#show_search_results").html(data);
    });
    $("#product_name").val("");
    }
    else if(code!=""){
    $.get("./?action=searchproduct",$("#searchp").serialize()+"&go=code",function(data){
      $("#show_search_results").html(data);
    });
    $("#product_code").val("");
    }

  });
  });

$(document).ready(function(){
    $("#product_code").keydown(function(e){
        if(e.which==17 || e.which==74){
            e.preventDefault();
        }else{
            console.log(e.which);
        }
    })
});
</script>

<?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>
<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>
<form method="post" class="form-horizontal" id="processsell" action="./?action=processsell">
<h2>Resumen</h2>
<div class="row">
<div class="col-md-3">
    <label class="control-label">Tipo de Venta</label>
    <div class="col-lg-12">
    <select name="k" id="k" required class="form-control">
    <option value="1">Venta en el Restaurante</option>
    <option value="2">Para Llevar</option>
    <option value="3">A Domicilio</option>
    </select>
    </div>
  </div>

<div class="col-md-3">
    <label class="control-label">Mesero</label>
    <div class="col-lg-12">
    <?php if(Core::$user->kind==2):?>
      <?php echo Core::$user->name." ".Core::$user->lastname; ?>
<input type="hidden" name="waiter_id" value="<?php echo Core::$user->id; ?>">
    <?php else:?>
    <?php 
$clients = UserData::getMeseros();
    ?>
    <select name="waiter_id" id="waiter_id" class="form-control">
    <option value="">-- NINGUNO --</option>
    <?php foreach($clients as $client):?>
      <option value="<?php echo $client->id;?>"><?php echo $client->name." ".$client->lastname;?></option>
    <?php endforeach;?>
      </select>
    <?php endif; ?>
    </div>
  </div>
  <div class="col-md-3">
    <label class="control-label">Mesa</label>
    <div class="col-lg-12">
    <?php 
$clients = ItemData::getAll();
    ?>
    <select name="item_id" id="item_id" class="form-control">
    <option value="">-- NINGUNO --</option>
    <?php foreach($clients as $client):?>
      <option value="<?php echo $client->id;?>"><?php echo $client->name;?></option>
    <?php endforeach;?>
      </select>
    </div>
  </div>

  </div>

<input type="hidden" name="discount" value="0">
<input type="hidden" name="money" value="0">
<input type="hidden" name="tip" value="0">
<input type="hidden" name="t_id" value="1">
<input type="hidden" name="p_id" value="1">
<input type="hidden" name="d_id" value="1">


      <input type="hidden" name="total" value="<?php echo $total; ?>" class="form-control" placeholder="Total">
      <div class="clearfix"></div>
<br>
  <div class="row">
<div class="col-md-6 col-md-offset-6">
<div class="box box-primary">
<table class="table table-bordered">
<tr>
  <td><p>Subtotal</p></td>
  <td><p><b>$ <?php echo number_format($total*(1 - ($iva_val/100) ),2,'.',','); ?></b></p></td>
</tr>
<tr>
  <td><p><?php echo $iva_name." (".$iva_val."%) ";?></p></td>
  <td><p><b>$ <?php echo number_format($total*($iva_val/100),2,'.',','); ?></b></p></td>
</tr>
<tr>
  <td><p>Total</p></td>
  <td><p><b>$ <?php echo number_format($total,2,'.',','); ?></b></p></td>
</tr>

</table>
</div>
  <div class="form-group">
    <div class="col-lg-offset-2 col-lg-10">
      <div class="checkbox">
        <label>
          <input name="is_oficial" type="hidden" value="1">
        </label>
      </div>
    </div>
  </div>
<div class="form-group">
    <div class="col-lg-offset-2 col-lg-10">
      <div class="checkbox">
        <label>
    <a href="index.php?view=clearcart" class="btn btn-danger"><i class="glyphicon glyphicon-remove"></i> Cancelar</a>
        <button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-usd"></i><i class="glyphicon glyphicon-usd"></i> Finalizar Venta</button>
        </label>
      </div>
    </div>
  </div>
</form>
<script>
  $(document).ready(function(){
    $("#processsell").submit(function(e){
      if($("#k").val()=="1"){
        if($("#client_id").val()==""&&$("#item_id").val()==""){
          e.preventDefault();
          alert("Debes seleccionar Mesa y Mesero");
        }
      }
    })
  });
</script>
</div>
</div>

<?php endif; ?>

</div>
</section>
