@extends('layouts.app')
@section('title', $title)
@section('content')
@php($period = $type === 'stock' ? __('Current Stock') : trim(($from ?: __('Start')).' - '.($to ?: __('Today'))))
<div class="card">
    <div class="card-body p-4">
        @include('reports.partials.header', ['title' => $title, 'period' => $period])
        <div class="d-flex flex-wrap justify-content-between gap-2 mb-3 no-print">
            <form class="row g-2">
                @if($type === 'stock')
                    <div class="col-md-8"><input name="search" class="form-control" value="{{ $search ?? '' }}" placeholder="{{ __('Search product or SKU') }}"></div>
                @else
                    <div class="col-md-5"><input name="from" type="date" class="form-control" value="{{ $from ?? '' }}"></div>
                    <div class="col-md-5"><input name="to" type="date" class="form-control" value="{{ $to ?? '' }}"></div>
                @endif
                <div class="col-auto"><button class="btn btn-light"><i class="fa-solid fa-filter me-1"></i>{{ __('Filter') }}</button></div>
            </form>
            <div>
                <a href="{{ request()->fullUrlWithQuery(['export' => 1]) }}" class="btn btn-success btn-sm"><i class="fa-solid fa-file-csv me-1"></i>{{ __('Export CSV') }}</a>
                <button onclick="window.print()" class="btn btn-light btn-sm"><i class="fa-solid fa-print me-1"></i>{{ __('Print') }}</button>
            </div>
        </div>
        <div class="table-responsive">
            @if($type === 'sales')
                <table class="table"><thead><tr><th>{{ __('Date') }}</th><th>{{ __('Invoice') }}</th><th>{{ __('Customer') }}</th><th>{{ __('Total') }}</th><th>{{ __('Paid') }}</th><th>{{ __('Due') }}</th></tr></thead><tbody>@foreach($rows as $row)<tr><td>{{ \App\Support\DualDate::format($row->invoice_date, 'd M Y') }}</td><td>{{ $row->invoice_number }}</td><td>{{ $row->customer?->name ?? $row->walk_in_name ?? __('Walk-in Customer') }}</td><td class="money">{{ number_format($row->total, 2) }} {{ $row->currency_code }}</td><td class="text-success money">{{ number_format($row->paid_amount, 2) }}</td><td class="text-danger money">{{ number_format($row->due_amount, 2) }}</td></tr>@endforeach</tbody><tfoot><tr><th colspan="3">{{ __('Total') }}</th><th class="money">{{ number_format($rows->sum('total'), 2) }} {{ $moneyCode }}</th><th class="text-success money">{{ number_format($rows->sum('paid_amount'), 2) }}</th><th class="text-danger money">{{ number_format($rows->sum('due_amount'), 2) }}</th></tr></tfoot></table>
            @elseif($type === 'purchases')
                <table class="table"><thead><tr><th>{{ __('Date') }}</th><th>{{ __('Purchase') }}</th><th>{{ __('Supplier') }}</th><th>{{ __('Total') }}</th><th>{{ __('Paid') }}</th><th>{{ __('Due') }}</th></tr></thead><tbody>@foreach($rows as $row)<tr><td>{{ \App\Support\DualDate::format($row->purchase_date, 'd M Y') }}</td><td>{{ $row->purchase_number }}</td><td>{{ $row->supplier?->name }}</td><td class="money">{{ number_format($row->subtotal, 2) }} {{ $row->currency_code }}</td><td class="text-success money">{{ number_format($row->paid_amount, 2) }}</td><td class="text-danger money">{{ number_format($row->due_amount, 2) }}</td></tr>@endforeach</tbody><tfoot><tr><th colspan="3">{{ __('Total') }}</th><th class="money">{{ number_format($rows->sum('subtotal'), 2) }} {{ $moneyCode }}</th><th class="text-success money">{{ number_format($rows->sum('paid_amount'), 2) }}</th><th class="text-danger money">{{ number_format($rows->sum('due_amount'), 2) }}</th></tr></tfoot></table>
            @elseif($type === 'stock')
                <table class="table"><thead><tr><th>{{ __('Product') }}</th><th>{{ __('Category') }}</th><th>{{ __('Unit') }}</th><th>{{ __('Stock') }}</th><th>{{ __('Cost Value') }}</th></tr></thead><tbody>@foreach($rows as $row)<tr><td>{{ $row->name }}</td><td>{{ $row->category?->name }}</td><td>{{ $row->unit }}</td><td>{{ $row->stock_quantity }}</td><td class="money">{{ number_format($row->stock_quantity * $row->cost_price, 2) }} {{ $moneyCode }}</td></tr>@endforeach</tbody><tfoot><tr><th colspan="3">{{ __('Total') }}</th><th>{{ number_format($rows->sum('stock_quantity'), 2) }}</th><th class="money">{{ number_format($rows->sum(fn ($row) => $row->stock_quantity * $row->cost_price), 2) }} {{ $moneyCode }}</th></tr></tfoot></table>
            @else
                <table class="table"><thead><tr><th>{{ __('Date') }}</th><th>{{ __('Party') }}</th><th>{{ __('Category') }}</th><th>{{ __('Amount') }}</th></tr></thead><tbody>@foreach($rows as $row)<tr><td>{{ \App\Support\DualDate::format($row['date'], 'd M Y') }}</td><td>{{ $row['party'] ?: '-' }}</td><td>{{ $row['category'] }}</td><td class="money">{{ number_format($row['amount'], 2) }} {{ $row['currency'] }}</td></tr>@endforeach</tbody><tfoot><tr><th colspan="3">{{ __('Total') }}</th><th class="money">{{ number_format($rows->sum('amount'), 2) }} {{ $moneyCode }}</th></tr></tfoot></table>
            @endif
        </div>
    </div>
</div>
@if(!empty($appSettings['report_footer']))<p class="text-center text-muted small mt-3">{{ $appSettings['report_footer'] }}</p>@endif
@endsection
