@extends ('layouts/template')

@section('content')
    <div class="p-3 mcp-theme-1">
        <div class="card">
            <div class="card-header px-2 py-1 d-flex align-items-center">
                <strong class="mr-4">
                    <i class="fas fa-user"></i>
                    Financial Transactions
                </strong>
                <div class="ml-auto">
                    <b class="mr-2">{{$pro->displayName()}}</b>
                    <i class="fa fa-arrow-right mr-2"></i>
                    <span class="text-secondary">
                        <strong>Current Balance:</strong> ${{ $pro->balance }}
                    </span>&nbsp;
                    <span class="text-secondary">
                        <?php $lastPayment = $pro->lastPayment(); ?>
                        <strong>Last Payment:</strong>
                        {{ $lastPayment ? '$' . $lastPayment->amount . ' (' . friendly_date_time($lastPayment->created_at, false) . ')' : '-' }}
                    </span>
                </div>
            </div>
            <div class="card-header p-3">
                <div class="d-flex align-items-center">
                    <select name="filter" class="form-control form-control-sm width-200px ml-2"
                            onchange="return transactionsFilter('t', this.value)">
                        <option {{request()->input('t') === '' ? 'selected' : ''}} value="">All Transactions</option>
                        <option {{request()->input('t') === 'PLUS' ? 'selected' : ''}} value="PLUS">Credits
                        </option>
                        <option {{request()->input('t') === 'MINUS' ? 'selected' : ''}} value="MINUS">Debits
                        </option>
                    </select>
                    @if($pro->pro_type == 'ADMIN')
                        <select name="filter" class="form-control form-control-sm width-200px ml-2"
                                onchange="return transactionsFilter('c', this.value)">
                            <option {{!request()->input('c') ? 'selected' : ''}} value="">All Companies</option>
                            @foreach($companies as $company)
                                <option {{request()->input('c') == $company->id ? 'selected' : ''}}
                                        value="{{$company->id}}">
                                    {{$company->name}}
                                </option>
                            @endforeach
                        </select>
                        <div class="width-200px">
                            <select provider-search data-pro-uid="{{ request()->input('p') ? request()->input('p') : '' }}"
                                    name="proUid" class="form-control form-control-sm mr-auto width-200px min-width-unset"
                                    onchange="return transactionsFilter('p', this.value)">
                                <option value="" {{!request()->input('p') ? 'selected' : ''}}>All Pros</option>
                            </select>
                        </div>
                    @endif
                    <div class="ml-4 d-inline-flex align-items-center">
                        <span class="mr-2">Date</span>
                        <input type="date"
                               value="{{request()->input('bs')}}"
                               onchange="return transactionsFilter('bs', this.value)"
                               class="form-control form-control-sm width-150px" name="bpdFrom">
                        <span class="mx-2">to</span>
                        <input type="date"
                               value="{{request()->input('be')}}"
                               onchange="return transactionsFilter('be', this.value)"
                               class="form-control form-control-sm width-150px" name="bpdTo">
                    </div>
                    @if(count(array_filter(request()->all(), function($_x) { return !!$_x;})))
                        <a href="/practice-management/financial-transactions" class="ml-4">Clear Filters</a>
                    @endif
                </div>
            </div>
            <div class="card-body p-0">
                <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>
                            @if($pro->pro_type === 'ADMIN')
                                <th class="border-0">Pro</th>
                                <th class="border-0">Company</th>
                            @endif

                            <th class="border-0">Context</th>
                            <th class="border-0">Details</th>
                            <th class="border-0">Amount</th>
                            <th class="border-0">Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($transactions as $transaction)
                            <tr>
                                <td class="px-3">
                                    {{ 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>
                                @if($pro->pro_type === 'ADMIN')
                                    <td>
                                        {{$transaction->pro ? $transaction->pro->displayName() : ''}}
                                    </td>
                                    <td>
                                        {{$transaction->company ? $transaction->company->name : ''}}
                                    </td>
                                @endif

                                <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>
                                    <a href="/patients/view/{{ $transaction->client->uid }}/care-months/view/{{ $transaction->bill->careMonth->uid }}">
                                        <b>{{ friendly_month($transaction->bill->careMonth->start_date) }}</b>
                                    </a>
                                </td>
                                <td>
                                    <b>{{ $transaction->bill->code }}</b>
                                    <div class="text-secondary text-sm">Created: {{ friendly_date_time($transaction->bill->created_at, true) }}</div>
                                </td>--}}
                                {{--<td class="stag-no-wrap-td">
                                    @if($transaction->bill)
                                        @if($transaction->bill->careMonth)
                                            <b>{{ $transaction->bill->code }}</b>
                                            -
                                            <a href="/patients/view/{{ $transaction->client->uid }}/care-months/view/{{ $transaction->bill->careMonth->uid }}">
                                                <b>{{ friendly_month($transaction->bill->careMonth->start_date) }}</b>
                                            </a>
                                        @elseif($transaction->bill->note)
                                            <b>{{ $transaction->bill->code }}</b>
                                            -
                                            <a href="/patients/view/{{ $transaction->client->uid }}/notes/view/{{ $transaction->bill->note->uid }}">
                                                <b>Note</b>
                                            </a>
                                        @endif
                                        @if(!empty($transaction->bill->reason1))
                                        <div class="text-secondary text-sm stag-no-wrap" title="{{ $transaction->bill->reason1 }}">{{ $transaction->bill->reason1 }}</div>
                                        @endif
                                    @else
                                        -
                                    @endif
                                </td>--}}
                                <td>
                                    @if($transaction->bill && $transaction->plus_or_minus === 'MINUS')
                                        {{$transaction->bill->debit_description ?: ''}}
                                    @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
                    </tbody>
                </table>
            </div>
        </div>

        <div class="d-flex align-items-center mt-3">
            {{ $transactions->withQueryString()->links() }}
        </div>
    </div>
    <script>
        (function() {
            window.transactionsFilter = function(_key, _value) {

                <?php
                $keys = ['p', 'bs', 'be', 't', 'c'];
                $currentParams = [];
                for($i = 0; $i < count($keys); $i++) {
                    if(!!request()->input($keys[$i])) {
                        $currentParams[$keys[$i]] = request()->input($keys[$i]);
                    }
                }
                ?>

                let base = '/practice-management/financial-transactions',
                    keys = ['p', 'bs', 'be', 't', 'c'].filter(_x => _x !== _key),
                    currentParams = {!! json_encode($currentParams) !!},
                    url = [];

                // base
                url.push(base);
                url.push('?');

                // params
                let getParams = [];
                for (let i = 0; i < keys.length; i++) {
                    if(currentParams[keys[i]]) {
                        getParams.push(keys[i] + '=' + encodeURIComponent(currentParams[keys[i]]));
                    }
                }

                // new params
                getParams.push(_key + '=' + encodeURIComponent(_value));

                // generate url
                url.push(getParams.join('&'));

                // go
                fastLoad(url.join(''));

                return false;
            };
        }).call(window);
    </script>
@endsection
