<?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>
<div class="container-fluid">
	<div class="row">
		<div class="col-md-12">
			<?php echo Form::open(array('url' => 'facilityrequest', 'id' => 'form-facilityrequest', 'method' => 'POST')); ?>

			<div class="panel panel-primary">
				<h1 class="panel-title" style="text-align:center"><?php echo HTML::image(Config::get('kblis.organization-logo'),  Config::get('kblis.country') . trans('messages.court-of-arms'), array('width' => '150px')); ?>

					<br>
					<strong><?php echo strtoupper(Config::get('constants.FACILITY_REQUEST_FORM_HEADER')); ?>

						<br> <?php echo Config::get('kblis.address-info'); ?></strong></h1>
						<br><br>
						<br>
						Please Fill this form properly and submit to UNHLS as per the NMS Distribution Schedule
						<div class="panel panel-warning">
							<tr>
								<td>
									<br>
									<div class="form-group col-xs-2">
										<?php echo Form::label('facility_id', 'Facility Name:',  array('class'=>'col-sm-2')); ?>

										<?php echo Form::select('facility_id', $facility, Input::old('facility_id'), array('class' => 'form-control col-sm-4')); ?>


										<?php if($errors->has('facility_id')): ?>
										<span class="text-danger">
											<strong><?php echo $errors->first('facility_id'); ?></strong>
										</span>
										<?php endif; ?>
									</div>

									<div class="form-group col-xs-2">
										<?php echo Form::label('district_id', 'Facility District:',  array('class'=>'col-sm-2')); ?>

										<?php echo Form::select('district_id', $district, Input::old('district_id'), array('class' => 'form-control col-sm-4')); ?>


										<?php echo Form::label('ip', 'Facility IP:', array('class'=>'col-sm-2')); ?>

										<?php echo Form::select('ip_id', $ip, Input::old('ip_id'), array('class' => 'form-control col-sm-4')); ?>

									</div>

									<div class="form-group col-xs-2">
										<?php echo Form::label('start_date', 'Start Date:',  array('class'=>'col-sm-2')); ?>

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


										<?php echo Form::label('end_date', 'End Date:',  array('class'=>'col-sm-2')); ?>

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

									</div>

									<div class="form-group col-xs-2">
										<?php echo Form::label('requested_by', 'Prepared By:', array('class' =>'col-sm-2 ')); ?>

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


										<?php echo Form::label('date_submitted', 'Date Submitted:', array('class' =>'col-sm-2 ')); ?>

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

									</div>

									<div class="form-group col-xs-2">
										<?php echo Form::label('incharge_mail', 'Incharge E-mail:', array('class' =>'col-sm-2 ')); ?>

										<?php echo Form::text('incharge_mail',Input::old('incharge_mail'), array('class' => 'form-control col-sm-4')); ?>

									</div>
							</td>

						</tr>
						<div class="panel panel-primary">

							<form method="post" id="insert_form"><input type="hidden" name="row_count" id="row_count" value=0>
								<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">UoM</th>
											<!-- <th class="text:center">Unit Price</th> -->
											<th class="text:center">Total Number of Samples<br><small>(Collected during the reporting period)</small></th>
											<th class="text:center">Opening Balance <small><br>at start <br>of opening cycle</small></th>
											<th class="text:center">Qty Received <small><br>during the <br> reporting period</small></th>
											<th class="text:center">Total Consumption <small><br>during the <br> reporting period</small></th>
											<th class="text:center">Losses / Adjustments<br><samll>(-/+)</small></th>
												<th class="text:center">Total Closing Balance</th>
												<th class="text:center">Maximum Stock Quantity Required</th>
												<th class="text:center">Quantity To Order</th>
												<!-- <th class="text:center">Total Cost</th> -->
												<th class="text:center">Comments</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', 'onclick' => 'submit()')); ?>

										</div>
									</div>
								</div>
							</div>
						</div>
					</form>
				</div>
			</div>
			</div>

			<script>
			$(".standard-datepicker-nofuture").datepicker({
				maxDate: 0
			});
			//Date picker
			$('#datepicker').datepicker({
				autoclose: true
			})
				$(document).ready(function(){

					$(document).on('click', '.add', function(){
						var row_count = parseInt($('#row_count').val())+1;
						$('#row_count').val(row_count);
						var keyup = 'onkeyup="compute('+row_count+')"';

						var html = '';
						html += '<tr>';
							html += '<td><select name ="commodity_id[]"> <option value="">Select Item.....</option><?php foreach($commodities as $key =>$commodity_id): ?><option value="<?php echo $key; ?>"><?php echo $commodity_id; ?></option> <?php endforeach; ?></td>';
								html += '<td><select name ="issue_unit[]"><option value="">Select Unit Of Measure</option> <option value="50 cards">50 Cards</option><option value="Test">Test</option><option value="Catridge">Catridge</option><option value=" 1 Book">1 Book</option></td>';
									// html += '<td colspan="1" style="background-color: #777777;"></td>';
									html += '<td><input type="text" name="samples_collected[]" '+keyup+' class="form-control samples_collected" id="samples_collected'+row_count+'" /></td>';
									html += '<td><input type="text" name="opening_balance[]" '+keyup+' class="form-control opening_balance" id="opening_balance'+row_count+'" /></td>';
									html += '<td><input type="text" name="qty_received[]" '+keyup+' class="form-control qty_received" id="qty_received'+row_count+'"  /></td>';
									html += '<td><input type="text" name="total_consumption[]" '+keyup+' class="form-control total_consumption" id="total_consumption'+row_count+'" /></td>';
									html += '<td><input type="text" name="losses_adjustments[]" '+keyup+' class="form-control losses_adjustments" id="losses_adjustments'+row_count+'" /></td>';
									html += '<td><input type="text" name="total_closing_balance[]"  '+keyup+' class="form-control total_closing_balance" id="total_closing_balance'+row_count+'" readonly="true"/></td>';
									html += '<td><input type="text" name="stock_required[]"  '+keyup+' class="form-control stock_required" id="stock_required'+row_count+'" readonly="true" /></td>';
									html += '<td><input type="text" name="qty_to_order[]" '+keyup+' class="form-control qty_to_order" id="qty_to_order'+row_count+'" readonly="true"/></td>';
									// html += '<td colspan="1" style="background-color: #777777;"></td>';
									html += '<td><input type="text" name="comments[]" class="form-control comments" /></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();
								});

								$('#insert_form').on('submit', function(event){
									event.preventDefault();
									var error = '';
									$('.issue_unit').each(function(){
										var count = 1;
										if($(this).val() == '')
										{
											error += "<p>Enter Unit Of Measure at "+count+" Row</p>";
											return false;
										}
										count = count + 1;
									});

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

									$('.quantity').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>');
									}
								});
							});

						//$(".opening_balance").change(function(){ alert("dee");});
						function compute(row_count){

							//variables to store values enterd for the fields.
							var ob = parseInt($("#opening_balance"+row_count).val());
							var qr = parseInt($("#qty_received"+row_count).val());
							var tc = parseInt($("#total_consumption"+row_count).val());
							var la = parseInt($("#losses_adjustments"+row_count).val());
							var sr = parseInt($("#stock_required"+row_count).val());

							//computes the total closing balance
							$("#total_closing_balance"+row_count).val((ob+qr)-(tc - la));

							//gets the computed total closing balance
							var tcc =	((ob+qr)-(tc - la));

							//computes the maximum stock quantity required
							$("#stock_required"+row_count).val(tc*1.5);

							//computes the quantity to be ordered
							$("#qty_to_order"+row_count).val(sr-tcc);
						}
						</script>
						<?php $__env->stopSection(); ?>

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