<!-- $o=Invoice::class -->

@use(App\Models\Checkout)
@use(App\Models\Service)

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

@section('htmlheader_title')
	Invoice #{{ $o->lid }}
@endsection
@section('page_title')
	{{ $o->sid }}
@endsection

@section('contentheader_title')
	Tax Invoice <strong>#{{ $o->lid }}</strong>
@endsection
@section('contentheader_description')
@endsection

@section('main-content')
	<div class="invoice p-3 mb-3">
		<div class="row">
			<div class="col-8">
				<h2>
					<img src="{{ $site->site_logo }}">
				</h2>
			</div>

			<div class="col-4 text-right">
				<h1 class="text-uppercase">Tax Invoice</h1>
			</div>
		</div>

		<div class="row invoice-info">
			<div class="col-4 invoice-col">
				<address>
					FROM: <strong>{{ $site->site_name }}</strong><br>
					{!! $site->address->join('<br>') !!}
					<br><br>
					<strong>Email:</strong> {{ $site->site_email }}<br>
					<strong>Phone:</strong> {{ $site->site_phone }}
				</address>
			</div>

			<div class="col-4 invoice-col">
				<address>
					TO: <strong>{{ $o->account->company }}</strong><br>
					{!! $o->account->address->join('<br>') !!}
					<br><br>
					<strong>Email:</strong> {{ $o->account->user->email }}<br>
					@if($o->account->phone)
						<strong>Phone:</strong> {{ $o->account->phone }}<br>
					@endif
				</address>
			</div>

			<div class="ml-auto col-3">
				<table class="table table-borderless text-right" style="font-size: 1.1rem;">
					<tr >
						<td class="p-0">Issue Date:</td><td class="p-0"><strong>{{ $o->created_at->format('Y-m-d') }}</strong></td>
					</tr>
					<tr >
						<td class="p-0">Account:</td><td class="p-0"><strong>{{ $o->account->sid }}</strong></td>
					</tr>
					<tr >
						<td class="p-0">Invoice:</td><td class="p-0"><strong>{{ $o->lid }}</strong></td>
					</tr>
					<tr>
						<td class="p-0">Payment Due:</td><td class="p-0"><strong>{{ $o->due_at->format('Y-m-d') }}</strong></td>
					</tr>
					<tr>
						<td class="p-0">This Invoice Due:</td><td class="p-0"><strong>${{ number_format($o->total,2) }}</strong></td>
					</tr>
					{{--
					<!-- @todo -->
					<tr>
						<td class="p-0">Total Account Due:</td><td class="p-0"><strong>${{ number_format($o->account->due,2) }}</strong></td>
					</tr>
					--}}
				</table>
			</div>
		</div>

		<div class="row">
			<div class="col-12">
				<table class="table table-striped table-hover">
					<thead>
						<tr>
							<th>Qty</th>
							<th>Product</th>
							<th colspan="2">Description</th>
							<th class="text-right" colspan="3">Subtotal</th>
						</tr>
					</thead>

					<tbody>
						@foreach($o->summary_products() as $item)
							<tr id="invoice-services">
								<td>{{ $item['services']->count() }}</td>
								<td>#{{ $item['product']->lid }}</td>
								<td colspan="2">{{ $item['product']->name }}</td>
								<td colspan="3" class="text-right">${{ number_format($item['total'],2) }}</td>
							</tr>

							@foreach(Service::whereIn('id',$item['services'])->get() as $so)
								<tr id="invoice-service-items" class="invoice-services @if($item['services']->count() > 1) d-print-table-row @endif">
									<td colspan="2">&nbsp;</td>
									<td colspan="2">Service: <strong>{{ $so->sid }}: [{{ $so->name }}]</strong></td>
									<td>&nbsp;</td>
									<td class="text-right">${{ number_format($o->product_service_items($item['product'],$so)->sum('total'),2) }}</td>
									<td>&nbsp;</td>
								</tr>

								@foreach($o->product_service_items($item['product'],$so) as $io)
									<tr class="invoice-service-items d-print-table-row">
										<td colspan="2">&nbsp;</td>
										<td width="5%">&nbsp;</td>
										<td>{{ $io->item_type_name }}</td>
										<td class="text-right">${{ number_format($io->total,2) }}</td>
										<td colspan="2">&nbsp;</td>
									</tr>
								@endforeach
							@endforeach
						@endforeach

						@if($o->summary_other()->count())
							<tr>
								<td colspan="7">{{ $item['description'] }}</td>
							</tr>
							@foreach($o->summary_other() as $item)
								<tr>
									<td colspan="2">&nbsp;</td>

									<td>{{ $item['description'] }}</td>
								</tr>
							@endforeach
						@endif
					</tbody>
				</table>
			</div>
		</div>

		<div class="row pb-5"></div>

		<div class="row">
			<div class="col-6">
				<p class="lead">Payment Methods:</p>

				<table class="table table-borderless">
					@foreach(Checkout::available() as $cho)
						<tr>
							<td style="width: 50px;"><i class="fa-2x fa-fw {{ $cho->icon }}"></i></td>
							<td>{{ $cho->name }}</td>
							<td>{{ $cho->description }}</td>
							<td class="w-25">@includeIf('theme.backend.adminlte.payment.widget.plugin.'.strtolower($cho->plugin),['o'=>$cho])</td>
						</tr>
					@endforeach
				</table>

				<p class="text-muted well well-sm no-shadow" style="position: absolute;bottom: 0;left: 0;">
					{{ config('osb.invoice_text') }}
				</p>
			</div>

			<div class="ml-auto col-4">
				<table class="table">
					<tr>
						<th colspan="3" style="width:50%">Subtotal:</th>
						<td colspan="2" class="text-right">${{ number_format($o->sub_total,2) }}</td>
					</tr>
					<tr>
						<th>&nbsp;</th>
						<th>Tax (GST 10%)</th>
						<td colspan="2" class="text-right">${{ number_format($o->tax_total,2) }}</td>
					</tr>
					<tr>
						<th>&nbsp;</th>
						<th>Other Charges:</th>
						<!-- @todo -->
						<td colspan="2" class="text-right">$0.00</td>
					</tr>
					<tr>
						<th colspan="2">Total:</th>
						<td colspan="2" class="text-right">${{ number_format($o->total,2) }}</td>
					</tr>
					@if($o->id)
						<tr>
							<th>&nbsp;</th>
							<th>Payments To Clear:</th>
							<td colspan="2" class="text-right">${{ number_format($o->paid_pending,2) }}</td>
						</tr>
						<tr>
							<th>&nbsp;</th>
							<th>Payments:</th>
							<td>
								@foreach($o->payment_items_active as $po)
									<span style="white-space:nowrap;">@can('view',$po->payment)#<a href="{{ url('r/payment',$po->payment_id) }}">{{ $po->payment_id }}</a>@else #{{ $po->payment_id }} @endcan <small>{{ $po->payment->paid_at->format('Y-m-d') }}</small></span>
								@endforeach
							</td>
							<td class="text-right">${{ number_format($o->paid,2) }}</td>
						</tr>
						<tr style="font-size: 145%">
							<th colspan="2">Invoice Due:</th>
							<td colspan="2" class="text-right">${{ number_format($o->due,2) }}</td>
						</tr>
					@endif
				</table>
			</div>
		</div>

		<!-- this row will not appear when printing -->
		<div class="row d-print-none">
			<div class="col-12">
				<button type="button" id="print" class="btn btn-default"><i class="fas fa-print"></i> Print</button>
				@if($o->id)
					<a href="{{ url('u/checkout/cart',$o->id) }}" class="btn btn-success float-right"><i class="fas fa-credit-card"></i> Pay</a>
				{{--
					<a href="{{ url(sprintf('u/invoice/%s/pdf',$o->id)) }}" class="btn btn-primary float-right mr-2">
						<i class="fas fa-download"></i> Download PDF
					</a>
				--}}
				@endif
			</div>
		</div>
	</div>
@endsection

@section('page-styles')
	<style media="print">
		/* Dont show URL and date in print output */
		@page {
			size: auto; /* auto is the initial value */
			margin: 0;  /* this affects the margin in the printer settings */
		}
	</style>
@append
@section('page-scripts')
	<script type="text/javascript">
		$(document).ready(function() {
			$('#print').on('click',function() {
				window.print();
			})
		});
	</script>
@append