@extends('layouts.app')

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

<form class="row g-2 mb-4 ajax-filter" method="GET" action="{{ route('reports.profits') }}" data-ajax-target="#report-results">
    <div class="col-md-2">
        <input class="form-control" name="date_from" type="date" value="{{ $filters['date_from'] ?? '' }}" placeholder="From">
    </div>
    <div class="col-md-2">
        <input class="form-control" name="date_to" type="date" value="{{ $filters['date_to'] ?? '' }}" placeholder="To">
    </div>
    <div class="col-md-2">
        <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="agent_id">
            <option value="">All agents</option>
            @foreach($agents as $agent)
                <option value="{{ $agent->id }}" @selected(($filters['agent_id'] ?? '') == $agent->id)>{{ $agent->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.profits', array_merge(request()->except('export'), ['export' => 'xls'])) }}">Excel</a>
    </div>
</form>

<div id="report-results">
<div class="row g-3 mb-4">
    <div class="col-md-6">
        <div class="card">
            <div class="card-body">
                <div class="text-muted small">Total Exchange Profit</div>
                <div class="h3 mb-0">{{ number_format((float) $totalExchangeProfit, 4) }}</div>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card">
            <div class="card-body">
                <div class="text-muted small">Total Profit</div>
                <div class="h3 mb-0">{{ number_format((float) $totalProfit, 4) }}</div>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped align-middle">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Type</th>
                        <th>Agent</th>
                        <th>Currency</th>
                        <th class="text-end">Exchange Profit</th>
                        <th class="text-end">Total Profit</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->agent)->name ?? '-' }}</td>
                            <td>{{ $transaction->currency->code }}</td>
                            <td class="text-end">{{ number_format((float) $transaction->exchange_profit, 4) }}</td>
                            <td class="text-end fw-semibold">{{ number_format((float) $transaction->total_profit, 4) }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td class="text-muted text-center" colspan="6">No profit records found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
</div>
@endsection
