@extends('layouts.app')

@section('content')
<h1 class="h3 mb-4">Account Report</h1>

<form class="row g-2 mb-4 ajax-filter" method="GET" action="{{ route('reports.accounts') }}" data-ajax-target="#report-results">
    <div class="col-md-3">
        <input class="form-control" name="date" type="date" value="{{ $filters['date'] ?? '' }}">
    </div>
    <div class="col-md-3">
        <select class="form-select" name="currency_id">
            <option value="">All currencies</option>
            @foreach($currencies as $currency)
                <option value="{{ $currency->id }}" @selected(($filters['currency_id'] ?? '') == $currency->id)>{{ $currency->code }}</option>
            @endforeach
        </select>
    </div>
    <div class="col-md-3">
        <select class="form-select" name="account_id">
            <option value="">All accounts</option>
            @foreach($accounts as $account)
                <option value="{{ $account->id }}" @selected(($filters['account_id'] ?? '') == $account->id)>{{ $account->name }}</option>
            @endforeach
        </select>
    </div>
    <div class="col-md-3 d-flex gap-2">
        <button class="btn btn-primary" type="submit">Filter</button>
        <a class="btn btn-outline-success export-link" href="{{ route('reports.accounts', array_merge(request()->except('export'), ['export' => 'xls'])) }}">Excel</a>
    </div>
</form>

<div id="report-results">
<div class="card mb-4">
    <div class="card-body">
        <h2 class="h4 mb-3">All Balances</h2>
        <div class="table-responsive">
            <table class="table table-striped align-middle">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Currency</th>
                        <th>Type</th>
                        <th class="text-end">Current Balance</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($accounts as $account)
                        <tr>
                            <td class="fw-semibold">{{ $account->name }}</td>
                            <td>{{ $account->currency->code }}</td>
                            <td class="text-capitalize">{{ $account->type }}</td>
                            <td class="text-end">{{ number_format((float) $account->balance, 4) }} {{ $account->currency->code }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td class="text-muted text-center" colspan="4">No accounts found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <h2 class="h4 mb-3">Transaction History</h2>
        <div class="table-responsive">
            <table class="table table-striped align-middle">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Type</th>
                        <th>Target Account</th>
                        <th>Payment Account</th>
                        <th class="text-end">Quantity</th>
                        <th class="text-end">Payment Amount</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($transactions as $transaction)
                        <tr>
                            <td>{{ $transaction->created_at->format('Y-m-d H:i') }}</td>
                            <td class="text-capitalize">{{ $transaction->type }}</td>
                            <td>{{ optional($transaction->account)->name ?? 'N/A' }}</td>
                            <td>{{ optional(App\Models\Account::find($transaction->payment_account_id))->name ?? 'N/A' }}</td>
                            <td class="text-end">{{ number_format((float) $transaction->quantity, 4) }} {{ $transaction->currency->code }}</td>
                            <td class="text-end">{{ number_format((float) $transaction->payment_amount, 4) }} {{ optional($transaction->paymentCurrency)->code }}</td>
                            <td class="text-end"><a class="btn btn-outline-primary btn-sm" href="{{ route('reports.transactions.show', $transaction) }}">Details</a></td>
                        </tr>
                    @empty
                        <tr>
                            <td class="text-muted text-center" colspan="7">No transactions found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
</div>
@endsection
