@extends('layouts.app')
@section('title', 'Cash Book')
@section('breadcrumb')
    @include('components.breadcrumb', ['items' => ['Cash & Bank' => route('cash.index'), 'Cash Book' => 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">Cash Book</h1>
            <p class="text-sm text-slate-500 mt-1">{{ $account->name }} — {{ $from }} to {{ $to }}</p>
        </div>
        <x-button variant="outline" icon="printer">Print</x-button>
    </div>

    <div class="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
        <x-stat-card title="Opening Balance" :value="$opening" icon="circle-dollar-sign" iconBg="primary" />
        <x-stat-card title="Total Receipts" :value="$total_receipt" icon="plus-circle" iconBg="success" />
        <x-stat-card title="Total Payments" :value="$total_payment" icon="minus-circle" iconBg="danger" />
        <x-stat-card title="Closing Balance" :value="$closing" icon="wallet" iconBg="info" />
    </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">
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase text-slate-500">Date</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase text-slate-500">Voucher</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase text-slate-500">Particulars</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase text-slate-500">Receipt (Dr)</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase text-slate-500">Payment (Cr)</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase text-slate-500">Balance</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    <tr class="bg-slate-50 dark:bg-slate-900/30 font-semibold">
                        <td colspan="3" class="px-4 py-3 text-sm">Opening Balance</td>
                        <td class="px-4 py-3 text-right">—</td>
                        <td class="px-4 py-3 text-right">—</td>
                        <td class="px-4 py-3 text-right font-bold counter-num">৳{{ number_format($opening) }}</td>
                    </tr>
                    @forelse($entries as $e)
                    <tr class="hover:bg-slate-50/50">
                        <td class="px-4 py-3 text-xs text-slate-500">{{ \Carbon\Carbon::parse($e->date)->format('d M Y') }}</td>
                        <td class="px-4 py-3 text-xs font-mono font-semibold text-slate-700">{{ $e->voucher_no }}</td>
                        <td class="px-4 py-3 text-sm">{{ $e->party ?? $e->description ?? '—' }}</td>
                        <td class="px-4 py-3 text-right text-emerald-600 font-semibold counter-num">{{ $e->receipt > 0 ? '৳' . number_format($e->receipt) : '—' }}</td>
                        <td class="px-4 py-3 text-right text-rose-600 font-semibold counter-num">{{ $e->payment > 0 ? '৳' . number_format($e->payment) : '—' }}</td>
                        <td class="px-4 py-3 text-right font-bold counter-num">৳{{ number_format($e->balance) }}</td>
                    </tr>
                    @empty
                    <tr><td colspan="6" class="text-center py-6 text-slate-500">No entries for this period.</td></tr>
                    @endforelse
                </tbody>
                <tfoot>
                    <tr class="bg-primary-50 dark:bg-primary-500/10 border-t-2 font-bold">
                        <td colspan="3" class="px-4 py-3 text-sm text-right text-primary-700">Totals</td>
                        <td class="px-4 py-3 text-right text-emerald-700 counter-num">৳{{ number_format($total_receipt) }}</td>
                        <td class="px-4 py-3 text-right text-rose-700 counter-num">৳{{ number_format($total_payment) }}</td>
                        <td class="px-4 py-3 text-right text-primary-700 counter-num">৳{{ number_format($closing) }}</td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </x-card>
</div>
@endsection
