@extends('layouts.invoice-center')
@section('inner-content')
    <div class="d-flex mb-2 pb-2 align-items-center border-bottom">
        <div class="font-size-14 font-weight-bold">Customers</div>
        <div moe class="ml-3">
            <a href="" start show class="btn btn-sm btn-primary font-weight-normal text-white">
                + Add Customer
            </a>
            <form url="/api/customer/create" class="mcp-theme-1">
                <p class="mb-2 text-secondary font-weight-bold">Add Customer</p>
                <div class="mb-2">
                    <label class="text-sm text-secondary mb-1">Company</label>
                    <input type="hidden" name="companyUid" value="{{@$company->uid}}">
                    <input type="text"
                           name="companyName"
                           target-key="uid"
                           target-field="companyUid"
                           autocomplete="off"
                           class="form-control form-control-sm"
                           stag-suggest
                           stag-suggest-ep="/company-suggest"
                           value="{{@$company ? @$company->name : ''}}"
                           {{@$company ? 'disabled readonly' : ''}}
                           required>
                </div>
                <div class="mb-2">
                    <label class="text-sm text-secondary mb-1">Client</label>
                    <input type="hidden" name="clientUid">
                    <input type="text"
                           name="clientName"
                           target-key="uid"
                           target-field="clientUid"
                           autocomplete="off"
                           class="form-control form-control-sm"
                           stag-suggest
                           stag-suggest-ep="/client-suggest"
                           required>
                </div>
                <div>
                    <button submit class="btn btn-sm btn-primary mr-2">Submit</button>
                    <button cancel class="btn btn-sm btn-default border">Cancel</button>
                </div>
            </form>
        </div>
    </div>
    @if(count($records))
        <table class="table table-sm table-bordered table-striped">
            <thead>
            <tr>
                <th class="border-bottom-0"></th>
                <th class="border-bottom-0">Customer</th>
                <th class="border-bottom-0">Age/Gender</th>
                <th class="border-bottom-0">Company</th>
                <th class="border-bottom-0">Customer<br>Balance</th>
                <th class="border-bottom-0">Pending Invoices<br>Balance</th>
                <th class="border-bottom-0">Portal</th>
                <th class="border-bottom-0">Created</th>
                <th class="border-bottom-0">Active?</th>
                <th class="border-bottom-0"></th>
            </tr>
            </thead>
            <tbody>
            <?php $i = 1; ?>
            @foreach($records as $record)
                <tr>
                    <td>{{ $i++ }}</td>
                    <td>
                        @if($record->client)
                            <a href="{{route('patients.view.dashboard', $record->client)}}">
                                {{$record->client->displayName()}}
                            </a>
                        @else
                            -
                        @endif
                        @if(!$record->is_active)
                            <span class="text-sm font-weight-bold text-secondary ml-2">[INACTIVE]</span>
                        @endif
                    </td>
                    <td>{{$record->client->age_in_years ?: '-' }} / {{ $record->client->sex ?: '-'}}</td>
                    <td>{{ $record->company ? $record->company->name : '-' }}</td>
                    <td>${{ is_null($record->customer_balance) ? 0 : $record->customer_balance }}</td>
                    <td>${{ is_null($record->pending_invoices_balance_total) ? 0 : $record->pending_invoices_balance_total }}</td>
                    <td>
                        <a href="#" data-uid="{{$record->uid}}" class="generate-and-visit-ic-portal" native target="_blank">Visit</a>
                        <a href="#" data-uid="{{$record->uid}}" class="generate-and-copy-ic-portal-url ml-1" native target="_blank">Copy</a>
                    </td>
                    <td>{{ friendly_date($record->created_at) }}</td>
                    <td class="{{ !$record->is_active ? 'text-warning-dark' : ''}}">{{ $record->is_active ? 'Yes' : 'No' }}</td>
                    <td>
                        <div class="d-flex flex-wrap">
                            @if($record->is_active)
                                <div moe class="mr-2">
                                    <a href="" start show>Deactivate</a>
                                    <form url="/api/customer/deactivate" class="mcp-theme-1" right>
                                        <p class="mb-2 text-nowrap">Deactivate this customer?</p>
                                        <input type="hidden" name="uid" value="{{$record->uid}}">
                                        <div>
                                            <button submit class="btn btn-sm btn-primary mr-2">Submit</button>
                                            <button cancel class="btn btn-sm btn-default border">Cancel</button>
                                        </div>
                                    </form>
                                </div>
                            @else
                                <div moe class="mr-2">
                                    <a href="" start show>Reactivate</a>
                                    <form url="/api/customer/reactivate" class="mcp-theme-1" right>
                                        <p class="mb-2 text-nowrap">Reactivate this customer?</p>
                                        <input type="hidden" name="uid" value="{{$record->uid}}">
                                        <div>
                                            <button submit class="btn btn-sm btn-primary mr-2">Submit</button>
                                            <button cancel class="btn btn-sm btn-default border">Cancel</button>
                                        </div>
                                    </form>
                                </div>
                            @endif
                            <a href="{{route('invoice-center.customerTransactions')}}?customerUid={{$record->uid}}"
                               class="mr-2"
                               open-in-stag-popup
                               update-parent
                               title="{{$record->client->displayName()}}: Customer Transactions"
                               popup-style="stag-popup-med overflow-visible"
                               mc-initer="invoice-center">Transactions</a>
                            <a href="{{route('invoice-center.invoices')}}?customerUid={{$record->uid}}"
                               class="mr-2"
                               open-in-stag-popup
                               update-parent
                               title="{{$record->client->displayName()}}: Invoices"
                               popup-style="stag-popup-med overflow-visible"
                               mc-initer="invoice-center">Invoices</a>
                        </div>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
        {{$records->withQueryString()->links()}}
    @else
        <div class="text-secondary">No customers yet!</div>
    @endif
@endsection