@extends('app.my-account.admin.orders.single')

@section('details')
    <?php
        $storeOrderDetailJson = $order->detailJson();
        $storeOrderSelectedOptions = json_decode(@$storeOrderDetailJson->selected_options ?? "{}");
        $selectedLab = @$storeOrderSelectedOptions->lab;
    ?>
    <div class="row">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0 fw-bold">Order Details </h5>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-bordered dashboard-stats-table mb-0">
                        <tbody>
                            <tr>
                                <th class="px-2" colspan="2">
                                    <span class="text-secondary">Created:</span>
                                    {{ friendly_date_time($order->created_at) }}
                                </th>
                            </tr>
                            <tr>
                                <th class="px-2" colspan="2">
                                    <span class="text-secondary">Client:</span>
                                    <a
                                        href="{{ route('admin.users.view.dashboard', $order->user) }}">{{ $order->user->displayName() }}</a>
                                </th>
                            </tr>

                                <tr>
                                    <th class="px-2" colspan="2">
                                        <div class="text-secondary">Lab:</div>
                                        @if($selectedLab)
                                            <?= selected_lab_html_address($selectedLab); ?>
                                        @else
                                        ---
                                        @endif
                                    </th>
                                </tr>
                            <tr>
                                <th class="px-2" colspan="2">
                                    <span class="text-secondary">Total:</span>
                                    {{ displayAmount('$', $order->total()) }}
                                </th>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                    <div class="d-flex align-items-center justify-content-between">
                        <h5 class="mb-0 fw-bold">Tests Requested</h5>
                    </div>
                </div>
                <div class="card-body">
                    <div>
                        <?php
                        $tests = [];
                        $subOrders = $order->allAssociatedOrders();
                        foreach($subOrders as $subOrder){
                            $tests = array_merge($tests, (array) $subOrder->getOrderTestsSummary());
                        }
                        ?>

                    </div>
                    <table class="table table-sm table-bordered border w-100 mb-0">
                        <tbody>
                            @foreach ($tests as $key=>$test)
                                <tr>
                                    <td class="w-50 w-lg-75 px-2">{{ getTestDisplayName($key) }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
            @if($order->getPartnerEmail())
            <div class="card mt-3">
                <div class="card-header">
                    <div class="d-flex align-items-center justify-content-between">
                        <h5 class="mb-0 fw-bold">Partner</h5>
                        <div class="d-flex align-items-center">
                            <div>
                                <?php
                                    $lab_tests_request_attachment = $order->getDetailJsonValue('lab_tests_request_attachment');
                                    $partner_results_attachment = $order->getDetailJsonValue('partner_results_attachment');
                                ?>
                                @if($partner_results_attachment)
                                    <span class="me-1">Partner Results: <i class="fas fa-paperclip fa-fw"></i>
                                        <a href="{{ $partner_results_attachment['attachmentAccessTokenUrl'] . '?fileType=partner_results_attachment' }}" target="_blank">{{ $partner_results_attachment['orginalFileName'] }}</a>
                                    </span>
                                @endif
                                @include('app.my-account.admin.orders.forms.upload-attachment', [
                                    'title' => 'Upload Partner Results PDF',
                                    'fileName' => 'partner_results_attachment',
                                    'edit' => $partner_results_attachment ? true:false
                                ])
                            </div>
                            @if($partner_results_attachment)
                                <span class="text-muted mx-1">|</span>

                                <div>
                                    @include('app.my-account.admin.orders.forms.email-attachment', [
                                        'title' => 'Email Results',
                                        'toEmail' => $order->getPartnerEmail(),
                                        'toName' => '',
                                        'subject' => 'Test Results',
                                        'attachmentUrl' => $partner_results_attachment['attachmentAccessTokenUrl'] . '?fileType=partner_results_attachment',
                                        'attachmentName' => $partner_results_attachment['orginalFileName'],
                                        'attachmentPath' => $partner_results_attachment['path']
                                    ])
                                </div>
                            @endif

                            @if($lab_tests_request_attachment)
                                <span class="text-muted mx-1">|</span>
                                <div>
                                    @include('app.my-account.admin.orders.forms.email-attachment', [
                                        'title' => 'Email Lab Requisition Form for Order #' . $order->iid,
                                        'toEmail' => $order->getPartnerEmail(),
                                        'toName' => '',
                                        'subject' => 'Lab Requisition Form for Order #' . $order->iid,
                                        'attachmentUrl' => $lab_tests_request_attachment['attachmentAccessTokenUrl'] . '?fileType=lab_tests_request_attachment',
                                        'attachmentName' => $lab_tests_request_attachment['orginalFileName'],
                                        'attachmentPath' => $lab_tests_request_attachment['path']
                                    ])
                                </div>
                            @endif
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="">
                      <span class="text-secondary">Email:</span> <b>{{ $order->getPartnerEmail() }}</b>
                    </div>
                    <?php
                        $partnerTests = [];
                        foreach($subOrders as $subOrder){
                            $partnerTests = array_merge($partnerTests, (array) $subOrder->getOrderTestsSummary());
                        }
                    ?>
                    @if(count($partnerTests))
                        <div class="card mt-3">
                            <div class="card-header">
                                <div class="d-flex align-items-center justify-content-between">
                                    <h5 class="mb-0 fw-bold">Tests Requested</h5>
                                </div>
                            </div>
                            <div class="card-body">
                                <table class="table table-sm table-bordered border w-100 mb-0">
                                    <tbody>
                                        @foreach ($partnerTests as $pKey=>$pTest)
                                            <tr>
                                                <td class="w-50 w-lg-75 px-2">{{ $pTest['name'] }}</td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
            @endif
        </div>
    </div>
    <div class="row mt-4">
        <div class="col-12">
            <?php
                $orders = [];
                array_push($orders, $order);
            ?>
            @include('app.my-account.admin.users.sub.info.customer-information-orders', ['parentOrders' => $orders, 'title' => 'Order Actions'])
        </div>
        <div class="col-12 mt-3">
             @include('app.my-account.admin.users.sub.info.customer-information', ['user' => $order->user])
        </div>
    </div>
@endsection
