@extends('layouts.app')

@section('title', 'Supplier Ledger')

@section('breadcrumb')
    @include('components.breadcrumb', ['items' => ['Reports' => null, 'Supplier Ledger' => 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">Supplier Ledger</h1>
            <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">View detailed transaction history for any supplier</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>

    {{-- Party Selector --}}
    <x-card class="mb-6" title="Select Supplier" icon="truck">
        <form method="GET" class="flex flex-col sm:flex-row gap-4 items-end">
            <div class="flex-1">
                <label class="block text-xs font-semibold text-slate-700 dark:text-slate-300 mb-1.5">Supplier</label>
                <select name="party_id" class="w-full 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" required>
                    <option value="">— Select Supplier —</option>
                    @foreach(\App\Models\Party::suppliers()->active()->orderBy('name')->get() as $sup)
                        <option value="{{ $sup->id }}" {{ request('party_id') == $sup->id ? 'selected' : '' }}>
                            {{ $sup->name }} ({{ $sup->code }})
                        </option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-xs font-semibold text-slate-700 dark:text-slate-300 mb-1.5">From</label>
                <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" />
            </div>
            <div>
                <label class="block text-xs font-semibold text-slate-700 dark:text-slate-300 mb-1.5">To</label>
                <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" />
            </div>
            <x-button variant="primary" icon="search">Generate</x-button>
        </form>
    </x-card>

    @if(isset($party) && $party)
        {{-- Summary Cards --}}
        <div class="grid grid-cols-1 sm:grid-cols-3 gap-4 mb-6">
            <x-stat-card title="Total Purchase" :value="$totals['total_purchase']" icon="shopping-cart" iconBg="primary" gradient />
            <x-stat-card title="Total Paid" :value="$totals['total_paid']" icon="check-circle" iconBg="success" gradient />
            <x-stat-card title="Current Due" :value="$totals['current_due']" icon="alert-circle" iconBg="danger" gradient />
        </div>

        {{-- Supplier Info --}}
        <x-card class="mb-6">
            <div class="flex flex-col sm:flex-row sm:items-center gap-4">
                <div class="w-14 h-14 rounded-2xl bg-gradient-to-br from-primary-500 to-primary-700 flex items-center justify-center font-bold text-white text-lg shadow-glow">
                    {{ strtoupper(substr($party->name, 0, 2)) }}
                </div>
                <div class="flex-1">
                    <h3 class="text-lg font-bold text-slate-900 dark:text-white">{{ $party->name }}</h3>
                    <p class="text-sm text-slate-500">{{ $party->phone }} • {{ $party->email ?? 'N/A' }}</p>
                    <p class="text-xs text-slate-400 mt-1">{{ $party->address ?? '' }}</p>
                </div>
                <div class="flex flex-wrap gap-2">
                    <x-badge variant="success" icon="check-circle-2">Active</x-badge>
                </div>
            </div>
        </x-card>

        {{-- Ledger Table --}}
        <x-card title="Transaction History" subtitle="Running balance calculation" icon="scroll-text" :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">Description</th>
                            <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Debit (Dr)</th>
                            <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Credit (Cr)</th>
                            <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Balance</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                        @forelse($entries as $entry)
                            <tr class="hover:bg-slate-50 dark:hover:bg-slate-800/50 transition">
                                <td class="px-4 py-3 text-xs text-slate-500 whitespace-nowrap">
                                    {{ \Carbon\Carbon::parse($entry->date)->format('M d, Y') }}
                                </td>
                                <td class="px-4 py-3 text-xs font-mono font-semibold text-primary-600">{{ $entry->voucher_no }}</td>
                                <td class="px-4 py-3 text-sm text-slate-700 dark:text-slate-300">
                                    {{ $entry->description }}
                                    @if($entry->remarks)
                                        <span class="text-slate-400">— {{ $entry->remarks }}</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-right text-sm font-semibold text-rose-600 counter-num">
                                    {{ $entry->debit > 0 ? '৳' . number_format($entry->debit) : '—' }}
                                </td>
                                <td class="px-4 py-3 text-right text-sm font-semibold text-emerald-600 counter-num">
                                    {{ $entry->credit > 0 ? '৳' . number_format($entry->credit) : '—' }}
                                </td>
                                <td class="px-4 py-3 text-right text-sm font-bold text-slate-900 dark:text-white counter-num">
                                    ৳{{ number_format($entry->balance) }}
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-8 text-sm text-slate-500">
                                    No transactions found for this period.
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                    @if($entries->isNotEmpty())
                    <tfoot>
                        <tr class="bg-slate-50 dark:bg-slate-900/50 border-t-2 border-slate-200 dark:border-slate-700">
                            <td colspan="3" class="px-4 py-3 text-sm font-bold text-right">Total</td>
                            <td class="px-4 py-3 text-right text-sm font-bold text-rose-600 counter-num">৳{{ number_format($entries->sum('debit')) }}</td>
                            <td class="px-4 py-3 text-right text-sm font-bold text-emerald-600 counter-num">৳{{ number_format($entries->sum('credit')) }}</td>
                            <td class="px-4 py-3 text-right text-sm font-bold text-primary-600 counter-num">৳{{ number_format($entries->last()?->balance ?? 0) }}</td>
                        </tr>
                    </tfoot>
                    @endif
                </table>
            </div>
        </x-card>
    @else
        <x-card>
            <div class="text-center py-12 text-slate-500">
                <i data-lucide="truck" class="w-12 h-12 mx-auto text-slate-300 dark:text-slate-700"></i>
                <p class="mt-3 text-sm">Select a supplier above to view their ledger.</p>
            </div>
        </x-card>
    @endif
</div>
@endsection
