@extends ('layouts/template')

@section('content')
<div class="p-3 mcp-theme-1" id="patients-list">
    <div class="card">

        <div class="card-header px-3 py-2 d-flex align-items-center">
            <strong class="mr-4">
                <i class="fas fa-calendar-alt"></i>
                Financial Transactions
            </strong>
        </div>

        <div class="card-body p-0">
            <!-- <div class="p-3">
                Filter
            </div> -->
            <table class="table table-sm table-striped p-0 m-0">
                <thead class="bg-light">
                    <tr>
                    <th class="px-3 border-0">Date</th>
                        <th class="border-0">Type</th>
                        <th class="border-0">Client</th>
                        <th class="border-0">Context</th>
                        <th class="border-0">Amount</th>
                        <th class="border-0">Balance</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($financialTransactions as $transaction)
                    <tr>
                        <td>{{ friendly_date_time($transaction->created_at, true) }}</td>
                        <td>{{ $transaction->plus_or_minus === 'PLUS' ? 'Credit' : 'Debit' }}</td>
                        <td>
                            @if($transaction->client)
                                <a href="/patients/view/{{ $transaction->client->uid }}">
                                    {{ implode(", ", [$transaction->client->name_last, $transaction->client->name_first]) }}
                                </a>
                            @else
                                -
                            @endif
                        </td>
                        <td class="text-nowrap">
                            @if($transaction->bill && $transaction->bill->note)
                                <a href="{{route('patients.view.notes.view.dashboard', ['patient'=>$transaction->client, 'note'=>$transaction->bill->note])}}">
                                    ({{$transaction->bill->note->effective_dateest}})
                                @if($transaction->bill->note->noteTemplate)
                                    {{ $transaction->bill->note->noteTemplate->title }}
                                @endif
                                </a>
                            @elseif($transaction->bill && $transaction->bill->careMonth)
                                <a href="/patients/view/{{ $transaction->client->uid }}/care-months/view/{{ $transaction->bill->careMonth->uid }}">
                                    Remote Monitoring, {{friendly_date_month_year($transaction->bill->careMonth->start_date)}}
                                </a>
                            @else
                                {{ $transaction->custom_memo ? $transaction->custom_memo : '-' }}
                            @endif
                        </td>
                        <td>
                            @if($transaction->amount > 0)
                                ${{ $transaction->amount }}
                            @else
                                <span class="text-success font-weight-bold">
                                    ${{ $transaction->amount }}
                                </span>
                            @endif
                        </td>
                        <td>${{ $transaction->resulting_balance }}</td>
                    </tr>
                    @endforeach

                    @if(count($financialTransactions) === 0)
                    <tr>
                        <td colspan="6">No records found!</td>
                    </tr>
                    @endif
                </tbody>

            </table>
            @if($financialTransactions->hasPages())
                <div class="ml-2 mt-2">
                    {{ $financialTransactions->appends(request()->input())->links() }}
                </div>
            @endif
        </div>
    </div>
</div>
@endsection