@extends('layouts.dashboard')
@section('title', 'Invoice ' . $invoice->invoice_number)
@section('page-title', 'Invoice ' . $invoice->invoice_number)
@section('content')
<div class="row">
    <div class="col-lg-8">
        <div class="content-card">
            <div class="d-flex justify-content-between mb-4">
                <div>
                    <h2 class="mb-0">{{ $invoice->invoice_number }}</h2>
                    <span class="badge {{ $invoice->statusBadgeClass() }} mt-2">{{ ucfirst($invoice->status) }}</span>
                </div>
                <div class="text-end">
                    <h2 class="text-accent">${{ number_format($invoice->amount, 2) }} <small>{{ $invoice->currency }}</small></h2>
                    <small class="text-muted">Created {{ $invoice->created_at->format('M d, Y') }}</small>
                </div>
            </div>

            <hr>

            <div class="row">
                <div class="col-md-6">
                    <h6 class="text-uppercase text-muted small">Billed To</h6>
                    <p class="mb-0"><strong>{{ $invoice->user->name }}</strong></p>
                    <p class="text-muted">{{ $invoice->user->email }}</p>
                    <p class="user-role-badge role-{{ $invoice->user->role }}">{{ $invoice->user->role }}</p>
                </div>
                <div class="col-md-6 text-md-end">
                    <h6 class="text-uppercase text-muted small">Details</h6>
                    <p class="mb-0">Due: <strong>{{ $invoice->due_date?->format('M d, Y') ?? '—' }}</strong></p>
                    @if($invoice->paid_at)<p>Paid: <strong>{{ $invoice->paid_at->format('M d, Y') }}</strong></p>@endif
                    @if($invoice->payment_method)<p>Method: {{ ucfirst($invoice->payment_method) }}</p>@endif
                </div>
            </div>

            <hr>

            <table class="table">
                <thead><tr><th>Description</th><th class="text-end">Amount</th></tr></thead>
                <tbody>
                <tr>
                    <td>{{ $invoice->package->name ?? 'Custom invoice' }}
                        @if($invoice->package)<br><small class="text-muted">{{ $invoice->package->blogs_count }} blogs</small>@endif
                    </td>
                    <td class="text-end">${{ number_format($invoice->amount, 2) }}</td>
                </tr>
                </tbody>
                <tfoot><tr><th>Total</th><th class="text-end">${{ number_format($invoice->amount, 2) }} {{ $invoice->currency }}</th></tr></tfoot>
            </table>

            @if($invoice->notes)
                <h6 class="text-uppercase text-muted small mt-4">Notes</h6>
                <p>{{ $invoice->notes }}</p>
            @endif
        </div>

        @if($invoice->payments->isNotEmpty())
        <div class="content-card">
            <div class="content-card-header"><h3>Payment History</h3></div>
            <table class="table">
                <thead><tr><th>Date</th><th>Gateway</th><th>Transaction ID</th><th>Amount</th><th>Status</th></tr></thead>
                <tbody>
                @foreach($invoice->payments as $pay)
                    <tr>
                        <td>{{ $pay->created_at->format('M d, Y H:i') }}</td>
                        <td>{{ ucfirst($pay->gateway) }}</td>
                        <td><code>{{ $pay->transaction_id }}</code></td>
                        <td>${{ number_format($pay->amount, 2) }}</td>
                        <td><span class="badge bg-{{ $pay->status === 'completed' ? 'success' : 'secondary' }}">{{ ucfirst($pay->status) }}</span></td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        </div>
        @endif
    </div>

    <div class="col-lg-4">
        <div class="content-card">
            <div class="content-card-header"><h3>Actions</h3></div>
            @if($invoice->isUnpaid())
                <form action="{{ route('admin.invoices.markPaid', $invoice) }}" method="POST" class="mb-2">
                    @csrf <button class="btn btn-success w-100">Mark as Paid</button>
                </form>
            @else
                <form action="{{ route('admin.invoices.markUnpaid', $invoice) }}" method="POST" class="mb-2">
                    @csrf <button class="btn btn-outline-warning w-100">Mark as Unpaid</button>
                </form>
            @endif
            <form action="{{ route('admin.invoices.destroy', $invoice) }}" method="POST" onsubmit="return confirm('Delete invoice?')">
                @csrf @method('DELETE')
                <button class="btn btn-outline-danger w-100">Delete Invoice</button>
            </form>
            <a href="{{ route('admin.invoices.index') }}" class="btn btn-link w-100 mt-2">← Back to invoices</a>
        </div>
    </div>
</div>
@endsection
