<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<?php $__env->startSection("content"); ?>
	<div>
		<ol class="breadcrumb">
			<li><a href="<?php echo e(URL::route('user.home')); ?>"><?php echo trans('messages.home'); ?></a></li>
			<li><a href="<?php echo e(URL::route('order.index')); ?>">Orders</a></li>
			<li class="active">New Order</li>
		</ol>
	</div>
	<br>
	<br>
	<br>

  <div class="container">
		<div class="row">
			<div class="col-md-12">
				<?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>
						<br/>
						<br/>

   <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>
     <div align="center">
			 <?php echo Form::button("<span class='glyphicon glyphicon-save'></span> ".trans('messages.save'),
 			array('class' => 'btn btn-primary', 'id'=>'save', 'onclick' => 'submit()')); ?>

     </div>
    </div>
    </div>
   </form>
</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(); ?>