<?php		
session_start();
require_once ("../../config/Cado.php");
require_once ("../../config/Datos.php");
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once("../producto/cProductomarcacodigo.php");
$oProductomarcacodigo = new cProductomarcacodigo();

require_once ("../consumopagar/cConsumopagar.php");
$oConsumopagar = new cConsumopagar();
	
require_once ("../formatos/formato.php");

$dts = $oConsumopagar->mostrarconsumopagardetalle($_POST["conpagid"]);
$num_rows = mysqli_num_rows($dts);

// echo $_POST["est"].'--'.$_POST["conpagid"];

?>
<style>
	#tabla_consumopagar_car td{
		vertical-align: middle;
		padding: 4px 4px !important;
	}
	#tabla_consumopagar_car td.editable,#tabla_consumopagar_car td.editable *{
		cursor: text !important;
		height: 100%; 
		border-bottom-color: #ccc;
		border-bottom-style: dashed; 
		border-bottom-width: 0.5px;
	}
	#tabla_consumopagar_car td.editando{
		background-color: rgba(18, 180, 243, 0.05);
		outline: 2px solid #35A4F3;
	}
	#tabla_consumopagar_car td input{
		height: 100%;
		width: 100%;
		box-sizing: border-box;
		display: inline-block;
		border: none;
		background: transparent;
	}
	#tabla_consumopagar_car td input:focus{
		background: transparent;
		height: 100%;
		width: 100%;
		outline: none;
		border: none;
	}
	#tabla_consumopagar_car td input[readonly]{
		background: transparent;
	}
