<!-- $o=Service::class,$p=Product::class -->

@use(Carbon\CarbonInterface)
@use(App\Models\Invoice)

@php($c=$o->product)

<table class="table table-sm">
	<thead>
	<tr>
		<th>&nbsp;</th>
		<th class="text-center" colspan="2">This Plan</th>
		<th>&nbsp;</th>
		@if($p->exists)
			<th class="text-center" colspan="2">Proposed Plan</th>
			<th>&nbsp;</th>
		@endif
	</tr>

	<tr>
		<th>&nbsp;</th>
		<th>Client</th>
		<th>Supplier</th>
		<th>&nbsp;</th>
		@if($p->exists)
			<th>Client</th>
			<th>Supplier</th>
			<th>&nbsp;</th>
		@endif
	</tr>
	</thead>

	<tbody>
    <tr>
        <th>Product</th>
		<td><a href="{{ route('product',['pdo'=>$o->product_id]) }}">#{{ $o->product_id }}: {{ $o->product->name }}</a></td>
		<td><a href="{{ route('supplier.product.type',['id'=>$c->supplied->id,'spo'=>$c->supplied->supplier_detail_id,'type'=>$c->category_lc]) }}">#{{ $c->supplied->id }}: {{ $c->supplied->name_long }}</a></td>
		<td>{{ $c->category_name }}</td>
		@if($p->exists)
			<th><a href="{{ route('product',['pdo'=>$p->id]) }}">#{{ $p->id }}: {{ $p->name }}</a></th>
			<td class="text-center"><a href="{{ route('supplier.product.type',['id'=>$p->supplied->id,'spo'=>$p->supplied->supplier_detail_id,'type'=>$p->category_lc]) }}">#{{ $p->supplied->id }}: {{ $p->supplied->name_long }}</a></td>
			<td>&nbsp;</td>
		@endif
    </tr>

	<tr>
		<th>Setup</th>
		<td>${{ number_format($b=$o->account->taxed($c->setup_charge()),2) }}</td>
		<td>${{ number_format($a=$o->account->taxed($c->setup_cost),2) }}</td>
		<td>{!! markup($a,$b) !!}</td>
		@if($p->exists)
			<td>${{ number_format($b=$o->account->taxed($p->setup_charge()),2) }}</td>
			<td>${{ number_format($a=$o->account->taxed($p->setup_cost),2) }}</td>
			<td>{!! markup($a,$b) !!}</td>
		@endif
	</tr>

	<tr>
		<th>Billed</th>
		<td>{{ $o->billing_interval_name }}</td>
		<td>{{ $c->billing_interval_name }}</td>
		<td>&nbsp;</td>
		@if($p->exists)
			<td>{{ $o->billing_interval_name }}</td>
			<td>{{ $p->billing_interval_name }}</td>
			<td>&nbsp;</td>
		@endif
	</tr>

	<tr>
		<th>Billing Price</th>
		<td @class(['text-danger'=>$o->is_charge_overridden])>${{ number_format($b=$o->billing_charge_taxed,2) }}</td>
		<td @class(['text-danger'=>$o->is_cost_overridden])>${{ number_format($a=$o->billing_cost_taxed,2) }}</td>
		<td>{!! markup($a,$b) !!}</td>
		@if($p->exists)
			<td @if($o->is_charge_overridden)class="text-danger"@endif>${{ number_format($b=$o->account->taxed($p->base_charge()),2) }}</td>
			<td>${{ number_format($a=$o->account->taxed($p->base_cost),2) }}</td>
			<td>{!! markup($a,$b) !!}</td>
		@endif
	</tr>

	<tr>
		<th>Monthly Price</th>
		<td @class(['text-danger'=>$o->is_charge_overridden])>
			@if($o->is_charge_overridden)
				<abbr title="${{ number_format($o->billing_charge_orig_normalised_taxed,2) }}">${{ number_format($b=$o->billing_charge_normalised_taxed,2) }}
			@else
				${{ number_format($b=$o->billing_charge_normalised_taxed,2) }}
			@endif
		</td>
		<td @class(['text-danger'=>$o->is_cost_overridden])>
			@if($o->is_cost_overridden)
				<abbr title="${{ number_format($o->billing_cost_orig_normalised_taxed,2) }}">${{ number_format($a=$o->billing_cost_normalised_taxed,2) }}
			@else
				${{ number_format($a=$o->billing_cost_normalised_taxed,2) }}
			@endif
		</td>
		<td>{!! markup($a,$b) !!}</td>
		@if($p->exists)
			<td @class(['text-danger'=>$o->is_charge_overridden])>${{ number_format($b=$o->account->taxed($p->base_charge())*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }}</td>
			<td @class(['text-danger'=>$o->is_cost_overridden])>${{ number_format($a=$o->account->taxed($p->base_cost)*Invoice::billing_change($o->billing_interval,Invoice::BILL_MONTHLY),2) }}</td>
			<td>{!! markup($a,$b) !!}</td>
		@endif
	</tr>

	<tr>
		<th>Contract</th>
		<td>{{ $o->contract_term }} months</td>
		<td>{{ $c->supplied->contract_term }} months</td>
		<td>&nbsp;</td>
		@if($p->exists)
			<td>{{ $p->contract_term }} months</td>
			<td>{{ $p->supplied->contract_term }} months</td>
			<td>&nbsp;</td>
		@endif
	</tr>

	<tr>
		<th>Min Price</th>
		<!-- @todo change this to $o->min_charge when implemented -->
		<td>${{ number_format($b=$o->account->taxed($o->product->min_charge()),2) }}</td>
		<td>${{ number_format($a=$o->account->taxed($c->min_cost),2) }}</td>
		<td>{!! markup($a,$b) !!}</td>

		@if($p->exists)
			<td>${{ number_format($a=$o->account->taxed($p->min_charge()),2) }}</td>
			<td>${{ number_format($a=$o->account->taxed($p->min_cost),2) }}</td>
			<td>{!! markup($a,$b) !!}</td>
		@endif
	</tr>

	<tr>
		<th>Contract Left</th>
		@if($o->is_contracted)
			<td>${{ number_format($o->billing_charge_to($o->contract_end),2) }} (<small>{{ $o->paid_to?->format('Y-m-d') }}</small>)</td>
			<td>${{ number_format($o->billing_cost_to($o->contract_end),2) }} (<small>{{ $o->invoiced_to?->format('Y-m-d') }}</small>)</td>
			<td>{{ $o->contract_end->format('Y-m-d') }}<br><small>({{ $o->contract_end->diffForHumans(now(),CarbonInterface::DIFF_RELATIVE_TO_OTHER,FALSE,2) }} today)</small></td>
		@else
			<td colspan="2" class="text-center">Not on contract</td>
			<td>&nbsp;</td>
		@endif

		@if($p->exists)
			<td colspan="3">&nbsp;</td>
		@endif
	</tr>
	</tbody>
</table>