@extends ('layouts.patient')
@section('inner-content')
    <div class="">
        <div class="d-flex align-items-end pb-3">
            <h4 class="font-weight-bold m-0">
                <i class="fa fa-link"></i>
                Linked Accounts
            </h4>
            <span class="mx-2 text-secondary">|</span>
            <div moe relative class="">
                <a href="#" start show class="col-2-button">
                    + Add
                </a>
                <form url="/api/accountInvite/create" class="mcp-theme-1">
                    <input type="hidden" name="forClientUid" value="{{$patient->uid}}">
                    <div class="mb-2">
                        <label class="text-secondary text-sm">Email Address</label>
                        <input type="email" name="toEmailAddress" value="{{ $patient->email_address }}"
                               class="form-control form-control-sm" required>
                    </div>
                    <div class="mb-2">
                        <label class="text-secondary text-sm">First Name</label>
                        <input type="text" name="firstName" value="{{ $patient->name_first }}"
                               class="form-control form-control-sm" required>
                    </div>
                    <div class="mb-2">
                        <label class="text-secondary text-sm">Last Name</label>
                        <input type="text" name="lastName" value="{{ $patient->name_last }}"
                               class="form-control form-control-sm" required>
                    </div>
                    <div class="mb-2">
                        <label class="text-secondary text-sm">Cell Number</label>
                        <input type="tel" name="cellNumber" value="{{ $patient->cell_number }}"
                               class="form-control form-control-sm">
                    </div>
                    <div>
                        <button submit class="btn btn-sm btn-primary mr-2">Yes</button>
                        <button cancel class="btn btn-sm btn-default border">Cancel</button>
                    </div>
                </form>
            </div>
        </div>
        <table class="table table-sm table-striped table-bordered mb-0">
            @if($patient->linkedAccounts && count($patient->linkedAccounts))
                <thead>
                <tr class="bg-light">
                    <th class="text-secondary border-0">Created At</th>
                    <th class="text-secondary border-0">Name</th>
                    <th class="text-secondary border-0">Email</th>
                    <th class="text-secondary border-0">Cell Number</th>
                    <th class="text-secondary border-0">Access Allowed?</th>
                    <th class="text-secondary border-0">Is Removed?</th>
                    <th class="text-secondary border-0">&nbsp;</th>
                </tr>
                </thead>
                <tbody>
                @foreach($patient->linkedAccounts as $linkedAccount)
                    <tr class="{{$linkedAccount->is_removed ? 'bg-light text-secondary' : ''}}">
                        <td>{{ friendlier_date_time($linkedAccount->created_at) }}
                            @if($linkedAccount->is_removed)
                                <span class="text-sm text-secondary">(REMOVED)</span>
                            @endif
                        </td>
                        <td>
                        @if($linkedAccount->account)
                            {{ $linkedAccount->account->name_first }} {{ $linkedAccount->account->name_last }}
                        @endif
                        </td>
                        <td>{{ $linkedAccount->account->email ?? '' }}</td>
                        <td>{{ $linkedAccount->account->phone_number ?? '' }}</td>
                        <td>
                            <div class="d-flex">
                                <span class="width-40px">{{ $linkedAccount->is_access_allowed ? 'YES' : 'NO' }}</span>
                                @if(!$linkedAccount->is_removed)
                                    @if(!$linkedAccount->is_access_allowed)
                                        <span moe class="ml-1" relative>
                                            <a class="" href="" show start>Allow</a>
                                            <form url="/api/accountClient/proAllowAccess" right>
                                                <input type="hidden" name="uid" value="{{$linkedAccount->uid}}">
                                                <p>Access access to this client from this linked account?</p>
                                                <div class="mb-0">
                                                    <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                    <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                </div>
                                            </form>
                                        </span>
                                    @else
                                        <span moe class="ml-1" relative>
                                            <a class="" href="" show start>Deny</a>
                                            <form url="/api/accountClient/proDenyAccess" right>
                                                <input type="hidden" name="uid" value="{{$linkedAccount->uid}}">
                                                <p>Deny access to this client from this linked account?</p>
                                                <div class="mb-0">
                                                    <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                    <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                </div>
                                            </form>
                                        </span>
                                    @endif
                                @endif
                            </div>
                        </td>
                        <td>{{ $linkedAccount->is_removed ? 'YES' : 'NO' }}</td>
                        <td>
                            @if(!$linkedAccount->is_removed)
                                <div moe class="ml-1" relative>
                                    <a class="text-danger" href="" show start><i class="fa fa-trash-alt"></i></a>
                                    <form url="/api/accountClient/remove" right>
                                        <input type="hidden" name="uid" value="{{$linkedAccount->uid}}">
                                        <p>Remove this linked account?</p>
                                        <div class="mb-0">
                                            <button class="btn btn-primary btn-sm" submit>Submit</button>
                                            <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                        </div>
                                    </form>
                                </div>
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            @else
                <tbody>
                <tr>
                    <td class="text-secondary p-3">No linked accounts</td>
                </tr>
                </tbody>
            @endif
        </table>

        @if($patient->accountInvites && count($patient->accountInvites))
            <div class="d-flex align-items-end pb-3 mt-4">
                <h4 class="font-weight-bold m-0 font-size-14">
                    <i class="fa fa-envelope"></i>
                    Accounts Invites
                </h4>
            </div>
            <table class="table table-sm table-bordered mb-0">
                <thead>
                <tr class="bg-light">
                    <th class="px-2 text-secondary border-bottom-0">Created At</th>
                    <th class="px-2 text-secondary border-bottom-0">Name</th>
                    <th class="px-2 text-secondary border-bottom-0">Email</th>
                    <th class="px-2 text-secondary border-bottom-0">Cell Number</th>
                    <th class="px-2 text-secondary border-bottom-0">Status</th>
                    <th class="px-2 text-secondary border-bottom-0">&nbsp;</th>
                </tr>
                </thead>
                <tbody>
                @foreach($patient->accountInvites as $accountInvite)
                    <tr>
                        <td class="px-2">
                            {{ friendlier_date_time($accountInvite->created_at) }}
                            @if($accountInvite->status === 'CANCELLED')
                                <span class="text-sm text-secondary">(CANCELLED)</span>
                            @endif
                        </td>
                        <td class="px-2">{{ $accountInvite->first_name }} {{ $accountInvite->last_name }}</pre></td>
                        <td class="px-2">{{ $accountInvite->to_email_address }}</td>
                        <td class="px-2">{{ $accountInvite->cell_number }}</td>
                        <td class="px-2">{{ $accountInvite->status }} <span class="text-sm text-secondary">(Updated: {{friendlier_date_time($accountInvite->status_updated_at)}})</span>
                            @if($accountInvite->status_memo)
                                <div class="py-1 font-italic text-secondary text-sm">{{$accountInvite->status_memo}}</div>
                            @endif
                        </td>
                        <td class="px-2">
                            <div class="d-flex">
                                @if($accountInvite->status === 'PENDING')
                                    <span moe class="ml-1" relative>
                                        <a class="" href="" show start>Cancel</a>
                                        <form url="/api/accountInvite/markAsCancelled" right>
                                            <input type="hidden" name="uid" value="{{$accountInvite->uid}}">
                                            <p>Cancel this invite?</p>
                                            <div class="mb-0">
                                                <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </span>
                                    <span class="d-inline-block text-secondary mx-1">|</span>
                                    <span moe class="ml-1" relative>
                                        <a class="" href="" show start>Resend</a>
                                        <form url="/api/accountInvite/resendInvite" right>
                                            <input type="hidden" name="uid" value="{{$accountInvite->uid}}">
                                            <p>Resend the invite email?</p>
                                            <div class="mb-0">
                                                <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </span>
                                @endif
                                @if($accountInvite->status == 'CANCELLED')
                                    <span moe class="ml-1" relative>
                                        <a class="" href="" show start>Undo Cancel</a>
                                        <form url="/api/accountInvite/undoMarkAsCancelled" right>
                                            <input type="hidden" name="uid" value="{{$accountInvite->uid}}">
                                            <p>Un-cancel this invite?</p>
                                            <div class="mb-0">
                                                <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </span>
                                @endif
                            </div>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
        @endif
    </div>
@endsection
