<?php
require_once ("../../config/Cado.php");
require_once ("cCategoria.php");
$oCategoria = new cCategoria();
if($_POST['action']=="insertar")
{
	$cat_nom=$_POST['cat_nom'];
}
if($_POST['action']=="editar"){
	$dts=$oCategoria->mostrarUno($_POST['cat_id']);
	$dt = mysqli_fetch_array($dts);
	$cat_nom=$dt['tb_categoria_nom'];
	$cat_idp=$dt['tb_categoria_idp'];
	$cat_color=$dt['tb_categoria_color'];
	$verped=$dt['tb_categoria_verped'];
	mysqli_free_result($dts);
}
?>
				<form role="form" id="for_cat" method="POST">
						<input name="action_categoria" id="action_categoria" type="hidden" value="<?php echo $_POST['action']?>">
						<input name="hdd_cat_id" id="hdd_cat_id" type="hidden" value="<?php echo $_POST['cat_id']?>">
						
					<div class="box-header">
						<h3 class="box-title">Información de la Categoría</h3>
					</div>
					<div class="box-body">
					<div class="form-group">							
							<div class="row">
								<div class="col-lg-6 col-sm-3 col-xs-12">
									<label for="txt_cat_nom">Nombre:</label>
									<input name="txt_cat_nom" id="txt_cat_nom" type="text" value="<?php echo $cat_nom ?>" class="form-control">
								</div>
								
								
								<div class="col-lg-6 col-sm-3 col-xs-12"   >
									<label for="cmb_cat_idp">Categoría Padre</label>      
									<select name="cmb_cat_idp" id="cmb_cat_idp" class="form-control"></select>
								</div>
								<!--
								<div class="col-lg-2 col-sm-3 col-xs-6">
									<label for="cmb_cat_idp">Eliga un color:</label>   <br />   
									<input type="color" name = "txt_color" id="txt_color" value="<?php echo $cat_color;?>" />
								</div>
								
								 <div class="col-lg-2 col-sm-3 col-xs-6">	
										
									 	<div style="position:absolute;top:19px" class=" input-group checkbox icheck">
									 		<label title="Ver en Pedidos" >
									 			<input title="Ver en Pedidos" name="chk_cat_verped" id="chk_cat_verped"  
												<?php if(isset($verped)){ if($verped==1){echo 'checked';}else{}  }else{echo '';}?> type="checkbox"> Ver Pedidos
									 		</label>   
									 	</div>	 
									 </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">
	$('input').iCheck({
		checkboxClass: 'icheckbox_square-blue',
		radioClass: 'iradio_square-blue',
      increaseArea: '20%' 
	});
	
	$('#txt_cat_nom').change(function(){
		$(this).val($(this).val().toUpperCase());
	});
	$("#save_form").click(function() {
		$("#for_cat").submit();
	});
	function cmb_cat_idp(){
		$.ajax({
			type: "POST",
			url: "../categoria/cmb_cat_idp.php",
			async:true,
			dataType: "html",                      
			data: ({
				cat_id: "<?php echo $_POST['cat_id']?>",
				cat_idp: "<?php echo $cat_idp?>"
			}),
			beforeSend: function() {
				$('#cmb_cat_idp').html('<option value="">Cargando...</option>');
			},
			success: function(html){
				$('#cmb_cat_idp').html(html);
			}
		});
	}
	$(document).ready(function() {
		$('#for_cat').bootstrapValidator({
	      //live: 'disabled',
	      message: 'Valor no valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: {	      	
	      	txt_cat_nom: {
	      		group: '.col-xs-6',
	      		validators: {
	      			notEmpty: {
	      				message: 'Este campo es requerido'
	      			}
	      		}
	      	}
	      }
	  }).on('success.form.bv', function(e) {
	  	$('#msj_categoria').html("Cargando...");
	  	$('#msj_categoria').show(100);
	  	/*--- Save ---*/
	  	$.ajax({
	  		type: "POST",
	  		url: "../categoria/categoria_reg.php",
	  		async:true,
	  		dataType: "json",
	  		data: $("#for_cat").serialize(),
	  		beforeSend: function() {
	  			$('#msj_categoria').show(100);
	  		},
	  		success: function(data){			
	  			
	  			
				<?php if($_POST['vista']=="producto"){?>
	  				$('#div_categoria_form_dialog').modal('hide');
					$('.div_categoria_form').html('');
					toastr["success"](data.cat_msj);
					cmb_cat_id(data.cat_id);					
	  			<?php }else if($_POST['vista']=="categoria"){?>
				$('.modal').modal('hide');
				$('.in').removeClass('modal-backdrop');			
				toastr["success"](data.cat_msj);					
				<?php 					
				}?>
				
	  		},
	  		complete: function(){
				}
			});
	  });
	// EJECUTAR APENAS CARGUE
	cmb_cat_idp();
	$('#cmb_cli_tip').focus();
});
	/*$("#txt_cli_doc").autoNumeric({
		aSep:'',
		vMin:'',
		vMax:'99999999999'
	});*/
	
	$("#txt_cat_nom").keypress(function(e){
		if(e.which==13){$("#cmb_cat_idp").focus();return false;}
	});
	$("#cmb_cat_idp").keydown(function(e){
		if (e.which == 13){$("#save_form").focus();return false;}
	});
	
</script>