@extends('layouts.admin')

@section('title', $invoice->invoice_number ?: 'Draft Invoice')

@section('content')
<div class="space-y-6">
    <div class="flex flex-col lg:flex-row lg:items-start justify-between gap-4">
        <div>
            <p class="text-sm text-gray-400">{{ ucfirst($invoice->source_type) }} invoice</p>
            <h1 class="text-2xl font-bold text-gray-900 mt-1">{{ $invoice->invoice_number ?: 'Draft #' . $invoice->id }}</h1>
            <div class="mt-3 flex flex-wrap gap-2">
                <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold {{ $invoice->status === 'issued' ? 'bg-brand-100 text-brand-700' : ($invoice->status === 'review' ? 'bg-indigo-100 text-indigo-700' : ($invoice->status === 'cancelled' ? 'bg-rose-100 text-rose-700' : 'bg-amber-100 text-amber-700')) }}">{{ ucfirst($invoice->status) }}</span>
                <span class="inline-flex items-center rounded-full px-3 py-1 text-xs font-semibold {{ $invoice->payment_status === 'paid' ? 'bg-emerald-100 text-emerald-700' : ($invoice->payment_status === 'partial' ? 'bg-sky-100 text-sky-700' : 'bg-amber-100 text-amber-700') }}">{{ ucfirst($invoice->payment_status) }}</span>
                @if(! empty($isCancellationInvoice))
                <span class="inline-flex items-center gap-1 rounded-full px-3 py-1 text-xs font-bold bg-rose-100 text-rose-700 border border-rose-200">
                    <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                    </svg>
                    Nota Pembatalan
                </span>
                @endif
            </div>
        </div>
        <div class="flex flex-wrap gap-2">
            @if($invoice->status === 'draft')
            <a href="{{ route('admin.invoices.edit', $invoice) }}" class="inline-flex items-center gap-2 rounded-xl border border-brand-200 text-brand-700 px-4 py-2.5 text-sm font-semibold hover:bg-brand-50 transition">Edit Draft</a>
            @endif
            @if($invoice->status === 'draft')
            <form method="POST" action="{{ route('admin.invoices.submit-review', $invoice) }}">
                @csrf
                <button type="submit" class="inline-flex items-center gap-2 rounded-xl border border-amber-200 text-amber-700 px-4 py-2.5 text-sm font-semibold hover:bg-amber-50 transition">Ajukan Review</button>
            </form>
            @endif
            @if(in_array($invoice->status, ['draft', 'review']) && auth()->user()?->isAdmin())
            <form method="POST" action="{{ route('admin.invoices.issue', $invoice) }}">
                @csrf
                <button type="submit" class="inline-flex items-center gap-2 rounded-xl bg-brand-600 text-white px-4 py-2.5 text-sm font-semibold hover:bg-brand-700 transition">Terbitkan Invoice</button>
            </form>
            @endif
            <a href="{{ route('admin.invoices.pdf', $invoice) }}" class="inline-flex items-center gap-2 rounded-xl border border-gray-300 text-gray-700 px-4 py-2.5 text-sm font-semibold hover:bg-gray-50 transition">Download PDF</a>
        </div>
    </div>

    <div class="grid grid-cols-1 xl:grid-cols-3 gap-5">
        <div class="xl:col-span-2 space-y-5">
            <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5 grid grid-cols-1 md:grid-cols-2 gap-5">
                <div>
                    <p class="text-xs font-semibold uppercase tracking-wide text-gray-400 mb-1">Pelanggan</p>
                    <p class="text-lg font-semibold text-gray-900">{{ $invoice->customer->name }}</p>
                    <p class="text-sm text-gray-500 mt-1">{{ $invoice->customer->whatsapp ?: '-' }}</p>
                    <p class="text-sm text-gray-500">{{ $invoice->customer->email ?: 'Tanpa email' }}</p>
                </div>
                <div>
                    <p class="text-xs font-semibold uppercase tracking-wide text-gray-400 mb-1">Informasi Invoice</p>
                    <p class="text-sm text-gray-700">Tanggal: {{ optional($invoice->invoice_date)->format('d M Y') ?: '-' }}</p>
                    <p class="text-sm text-gray-700">Jatuh tempo: {{ optional($invoice->due_date)->format('d M Y') ?: '-' }}</p>
                    <p class="text-sm text-gray-700">Diterbitkan oleh: {{ $invoice->issuer?->name ?: '-' }}</p>
                    @if($verifyUrl)
                    <p class="text-sm text-gray-700 break-all">Verifikasi: <a href="{{ $verifyUrl }}" target="_blank" class="text-brand-600 hover:underline">{{ $verifyUrl }}</a></p>
                    @endif
                </div>
            </div>

            <div class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
                <div class="px-5 py-4 border-b border-gray-100">
                    <h2 class="text-sm font-semibold text-gray-800">Item Tagihan</h2>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full text-sm">
                        <thead>
                            <tr class="bg-gray-50 text-left border-b border-gray-100">
                                <th class="px-5 py-3 font-semibold text-gray-600">Deskripsi</th>
                                <th class="px-5 py-3 font-semibold text-gray-600">Qty</th>
                                <th class="px-5 py-3 font-semibold text-gray-600">Harga</th>
                                <th class="px-5 py-3 font-semibold text-gray-600">Diskon</th>
                                <th class="px-5 py-3 font-semibold text-gray-600">Pajak</th>
                                <th class="px-5 py-3 font-semibold text-gray-600 text-right">Total</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-50">
                            @foreach($invoice->items as $item)
                            <tr>
                                <td class="px-5 py-3">
                                    <p class="font-medium text-gray-900">{{ $item->description }}</p>
                                    <p class="text-xs text-gray-400">{{ ucfirst(str_replace('_', ' ', $item->item_type)) }}</p>
                                </td>
                                <td class="px-5 py-3 text-gray-700">{{ rtrim(rtrim(number_format((float) $item->qty, 2, '.', ''), '0'), '.') }} {{ $item->unit }}</td>
                                <td class="px-5 py-3 text-gray-700">Rp {{ number_format((float) $item->unit_price, 0, ',', '.') }}</td>
                                <td class="px-5 py-3 text-gray-700">Rp {{ number_format((float) $item->discount_amount, 0, ',', '.') }}</td>
                                <td class="px-5 py-3 text-gray-700">Rp {{ number_format((float) $item->tax_amount, 0, ',', '.') }}</td>
                                <td class="px-5 py-3 text-right font-semibold text-gray-900">Rp {{ number_format((float) $item->line_total, 0, ',', '.') }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-5">
                <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5">
                    <h2 class="text-sm font-semibold text-gray-800 mb-3">Catatan Customer</h2>
                    <p class="text-sm text-gray-600 leading-relaxed">{{ $invoice->public_note ?: 'Tidak ada catatan invoice.' }}</p>
                </div>
                <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5">
                    <h2 class="text-sm font-semibold text-gray-800 mb-3">Catatan Internal</h2>
                    <p class="text-sm text-gray-600 leading-relaxed">{{ $invoice->internal_note ?: 'Tidak ada catatan internal.' }}</p>
                </div>
            </div>

            <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5">
                <div class="flex items-center justify-between gap-3 mb-4">
                    <h2 class="text-sm font-semibold text-gray-800">Riwayat Aktivitas</h2>
                    <span class="text-xs text-gray-400">{{ $invoice->activities->count() }} log</span>
                </div>
                <ol class="space-y-3">
                    @forelse($invoice->activities as $activity)
                    <li class="rounded-xl bg-gray-50 border border-gray-100 px-4 py-3">
                        <p class="text-sm font-medium text-gray-800">{{ $activity->description }}</p>
                        <p class="mt-1 text-xs text-gray-400">{{ $activity->created_at->format('d M Y H:i') }} • {{ $activity->user?->name ?: 'System' }}</p>
                    </li>
                    @empty
                    <li class="text-sm text-gray-400">Belum ada aktivitas.</li>
                    @endforelse
                </ol>
            </div>
        </div>

        <div class="space-y-5">
            <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5">
                <h2 class="text-sm font-semibold text-gray-800 mb-4">Ringkasan Tagihan</h2>
                <dl class="space-y-2 text-sm">
                    <div class="flex justify-between"><dt class="text-gray-500">Subtotal</dt><dd class="font-semibold text-gray-900">Rp {{ number_format((float) $invoice->subtotal, 0, ',', '.') }}</dd></div>
                    <div class="flex justify-between"><dt class="text-gray-500">Diskon</dt><dd class="font-semibold text-gray-900">Rp {{ number_format((float) $invoice->discount_total, 0, ',', '.') }}</dd></div>
                    <div class="flex justify-between"><dt class="text-gray-500">Pajak</dt><dd class="font-semibold text-gray-900">Rp {{ number_format((float) $invoice->tax_total, 0, ',', '.') }}</dd></div>
                    <div class="flex justify-between border-t border-gray-100 pt-3"><dt class="text-gray-900 font-semibold">Grand Total</dt><dd class="font-bold text-brand-700">Rp {{ number_format((float) $invoice->grand_total, 0, ',', '.') }}</dd></div>
                    <div class="flex justify-between"><dt class="text-gray-500">Sudah Dibayar</dt><dd class="font-semibold text-gray-900">Rp {{ number_format((float) $invoice->amount_paid, 0, ',', '.') }}</dd></div>
                    <div class="flex justify-between"><dt class="text-gray-500">Sisa</dt><dd class="font-semibold {{ (float) $invoice->balance_due > 0 ? 'text-amber-600' : 'text-emerald-600' }}">Rp {{ number_format((float) $invoice->balance_due, 0, ',', '.') }}</dd></div>
                </dl>
            </div>

            @if($invoice->signature_path || $verifyUrl)
            <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5 grid grid-cols-1 gap-5">
                @if($invoice->signature_path)
                <div>
                    <h2 class="text-sm font-semibold text-gray-800 mb-2">Tanda Tangan</h2>
                    <img src="{{ asset('storage/' . $invoice->signature_path) }}" alt="Signature" class="h-20 rounded-xl border border-gray-200 bg-white p-1">
                </div>
                @endif
                @if($verifyUrl)
                <div>
                    <h2 class="text-sm font-semibold text-gray-800 mb-2">QR Verifikasi</h2>
                    <div class="inline-flex items-center justify-center rounded-xl border border-gray-200 bg-white p-3">{!! \SimpleSoftwareIO\QrCode\Facades\QrCode::size(120)->generate($verifyUrl) !!}</div>
                </div>
                @endif
            </div>
            @endif

            <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5">
                <div class="flex items-center justify-between gap-3 mb-4">
                    <h2 class="text-sm font-semibold text-gray-800">Pembayaran</h2>
                    <span class="text-xs text-gray-400">{{ $invoice->payments->count() }} transaksi</span>
                </div>

                @if($invoice->status === 'issued' && (float) $invoice->balance_due > 0)
                <form method="POST" action="{{ route('admin.invoices.payments.store', $invoice) }}" class="space-y-3 mb-5">
                    @csrf
                    <div>
                        <label class="block text-xs font-semibold uppercase tracking-wide text-gray-500 mb-1">Tanggal Bayar</label>
                        <input type="datetime-local" name="payment_date" value="{{ old('payment_date', now()->format('Y-m-d\TH:i')) }}" class="w-full rounded-xl border border-gray-200 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-xs font-semibold uppercase tracking-wide text-gray-500 mb-1">Nominal</label>
                        <input type="number" min="1" max="{{ (float) $invoice->balance_due }}" step="0.01" name="amount" class="w-full rounded-xl border border-gray-200 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-xs font-semibold uppercase tracking-wide text-gray-500 mb-1">Metode</label>
                        <select name="method" class="w-full rounded-xl border border-gray-200 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                            @foreach(\App\Models\Payment::METHODS as $method)
                            <option value="{{ $method }}">{{ ucfirst(str_replace('_', ' ', $method)) }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-xs font-semibold uppercase tracking-wide text-gray-500 mb-1">Referensi</label>
                        <input type="text" name="reference_number" class="w-full rounded-xl border border-gray-200 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-xs font-semibold uppercase tracking-wide text-gray-500 mb-1">Catatan</label>
                        <textarea name="note" rows="3" class="w-full rounded-xl border border-gray-200 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent"></textarea>
                    </div>
                    <button type="submit" class="w-full rounded-xl bg-brand-600 hover:bg-brand-700 text-white text-sm font-semibold px-4 py-3 transition">Catat Pembayaran</button>
                </form>
                @endif

                <div class="space-y-3">
                    @forelse($invoice->payments as $payment)
                    <div class="rounded-xl border border-gray-100 bg-gray-50 px-4 py-3">
                        <div class="flex items-center justify-between gap-3">
                            <div>
                                <p class="text-sm font-semibold text-gray-900">Rp {{ number_format((float) $payment->amount, 0, ',', '.') }}</p>
                                <p class="text-xs text-gray-400">{{ $payment->payment_date->format('d M Y H:i') }} • {{ ucfirst(str_replace('_', ' ', $payment->method)) }}</p>
                            </div>
                            <span class="text-xs text-gray-400">{{ $payment->receiver?->name ?: '-' }}</span>
                        </div>
                    </div>
                    @empty
                    <p class="text-sm text-gray-400">Belum ada pembayaran.</p>
                    @endforelse
                </div>
            </div>

            <div class="bg-white rounded-2xl border border-gray-100 shadow-sm p-5">
                <div class="flex items-center justify-between gap-3 mb-4">
                    <h2 class="text-sm font-semibold text-gray-800">Credit Note</h2>
                    <span class="text-xs text-gray-400">{{ $invoice->creditNotes->count() }} dokumen</span>
                </div>

                @if($invoice->status === 'issued' && auth()->user()?->isAdmin())
                <form method="POST" action="{{ route('admin.invoices.credit-notes.store', $invoice) }}" class="space-y-3 mb-5">
                    @csrf
                    <div>
                        <label class="block text-xs font-semibold uppercase tracking-wide text-gray-500 mb-1">Nominal Credit</label>
                        <input type="number" min="1" step="0.01" name="credit_amount" class="w-full rounded-xl border border-gray-200 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent">
                    </div>
                    <div>
                        <label class="block text-xs font-semibold uppercase tracking-wide text-gray-500 mb-1">Alasan</label>
                        <textarea name="credit_reason" rows="3" class="w-full rounded-xl border border-gray-200 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500 focus:border-transparent"></textarea>
                    </div>
                    <button type="submit" class="w-full rounded-xl bg-indigo-600 hover:bg-indigo-700 text-white text-sm font-semibold px-4 py-3 transition">Buat Credit Note</button>
                </form>
                @endif

                <div class="space-y-3">
                    @forelse($invoice->creditNotes as $credit)
                    <div class="rounded-xl border border-gray-100 bg-gray-50 px-4 py-3">
                        <div class="flex items-start justify-between gap-3">
                            <div>
                                <p class="text-sm font-semibold text-gray-900">{{ $credit->credit_note_number }}</p>
                                <p class="text-xs text-gray-400">{{ $credit->credit_date->format('d M Y') }} • {{ $credit->creator?->name ?: 'System' }}</p>
                                <p class="text-xs text-gray-500 mt-1">{{ $credit->reason }}</p>
                            </div>
                            <span class="text-sm font-bold text-indigo-700">Rp {{ number_format((float) $credit->grand_total, 0, ',', '.') }}</span>
                        </div>
                    </div>
                    @empty
                    <p class="text-sm text-gray-400">Belum ada credit note.</p>
                    @endforelse
                </div>
            </div>

            @if($invoice->status !== 'cancelled')
            <div class="bg-white rounded-2xl border border-rose-100 shadow-sm p-5">
                <h2 class="text-sm font-semibold text-rose-700 mb-3">Batalkan Invoice</h2>
                <form method="POST" action="{{ route('admin.invoices.cancel', $invoice) }}" class="space-y-3">
                    @csrf
                    @method('PATCH')
                    <textarea name="cancel_reason" rows="3" placeholder="Alasan pembatalan" class="w-full rounded-xl border border-rose-200 px-4 py-2.5 text-sm focus:outline-none focus:ring-2 focus:ring-rose-400 focus:border-transparent"></textarea>
                    <button type="submit" class="w-full rounded-xl bg-rose-600 hover:bg-rose-700 text-white text-sm font-semibold px-4 py-3 transition">Batalkan Invoice</button>
                </form>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection