<x-app-layout>
    <x-slot name="header">{{ __('Vendor Report') }}</x-slot>

    {{-- Date Filter --}}
    <div class="card shadow-sm border-0 mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('reports.vendor') }}" class="row g-3 align-items-end">
                <div class="col-12 col-md-4">
                    <label class="form-label fw-semibold">{{ __('From Date') }}</label>
                    <input type="date" name="from" class="form-control" value="{{ $from }}">
                </div>
                <div class="col-12 col-md-4">
                    <label class="form-label fw-semibold">{{ __('To Date') }}</label>
                    <input type="date" name="to" class="form-control" value="{{ $to }}">
                </div>
                <div class="col-12 col-md-4">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-funnel me-1"></i>{{ __('Filter') }}
                    </button>
                </div>
            </form>
        </div>
    </div>

    {{-- Summary Cards --}}
    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-bg-primary">
                <div class="card-body py-3">
                    <div class="small fw-semibold mb-1">{{ __('Total Vendors') }}</div>
                    <h5 class="mb-0">{{ $vendors->count() }}</h5>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-bg-success">
                <div class="card-body py-3">
                    <div class="small fw-semibold mb-1">{{ __('Total Purchases') }}</div>
                    <h5 class="mb-0">{{ number_format($vendors->sum('period_total'), 2) }}</h5>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-bg-warning">
                <div class="card-body py-3">
                    <div class="small fw-semibold mb-1">{{ __('Cash Paid') }}</div>
                    <h5 class="mb-0">{{ number_format($vendors->sum('period_cash'), 2) }}</h5>
                </div>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="card border-0 shadow-sm text-bg-danger">
                <div class="card-body py-3">
                    <div class="small fw-semibold mb-1">{{ __('Total Due (All Time)') }}</div>
                    <h5 class="mb-0">{{ number_format($vendors->sum('current_due'), 2) }}</h5>
                </div>
            </div>
        </div>
    </div>

    {{-- Table --}}
    <div class="card shadow-sm border-0">
        <div class="card-header bg-white border-0 pt-3 pb-0">
            <h6 class="fw-semibold mb-0">
                <i class="bi bi-truck me-2 text-primary"></i>
                {{ __('Vendor Report') }}: {{ \Carbon\Carbon::parse($from)->format('d M Y') }} → {{ \Carbon\Carbon::parse($to)->format('d M Y') }}
            </h6>
        </div>
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th>{{ __('#') }}</th>
                        <th>{{ __('Vendor') }}</th>
                        <th>{{ __('Phone') }}</th>
                        <th class="text-end">{{ __('Period Purchases') }}</th>
                        <th class="text-end">{{ __('Cash') }}</th>
                        <th class="text-end">{{ __('Period Due') }}</th>
                        <th class="text-end">{{ __('Current Due (All Time)') }}</th>
                        <th>{{ __('Status') }}</th>
                        <th>{{ __('Actions') }}</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($vendors as $v)
                        <tr>
                            <td>{{ $loop->iteration }}</td>
                            <td class="fw-semibold">{{ $v->name }}</td>
                            <td>{{ $v->phone ?? '—' }}</td>
                            <td class="text-end">{{ number_format($v->period_total, 2) }}</td>
                            <td class="text-end text-success">{{ number_format($v->period_cash, 2) }}</td>
                            <td class="text-end text-danger">{{ number_format($v->period_due, 2) }}</td>
                            <td class="text-end {{ $v->current_due > 0 ? 'text-danger fw-bold' : 'text-muted' }}">
                                {{ number_format($v->current_due, 2) }}
                            </td>
                            <td>
                                @if($v->is_active)
                                    <span class="badge bg-success">{{ __('Active') }}</span>
                                @else
                                    <span class="badge bg-secondary">{{ __('Inactive') }}</span>
                                @endif
                            </td>
                            <td>
                                <a href="{{ route('vendors.ledger', $v) }}" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-journal-text"></i> {{ __('Ledger') }}
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="9" class="text-center text-muted py-4">{{ __('No vendors found.') }}</td>
                        </tr>
                    @endforelse
                </tbody>
                @if($vendors->count())
                    <tfoot class="table-light fw-bold">
                        <tr>
                            <td colspan="3">{{ __('Total') }}</td>
                            <td class="text-end">{{ number_format($vendors->sum('period_total'), 2) }}</td>
                            <td class="text-end text-success">{{ number_format($vendors->sum('period_cash'), 2) }}</td>
                            <td class="text-end text-danger">{{ number_format($vendors->sum('period_due'), 2) }}</td>
                            <td class="text-end text-danger">{{ number_format($vendors->sum('current_due'), 2) }}</td>
                            <td colspan="2"></td>
                        </tr>
                    </tfoot>
                @endif
            </table>
        </div>
    </div>
</x-app-layout>
