@extends('super-admin.layout')

@section('title', $invoice->invoice_number)

@section('content')
<div class="pg-head">
    <div>
        <h1>{{ $invoice->invoice_number }}</h1>
        <p>{{ $invoice->organisation?->name }} · {{ $invoice->event?->name ?? 'No event' }}</p>
    </div>
    <a href="{{ route('mexco.super.invoices') }}" class="btn-outline">← All invoices</a>
</div>

<div class="stat-row" style="grid-template-columns:repeat(4,1fr)">
    <div class="card stat-card">
        <div class="stat-label">Status</div>
        <div class="stat-num" style="font-size:1rem">{{ $invoice->statusLabel() }}</div>
    </div>
    <div class="card stat-card">
        <div class="stat-label">Total</div>
        <div class="stat-num" style="font-size:1.1rem">{{ $invoice->formattedTotal() }}</div>
    </div>
    <div class="card stat-card">
        <div class="stat-label">Paid</div>
        <div class="stat-num" style="font-size:1.1rem">MYR {{ number_format($invoice->amountPaidCents() / 100, 2) }}</div>
    </div>
    <div class="card stat-card">
        <div class="stat-label">Amount due</div>
        <div class="stat-num" style="font-size:1.1rem">MYR {{ number_format($invoice->amountDueCents() / 100, 2) }}</div>
    </div>
</div>

<div style="display:grid;grid-template-columns:1.2fr .8fr;gap:1rem;align-items:start">
    <div class="card">
        <div class="form-panel">
            <h2>Line items</h2>
            <div class="tbl-wrap" style="margin-top:.75rem">
                <table class="data-tbl">
                    <thead>
                        <tr>
                            <th>Description</th>
                            <th>Qty</th>
                            <th style="text-align:right">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($invoice->items as $item)
                            <tr>
                                <td>{{ $item->description }}</td>
                                <td>{{ $item->quantity }}</td>
                                <td style="text-align:right">{{ $item->formattedTotal() }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="2" style="text-align:right;font-weight:600;padding-top:1rem">Subtotal</td>
                            <td style="text-align:right;padding-top:1rem">MYR {{ number_format($invoice->subtotal_cents / 100, 2) }}</td>
                        </tr>
                        @if($invoice->discount_cents > 0)
                            <tr>
                                <td colspan="2" style="text-align:right;color:var(--mexco-success)">Discount / credit</td>
                                <td style="text-align:right;color:var(--mexco-success)">− MYR {{ number_format($invoice->discount_cents / 100, 2) }}</td>
                            </tr>
                        @endif
                        @if($invoice->tax_cents > 0)
                            <tr>
                                <td colspan="2" style="text-align:right">Tax</td>
                                <td style="text-align:right">MYR {{ number_format($invoice->tax_cents / 100, 2) }}</td>
                            </tr>
                        @endif
                        <tr>
                            <td colspan="2" style="text-align:right;font-weight:800">Total</td>
                            <td style="text-align:right;font-weight:800">{{ $invoice->formattedTotal() }}</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>

        @if($invoice->payments->isNotEmpty())
            <div class="form-panel" style="border-top:1px solid var(--border)">
                <h2>Payments</h2>
                <div class="tbl-wrap" style="margin-top:.75rem">
                    <table class="data-tbl">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Method</th>
                                <th>Reference</th>
                                <th style="text-align:right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($invoice->payments as $payment)
                                <tr>
                                    <td>{{ $payment->paid_at?->format('d M Y, H:i') ?? '-' }}</td>
                                    <td>{{ ucfirst($payment->method ?? '-') }}</td>
                                    <td class="mono cell-sub">{{ $payment->gateway_reference ?? '-' }}</td>
                                    <td style="text-align:right">{{ $payment->formattedAmount() }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        @endif
    </div>

    <div style="display:grid;gap:1rem">
        @if($invoice->status === 'draft')
            <div class="card form-panel">
                <h2>Issue invoice</h2>
                <p class="panel-lead">Send to organisation for payment (due in {{ config('mexco.billing.default_due_days') }} days).</p>
                <form method="POST" action="{{ route('mexco.super.invoices.issue', $invoice) }}">
                    @csrf
                    <button type="submit" class="btn-purple">Issue invoice</button>
                </form>
            </div>
        @endif

        @if(in_array($invoice->status, ['draft', 'pending'], true))
            <div class="card form-panel">
                <h2>Promo code</h2>
                <form method="POST" action="{{ route('mexco.super.invoices.promo', $invoice) }}" class="d-flex gap-2">
                    @csrf
                    <input type="text" name="promo_code" class="form-input mono" placeholder="CODE" value="{{ $invoice->promoCode?->code }}">
                    <button type="submit" class="btn-outline">Apply</button>
                </form>
            </div>

            @if($availableCredit > 0)
                <div class="card form-panel">
                    <h2>Organisation credit</h2>
                    <p class="panel-lead">Available: MYR {{ number_format($availableCredit / 100, 2) }}</p>
                    <form method="POST" action="{{ route('mexco.super.invoices.credit', $invoice) }}">
                        @csrf
                        <button type="submit" class="btn-outline">Apply full available credit</button>
                    </form>
                </div>
            @endif
        @endif

        @if(in_array($invoice->status, ['pending', 'paid'], true) && $invoice->amountDueCents() > 0)
            <div class="card form-panel">
                <h2>Record payment</h2>
                <p class="panel-lead">Manual bank transfer or offline payment.</p>
                <form method="POST" action="{{ route('mexco.super.invoices.payment', $invoice) }}" class="form-stack">
                    @csrf
                    <div class="form-row">
                        <label class="form-label">Amount (sen)</label>
                        <input type="number" name="amount_cents" class="form-input" value="{{ $invoice->amountDueCents() }}" min="1" required>
                        <div class="form-hint">Due: MYR {{ number_format($invoice->amountDueCents() / 100, 2) }}</div>
                    </div>
                    <div class="form-row">
                        <label class="form-label">Reference</label>
                        <input type="text" name="reference" class="form-input" placeholder="Bank ref / receipt no.">
                    </div>
                    <div class="form-row">
                        <label class="form-label">Notes</label>
                        <textarea name="notes" class="form-input" rows="2"></textarea>
                    </div>
                    <button type="submit" class="btn-purple">Record payment</button>
                </form>
            </div>
        @endif

        @if(! in_array($invoice->status, ['paid', 'cancelled'], true))
            <div class="card form-panel">
                <form method="POST" action="{{ route('mexco.super.invoices.cancel', $invoice) }}" onsubmit="return confirm('Cancel this invoice?')">
                    @csrf
                    <button type="submit" class="btn-danger btn-sm">Cancel invoice</button>
                </form>
            </div>
        @endif

        <div class="card form-panel">
            <h2>Event link</h2>
            @if($invoice->event_id)
                <p class="panel-lead">{{ $invoice->license?->plan?->name ?? 'Plan' }} · <span class="mono">{{ $invoice->event_id }}</span></p>
                <a href="{{ route('mexco.super.events.edit', $invoice->event_id) }}" class="btn-outline btn-sm">Edit event</a>
            @else
                <p class="panel-lead">No linked event.</p>
            @endif
        </div>
    </div>
</div>
@endsection
