<?php 
session_start();
date_default_timezone_set('America/Lima');
require_once ("../../config/Cado.php");
require_once ("cDevolucionproveedor.php");
$oDevolucionproveedor = new cDevolucionproveedor();
require_once ("../compra/cCompra.php");
$oCompra = new cCompra();
require_once ("../producto/cCatalogo.php");
$oCatalogo = new cCatalogo();
require_once("../formatos/formato.php");
?>
<style type="text/css">
.ui-autocomplete
{
    z-index: 99999; 
}
</style>
<div class="col-lg-12 col-md-12 col-xs-12 " >
	<form  id="for_dev2" > 
		<div class="box-header with-border">
			<h3 class="box-title">Busque y seleccione un documento</h3>
		</div>
		
		<input type="hidden" name="hdd_emp_id" id="hdd_emp_id" value="<?php echo $_POST['empid']; ?>" />
		<input type="hidden" name="hdd_dev_id" id="hdd_dev_id" value="<?php echo $_POST['devid']; ?>" />
		<input type="hidden" name="action" id="action" value="relacionar" />
		<div class="col-lg-6 col-sm-6 col-xs-12"> 
			<label for="txt_dev_numdocrel"> NUMERO DOCUMENTO:</label></br>
			<input name="txt_dev_numdocrel" id="txt_dev_numdocrel" type="text" value="<?php echo $emp; ?>" class="form-control" >
			<input type="hidden" id="hdd_fil_doc_id" name="hdd_fil_doc_id" />
		</div>
		
		<div class="col-lg-12 col-sm-12 col-xs-12" style="height:15px;"> </div>		
	</form>
	<div class="box-footer float-right ">
			<span  data-dismiss="modal" style="cursor:pointer" class="btn btn-default cancel">Cancelar</span>
			<span data-dismiss="modal" style="cursor:pointer" class="btn btn-primary" id="save_form">Guardar</span>
	</div>	
</div>				
<script type="text/javascript">
$("#save_form").hide();
		$("#txt_dev_numdocrel").keydown(function(e){
			if (e.which == 13){
			
			
				return false;
			}
		});
		
$( "#txt_dev_numdocrel" ).autocomplete({
			minLength: 1,
			source: "../devolucionproveedor/devolucionproveedor_complete_rel.php",
			select: function(event, ui){			
				$("#hdd_fil_doc_id").val(ui.item.id);
				
				validar_items(ui.item.id);
				
			}
		});
		function validar_items(docrelid){
			$('#div_mensaje_dialog').modal();
		
			$.ajax({
	  		type: "POST",
	  		url: "../devolucionproveedor/devolucionproveedor_validarrelacion.php",
	  		async:true,
	  		dataType: "json",
	  		data: ({
				action :'<?php echo $_POST['action']?>',
				devid : <?php echo $_POST['devid']?>,
				docrelid : docrelid
			}),
	  		beforeSend: function(){					 
	  			
			var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
				$('.div_mensaje').html(div);
			
			toastr["info"]('Validando detalles...');
			
	  		},
	  		success: function(data){
			
			$('#div_mensaje_dialog').modal('hide');
						
			if(data.est==false)
			{	
				$("#save_form").hide(100);
				$("#txt_dev_numdocrel").val('');
				$("#hdd_fil_doc_id").val('');
				toastr["error"](data.msj);
				
			}else{
				
				$("#save_form").show(100);
				toastr["success"](data.msj);
			
			}
			
	  		},
	  		complete: function(){  	
					// devolucion_tabla();
			}
	  	});	
			
		}
	
	
$('#save_form').on('click', function() {  
		$("#for_dev2").submit();
});
	
	$('#for_dev2').bootstrapValidator({
	     
	      framework: 'bootstrap',
	      message: 'Valor no valido',
	     /* feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },*/
	      fields: {	      	
	      	hdd_fil_doc_id: {
	      		group: '.col-lg-4',
	      		validators: {
	      			notEmpty: {
	      				message: 'Requerido *'
	      			}
	      		}
	      	}
	      }
	  }).on('success.form.bv', function(e) 
	  {
	  	
		
	  	$.ajax({
	  		type: "POST",
	  		url: "../devolucionproveedor/devolucionproveedor_reg.php",
	  		async:true,
	  		dataType: "json",
	  		data: $("#for_dev2").serialize(),
	  		beforeSend: function(){					 
	  			
				var div = "<center> <br> <span class='spinner-border spinner-border-lg mr-2'></span> <span style='font-size:1.5rem;'>PROCESANDO PETICIÓN</span></center>";	
					$('.div_devolucionproveedor_formrel').html(div);
				
	  		},
	  		success: function(data){
				
				toastr["info"](data.dev_msj);
				
				$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');	
	  		},
	  		complete: function(){  	
			devolucion_tabla();
			}
	  	});	 
	  });
			
			
			
			
	
</script>