@view('header', [ 'title' => 'Transactions' ])

<div class="breadcrumb">
    <ul>
        <li><a href="/">Banq</a></li>
        <li class="is-active"><a href="/transactions">Transactions</a></li>
    </ul>
</div>

<h1 class="title">Transactions</h1>

<div class="columns">
    <div class="column">
        <div class="buttons">
            <a class="button is-link" href="/transactions/create">Create</a>
        </div>
    </div>
    <form class="column" method="get">
        <div class="field has-addons">
            <div class="control" style="width: 100%;">
                <input class="input" type="text" id="q" name="q" placeholder="Search..." value="{{ request('q') }}">
            </div>
            <div class="control">
                <button class="button is-link" type="submit">Search</button>
            </div>
        </div>
    </form>
</div>

@if (count($transactions) > 0):
    @view('pagination', [ 'page' => $page, 'last_page' => $last_page ])

    @foreach ($transactions as $transaction):
        @if (!is_numeric($transaction->from_account_id) || (is_numeric($transaction->to_account_id) && $transaction->to_account->user_id == Auth::id())):
            <div class="box has-text-white has-background-success">
                <h2 class="title is-4">
                    <a class="has-text-white" href="/transactions/{{ $transaction->id }}">{{ $transaction->name }}</a>
                </h2>

                @if (is_numeric($transaction->from_account_id)):
                    <p>From: <a class="has-text-white" href="/accounts/{{ $transaction->from_account_id }}">{{ $transaction->from_account->name }}</a></p>
                @else:
                    <p>From: {{ $transaction->from_account_id }}</p>
                @endif

                @if (is_numeric($transaction->to_account_id)):
                    <p>To: <a class="has-text-white" href="/accounts/{{ $transaction->to_account_id }}">{{ $transaction->to_account->name }}</a></p>
                @else:
                    <p>To: {{ $transaction->to_account_id }}</p>
                @endif

                <p>Amount: {!! format_money($transaction->amount) !!}</p>
                <p>Send at: <i>{{ $transaction->created_at }}</i></p>
            </div>
        @endif

        @if (!is_numeric($transaction->to_account_id) || (is_numeric($transaction->from_account_id) && $transaction->from_account->user_id == Auth::id())):
            <div class="box has-text-white has-background-danger">
                <h2 class="title is-4">
                    <a class="has-text-white" href="/transactions/{{ $transaction->id }}">{{ $transaction->name }}</a>
                </h2>

                @if (is_numeric($transaction->from_account_id)):
                    <p>From: <a class="has-text-white" href="/accounts/{{ $transaction->from_account_id }}">{{ $transaction->from_account->name }}</a></p>
                @else:
                    <p>From: {{ $transaction->from_account_id }}</p>
                @endif

                @if (is_numeric($transaction->to_account_id)):
                    <p>To: <a class="has-text-white" href="/accounts/{{ $transaction->to_account_id }}">{{ $transaction->to_account->name }}</a></p>
                @else:
                    <p>To: {{ $transaction->to_account_id }}</p>
                @endif

                <p>Amount: {!! format_money($transaction->amount) !!}</p>
                <p>Send at: <i>{{ $transaction->created_at }}</i></p>
            </div>
        @endif
    @endforeach

    @view('pagination', [ 'page' => $page, 'last_page' => $last_page ])
@else:
    <p><i>There are no transactions found</i></p>
@endif

@view('footer')
