<?php
session_start();
require_once ("../../config/Cado.php");
require_once ("cTalonario.php");
$oTalonario = new cTalonario();
if($_POST['action']=="insertar")
{
	$est=1;
	$ini=1;
	$num=1;
	$fin='99999999';
	//$punven_id = $_SESSION['puntoventa_id'];
}
if($_POST['action']=="editar"){
	$dts=$oTalonario->mostrarUno($_POST['tal_id']);
	$dt = mysqli_fetch_array($dts);
	$doc_id=$dt['tb_documento_id'];
	$punven_id= $dt['tb_puntoventa_id'];
	$mod_id=$dt['tb_modulo_id'];
	$ser=$dt['tb_talonario_ser'];
	$ini=$dt['tb_talonario_ini'];
	$fin=$dt['tb_talonario_fin'];
	$num=$dt['tb_talonario_num'];
	$est=$dt['tb_talonario_est'];
	mysqli_free_result($dts);
	
	echo $doc_id.'--';
	
	
}
?>
			
				<form role="form" id="for_tal" method="POST">
					<input name="action_talonario" id="action_talonario" type="hidden" value="<?php echo $_POST['action']?>">
					<input name="hdd_tal_id" id="hdd_tal_id" type="hidden" value="<?php echo $_POST['tal_id']?>">
					<div class="box-header">
						<h3 class="box-title">Información de Talonario</h3>
					</div>
					<div class="box-body">
						<div class="form-group">
							<div class="row">        
								<div class="col-lg-3 col-xs-6">
									<label for="cmb_mod_id">Modúlo:</label>
									<select name="cmb_mod_id" id="cmb_mod_id" class="form-control"></select> 
								</div>
								<div class="col-lg-3 col-xs-6">
									<label for="cmb_doc_id">Documento:</label>
									<select name="cmb_doc_id" id="cmb_doc_id" class="form-control"></select> 
								</div> 
									<div class="col-lg-3  col-sm-3 col-xs-6">
								<label for="cmb_punven_id">Punto V.:</label>
								<select name="cmb_punven_id" id="cmb_punven_id" class="form-control"></select>
							</div>
							
								<div class="col-lg-3 col-xs-6">
									<label for="txt_tal_ser">Serie:</label>
									<input name="txt_tal_ser" type="text" id="txt_tal_ser" value="<?php echo $ser?>" class="form-control"> 
								</div> 
								<div class="col-lg-3 col-xs-6">
									<label for="txt_tal_ini">Inicio:</label>
									<input type="text" name="txt_tal_ini" id="txt_tal_ini" style="text-align:right" maxlength="8" value="<?php echo $ini?>" class="form-control">
								</div> 
								<div class="col-lg-3 col-xs-6">
									<label for="txt_tal_fin">Fin:</label>
									<input type="text" name="txt_tal_fin" id="txt_tal_fin" style="text-align:right" maxlength="8" value="<?php echo $fin;?>" class="form-control">
								</div> 
								<div class="col-lg-3 col-xs-6">
									<label for="cmb_doc_id">Correlativo:</label>
									<input type="text" name="txt_tal_num" id="txt_tal_num"  class="form-control" style="text-align:right" maxlength="8" value="<?php echo $num?>">
								</div> 
							
								<div class="col-lg-3 col-xs-6">
									<label for="cmb_tal_est">Estado:</label>
									<select name="cmb_tal_est" id="cmb_tal_est"  class="form-control">
										<option value="">-</option>
										<option value="1" <?php if($est=='1') echo 'selected'?>>ACTIVO</option>
										<option value="2" <?php if($est=='2') echo 'selected'?>>INACTIVO</option>
									</select> 
								</div> 
							</div>
						</div>
					</div>
				</form>
				<div class="box-footer float-right">
					<button type="button" data-dismiss="modal" class="btn btn-default cancel">Cancelar</button>
					<button class="btn btn-primary " id="save_form">Guardar</button>
				</div>
			
<script type="text/javascript">
cmb_punven_id(<?php echo $punven_id?>);
function cmb_mod_id(modid){
		$.ajax({
			type: "POST",
			url: "../modulo/cmb_mod_id.php",
			async : true,
			dataType: "html",                      
			data: ({
				mod_id: modid
			}),
			beforeSend: function() {
				$('#cmb_mod_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_mod_id').html(html);
			}
		});
	}
	function cmb_doc_id(modid,docid){
		$.ajax({
			type: "POST",
			url: "../documento/cmb_doc_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				mod_id:	modid,
				doc_id: docid,
				vista:	'cmb_doc_id'
			}),
			beforeSend: function() {
				$('#cmb_doc_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_doc_id').html(html);
			}
		});
	}
 function cmb_punven_id(punven_id){
	
	  	$.ajax({
			type: "POST",
			url: "../puntoventa/cmb_punvent_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				punven_id:punven_id
			}),
			beforeSend: function(){
				$('#cmb_punven_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){		
				$('#cmb_punven_id').html(html);
				
			}
		});		
	} 
 
 
	
	$("#save_form").click(function() {
		$("#for_tal").submit();
	});
	$(document).ready(function() {
		
	
		$('#for_tal').bootstrapValidator({
	      //live: 'disabled',
	      message: 'Valor no Valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: { 	      	      
	      	cmb_mod_id: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Este campo es requerido'
	      			}
	      		}
	      	},
	      	cmb_doc_id: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Este campo es requerido'
	      			}
	      		}
	      	},
	      	txt_tal_ser: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Este campo es requerido'
	      			}
	      		}
	      	},
	      	txt_tal_ini: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Este campo es requerido'
	      			},
	      			regexp: {
	      				regexp: /^[0-9]+$/,
	      				message: '*'
	      			}
	      		}
	      	},
	      	txt_tal_fin: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Este campo es requerido'
	      			},
	      			regexp: {
	      				regexp: /^[0-9]+$/,
	      				message: 'Caracteres no permitidos'
	      			}
	      		}
	      	},
	      	txt_tal_num: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Este campo es requerido'
	      			},
	      			regexp: {
	      				regexp: /^[0-9]+$/,
	      				message: 'Caracteres no permitidos'
	      			}
	      		}
	      	},
	      	cmb_tal_est: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: 'Este campo es requerido'
	      			}
	      		}
	      	},
	      }
	  }).on('success.form.bv', function(e) {
	  	/*--- Save ---*/
	  	$.ajax({
	  		type: "POST",
	  		url: "../talonario/talonario_reg.php",
	  		async:true,
	  		dataType: "html",
	  		data: $("#for_tal").serialize(),
	  		beforeSend: function() {		
	  		
	  		},
	  		success: function(html){						
	  			$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');			
				toastr["success"](html);	
	  		},
	  		complete: function(){
	  			talonario_tabla();
	  		}
	  	});
	  });
	});
	
	/*$("#txt_cli_doc").autoNumeric({
		aSep:'',
		vMin:'',
		vMax:'99999999999'
	});*/ 
	//juego de focos
	$("#cmb_mod_id").keydown(function(e){
		if (e.keyCode == 13){ $("#cmb_doc_id").focus(); return false;}
	});
	$("#cmb_doc_id").keydown(function(e){
		if (e.keyCode == 13){ $("#cmb_punven_id").focus(); return false;}
	});
	$("#cmb_punven_id").keydown(function(e){
		if (e.keyCode == 13){ $("#cmb_alm_id").focus(); return false;}
	});
	$("#cmb_alm_id").keydown(function(e){
		if (e.keyCode == 13){ $("#txt_tal_ser").focus(); return false;}
	});
	$("#txt_tal_ser").keydown(function(e){
		if (e.keyCode == 13){ $("#txt_tal_ini").focus(); return false;}
	});		
	$("#txt_tal_ini").keydown(function(e){
		if (e.keyCode == 13){ $("#txt_tal_fin").focus(); return false;}
	});
	$("#txt_tal_fin").keydown(function(e){
		if (e.keyCode == 13){ $("#txt_tal_num").focus(); return false;}
	});
	$("#txt_tal_num").keydown(function(e){
		if (e.keyCode == 13){ $("#cmb_imp_id").focus(); return false;}
	});
	$("#cmb_imp_id").keydown(function(e){
		if (e.keyCode == 13){$('#cmb_tal_est').focus(); return false;}
	});
	$("#cmb_tal_est").keydown(function(e){
		if (e.keyCode == 13){ $("#save_form").focus(); return false;}
	});
	cmb_mod_id(<?php echo $mod_id?>);
	$('#cmb_mod_id').focus();
	
	$('#cmb_mod_id').change(function(){
		cmb_doc_id($('#cmb_mod_id').val(),'<?php echo $doc_id?>');
	});
	
	cmb_doc_id('<?php echo $mod_id?>','<?php echo $doc_id?>');
	
	
	
</script>