@extends('layouts.admin')

@section('title', 'Expenses')

@section('page-title', 'Expenses')

@section('header-actions')
    <a href="{{ route('admin.expenses.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>Add Expense</span>
    </a>
@endsection

@section('content')
<div class="max-w-7xl mx-auto">
    <!-- Filters -->
    <div class="bg-white rounded-xl shadow-sm border border-[#e5e7eb] p-6 mb-6">
        <form method="GET" action="{{ route('admin.expenses.index') }}" class="grid grid-cols-1 md:grid-cols-5 gap-4">
            <div>
                <label class="block text-sm font-medium text-[#111418] text-gray-900 mb-2">Status</label>
                <select name="status" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary text-gray-900">
                    <option value="">All Statuses</option>
                    <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>Draft</option>
                    <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>Approved</option>
                    <option value="paid" {{ request('status') == 'paid' ? 'selected' : '' }}>Paid</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-[#111418] text-gray-900 mb-2">Account</label>
                <select name="account_id" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary text-gray-900">
                    <option value="">All Accounts</option>
                    @foreach($accounts as $account)
                        <option value="{{ $account->id }}" {{ request('account_id') == $account->id ? 'selected' : '' }}>{{ $account->name }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-[#111418] text-gray-900 mb-2">Vendor</label>
                <select name="vendor_id" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary text-gray-900">
                    <option value="">All Vendors</option>
                    @foreach($vendors as $vendor)
                        <option value="{{ $vendor->id }}" {{ request('vendor_id') == $vendor->id ? 'selected' : '' }}>{{ $vendor->name }}</option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-[#111418] text-gray-900 mb-2">From Date</label>
                <input type="date" name="date_from" value="{{ request('date_from') }}" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary text-gray-900">
            </div>
            <div>
                <label class="block text-sm font-medium text-[#111418] text-gray-900 mb-2">To Date</label>
                <input type="date" name="date_to" value="{{ request('date_to') }}" class="w-full h-11 rounded-lg border border-[#dbe0e6] bg-white px-3 py-2 text-sm outline-none focus:border-primary focus:ring-1 focus:ring-primary text-gray-900">
            </div>
            <div class="md:col-span-5 flex gap-2">
                <button type="submit" class="px-4 py-2 bg-primary text-white rounded-lg hover:bg-blue-600 transition-colors">Filter</button>
                <a href="{{ route('admin.expenses.index') }}" class="px-4 py-2 border border-[#dbe0e6] text-[#617589] rounded-lg hover:bg-background-light transition-colors">Clear</a>
            </div>
        </form>
    </div>

    <!-- Expenses Table -->
    <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">Expense</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">Account</th>
                        <th class="py-3 px-4 text-xs font-semibold text-[#617589] uppercase tracking-wider">Payment Method</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">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($expenses as $expense)
                        <tr class="hover:bg-background-light/50 transition-colors">
                            <td class="py-4 px-4">
                                <div class="flex items-center gap-3">
                                    <div class="size-10 rounded-lg bg-orange-100 text-orange-600 flex items-center justify-center shrink-0">
                                        <span class="material-symbols-outlined">receipt</span>
                                    </div>
                                    <div>
                                        <a href="{{ route('admin.expenses.show', $expense) }}" class="font-medium text-[#111418] text-gray-900 hover:text-primary">
                                            {{ $expense->expense_number }}
                                        </a>
                                        @if($expense->description)
                                            <p class="text-xs text-[#617589]">{{ Str::limit($expense->description, 40) }}</p>
                                        @endif
                                    </div>
                                </div>
                            </td>
                            <td class="py-4 px-4 text-right font-medium text-[#111418] text-gray-900">
                                {{ \App\Models\Currency::formatAmount($expense->total, $expense->currency) }}
                            </td>
                            <td class="py-4 px-4 text-sm text-[#617589]">
                                {{ $expense->account->name }}
                            </td>
                            <td class="py-4 px-4">
                                <span class="px-2 py-1 rounded-full text-xs font-medium bg-purple-100 text-purple-700">
                                    {{ ucfirst(str_replace('_', ' ', $expense->payment_method)) }}
                                </span>
                            </td>
                            <td class="py-4 px-4 text-sm text-[#617589]">
                                {{ $expense->expense_date->format('M d, Y') }}
                            </td>
                            <td class="py-4 px-4">
                                @php
                                    $statusColors = [
                                        'draft' => 'bg-gray-100 text-gray-800',
                                        'approved' => 'bg-green-100 text-green-800',
                                        'paid' => 'bg-blue-100 text-blue-800',
                                        'rejected' => 'bg-red-100 text-red-800',
                                    ];
                                    $color = $statusColors[$expense->status] ?? 'bg-gray-100 text-gray-800';
                                @endphp
                                <span class="px-2 py-1 rounded text-xs font-medium {{ $color }}">
                                    {{ ucfirst($expense->status) }}
                                </span>
                            </td>
                            <td class="py-4 px-4 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <a href="{{ route('admin.expenses.show', $expense) }}" 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($expense->status === 'draft')
                                        <form method="POST" action="{{ route('admin.expenses.approve', $expense) }}" class="inline">
                                            @csrf
                                            <button type="submit" class="p-2 text-[#617589] hover:text-green-600 hover:bg-green-50 rounded transition-colors" title="Approve">
                                                <span class="material-symbols-outlined text-lg">check_circle</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">receipt</span>
                                <p>No expenses found</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

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



