@extends('layouts.admin.app')

@section('title', translate('Operating Expenses'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon">
                    <i class="tio-money-vs"></i>
                </span>
                <span>{{ translate('Operating Expenses') }}</span>
            </h1>
            <div class="page-header-right">
                <a href="{{ route('admin.business-expense.create') }}" class="btn btn--primary">
                    <i class="tio-add-circle"></i> {{ translate('Add Expense') }}
                </a>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <form action="{{ route('admin.business-expense.index') }}" method="get">
                    <div class="row g-3">
                        <div class="col-md-4">
                            <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="{{ translate('Search by category, reference or note') }}">
                        </div>
                        <div class="col-md-3">
                            <select name="category" class="form-control">
                                <option value="all">{{ translate('All Categories') }}</option>
                                @foreach($categories as $key => $label)
                                    <option value="{{ $key }}" {{ request('category') === $key ? 'selected' : '' }}>{{ translate($label) }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2">
                            <input type="date" name="from" value="{{ request('from') }}" class="form-control">
                        </div>
                        <div class="col-md-2">
                            <input type="date" name="to" value="{{ request('to') }}" class="form-control">
                        </div>
                        <div class="col-md-1">
                            <button class="btn btn--primary btn-block" type="submit">{{ translate('Go') }}</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="card-header border-0">
                <h4 class="card-title">{{ translate('Expense History') }} <span class="badge badge-soft-secondary">{{ $expenses->total() }}</span></h4>
            </div>
            <div class="table-responsive">
                <table class="table table-borderless table-thead-bordered table-nowrap card-table">
                    <thead class="thead-light">
                    <tr>
                        <th>{{ translate('sl') }}</th>
                        <th>{{ translate('Date') }}</th>
                        <th>{{ translate('Category') }}</th>
                        <th>{{ translate('Reference') }}</th>
                        <th>{{ translate('Note') }}</th>
                        <th class="text-right">{{ translate('Amount') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($expenses as $key => $expense)
                        <tr>
                            <td>{{ $key + $expenses->firstItem() }}</td>
                            <td>{{ $expense->expense_date?->format('d M Y') }}</td>
                            <td>{{ translate($categories[$expense->category] ?? $expense->category) }}</td>
                            <td>{{ $expense->reference ?? translate('N/A') }}</td>
                            <td>{{ \Illuminate\Support\Str::limit($expense->note, 60, '...') ?: translate('N/A') }}</td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($expense->amount) }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="text-center py-4">{{ translate('No operating expense found') }}</td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($expenses->hasPages())
                <div class="card-footer">
                    {!! $expenses->links() !!}
                </div>
            @endif
        </div>
    </div>
@endsection
