<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("cTraspaso.php");
$oTraspaso = new cTraspaso();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once ("../formatos/formato.php");


if($_POST['tra_id']!=""){


	$dts= $oTraspaso->mostrarUno($_POST['tra_id']);

	$dt = mysqli_fetch_array($dts);

		$fec		=mostrarFecha($dt['tb_traspaso_fec']);

		$alm_id_ori	=$dt['tb_almacen_id_ori'];

		$alm_id_des	=$dt['tb_almacen_id_des'];

	mysqli_free_result($dts);


$dts1=$oTraspaso->mostrar_traspaso_detalle($_POST['tra_id']);

$num_rows= mysqli_num_rows($dts1);

?>

<script type="text/javascript">

$(document).ready(function() {


}); 
</script>


<div class="table-responsive">	
<table id="tabla_traspaso_detalle" class="table table-bordered table-striped">

	<thead>

		<tr>

			<th>ARTÍCULO</th>

			<th>MARCA</th>

          	<th>UNIDAD</th>

          	<th align="right">CANTIDAD</th>

		</tr>

	</thead>

	<?php

    if($num_rows>=1){

    ?>

    <tbody>

    <?php

		while($dt1 = mysqli_fetch_array($dts1))

		{

	?>

		<tr>


			<td>


				<?php echo $dt1['tb_producto_nom']; ?>


			</td>


			<td>


				<?php echo $dt1['tb_marca_nom']; ?>


			</td>


			<td>


				<?php echo $dt1['tb_unidad_nom']; ?>


			</td>


            <td align="right"><?php echo $dt1['tb_traspasodetalle_can'];?></td>


		</tr>


    <?php


        }


        mysqli_free_result($dts1);


    ?>


    </tbody>


    <?php }?>


</table>


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





<?php 


}


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


?>

<style>

	#tra_car_tabla td{

		vertical-align: middle;

		padding: 4px 4px !important;

	}

	#tra_car_tabla td.editable,#tra_car_tabla td.editable *{

		cursor: text !important;

		border-bottom-color: #ccc;

		border-bottom-style: dashed; 

		border-bottom-width: 0.5px;

	}


	#tra_car_tabla td.editando{

		background-color: #FFFABA;

		outline: 2px solid #35A4F3;

	}


	#tra_car_tabla td input{

		height: 100%;

		width: 100%;

		box-sizing: border-box;

		display: inline-block;

		border: none;

		background: transparent;

	}

	#tra_car_tabla td input:focus{

		background: transparent;

		height: 100%;

		width: 100%;

		outline: none;

		border: none;

	}

	#tra_car_tabla td input[readonly]{

		background: transparent;

	}

</style>

<div class="table-responsive">	

<table cellspacing="1" id="tra_car_tabla" class="table table-bordered table-striped" cellpadding="2">

	<thead>

		<tr>

			<th>PRODUCTO</th>

			<th>MARCA</th>
			<th>CATEGORIA</th>

			<th>UNIDAD</th>

			<th align="right">CANTIDAD</th>

			<th></th>

		</tr>

	</thead>

	<tbody>

		<?php $total = 0;
			if($num_rows>0){
				
			foreach($_SESSION['traspaso_can'] as $indice=>$cantidad) {

			$dts1=$oCatalogo->mostrarUno($_SESSION['traspaso_catid'][$indice],$_SESSION['traspaso_marid'][$indice]);
			$dt1 = mysqli_fetch_array($dts1);
			

		?>

			<tr class="even">

				<td>

					<?php echo $dt1['tb_producto_nom']?>

				</td>

				<td>

					<?php echo $dt1['tb_marca_nom']?>

				</td>
				<td>

					<?php echo $dt1['tb_categoria_nom']?>

				</td>

				<td>

					<?php echo $dt1['tb_unidad_abr']?>

				</td>

				<td align="right" width="100" class="editable">

					<input type="text" value="<?php echo $cantidad ?>" 

					data-original="<?php echo $cantidad ?>" readonly 

					data-cat_id="<?php echo $_SESSION['traspaso_catid'][$indice] ?>"

					data-mar_id="<?php echo $_SESSION['traspaso_marid'][$indice] ?>" 

					style="text-align: right; font-size: 9pt;">

				</td>

				<td align="center">

					<span style="cursor:pointer" onclick="eliminar_itemagregado('<?php echo $_SESSION['traspaso_catid'][$indice].$_SESSION['traspaso_marid'][$indice];?>')" class="btn btn-danger"><i class="fas fa-trash"></span>

				</td>

			</tr>

			<?php mysqli_free_result($dts1); }
			
			}?>

	</tbody>

</table>

</div>

<input type="hidden" id="hdd_tra_numite" name="hdd_tra_numite" value="<?php echo ($numitems)?$numitems:'' ?>">

<script>


 $("#tra_car_tabla").DataTable( {

      "columnDefs": [

      { "orderable": false, "targets": [0] }

      ],

      "searching": false,

      "order":[[0,"asc"]]

    });


	$('#tra_car_tabla td input').autoNumeric({

		aSep: '',

		vMin: '0',

		vMax: '9999999'

	});


	$('#tra_car_tabla td input').keydown(function(ev) {

		if(ev.keyCode == 13 && !$(this).attr('readonly'))

		{

			var cat_id = $(this).attr('data-cat_id');

			var mar_id = $(this).attr('data-mar_id');

			var cant = $(this).val();
			var pre = 0;

			agregar_producto('editar',cat_id,cant,pre,mar_id);

			$(this).attr('readonly', 'true');

			$(this).parent().removeClass('editando');

		}

	});



	$('#tra_car_tabla td input').parent().click(function(ev) {

		var $input = $(this).find('input');

		var $td = $input.parent().addClass('editando');

		$input.removeAttr('readonly').focus().select();

	});





	$('#tra_car_tabla td input').blur(function(event) {


		$(this).val($(this).attr('data-original'));


		$(this).attr('readonly', 'true');


		$(this).parent().removeClass('editando');


	});


	$('#tra_car_tabla td input').keydown(function(ev) {

		if(ev.keyCode==27){

			$(this).val($(this).attr('data-original'));

			$(this).attr('readonly', 'true');

			$(this).parent().removeClass('editando');

		}

	});



	function eliminar_itemagregado(cat_id){

		if(confirm('¿Seguro de quitar el ítem?')){

				agregar_producto('eliminar', cat_id);

		}

	}


</script>

	

<?php 

}

?>