<?php $__env->startSection("content"); ?>

<?php if(Session::has('message')): ?>
<div class="alert alert-info"><?php echo trans(Session::get('message')); ?></div>
<?php endif; ?>
<?php if($errors->all()): ?>
<div class="alert alert-danger">
	<?php echo HTML::ul($errors->all()); ?>

</div>
<?php endif; ?>
<div class="panel panel-default">
	<div class="panel-heading ">
		<span class="glyphicon glyphicon-list"></span>
		Departmental Request
	</div>
	<div class="panel-body">
		<?php echo Form::open(array('url' => 'inhouserequest', 'id' => 'inhouserequest', 'method' => 'POST')); ?>



		<div class="panel panel-primary">
			<table class="table table-bordered">

				<?php
				$voucher_number = intval( "0" .mt_rand(1,9) . mt_rand(0,9) . mt_rand(0,9) . mt_rand(0,9) );
				?>
				<td>
					<div class="form-group">
						<?php echo Form::label('department', 'Requesting Department', array('class'=>'control-label')); ?>

						<?php echo Form::select('department', array_merge(array(null => 'Select department...	'), $department_list), Input::old('department'), array('class' => 'form-control', 'id'=>'dept')); ?>

					</div>

					<div class="form-group">
						<?php echo Form::label('incharge', 'Department Head', array('class'=>'control-label')); ?>

						<?php echo Form::select('incharge', array_merge(array(null => 'Select your department head...	'), $incharge), Input::old('incharge'), array('class' => 'form-control', 'id' => 'incharge')); ?>

					</div>

					<div class="form-group">
						<?php echo Form::label('incharge_mail', 'Supervisor Email', array('class'=>'control-label')); ?>

						<?php echo Form::text('incharge_mail','',array('class' => 'form-control', 'rows' => '2', 'id'=>'incharge_mail')); ?>

					</div>

					<div class="form-group">
						<?php echo Form::label('requested_by', 'Requested By'); ?>

						<?php echo Form::text('requested_by', Auth::user()->name,array('class' => 'form-control', 'rows' => '2', 'id'=>'requested_by', 'readonly')); ?>

					</div>
					<div class="form-group">
						<?php echo Form::label('requestor_email', 'Requestor Mail'); ?>

						<?php echo Form::text('requestor_email', Auth::user()->email,array('class' => 'form-control','id'=>'requestor_email','readonly')); ?>

					</div>
				</td>

				<td class="text-center">
					<div class="form-group" style="color:black; background-color:powderblue; font-family:typewriter;">
						<h3>Voucher Number :: 0<?php echo $voucher_number; ?></h3>
						<?php echo Form::text('folio_number', $voucher_number, array('class' => 'control-label', 'readonly', 'hidden')); ?>

					</div>
				</td>
			</div>
		</table>


		<form method="post" id="insert_form">
			<div class="table-repsonsive">
				<span id="error"></span>
				<table class="table table-bordered" id="item_table">
					<tr>
						<th class="text:center">UoM</th>
						<th class="text:center">Item/Commodity</th>
						<th class="text:center">Quantity</th>
						<th><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus"></span></button></th>
					</tr>
				</table>
			</form>
			<br>
			<div class="form-group actions-row">
				<?php echo Form::button("<span class='glyphicon glyphicon-save'></span> ".trans('messages.save'),
				array('class' => 'btn btn-primary', 'onclick' => 'submit()')); ?>

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


			<?php
			Session::put('SOURCE_URL', URL::full());?>
		</div>
	</div>


	<script>
		$(document).ready(function(){

			$(document).on('click', '.add', function(){
				var html = '';
				html += '<tr>';
					html += '<td> <select id="uom" name="issue_unit[]"> <?php foreach($metrics as $key =>$metric): ?><option value="<?php echo $key; ?>"><?php echo $metric; ?></option><?php endforeach; ?></td>';
						html += '<td><select id="commodity" name ="commodity_id[]"> <?php foreach($commodities as $key =>$commodity_id): ?><option value="<?php echo $key; ?>"><?php echo $commodity_id; ?></option> <?php endforeach; ?></td>';
							html += '<td><input id="qty" type="text" name="quantity[]" class="form-control quantity" /></td>';
							html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus"></span></button></td></tr>';
							$('#item_table').append(html);
						});

						$(document).on('click', '.remove', function(){
							$(this).closest('tr').remove();
						});

						let inputs = document.querySelectorAll("#dept, #incharge, #incharge_mail, #uom, #commodity, #qty");
						var savebtn = document.querySelector('#save')
						savebtn.disabled = true

						for (i = 0; i < inputs.length; i++) {
							inputs[i].addEventListener('input',() => {
								let values = []
								inputs.forEach(v => values.push(v.value))
								savebtn.disabled = values.includes('')
							})
						}




						$('#insert_form').on('submit', function(event){
							event.preventDefault();
							var error = '';
							$('.uom').each(function(){
								var count = 1;

								if($(this).val() == '')
								{
									error += "<p>Enter Unit Of Measure at "+count+" Row</p>";
									return false;
								}
								count = count + 1;
							});

							$('.commodity').each(function(){
								var count = 1;
								if($(this).val() == '')
								{
									error += "<p>Please Select atleast an Item "+count+" Row</p>";
									return false;
								}
								count = count + 1;
							});

							$('.qty').each(function(){
								var count = 1;
								if($(this).val() == '')
								{
									error += "<p>Hey, enter the quantities needed at "+count+" Row</p>";
									return false;
								}
								count = count + 1;
							});

							var form_data = $(this).serialize();
							if(error == '')
							{
								$.ajax({
									url:"insert.php",
									method:"POST",
									data:form_data,
									success:function(data)
									{
										if(data == 'ok')
										{
											$('#inhouserequest').find("tr:gt(0)").remove();
											$('#error').html('<div class="alert alert-success">Request Submitted</div>');
										}
									}
								});
							}
							else
							{
								$('#error').html('<div class="alert alert-danger">'+error+'</div>');
							}
						});

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

<?php echo $__env->make("layout", array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>