@extends ('app.patient.invoicing.layout')
@section('invoicing-content')
    <div class="d-flex align-items-baseline pb-3">
        <a href="{{route('patients.view.invoicing-companies', ['patient' => $patient])}}" class="mr-2"><i class="fa fa-chevron-left mr-1"></i> Back</a>
        <h4 class="font-weight-bold m-0 font-size-16">{{$customer->company->name}} <i class="fa fa-chevron-right text-sm text-secondary mx-1"></i> Invoices</h4>
        <span class="text-secondary mx-2">|</span>
        <div moe>
            <a href="" start show class="font-weight-bold">
                + Add
            </a>
            <form url="/api/invoice/create" class="mcp-theme-1">
                <input type="hidden" name="customerUid" value="{{$customer->uid}}">
                <p class="mb-2 text-secondary font-weight-bold">Add Invoice</p>
                <div class="mb-2">
                    <label class="text-sm text-secondary mb-1">Amount</label>
                    <input type="text"
                           name="amount"
                           autocomplete="off"
                           class="form-control form-control-sm"
                           required>
                </div>
                <div class="mb-2">
                    <label class="text-sm text-secondary mb-1">Description</label>
                    <textarea rows="2"
                              name="description"
                              autocomplete="off"
                              class="form-control form-control-sm" required></textarea>
                </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($customer->invoices))
        <div class="border p-2">This patient does not have any invoices from {{$customer->company->name}} 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">Created</th>
                <th class="border-bottom-0">Amount</th>
                <th class="border-bottom-0">Description</th>
                <th class="border-bottom-0">Context</th>
                <th class="border-bottom-0">Pay Link</th>
                <th class="border-bottom-0">Paid</th>
                <th class="border-bottom-0">Balance</th>
                <th class="border-bottom-0">Active?</th>
                <th class="border-bottom-0"></th>
            </tr>
            </thead>
            <tbody>
            <?php $i = 1; ?>
            @foreach($customer->invoices as $record)
                <tr>
                    <td>{{ $i++ }}</td>
                    <td>{{ friendly_date($record->created_at) }}</td>
                    <td>${{ is_null($record->amount) ? 0 : $record->amount }}
                        <div moe relative class="ml-1">
                            <a href="#" start show><i class="fa fa-edit on-hover-opaque"></i></a>
                            <form url="/api/invoice/changeAmount">
                                <input type="hidden" name="invoiceUid" value="{{$record->uid}}">
                                <div class="mb-2">
                                    <label class="text-sm text-secondary mb-1">Amount</label>
                                    <input type="text"
                                           name="amount"
                                           autocomplete="off"
                                           class="form-control form-control-sm"
                                           value="{{$record->amount}}"
                                           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>
                    </td>
                    <td>{{ $record->description ?: '' }}
                        <div moe relative class="ml-1">
                            <a href="#" start show><i class="fa fa-edit on-hover-opaque"></i></a>
                            <form url="/api/invoice/changeDescription">
                                <input type="hidden" name="invoiceUid" value="{{$record->uid}}">
                                <div class="mb-2">
                                    <label class="text-sm text-secondary mb-1">Description</label>
                                    <textarea rows="2"
                                              name="description"
                                              autocomplete="off"
                                              class="form-control form-control-sm" required>{{$record->description}}</textarea>
                                </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>
                    </td>
                    <td>
                        @if($record->note_id || $record->care_month_id)
                            @if($record->note_id)
                                <?php $note = \App\Models\Note::where('id', $record->note_id)->first(); ?>
                                <a href="{{ route('patients.view.notes.view.dashboard', ['patient' => $note->client, 'note' => $note]) }}">
                                    Note
                                </a>
                            @else
                                <?php $careMonth = \App\Models\CareMonth::where('id', $record->care_month_id)->first(); ?>
                                <a href="{{route('patients.view.care-months.view.dashboard', ['patient' => $careMonth->client, 'careMonth' => $careMonth])}}">
                                    Care Month
                                </a>
                            @endif
                        @else
                            -
                        @endif
                    </td>
                    <td>
                        <a href="#" data-invoice-uid="{{$record->uid}}" data-uid="{{$record->customer->uid}}" class="generate-and-visit-ic-pay" native target="_blank">Visit</a>
                        <a href="#" data-invoice-uid="{{$record->uid}}" data-uid="{{$record->customer->uid}}" class="generate-and-copy-ic-pay-url ml-1" native target="_blank">Copy</a>
                    </td>
                    <td>${{ is_null($record->paid) ? 0 : $record->paid }}</td>
                    <td>${{ is_null($record->balance) ? 0 : $record->balance }}</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/invoice/deactivate" class="mcp-theme-1" right>
                                        <p class="mb-2 text-nowrap">Deactivate this invoice?</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/invoice/reactivate" class="mcp-theme-1" right>
                                        <p class="mb-2 text-nowrap">Reactivate this invoice?</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-invoice-transactions', ['patient' => $patient, 'invoice' => $record])}}" class="mr-2">Transactions</a>
                        </div>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    @endif
@endsection