</style> 
			
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding:0px;" >
	
	<div class="row" >
	
	<div class="col-lg-8 col-md-12 col-sm-12 col-xs-12" style="padding:0px;" >
	<div class="table-responsive">
	<input name="hdd_ven_numite" id="hdd_ven_numite" type="hidden" value="<?php echo $num_rows?>">
	
	<table id="tabla_consumopagar_car" class="table table-bordered table-striped">
		<thead>
			<tr>
				<th>ARTÍCULO</th>
				<th>MARCA</th>
				<th align="right">CAN</th>
				<th nowrap align="right" style="background-color:#00ACC1;color:#fff" >PRE. UNI.</th>			
				<th nowrap align="right" style="background-color:#00ACC1;color:#fff" >PRE. TOTAL</th>
				<th >&nbsp;</th>
			</tr>
		</thead>		 
		<tbody>
		
			<?php 
			$total = 0;
			if($num_rows>0){
				$banrojo = true;
				while($dt = mysqli_fetch_array($dts)) {
				
			$dts1=$oCatalogo->mostrarUno($dt['tb_catalogo_id'],$dt['tb_marca_id']);
			$dt1 = mysqli_fetch_array($dts1);
			$arttip_nom =  $dt1['tb_articulotipo_nom'];
			$emp_fac =  $dt1['tb_empaque_fac'];
			$preven =  $dt1['tb_productomarca_preven'];
			$prevenf =  $dt1['tb_productomarca_prevenf'];
			
			//$cantidad=$dt['tb_consumopagardetalle_can'];
			//$can2=$dt['tb_consumopagardetalle_can'];
			$can=$dt['tb_consumopagardetalle_can']*1;
			$can1=$dt['tb_consumopagardetalle_can']*1;
			
			//echo $can.'-' ;
			
			if($can>=$emp_fac){
				$can = $can/$emp_fac;
			}else{
				$can = $can;
			}
		
			
			//$can = $detven_can/$emp_fac;
		
			list($cane,$canf) = explode('.',$can);
			$canf = $canf * 1;
			
			//echo $cane.'--'.$canf ;
			//echo $canf.'-' ;


			if($cane!=="" && $canf==""){ 
				$can = $cane;
			}else if($cane=="" && $canf!==""){ 
				$can = $canf;
			}
			else if($cane!=="" && $canf!==""){ 
			
				$can = $can1;
			}
			
			
			$des = 0;
			$valor_uni=$dt['tb_consumopagardetalle_valuni'];
			
			$igv_uni = $valor_uni * $app_igv_igv;
			$pre_uni=$valor_uni * $app_igv_sub;
			$pre_uni=formato_money($pre_uni);
			
			//echo $can;
			
	
			
			$pre_tot = $pre_uni * $can;
			
			$val_tot = $pre_tot/$app_igv_sub;
			$igv_tot = $val_tot*$app_igv_igv;
			
			if($pre_tot<=0){
				$banrojo = false;
			}
					
					
					?>
					<tr style="font-weight:bold;" >
						<td nowrap style="font-size:10pt; font-weight:bold"><?php echo $dt1['tb_producto_nom']?></td>
						<td style="font-weight:bold;" ><?php echo $dt1['tb_marca_nom']?></td>
						<td align="right" width="50" class="" style="font-size:9pt; font-weight:bold"><?php echo $can ?></td>		
						<td style="font-weight:bold;" class="d-none" align="right"><?php echo formato_money($valor_uni)?></td>			
						<td style="font-weight:bold;" class="d-none" align="right"><?php echo formato_money($igv_uni)?></td>	
											
						<td align="right" width="80" class="editable" style="font-size:14pt; font-weight:bold;background-color:#fff9c4;">
							<input type="text" value="<?php echo formato_money($pre_uni) ?>" 
							data-preven="<?php echo formato_money($pre_uni) ?>" data-tip = "preuni" 
							data-original="<?php echo formato_money($pre_uni) ?>" 
							data-conpagdet="<?php echo $dt['tb_consumopagardetalle_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>" readonly 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" style="text-align: right;font-weight:bold;">
						</td>
						
						
						<td align="right" width="100" class="d-none" style="font-size:10pt; font-weight:bold" > <?php echo formato_money($val_tot) ; ?> </td>
						<td align="right" width="70" class="d-none" style="font-size: 9pt; font-weight: bold;"> <strong class="text-red"> <?php echo formato_money($igv_tot)?> </strong> </td>			
						
						<td align="right" width="100" class="editable" style="font-size:15pt; font-weight:bold;background-color:#fff9c4;">
							<input type="text" class="text-red" value="<?php echo formato_money($pre_tot) ; ?>" 
							data-original="<?php echo formato_money($pre_tot) ?>" 
							data-tip = "pretot"
							data-can="<?php echo $can ?>" 
							data-conpagdet="<?php echo $dt['tb_consumopagardetalle_id'] ?>" 
							data-cat_id="<?php echo $dt1['tb_catalogo_id'] ?>" 
							data-mar_id ="<?php echo $dt1['tb_marca_id'] ?>"
							readonly 
							style="text-align: right;font-weight:bold;">
						</td>
			
			
						<td align="center" nowrap="nowrap"> 
						<!--	<span  title="Editar" class="btn btn-primary btn-sm btn_editar" ><i class="fa fa-pencil"></i></span>
							-->
							<span data-indice ="<?php echo $dt['tb_consumopagardetalle_id']; ?>" class="btn btn-warning btn-sm btn_eliminar btn-flat" ><i class="fas fa-trash-alt"></i></span> 		
						<!--	<span class="btn btn-primary btn-sm" id="demo-ajax_<?php echo $dt1['tb_catalogo_id'] ?>" ><i class="fa fa-info"></i></span> 	
						-->
						</td>				
					</tr>
					
					<?php //if ($dt1['tb_producto_des']!=""){?>
					<!--
					<tr style="background-color:#fff;">
					<td colspan="8" >
					<span class="text-red" style="font-weight:bold;">ESPECIFICACIONES: Estructura / Accesorios y Equipamiento.</span>
					<div class="col-lg-12 col-sm-12 col-xs-12" style="word-wrap: break-word;word-break: break-all; font-weight:bold;" >
					<?php echo nl2br($dt1['tb_producto_des']); ?>
					</div>
					
					</td>
					</tr>
					-->
					<?php
					//}
					mysqli_free_result($dts1);
					$total1	+=$pre_tot;
				}
				
				$total_valorventa =$total1/1.18;
			$total_igv	=$total_valorventa*0.18;
			$total	=$total1;
				
			}
			?>
		</tbody>
	</table>

	<?php 
	if($num_rows=="" or $num_rows==0)
		echo 'Ningún producto agregado. <input type="hidden" id="evaluar_cantidad_items_venta" value="'.$cantidad_productos.'">';
	if($num_rows==1)
		echo $num_rows.' producto agregado. <input type="hidden" id="evaluar_cantidad_items_venta" value="'.$cantidad_productos.'">';
	if($num_rows>=2)
		echo $num_rows.' productos agregados. <input type="hidden" id="evaluar_cantidad_items_venta" value="'.$cantidad_productos.'">';
	
	

	?>

		
	</div>		
	</div>		
					
		<div class="col-lg-4 col-sm-12 col-12">
							<div class="row">

							<div class="col-lg-4 col-sm-6 col-4">
								<label>SubTotal:</label>
								<input name="txt_conpag_valven_edi" type="text"class="form-control form-control-lg " id="txt_conpag_valven_edi" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total_valorventa)?>"  > 							
							</div>
						
							<div class="col-lg-4 col-sm-6  col-4">
								<label>IGV:</label>
								<input name="txt_conpag_igv_edi" type="text" class="form-control form-control-lg " id="txt_conpag_igv_edi" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total_igv)?>"   > 
							</div>					
							<div class="col-lg-4 col-sm-6  col-4">
								<label>TOTAL:</label>
								<input name="txt_conpag_totsdes_edi" type="text" class="form-control form-control-lg " id="txt_conpag_totsdes_edi" style="text-align:right;font-weight: bold;" value="<?php echo formato_money($total)?>" > 
							</div>
							
							</div>
		</div>
		
		<div class="col-lg-12 col-sm-12 col-12   ">
			<div class="box-footer  div_botones_venta float-right ">	
				<button type="button" data-dismiss="modal" class="btn btn-lg btn-default d-none" >CERRAR</button>
				<button class="btn btn-danger btn-lg"  id="btn_editarconsumo">GUARDAR | CERRAR</button>
				<div class="col-lg-12 col-md-12 col-12" style="padding:5px;"></div> 
			</div>
		</div>
		
		
		</div>
		
		
		
		</div>
	
	
