<?php $__env->startSection("content"); ?>
<div class="container-fluid">
	<div class="row">
		<div class="col-md-12">
			<?php
						$serial_number = intval( "0" .mt_rand(1,9) . mt_rand(0,9) . mt_rand(0,9) . mt_rand(0,9) );
						$s = "unhls-ims-0".$serial_number;
						?>

			<?php echo Form::open(array('url' => 'facilityrequest', 'id' => 'form-facilityrequest', 'method' => 'POST')); ?>

			<div class="panel panel-primary">
				<p 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></p>
						<div class="panel panel-warning">
							<tr>
								<td>
									<br>
									<div class="form-group col-xs-2">
										<?php echo Form::label('facility_id', 'Requesting Facility', array('class' => 'required')); ?>

										<?php echo Form::select('facility_id', $facility, null,array('class' => 'form-control','name'=>'facility_id','id'=>'facility_id')); ?>

										<?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 hidden">
										<?php echo Form::text('order_number',$s,array('class' => 'form-control col-sm-4')); ?>

									</div>

									<div class="form-group col-xs-2">
										<?php echo Form::label('hub', 'Hub'); ?>

										<?php echo Form::select('hub',$hub, null,array('class' => 'form-control','id'=>'hub_id','name'=>'hub_id', 'required')); ?>

									</div>

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

										<?php echo Form::select('district_id',$district, null,array('class' => 'form-control','id'=>'district_id','name'=>'district_id')); ?>

									</div>

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


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

									</div>
									<div class="form-group col-xs-1">
										<?php echo Form::label('end_date', 'End:',  array('class'=>'col-sm-2')); ?>

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

									</div>


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

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

									</div>

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

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


										<?php echo Form::label('requested_by', 'Prepared By:',array('class' =>'col-sm-2', 'hidden')); ?>

										<?php echo Form::text('requested_by', Auth::user()->username,array('class' => 'hidden', '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 table-responsive" id="item_table">
											<tr>
												<!-- <th class="text:center">UoM</th> -->
												<th class="text:center col-sm-2">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>
											<br>
										</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 style="width: 50%" name ="commodity_id[]" id = "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[]", id = "issue_unit"><option value="">Select Unit Of Measure</option><?php foreach($uom as $key => $u): ?><option value="<?php echo $key; ?>"><?php echo $u; ?></option><?php endforeach; ?></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();
								});
							});

							//$(".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*2);

								//computes the quantity to be ordered
								// var x = sr-tcc;
								$("#qty_to_order"+row_count).val(sr-tcc);
							}


							// Generates auto-fill of distri, hub and IP text boxes
							$("#facility_id").change(function(){
								$.ajax({
									url: "/facilities/" + $(this).val(),
									type: 'GET',

									success: function(data) {
										$('#hub_id').val(data.hubID);
										$('#hub_id').attr('readonly','true');

										$('#district_id').val(data.districtID);
										$('#district_id').attr('readonly','true');

										$('#ip_id').val(data.ipID);
										$('#ip_id').attr('readonly','true');

										console.log(data);
									}
								});
							});

							//Auto-fill UOM
							$("#commodity_id").change(function(){
								$.ajax({
									url: "/items/" + $(this).val(),
									type: 'GET',
									success: function(data) {

										$('#issue_unit').val(data.metric_id);
										$('#issue_unit').attr('readonly','true');
									}
								});
							});

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


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