<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')); ?>">Dispatch</a></li>
		<li class="active">Dispatch</li>
	</ol>
</div>
<br>
<br>
<br>

	<div class="col-md-12">
			<?php echo Form::open(array('url' => 'dispatch', 'id' => 'dispatch', 'method' => 'POST')); ?>

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

					<?php
					$dispatch_number = intval( "0" .mt_rand(0,5) .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('dispatched_by', 'Dispatched By'); ?>

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

						</div>

						<div class="form-group">
							<?php echo Form::label('dispatch_date', 'Date Dispatched'); ?>

							<?php echo Form::text('dispatch_date', Input::old('dispatch_date'),array('class' => 'form-control standard-datepicker standard-datepicker-nofuture','required'=>'required', 'id'=>'dispatch_date')); ?>

						</div>

					</td>

					<td class="text-center">
						<div class="form-group" style="color:black; background-color:powderblue; font-family:typewriter;">
							<h3>Dispatch Number :: 0<?php echo $dispatch_number; ?></h3>
							<?php echo Form::text('dispatch_number', $dispatch_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">Hub / Facility</th>
							<th class="text:center">District</th>
							<th class="text:center">Health Region</th>
							<th class="text:center">Item / Commodity</th>
							<th class="text:center">Qty</th>
							<th class="text:center">UoM</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>

		<script>
			$(".standard-datepicker-nofuture").datepicker({
				maxDate: 0
			});
		</script>
		<script>


			$(document).ready(function(){

				$(document).on('click', '.add', function(){
					var html = '';
					html += '<tr>';
						html += '<td> <select id="facility_id" name="facility_id[]"> <?php foreach($facility as $key =>$facility): ?><option value="<?php echo $key; ?>"><?php echo $facility; ?></option><?php endforeach; ?></td>';
						html += '<td> <select id="uom" name="district_id[]"> <?php foreach($district as $key =>$district): ?><option value="<?php echo $key; ?>"><?php echo $district; ?></option><?php endforeach; ?></td>';
						html += '<td><input id="region" type="text" name="region[]" class="form-control region" /></td>';
						html += '<td> <select id="commodity_id" name="commodity[]"> <?php foreach($commodity as $key =>$commodity): ?><option value="<?php echo $key; ?>"><?php echo $commodity; ?></option><?php endforeach; ?></td>';
								html += '<td><input id="qty" type="text" name="quantity[]" class="form-control quantity" /></td>';
								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><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();
							});

							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')
											{
												$('#dispatch').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 echo $__env->make("layout", array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>