<script>

	$(function() {
		
		
	
		$('#tabla_consumopagar_car 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 cantidad = $(this).attr('data-can')
			var conpagdet_id = $(this).attr('data-conpagdet')
			var tip = $(this).attr('data-tip');
			var valor = $(this).val();
			
			if(tip=="pretot"){
					
				preuni = valor.toString().replace(',','')/cantidad;	
				valuni = preuni.toString().replace(',','');
				valuni = valuni / 1.18;
				agregar_producto('actualizaritem',cat_id, cantidad, valuni, mar_id,conpagdet_id);
			
			}else if(tip=="preuni"){
					
				valuni = valor.toString().replace(',','');
				valuni = valuni / 1.18;
				agregar_producto('actualizaritem', cat_id, cantidad, valuni, mar_id,conpagdet_id);
			
			}
				
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
				return false;
			}
		});
		$('#tabla_consumopagar_car td input').parent().click(function(ev) {
			var $input = $(this).find('input');
			var $td = $input.parent().addClass('editando');
			$input.removeAttr('readonly').focus().select();
		});
		$('#tabla_consumopagar_car td input').blur(function(event) {
			$(this).val($(this).attr('data-original'));
			$(this).attr('readonly', 'true');
			$(this).parent().removeClass('editando');
		});
		$('#tabla_consumopagar_car td input').keydown(function(ev) {
			if(ev.keyCode==27){
				$(this).val($(this).attr('data-original'));
				$(this).attr('readonly', 'true');
				$(this).parent().removeClass('editando');
			}
		});
	/* 	$( ".btn_editar" ).click(function() { 
			var $input = $(this).parent().parent().find('input[data-cantidad]');
			var $td = $input.parent().addClass('editando');
			$input.removeAttr('readonly').focus().select();
		}); */
		$( ".btn_eliminar" ).click(function() { 
			
			var indice = $(this).attr('data-indice');
				
			if(confirm('¿Seguro de quitar el item?')){
				agregar_producto('quitaritem', indice);
			}
		});
		
		$("#btn_editarconsumo").click(function() { 
			
			var indice = $(this).attr('data-indice');
			var valven = $("#txt_conpag_valven_edi").val();
			var igv = $("#txt_conpag_igv_edi").val();
			var tot = $("#txt_conpag_totsdes_edi").val();
				
			
				guardarcoti('editar',valven,igv,tot);
			
			
			
		});
		
		
		
		
	}); 
	
	
		
	
	
	
	
		function cambiar_estado(conpag_id){
					
			$.ajax({
				type: "POST",
				url: "../consumopagar/consumopagar_reg.php",
				async:true,
				dataType: "json",                      
				data: ({
					action:'actualizar_estado',
					conpag_id:conpag_id
				}),
				beforeSend: function() {
					
				},
				success: function(data){
					toastr["success"](data.msj);
					consumopagar_tabla();			
				}
			});
		}
	
	
	
	
</script>