@extends('layouts.app')
@section('title','Recurring Expenses')
@section('content')
<x-ui.page-toolbar title="Recurring Expenses" description="Schedule regular operating costs and surface reminders before each due date." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Expenses','url'=>route('expenses.index')],['label'=>'Recurring']]">
    <x-slot:actions>@if(auth()->user()->can('expenses.recurring.manage'))<a class="mf-btn mf-btn-primary" href="{{ route('recurring-expenses.create') }}">New Schedule</a>@endif</x-slot:actions>
</x-ui.page-toolbar>

<div class="mf-card mb-4">
    <div class="border-b p-4"><h2 class="font-semibold">Schedules</h2><p class="mt-1 text-xs text-muted-foreground">When a due date arrives the system creates a pending expense. Approval is still required before cash is reduced.</p></div>
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table>
        <thead><tr><th>Description</th><th>Category</th><th>Branch / Account</th><th>Amount</th><th>Frequency</th><th>Next due</th><th>Reminder</th><th>Status</th><th></th></tr></thead>
        <tbody>
        @forelse($schedules as $schedule)
            <tr>
                <td class="font-medium">{{ $schedule->description }}<div class="text-xs text-muted-foreground">{{ $schedule->supplier ?: 'No supplier' }}</div></td>
                <td>{{ $schedule->category->name }}</td>
                <td>{{ $schedule->branch->name }}<div class="text-xs text-muted-foreground">{{ $schedule->financialAccount->name }}</div></td>
                <td class="font-semibold">{{ number_format((float)$schedule->amount,2) }}</td>
                <td>{{ str($schedule->recurrence)->replace('_',' ')->title() }}@if($schedule->recurrence==='custom_days') · {{ $schedule->interval_days }} days @endif</td>
                <td>{{ $schedule->next_due_date->format('d M Y') }}</td>
                <td>{{ $schedule->reminder_days_before }} day(s) before</td>
                <td><span class="mf-badge {{ $schedule->status==='active'?'mf-badge-success':'' }}">{{ ucfirst($schedule->status) }}</span></td>
                <td class="text-right">@if(auth()->user()->can('expenses.recurring.manage'))<a class="mf-btn" href="{{ route('recurring-expenses.edit',$schedule) }}">Edit</a>@endif</td>
            </tr>
        @empty<tr><td colspan="9" class="text-center text-muted-foreground">No recurring expense schedules configured.</td></tr>@endforelse
        </tbody>
    </table></div>
</div>
<div class="mb-4">{{ $schedules->links() }}</div>

<div class="mf-card">
    <div class="border-b p-4"><h2 class="font-semibold">Reminder history</h2><p class="mt-1 text-xs text-muted-foreground">Rent, internet, security, software, insurance and any custom category can use the same reminder engine.</p></div>
    <div class="mf-table-frame border-0"><table class="mf-table" data-standard-table>
        <thead><tr><th>Due date</th><th>Schedule</th><th>Message</th><th>Generated expense</th><th>Status</th><th></th></tr></thead>
        <tbody>
        @forelse($reminders as $reminder)
            <tr>
                <td>{{ $reminder->due_date->format('d M Y') }}<div class="text-xs text-muted-foreground">Reminder: {{ $reminder->reminder_date->format('d M Y') }}</div></td>
                <td>{{ $reminder->recurringExpense->description }}<div class="text-xs text-muted-foreground">{{ $reminder->recurringExpense->category->name }}</div></td>
                <td>{{ $reminder->message }}</td>
                <td>@if($reminder->expense)<a class="text-primary hover:underline" href="{{ route('expenses.show',$reminder->expense) }}">{{ $reminder->expense->expense_number }}</a>@else—@endif</td>
                <td><span class="mf-badge {{ $reminder->status==='acknowledged'?'mf-badge-success':($reminder->status==='generated'?'mf-badge-warning':'') }}">{{ ucfirst($reminder->status) }}</span></td>
                <td class="text-right">@if($reminder->status!=='acknowledged' && auth()->user()->can('expenses.recurring.manage'))<form method="POST" action="{{ route('expense-reminders.acknowledge',$reminder) }}">@csrf<button class="mf-btn">Acknowledge</button></form>@endif</td>
            </tr>
        @empty<tr><td colspan="6" class="text-center text-muted-foreground">No recurring expense reminders have been generated.</td></tr>@endforelse
        </tbody>
    </table></div>
</div>
<div class="mt-4">{{ $reminders->links() }}</div>
@endsection