@extends('layouts.admin')

@section('title', 'Recurring Invoices')

@section('page-title', 'Recurring Invoices')

@section('header-actions')
    <a href="{{ route('admin.recurring-invoices.create') }}" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors flex items-center gap-2">
        <span class="material-symbols-outlined text-lg">add</span>
        <span>New Recurring Invoice</span>
    </a>
@endsection

@section('content')
<div class="max-w-7xl mx-auto">
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left">
                <thead class="bg-background-light border-b border-[#e5e7eb]">
                    <tr>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Name</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Customer</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Frequency</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Next Invoice</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Generated</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Status</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider text-right">Actions</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-[#e5e7eb]">
                    @forelse($recurringInvoices as $recurring)
                        <tr class="hover:bg-background-light/50 transition-colors">
                            <td class="py-4 px-4">
                                <a href="{{ route('admin.recurring-invoices.show', $recurring) }}" class="text-primary hover:underline font-medium">
                                    {{ $recurring->name }}
                                </a>
                            </td>
                            <td class="py-4 px-4 text-sm text-[#111418] text-gray-900">
                                {{ $recurring->customer->name }}
                            </td>
                            <td class="py-4 px-4 text-sm text-[#617589]">
                                {{ ucfirst($recurring->frequency) }}
                            </td>
                            <td class="py-4 px-4 text-sm text-[#617589]">
                                {{ $recurring->next_invoice_date ? $recurring->next_invoice_date->format('M d, Y') : 'N/A' }}
                            </td>
                            <td class="py-4 px-4 text-sm text-[#617589]">
                                {{ $recurring->invoices->count() }} invoices
                            </td>
                            <td class="py-4 px-4">
                                <span class="px-2 py-1 rounded text-xs font-medium {{ $recurring->status === 'active' ? 'bg-green-100 text-green-800' : ($recurring->status === 'paused' ? 'bg-yellow-100 text-yellow-800' : 'bg-gray-100 text-gray-800') }}">
                                    {{ ucfirst($recurring->status) }}
                                </span>
                            </td>
                            <td class="py-4 px-4 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{{ route('admin.recurring-invoices.show', $recurring) }}" class="p-2 text-[#617589] hover:text-primary hover:bg-primary/10 rounded transition-colors" title="View">
                                        <span class="material-symbols-outlined text-lg">visibility</span>
                                    </a>
                                    @if($recurring->status === 'active')
                                        <form method="POST" action="{{ route('admin.recurring-invoices.pause', $recurring) }}" class="inline">
                                            @csrf
                                            <button type="submit" class="p-2 text-[#617589] hover:text-yellow-600 hover:bg-yellow-50 rounded transition-colors" title="Pause">
                                                <span class="material-symbols-outlined text-lg">pause</span>
                                            </button>
                                        </form>
                                    @elseif($recurring->status === 'paused')
                                        <form method="POST" action="{{ route('admin.recurring-invoices.resume', $recurring) }}" class="inline">
                                            @csrf
                                            <button type="submit" class="p-2 text-[#617589] hover:text-green-600 hover:bg-green-50 rounded transition-colors" title="Resume">
                                                <span class="material-symbols-outlined text-lg">play_arrow</span>
                                            </button>
                                        </form>
                                    @endif
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="py-12 px-4 text-center text-[#617589]">
                                <span class="material-symbols-outlined text-4xl mb-2 block">repeat</span>
                                <p>No recurring invoices found</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($recurringInvoices->hasPages())
            <div class="px-4 py-4 border-t border-[#e5e7eb]">
                {{ $recurringInvoices->links() }}
            </div>
        @endif
    </div>
</div>
@endsection



