@extends('adminlte::layouts.app')

@section('htmlheader_title')
	Payment Cart
@endsection
@section('page_title')
	Payments
@endsection

@section('contentheader_title')
	Payment Cart
@endsection
@section('contentheader_description')
@endsection

@section('main-content')
	<div class="row">
		<div class="col-4">
			<div class="card card-dark">
				<div class="card-header">
					<span class="card-title">Invoices to Pay</span>
				</div>

				<div class="card-body">
					<form method="POST" action="{{ url('u/checkout/pay') }}">
						@csrf

						<input type="hidden" name="type" value="invoice">

						<div class="input-group mb-5">
							<div class="input-group-prepend">
								<span class="input-group-text">Payment Method</span>
							</div>

							<select class="form-control" id="paymethod" name="checkout_id[]" required>
								<option></option>
								@foreach (\App\Models\Checkout::active()->orderBy('name')->get() as $oo)
									<option value="{{ $oo->id }}">{{ $oo->name }}</option>
								@endforeach
							</select>
						</div>

						<table id="invoices" class="table table-sm w-100">
							<tr>
								<th>Invoice</th>
								<th class="text-right">Balance Due</th>
							</tr>

							@foreach ($invoices as $io)
								<input type="hidden" name="invoice_id[]" value="{{ $io->id }}">
								<tr>
									<td>{{ $io->sid }}</td>
									<td class="text-right">{{ number_format($io->due,2) }}</td>
								</tr>
							@endforeach

							<tfoot>
							<tr>
								<th class="text-right">Sub Total</th>
								<td class="text-right">{{ number_format($invoices->sum('due'),2) }}</td>
							</tr>
							<tr>
								<th class="text-right">Payment Fees</th>
								<td class="text-right"><span id="payfee">TBA</span></td>
							</tr>
							<tr>
								<th class="text-right">Payment Total</th>
								<th class="text-right"><span id="paytotal">TBA</span></th>
							</tr>
							<tr>
								<th colspan="2">
									<input type="submit" class="btn btn-dark mt-2" name="pay" value="Cancel">
									<input type="submit" class="btn btn-success mt-2 float-right" name="pay" value="Submit" disabled>
									<a href="{{ url('/home') }}" class="btn btn-danger mt-2 float-right mr-2">Add Invoice</a>
								</th>
							</tr>
							</tfoot>
						</table>
					</form>
				</div>
			</div>
		</div>
	</div>
@endsection

@section('page-scripts')
	<script>
		$(document).ready(function() {
			$('#paymethod').on('change',function(item) {
				$.ajax({
					type: "POST",
					data: {total: {{ $invoices->sum('due') }},count: {{ $invoices->count() }} },
					dataType: "json",
					cache: true,
					url: '{{ url('api/u/checkout/fee') }}'+'/'+$(this).val(),
					timeout: 25000,
					error: function(x) {
						alert("Failed to submit, please try again...");
					},
					success: function(data) {
						$("span[id=payfee]").html(data.toFixed(2));
						$("span[id=paytotal]").html(({{ $invoices->sum('due') }}+data).toFixed(2));
						$("input[type=submit]").prop('disabled',false);
					}
				});
			});
		});
	</script>
@append