@extends('layouts.app', [
    'pageTitle' => $title,
])

@section('content')
<div>
    <!-- Header -->
    <div class="mb-6 flex flex-wrap items-center justify-between gap-3">
        <div>
            <h2 class="text-xl font-semibold text-gray-800 dark:text-white/90">{{ $title }}</h2>
            <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">Catat dan kelola pengeluaran</p>
        </div>
        <a href="{{ route('expenses.create') }}"
           class="inline-flex items-center justify-center gap-2 rounded-lg bg-brand-500 px-4 py-2.5 text-sm font-medium text-white shadow-theme-xs hover:bg-brand-600">
            <svg class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
            </svg>
            Tambah Pengeluaran
        </a>
    </div>

    @include('partials.alerts')

    <!-- Total Card -->
    <div class="mb-6 rounded-xl border border-error-200 bg-error-50 p-4 dark:border-error-500/30 dark:bg-error-500/15">
        <p class="text-sm text-error-600 dark:text-error-400">Total Pengeluaran (Filter saat ini)</p>
        <p class="text-2xl font-bold text-error-700 dark:text-error-300">Rp {{ number_format($totalExpense, 0, ',', '.') }}</p>
    </div>

    <!-- Filters -->
    <div class="mb-6 rounded-xl border border-gray-200 bg-white p-4 dark:border-gray-800 dark:bg-white/[0.03]">
        <form action="{{ route('expenses.index') }}" method="GET" class="flex flex-wrap gap-4">
            <div class="w-full sm:flex-1">
                <x-form.select name="category_id" placeholder="Semua Kategori">
                    @foreach($categories as $category)
                        <option value="{{ $category->id }}" {{ request('category_id') == $category->id ? 'selected' : '' }}>
                            {{ $category->name }}
                        </option>
                    @endforeach
                </x-form.select>
            </div>
            <div class="w-full sm:flex-1">
                <x-form.date-picker name="date_from" :defaultDate="request('date_from')" placeholder="Dari Tanggal"/>
            </div>
            <div class="w-full sm:flex-1">
                <x-form.date-picker name="date_to" :defaultDate="request('date_to')" placeholder="Sampai Tanggal"/>
            </div>
            <div class="w-full sm:flex-1">
                <x-form.input name="search" value="{{ request('search') }}" placeholder="Cari keterangan..."/>
            </div>
            <button type="submit" class="rounded-lg bg-gray-100 px-4 py-2.5 text-sm font-medium text-gray-700 hover:bg-gray-200 dark:bg-gray-800 dark:text-gray-300 dark:hover:bg-gray-700">
                Filter
            </button>
            <a href="{{ route('expenses.index') }}" class="rounded-lg border border-gray-300 px-4 py-2.5 text-sm font-medium text-gray-700 hover:bg-gray-50 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-800">
                Reset
            </a>
        </form>
    </div>

    <!-- Table -->
    <div class="overflow-hidden rounded-xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="border-b border-gray-100 dark:border-gray-800">
                    <tr class="text-left">
                        <th class="px-5 py-3 text-sm font-medium text-gray-500 dark:text-gray-400">Tanggal</th>
                        <th class="px-5 py-3 text-sm font-medium text-gray-500 dark:text-gray-400">Kategori</th>
                        <th class="px-5 py-3 text-sm font-medium text-gray-500 dark:text-gray-400">Jumlah</th>
                        <th class="px-5 py-3 text-sm font-medium text-gray-500 dark:text-gray-400">Keterangan</th>
                        <th class="px-5 py-3 text-sm font-medium text-gray-500 dark:text-gray-400">Oleh</th>
                        <th class="px-5 py-3 text-sm font-medium text-gray-500 dark:text-gray-400"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100 dark:divide-gray-800">
                    @forelse($expenses as $expense)
                    <tr>
                        <td class="px-5 py-4 text-sm text-gray-800 dark:text-white/90">
                            {{ $expense->expense_date->format('d/m/Y') }}
                        </td>
                        <td class="px-5 py-4">
                            <span class="inline-flex rounded-full bg-gray-100 px-2 py-1 text-xs font-medium text-gray-700 dark:bg-gray-700 dark:text-gray-300">
                                {{ $expense->category->name ?? '-' }}
                            </span>
                        </td>
                        <td class="px-5 py-4 text-sm font-medium text-error-600 dark:text-error-400">
                            {{ $expense->formatted_amount }}
                        </td>
                        <td class="px-5 py-4 text-sm text-gray-500 dark:text-gray-400 max-w-xs truncate">
                            {{ $expense->description ?? '-' }}
                        </td>
                        <td class="px-5 py-4 text-sm text-gray-500 dark:text-gray-400">
                            {{ $expense->creator->name ?? '-' }}
                        </td>
                        <td class="px-5 py-4">
                            <div class="flex items-center gap-2">
                                <a href="{{ route('expenses.edit', $expense) }}" class="text-sm text-brand-500 hover:text-brand-600">Edit</a>
                                <button @click="$dispatch('open-delete-modal', { action: '{{ route('expenses.destroy', $expense) }}', title: 'Hapus Pengeluaran?', description: 'Apakah anda yakin ingin menghapus data pengeluaran ini? Data yang dihapus tidak dapat dikembalikan.' })" type="button" class="text-sm text-error-500 hover:text-error-600">Hapus</button>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" class="px-5 py-8 text-center text-sm text-gray-500 dark:text-gray-400">
                            Belum ada data pengeluaran
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    @if($expenses->hasPages())
    <div class="mt-4">
        {{ $expenses->links() }}
    </div>
    @endif
</div>
    <x-ui.delete-confirm />
@endsection
