@extends('layouts.app')

@section('content')
<div class="container">
    <h2 class="mt-4">Tenant Transactions</h2>
    <!-- Form Section -->
    <form action="{{ route('fetchAllTransactions') }}" method="POST" class="mb-4">
        @csrf
        <div class="row">
            <!-- From Date -->
            <div class="col-md-3">
                <label for="from" class="form-label">From Date</label>
                <input type="date" class="form-control" id="from" name="from" value="{{ old('from') }}" required>
            </div>

            <!-- To Date -->
            <div class="col-md-3">
                <label for="to" class="form-label">To Date</label>
                <input type="date" class="form-control" id="to" name="to" value="{{ old('to') }}" required>
            </div>

            <!-- Currency -->
            <div class="col-md-3">
                <label for="curr" class="form-label">Currency</label>
                <select class="form-select" id="curr" name="curr" required>
                    <option value="">Select Currency</option>
                    @foreach($currencies as $currency)
                        <option value="{{ $currency->Code }}" {{ old('curr') == $currency->Code ? 'selected' : '' }}>
                            {{ $currency->Name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <!-- Submit Button -->
            <div class="col-md-3 d-flex align-items-end">
                <button type="submit" class="btn btn-primary w-100">Fetch Transactions</button>
            </div>
        </div>
    </form>

    <!-- Transactions Table -->
    @if(isset($allTransactions) && count($allTransactions) > 0)
        <div class="table-responsive">
            <table class="table table-bordered table-hover">
                <thead class="table-light">
                    <tr>
                        <th>Tenant Name</th>
                        <th>Account Sub Code</th>
                        <th>Transaction Date</th>
                        <th>Details</th>
                        <th>Amount</th>
                        <th>Currency</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($allTransactions as $subCode => $details)
                        @if(count($details['transactions']) > 0)
                            @foreach($details['transactions'] as $transaction)
                                <tr>
                                    <td>{{ $details['name'] }}</td>
                                    <td>{{ $subCode }}</td>
                                    <td>{{ $transaction->TransDate }}</td>
                                    <td>{{ $transaction->Details }}</td>
                                    <td>{{ number_format($transaction->Amount, 2) }}</td>
                                    <td>{{ $currnames }}</td>
                                </tr>
                            @endforeach
                        @else
                            <tr>
                                <td>{{ $details['name'] }}</td>
                                <td>{{ $subCode }}</td>
                                <td colspan="4" class="text-center">No transactions found for this tenant</td>
                            </tr>
                        @endif
                    @endforeach
                </tbody>
            </table>
        </div>
    @else
        <div class="alert alert-info mt-4">
            No transactions available. Please use the form above to fetch data.
        </div>
    @endif
</div>
@endsection
