@use(App\Models\Payment)

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

@section('htmlheader_title')
	Unapplied Payments
@endsection
@section('page_title')
	Unapplied
@endsection

@section('contentheader_title')
	Unapplied Payments
@endsection
@section('contentheader_description')
@endsection

@section('main-content')
	<div class="row">
		<div class="col">
			<div class="card">
				<div class="card-body">
					<table class="table table-striped table-hover" id="unapplied_payments">
						<thead>
						<tr>
							<th>ID</th>
							<th>Date Paid</th>
							<th>Account</th>
							<th>Method</th>
							<th class="text-right">Total</th>
							<th class="text-right">Balance</th>
							<th>Invoices</th>
						</tr>
						</thead>

						<tbody>
						@foreach(Payment::AccountUserAuthorised()->active()->unapplied()->with(['account.user','checkout','items'])->get() as $o)
							@continue(! $o->balance)
							<tr>
								<td><a href="{{ url('r/payment',$o->id) }}">{{ $o->id }}</td>
								<td>{{ $o->paid_at->format('Y-m-d') }}</td>
								<td>{{ $o->account->name }}</td>
								<td>{{ $o->checkout->name }}</td>
								<td class="text-right">{{ number_format($o->total_amt,2) }}</td>
								<td class="text-right">{{ number_format($o->balance,2) }}</td>
								<td>{!! $o->items->pluck('invoice_id')->map(function($item) { return sprintf('<a href="%s">%d</a>',url('u/invoice',$item),$item); })->join(', ')  !!}</td>
							</tr>
						@endforeach
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>
@endsection

@pa(datatables,rowgroup|conditionalpaging)

@section('page-scripts')
	<script type="text/javascript">
		$(document).ready(function() {
			$('#unapplied_payments').DataTable({
				order: [1,'desc'],
				rowGroup: {
					dataSrc: 2,
				},
				conditionalPaging: true,
			});
		});
	</script>
@append