@extends('customer.layouts.app')

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

@section('page-title', 'Invoice Details')

@section('breadcrumbs')
    <a href="{{ route('customer.invoices.index') }}" class="hover:text-primary">Invoices</a>
    <span class="material-symbols-outlined text-xs">chevron_right</span>
    <span class="text-primary font-medium">{{ $invoice->invoice_number }}</span>
@endsection

@section('header-actions')
    <a href="{{ route('customer.invoices.download', $invoice->id) }}" target="_blank" rel="noopener noreferrer" class="px-4 py-2 border border-[#dbe0e6] text-[#617589] rounded-lg hover:bg-background-light transition-colors flex items-center gap-2">
        <span class="material-symbols-outlined text-lg">download</span>
        <span>Download PDF</span>
    </a>
@endsection

@section('content')
<div class="max-w-5xl mx-auto flex flex-col gap-6">
    <!-- Invoice Header -->
    <div class="bg-white dark:bg-[#1e2630] rounded-xl shadow-sm border border-[#e5e7eb] dark:border-gray-700 p-6">
        <div class="flex justify-between items-start mb-6">
            <div>
                <h2 class="text-2xl font-bold text-[#111418] dark:text-white mb-2">{{ $invoice->invoice_number }}</h2>
                <div class="flex items-center gap-4 text-sm text-[#617589]">
                    <span>Issue Date: {{ $invoice->issue_date->format('M d, Y') }}</span>
                    <span>Due Date: {{ $invoice->due_date->format('M d, Y') }}</span>
                </div>
            </div>
            <div class="text-right">
                @php
                    $statusColors = [
                        'draft' => 'bg-gray-100 text-gray-800',
                        'sent' => 'bg-blue-100 text-blue-800',
                        'viewed' => 'bg-purple-100 text-purple-800',
                        'partial' => 'bg-yellow-100 text-yellow-800',
                        'paid' => 'bg-green-100 text-green-800',
                        'overdue' => 'bg-red-100 text-red-800',
                    ];
                    $status = $invoice->isOverdue() ? 'overdue' : $invoice->status;
                @endphp
                <span class="px-3 py-1 rounded-lg text-sm font-medium {{ $statusColors[$status] ?? 'bg-gray-100 text-gray-800' }}">
                    {{ ucfirst($status) }}
                </span>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
                <p class="text-xs font-semibold text-[#617589] uppercase tracking-wider mb-2">Billed To</p>
                <div class="text-sm text-[#111418] dark:text-white">
                    <p class="font-semibold">{{ $invoice->customer->name }}</p>
                    @if($invoice->customer->email)
                        <p class="text-[#617589]">{{ $invoice->customer->email }}</p>
                    @endif
                    @if($invoice->customer->address)
                        <p class="text-[#617589]">{{ $invoice->customer->address }}</p>
                    @endif
                </div>
            </div>
            <div>
                <p class="text-xs font-semibold text-[#617589] uppercase tracking-wider mb-2">Payment Summary</p>
                <div class="text-sm space-y-1">
                    <div class="flex justify-between">
                        <span class="text-[#617589]">Total Amount:</span>
                        <span class="font-medium text-[#111418] dark:text-white">{{ $invoice->currency }} {{ number_format($invoice->total, 2) }}</span>
                    </div>
                    <div class="flex justify-between">
                        <span class="text-[#617589]">Paid Amount:</span>
                        <span class="font-medium text-green-600">{{ $invoice->currency }} {{ number_format($invoice->paid_amount, 2) }}</span>
                    </div>
                    <div class="flex justify-between pt-2 border-t border-[#e5e7eb]">
                        <span class="font-semibold text-[#111418] dark:text-white">Balance Due:</span>
                        <span class="font-bold text-lg {{ $invoice->balance > 0 ? 'text-red-600' : 'text-green-600' }}">
                            {{ $invoice->currency }} {{ number_format($invoice->balance, 2) }}
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Invoice Items -->
    <div class="bg-white dark:bg-[#1e2630] rounded-xl shadow-sm border border-[#e5e7eb] dark:border-gray-700 p-6">
        <h3 class="text-lg font-bold text-[#111418] dark:text-white mb-6">Invoice Items</h3>
        <div class="overflow-x-auto">
            <table class="w-full text-left">
                <thead class="bg-background-light dark:bg-[#111418] border-b border-[#e5e7eb]">
                    <tr>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Description</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Quantity</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Unit Price</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Tax Rate</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Amount</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-[#e5e7eb]">
                    @foreach($invoice->items as $item)
                        <tr>
                            <td class="py-4 px-4 text-sm text-[#111418] dark:text-white">{{ $item->description }}</td>
                            <td class="py-4 px-4 text-sm text-[#617589] text-right">{{ number_format($item->quantity, 2) }}</td>
                            <td class="py-4 px-4 text-sm text-[#617589] text-right">{{ $invoice->currency }} {{ number_format($item->unit_price, 2) }}</td>
                            <td class="py-4 px-4 text-sm text-[#617589] text-right">{{ number_format($item->tax_rate, 2) }}%</td>
                            <td class="py-4 px-4 text-sm font-medium text-[#111418] dark:text-white text-right">{{ $invoice->currency }} {{ number_format($item->total, 2) }}</td>
                        </tr>
                    @endforeach
                </tbody>
                <tfoot class="bg-background-light dark:bg-[#111418]">
                    <tr>
                        <td colspan="4" class="py-4 px-4 text-sm font-semibold text-[#111418] dark:text-white text-right">Subtotal:</td>
                        <td class="py-4 px-4 text-sm font-semibold text-[#111418] dark:text-white text-right">{{ $invoice->currency }} {{ number_format($invoice->subtotal, 2) }}</td>
                    </tr>
                    @if($invoice->tax_amount > 0)
                        <tr>
                            <td colspan="4" class="py-4 px-4 text-sm font-semibold text-[#111418] dark:text-white text-right">Tax:</td>
                            <td class="py-4 px-4 text-sm font-semibold text-[#111418] dark:text-white text-right">{{ $invoice->currency }} {{ number_format($invoice->tax_amount, 2) }}</td>
                        </tr>
                    @endif
                    @if($invoice->discount_amount > 0)
                        <tr>
                            <td colspan="4" class="py-4 px-4 text-sm font-semibold text-[#111418] dark:text-white text-right">Discount:</td>
                            <td class="py-4 px-4 text-sm font-semibold text-[#111418] dark:text-white text-right">- {{ $invoice->currency }} {{ number_format($invoice->discount_amount, 2) }}</td>
                        </tr>
                    @endif
                    <tr class="border-t-2 border-[#e5e7eb]">
                        <td colspan="4" class="py-4 px-4 text-lg font-bold text-[#111418] dark:text-white text-right">Total:</td>
                        <td class="py-4 px-4 text-lg font-bold text-primary text-right">{{ $invoice->currency }} {{ number_format($invoice->total, 2) }}</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>

    <!-- Payment History -->
    @if($invoice->payments->count() > 0)
        <div class="bg-white dark:bg-[#1e2630] rounded-xl shadow-sm border border-[#e5e7eb] dark:border-gray-700 p-6">
            <h3 class="text-lg font-bold text-[#111418] dark:text-white mb-6">Payment History</h3>
            <div class="overflow-x-auto">
                <table class="w-full text-left">
                    <thead class="bg-background-light dark:bg-[#111418] border-b border-[#e5e7eb]">
                        <tr>
                            <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Payment #</th>
                            <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Date</th>
                            <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Method</th>
                            <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Amount</th>
                            <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Status</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-[#e5e7eb]">
                        @foreach($invoice->payments as $payment)
                            <tr>
                                <td class="py-4 px-4 text-sm text-[#111418] dark:text-white">
                                    <a href="{{ route('customer.payments.show', $payment->id) }}" class="text-primary hover:underline">
                                        {{ $payment->payment_number }}
                                    </a>
                                </td>
                                <td class="py-4 px-4 text-sm text-[#617589]">{{ $payment->payment_date->format('M d, Y') }}</td>
                                <td class="py-4 px-4 text-sm text-[#617589]">{{ ucfirst(str_replace('_', ' ', $payment->payment_method)) }}</td>
                                <td class="py-4 px-4 text-sm font-medium text-[#111418] dark:text-white text-right">{{ $payment->currency }} {{ number_format($payment->amount, 2) }}</td>
                                <td class="py-4 px-4">
                                    @php
                                        $statusColors = [
                                            'pending' => 'bg-yellow-100 text-yellow-700',
                                            'completed' => 'bg-green-100 text-green-700',
                                            'failed' => 'bg-red-100 text-red-700',
                                            'refunded' => 'bg-gray-100 text-gray-700',
                                        ];
                                    @endphp
                                    <span class="px-2 py-1 rounded-full text-xs font-medium {{ $statusColors[$payment->status] ?? 'bg-gray-100 text-gray-700' }}">
                                        {{ ucfirst($payment->status) }}
                                    </span>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    @endif
</div>
@endsection



