<?php
require_once ("../../config/Cado.php");
require_once ("cCaja.php");
$oCaja = new cCaja();
if($_POST['action']=="insertar")
{
	$tip=1;
}
if($_POST['action']=="editar")
{
	$dts=$oCaja->mostrarUno($_POST['caj_id']);
	$dt = mysqli_fetch_array($dts);
		$nom=$dt['tb_caja_nom'];
		$tip=$dt['tb_caja_tip'];
		$punvenid=$dt['tb_puntoventa_id'];
		$monid=$dt['tb_moneda_id'];
		$banid=$dt['tb_banco_id'];
		$cci=$dt['tb_caja_cci'];
		mysqli_free_result($dts);
}
?>
		<form role="form" id="for_caj" >
		<input name="action_caja" id="action_caja" type="hidden" value="<?php echo $_POST['action']?>">
		<input name="hdd_caj_id" id="hdd_caj_id" type="hidden" value="<?php echo $_POST['caj_id']?>">
			<div class="box-header">
            <h3 class="box-title">Información de Caja</h3>
			</div>
          
		  <div class="box-body">
			<div class="form-group">
			<div class="row">
       <div class="col-lg-4 col-xs-6">
          <label for="cmb_caj_tip">Tipo:</label>
          <select name="cmb_caj_tip" id="cmb_caj_tip" class="form-control">
				<option value="1" <?php if($tip==1)echo 'selected'?>>CAJA</option>
				<option value="2" <?php if($tip==2)echo 'selected'?>>BANCO</option> 
				<option value="3" <?php if($tip==3)echo 'selected'?>>BILLETERA DIGITAL</option> 
         	</select>
        </div>
		
		<div  id="" class="col-lg-8 col-sm-4 col-xs-6">
			<label id="" for="cmb_ban_id">Banco:</label>
			<select name="cmb_ban_id" id="cmb_ban_id"  class="form-control" >
			</select>
		</div>
		<div class="col-lg-4 col-sm-4 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  id="" class="col-lg-8 col-sm-4 col-xs-6">
			<label id="" for="cmb_mon_id">Moneda:</label>
			<select name="cmb_mon_id" id="cmb_mon_id"  class="form-control" >
			</select>
		</div>
		
        <div class="col-lg-6 col-xs-6">
           <label for="txt_caj_nom">Nro cuenta:</label>
        <input name="txt_caj_nom" type="text" id="txt_caj_nom"  value="<?php echo $nom?>" autocomplete="off"  class="form-control" >
        </div>
		
		<div class="col-lg-6 col-xs-6">
           <label for="txt_caj_cci">CCI:</label>
        <input name="txt_caj_cci" type="text" id="txt_caj_cci"  value="<?php echo $cci?>" autocomplete="off" class="form-control" >
        </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">
	
 function cmb_punven_id(punven_id){
	
	  	$.ajax({
			type: "POST",
			url: "../puntoventa/cmb_punven_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);
				
			}
		});		
	} 
 
	  
	function cmb_ban_id(banid)
	{	
		$.ajax({
			type: "POST",
			url: "../banco/cmb_ban_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				ban_id: banid
			}),
			beforeSend: function(){
				$('#cmb_ban_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_ban_id').html(html);
			}
		});
	}
	
	function cmb_mon_id(monid)
	{	
		$.ajax({
			type: "POST",
			url: "../moneda/cmb_mon_id.php",
			async:true,
			dataType: "html",                      
			data: ({
				mon_id: monid
			}),
			beforeSend: function(){
				$('#cmb_mon_id').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_mon_id').html(html);
			}
		});
	}
	
$(function() {
	
	$('#cmb_caj_tip').focus();
	cmb_mon_id(<?php echo $monid; ?>);
	cmb_ban_id(<?php echo $banid; ?>);
	cmb_punven_id(<?php echo $punvenid; ?>);
	
	$('#txt_caj_nom').change(function(){
		$(this).val($(this).val().toUpperCase());
	});
  	$("#save_form").click(function() {
	    $("#for_caj").submit();
  	});
  	$(document).ready(function() {
	    $('#for_caj').bootstrapValidator({
	      message: 'This value is not valid',
	      feedbackIcons: {
	        valid: 'glyphicon glyphicon-ok',
	        invalid: 'glyphicon glyphicon-remove',
	        validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: {
	        txt_caj_nom: {
	          group: '.col-lg-6',
	          validators: {
	            notEmpty: {
	              message: 'Este campo es requerido'
	            }
	          }
	        }
	      }
	    }).on('success.form.bv', function(e) {
	 
	      $.ajax({
				type: "POST",
				url: "../caja/caja_reg.php",
				async:true,
				dataType: "json",
				data: $("#for_caj").serialize(),
				beforeSend: function() {
					
				},
				success: function(data){
					$('.modal').modal('hide');
					$('.in').removeClass('modal-backdrop');						
					toastr["success"](data.caj_msj);	
					<?php
					if($_POST['vista']=="cmb_mar_id")
					{
						echo $_POST['vista'].'(data.caj_id)';
					}
					?>
				},
				complete: function(){
					<?php
					if($_POST['vista']=="caja_tabla")
					{
						echo $_POST['vista'].'()';
					}
					?>
					<?php if($_POST['vista']=="cmb_mar_id"){?>
						window.setTimeout(function() {
							$('#cmb_mar_id').siblings('input').focus();
						}, 200);
					<?php }?>
					
				}
			});
	    });
	});
	$('#cmb_caj_tip').keydown(function(ev) {
		if(ev.keyCode==13){$('#txt_caj_nom').focus(); return false;}
	});
});
$("#txt_caj_nom").keypress(function(e){
	if(e.which==13){
		$("#save_form").focus();return false;
	}
});
</script>