<?php
//ini_set('display_errors',1);
session_start();
require_once ("../../config/Cado.php");
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once("../producto/cProductomarcacodigo.php");
$oProductomarcacodigo = new cProductomarcacodigo();

require_once ("../formatos/formato.php");

if(isset($_SESSION['ordencompra_can']))
{
	$num_rows=count($_SESSION['ordencompra_can']);
	if($num_rows==0)$num_rows="";
	
}
else
{
	$num_rows="0";
}


?>


<style>
	#tabla_ordencompra_detalle td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_ordencompra_detalle td.editable,#tabla_ordencompra_detalle td.editable *{
		cursor: text !important;
		height: 100%; 
		 border-bottom-color: #ccc;
		border-bottom-style: dashed; 
		border-bottom-width: 0.5px;
	}
	#tabla_ordencompra_detalle td.editando{
		background-color: rgba(18, 180, 243, 0.05);
			outline: 2px solid #35A4F3;
	}
	#tabla_ordencompra_detalle td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_ordencompra_detalle td input:focus{
		background: transparent;
			height: 100%;
			width: 100%;
			outline: none;
			border: none;
	}
	#tabla_ordencompra_detalle td input[readonly]{
		background: transparent;
	}
</style>


	    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 ">				

<div class="table-responsive">

	   
<table cellspacing="1" id="tabla_ordencompra_detalle" class="table table-bordered table-striped"> 
    <thead>
        <tr >
            <th style="white-space: nowrap;">CODIGO</th>
			<th style="white-space: nowrap;">ARTÍCULO</th>
			<th class="hidden-xs" style="white-space: nowrap;" >MARCA</th> 
			<th class="hidden-xs" style="white-space: nowrap;" >CATEGORIA</th> 
			<th align="right">CANTIDAD</th>
			<th align="right">PRECIO COSTO</th>
        </tr>
    </thead>
    <tbody>
    <?php
	if($num_rows>0){
	
	foreach($_SESSION['ordencompra_can'] as $indice=>$can)
	{
		$dts1=$oCatalogo->mostrarUno($_SESSION['ordencompra_catid'][$indice],$_SESSION['ordencompra_marid'][$indice]);
		$dt1 = mysqli_fetch_array($dts1);
	
	?> 
	  
	  	<tr >
			
		<td  style="white-space: nowrap; font-size: 9pt; font-weight: bold;"><?php echo $dt1['tb_producto_codint']?></td>
			<td  style="white-space: nowrap; font-size: 9pt; font-weight: bold;"><?php echo $dt1['tb_producto_nom']?></td>
			<td class="" ><?php echo $dt1['tb_marca_nom']?></td>
			<td><?php echo $dt1['tb_categoria_nom']?></td>

			<td align="right" width="50" class="editable" style="font-size:9pt; font-weight:bold">
				<input type="text" value="<?php echo $can ?>" 
				data-cantidad="<?php echo $can ?>" 
				data-original="<?php echo $can ?>" readonly 
				data-cat_id="<?php echo $_SESSION['ordencompra_catid'][$indice] ?>"
				data-mar_id ="<?php echo $_SESSION['ordencompra_marid'][$indice] ?>" 
				style="text-align: right;">
			</td>
			<td><?php echo $_SESSION['ordencompra_precom'][$indice]?></td>
			<td align="center" nowrap="nowrap">
			<span data-toggle="tooltip" data-placement="top" title="Quitar del pedido" data-indice ="<?php echo $indice ?>" class="btn_eliminar btn btn-danger btn-xs" ><i class="fas fa-times"></i></span>
			</td>
				
		</tr>
	 
	  
	  
    <?php
	
	$total += $_SESSION['ordencompra_precom'][$indice];
	
    }

	}
    ?>
    
    <tr >
			
			<td colspan="5" >TOTAL</td>
			<td><?php echo formato_money($total)?></td>
			<td align="center" nowrap="nowrap">
			</td>
				
		</tr>
		
  </tbody>
</table>



<input type="hidden" name="numrow_items" id="numrow_items" value="<?php echo $num_rows;?>">
<?php
	if($num_rows=="" or $num_rows==0)echo $num_rows.' Ningún registro';
	if($num_rows==1)echo $num_rows.' registro';
	if($num_rows>=2)echo $num_rows.' registros';
?>

</div>

	<div style="height:10px;" class="col-lg-12 col-xs-12"></div>													
	

</div> 


<script type="text/javascript">


	$('#tabla_ordencompra_detalle td input').keydown(function(ev) {
		if(ev.keyCode == 13 && !$(this).attr('readonly')) {
			 ev.preventDefault();
			var cat_id = $(this).attr('data-cat_id');
			var mar_id = $(this).attr('data-mar_id');
			var cantidad = $(this).parent().parent().find('input[data-cantidad]').val();
			
			agregar_pedidocompra('agregar', cantidad, cat_id, mar_id,'editar');
			
			
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		}
	});

	$('#tabla_ordencompra_detalle td input').parent().click(function(ev) {
		var $input = $(this).find('input');
		var $td = $input.parent().addClass('editando');
		$input.removeAttr('readonly').focus().select();
	});

	$('#tabla_ordencompra_detalle td input').blur(function(event) {
		$(this).val($(this).attr('data-original'));
		$(this).attr('readonly', 'true');
		$(this).parent().removeClass('editando');
	});

	$('#tabla_ordencompra_detalle td input').keydown(function(ev) {
		if(ev.keyCode==27){
			$(this).val($(this).attr('data-original'));
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		}
	});

	 	$('.btn_eliminar').click(function(event) {
				var indice = $(this).attr('data-indice');	
				if(confirm('¿Seguro de quitar el item?')){
					agregar_pedidocompra('quitar', indice,'','','','quitar');
				}
			});


</script>
