@extends ('app.patient.invoicing.layout')
@section('invoicing-content')
    <div class="d-flex align-items-baseline pb-3">
        <h4 class="font-weight-bold m-0 font-size-16">Companies</h4>
        <span class="text-secondary mx-2">|</span>
        <div moe>
            <a href="" start show class="font-weight-bold">
                + Add
            </a>
            <form url="/api/customer/create" class="mcp-theme-1">
                <p class="mb-2 text-secondary font-weight-bold">Add As Customer</p>
                <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                <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>
                    <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($patient->customers))
        <div class="border p-2">This patient is not registered as a customer in any company yet.</div>
    @else
        <table class="table table-sm table-bordered table-striped">
            <thead>
            <tr>
                <th class="border-bottom-0"></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($patient->customers as $record)
                <tr>
                    <td>{{ $i++ }}</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('patients.view.invoicing-customer-transactions', ['patient' => $patient, 'customer' => $record])}}" class="mr-2">Transactions</a>
                            <a href="{{route('patients.view.invoicing-invoices', ['patient' => $patient, 'customer' => $record])}}">Invoices</a>
                        </div>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    @endif
@endsection
