@extends('layouts.app')

@section('title', 'Purchase Report')

@section('breadcrumb')
    @include('components.breadcrumb', ['items' => ['Reports' => null, 'Purchase Report' => null]])
@endsection

@section('content')
<div>
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
        <div>
            <h1 class="text-2xl font-bold text-slate-900 dark:text-white">Purchase Report</h1>
            <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">Filter and analyze purchase data</p>
        </div>
        <div class="flex flex-wrap items-center gap-2">
            <x-button variant="outline" icon="printer">Print</x-button>
            <x-button variant="success" icon="file-spreadsheet">Excel</x-button>
            <x-button variant="danger" icon="file-text">PDF</x-button>
        </div>
    </div>

    {{-- Filters --}}
    <x-card class="mb-6" title="Filters" icon="sliders-horizontal">
        <form method="GET" class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4">
            <input type="date" name="from_date" value="{{ request('from_date', now()->startOfMonth()->format('Y-m-d')) }}"
                class="rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 px-3.5 py-2.5 text-sm focus:ring-2 focus:ring-primary-500" />
            <input type="date" name="to_date" value="{{ request('to_date', now()->format('Y-m-d')) }}"
                class="rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 px-3.5 py-2.5 text-sm focus:ring-2 focus:ring-primary-500" />
            <select name="supplier_id" class="rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 px-3.5 py-2.5 text-sm focus:ring-2 focus:ring-primary-500">
                <option value="">All Suppliers</option>
                @foreach(\App\Models\Party::suppliers()->active()->orderBy('name')->get() as $s)
                    <option value="{{ $s->id }}" {{ request('supplier_id') == $s->id ? 'selected' : '' }}>{{ $s->name }}</option>
                @endforeach
            </select>
            <div></div>
            <x-button variant="primary" icon="search" class="self-end">Generate</x-button>
        </form>
    </x-card>

    <div class="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
        <x-stat-card title="Total Purchase" :value="$totals['grand_total']" icon="shopping-cart" iconBg="primary" />
        <x-stat-card title="Total Records" :value="$totals['count']" prefix="" icon="file-text" iconBg="info" />
        <x-stat-card title="Total Paid" :value="$totals['paid']" icon="check-circle" iconBg="success" />
        <x-stat-card title="Total Due" :value="$totals['due']" icon="alert-circle" iconBg="danger" />
    </div>

    <x-card :padding="false">
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr class="bg-slate-50 dark:bg-slate-900/50 border-b border-slate-200 dark:border-slate-800">
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase tracking-wider text-slate-500">Date</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase tracking-wider text-slate-500">Voucher</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase tracking-wider text-slate-500">Supplier</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase tracking-wider text-slate-500">Items</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Qty</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Amount</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    @forelse($purchases as $p)
                        <tr class="hover:bg-slate-50 dark:hover:bg-slate-800/50">
                            <td class="px-4 py-3 text-xs text-slate-500">{{ $p->purchase_date->format('M d, Y') }}</td>
                            <td class="px-4 py-3 text-xs font-mono text-primary-600 font-semibold">{{ $p->voucher_no }}</td>
                            <td class="px-4 py-3 text-sm font-medium">{{ $p->party->name ?? '—' }}</td>
                            <td class="px-4 py-3 text-xs text-slate-500">
                                @foreach($p->items as $item)
                                    {{ $item->yarn->name ?? '—' }}{{ !$loop->last ? ', ' : '' }}
                                @endforeach
                            </td>
                            <td class="px-4 py-3 text-right text-sm counter-num">{{ number_format($p->items->sum('quantity')) }}</td>
                            <td class="px-4 py-3 text-right text-sm font-bold text-primary-600 counter-num">৳{{ number_format($p->grand_total) }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="text-center py-8 text-sm text-slate-500">No records found.</td></tr>
                    @endforelse
                </tbody>
                @if($purchases->isNotEmpty())
                <tfoot>
                    <tr class="bg-primary-50 dark:bg-primary-500/10 border-t-2">
                        <td colspan="4" class="px-4 py-3 text-sm font-bold text-right text-primary-700 dark:text-primary-300">Total</td>
                        <td class="px-4 py-3 text-right text-sm font-bold text-primary-700 dark:text-primary-300 counter-num">{{ number_format($purchases->sum(fn ($p) => $p->items->sum('quantity'))) }}</td>
                        <td class="px-4 py-3 text-right text-base font-bold text-primary-700 dark:text-primary-300 counter-num">৳{{ number_format($totals['grand_total']) }}</td>
                    </tr>
                </tfoot>
                @endif
            </table>
        </div>
        @if($purchases->hasPages())
        <div class="px-4 py-3 border-t border-slate-200 dark:border-slate-800">
            {{ $purchases->withQueryString()->links() }}
        </div>
        @endif
    </x-card>
</div>
@endsection
