<?php $__env->startSection('titulo', 'Agregar un nuevo medicamento'); ?>

<?php $__env->startSection('botonera'); ?>
	<?php echo $__env->make('componentes.boton-volver-al-listado-de-medicamentos', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
	<?php echo $__env->make('componentes.boton-salir-con-aviso', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
	<?php /* modals */ ?>
	<?php echo $__env->make('componentes.modal-salir', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
	<?php echo $__env->make('componentes.modal-volver-al-listado-de-medicamentos', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
	<?php echo $__env->make('alerts.request', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

	<?php echo Form::open(['route'=>'medicamento.store', 'method'=>'POST']); ?>

		<?php echo $__env->make('medicamentos.forms.medicamentos', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
		<div class="col-xs-12">
			<?php echo Form::submit('Crear medicamento', ['class'=>'btn btn-primary']); ?>

		</div>
	<?php echo Form::close(); ?>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('scripts'); ?>
	<script type="text/javascript">
		$(document).ready(function(){
			var xhr = null;

			// Validar el codigo
			$('input[name=id]').on('keyup change', function(){
				var t=$(this);
				if(xhr) {xhr.abort()};
				xhr = $.ajax({
					headers:{'X-CSRF-TOKEN':'<?php echo e(csrf_token()); ?>'},
					url:'<?php echo e(URL::to("medicamento-validar-campos")); ?>',
					method:'post',
					data:{
						id:t.val()
					},
					beforeSend:function(){
						t.parent().removeClass('has-error has-success');
					},
					success:function(r){
						r ? t.parent().removeClass('has-succes').addClass('has-error') : t.parent().removeClass('has-error').addClass('has-success');
					}
				});
			});

			// Validar el nombre
			$('input[name=nombre]').on('keyup change', function(){
				var t=$(this);
				if(xhr) {xhr.abort()};
				xhr = $.ajax({
					headers:{'X-CSRF-TOKEN':'<?php echo e(csrf_token()); ?>'},
					url:'<?php echo e(URL::to("medicamento-validar-campos")); ?>',
					method:'post',
					data:{
						nombre:t.val()
					},
					beforeSend:function(){
						t.parent().removeClass('has-error has-success');
					},
					success:function(r){
						r ? t.parent().removeClass('has-succes').addClass('has-error') : t.parent().removeClass('has-error').addClass('has-success');
					}
				});
			});

			// Resaltar campos al eviar el formulario, detectando errores de validación
			<?php if(count($errors) > 0): ?>
				$('input').parent().addClass('has-success');
				<?php foreach($errors->getMessages() as $key => $value): ?>
					$('input[name=<?php echo e($key); ?>]').parent().removeClass('has-success').addClass('has-error');
				<?php endforeach; ?>
			<?php endif; ?>

			<?php echo $__env->make('componentes.script-mostrar-modal-request', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>;

		});
	</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.admin-bloqueo-vista', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>