<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Med X Eligibility</title>
        <style>
            * {
            margin: 0;
            text-indent: 0;
            }
            .eligibility-area, .eligibility-list-area thead tr th{
            margin-bottom: 15px;
            margin-right: 0;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
            }
            .border-redius {
            border: 0 solid #1c3d6e; 
            background: #f5f7f7;
            padding: 20px 20px;
            margin: 0 10px;
            color: #1c3d6e;
            margin-top:20px;
            font-size:12px;
            }
            .border-redius h2 {
            font-size: 16px;
            margin-bottom: 4px;
            }
            .border-redius img {
            float: left;
            font-size: 28px;
            min-height: 53px;
            line-height: 53px;
            color: #1c3d6e;
            margin-right: 20px;
            }
            .plan-area-detail td {
            border: 0;
            }
            .plan-area-detail.message span {
            width: 100%;
            margin-bottom: 10px;
            padding-bottom: 10px;
            font-weight: 400;
            }
            .eligibility-title {
            font-size: 18px;
            color: #1c3d6e;
            margin-top: 20px;
            margin-bottom: 10px;
            margin-left: 10px;
            }
            .eligibility-list-area thead tr th {
            color: #1c3d6e;
            font-weight: 700;
            padding: 8px 10px;
            background: #34b6b340;
            border: 0;
            line-height: 1.2;
            font-size: 16px;
            }
            .eligibility-list-area,
            .eligibility-list-area thead tr th,
            .eligibility-list-area tr td strong,
            .eligibility-title,
            .other-list-tbl,
            .other-list-tbl tr th {
            font-family: "-apple-system,BlinkMacSystemFont," Segoe UI ",Roboto," Helvetica Neue ",Arial," Noto Sans ",sans-serif," Apple Color Emoji "," Segoe UI Emoji "," Segoe UI Symbol "," Noto Color Emoji "";
            }
            .eligibility-list table,
            .eligibility-list-area table {
            border: 1px solid #e8ebeb;
            }
            .eligibility-list-area table tr td {
            border-bottom: 1px solid #e8ebeb;
            font-size: 12px;
            padding: 5px 10px;
            }
            .group-th .bg-none {
            background: 0 0;
            }
            .group-th .bg-th {
            background: #f5f7f7;
            }
            table thead tr .border-right {
            border-right: 1px solid #e8ebeb;
            }
            table tr.border-bottom-none td {
            border-bottom: 0;
            }
            table tr.border-top-none td {
            border-top: 0;
            padding-top: 0;
            }
            .refresh {
            align-content: space-around;
            border: 1px solid #34b6b3;
            width: 40px;
            text-align: center;
            border-radius: 5px;
            height: 36px;
            }
            .eligibility-list-area thead tr th {
            color: #1c3d6e;
            font-weight: bold;
            padding: 8px 10px;
            background: #34b6b340;
            border: 0px;
            line-height: 1.2;
            font-size: 12px;
            }
            .eligibility-list-area tr td {
            border-bottom: 1px solid #e8ebeb;
            font-size: 12px;
            padding: 5px 10px;
            color: #646363;
            }
            .eligibility-list-area th,
            .other-list-tbl tr th {
            color: #646363;
            font-weight: bold;
            padding: 8px 10px;
            border: 0;
            line-height: 1.2;
            text-align:left;
            }
            .eligibility-list-area table th {
            font-size: 14px;
            background: #34b6b340;
            text-align: left;
            }
            .eligibility-list-area tr td strong {
            font-weight: 700 !important;
            font-size: 12px;
            color: #646363;
            }
            .other-list-tbl {
            border: 1px solid #e8ebeb;
            margin-left: 10px;
            padding-right: 10px;
            }
            .other-list-tbl tr th {
            font-size: 16px;
            }
            .plan-area {
            padding: 0px;
            margin: 10px 0;
            border: 1px solid #e8ebeb;
            background:#f5f7f7;
            font-size: 14px;
            }
            .plan-area h2 {
            font-size: 18px;
            color: #1c3d6e;
            width: 100%;
            padding: 8px 10px;
            background: #34b6b340;
            }
            .plan-area span {
            /*font-size: 16px;*/
            color: #646363;
            font-weight: 500;
            }
            .plan-area p {
            margin-bottom: 8px;
            }
        </style>
    </head>
    <body>
        <div class="content" styale=" width: 100%;    overflow-x: auto;    white-space: nowrap;">
            @if(!empty($response['subscriber']))
                @php
                    $subscriberData = $response['subscriber'];
                @endphp
                <table class="eligibility-area w-100" style="border-spacing: 0;  border-collapse: collapse; width:100%; white-space:nowrap;" cellspacing="0" cellpadding="1">
                    <tr>
                        <td>
                            <div class="border-redius">
                                <!--<img height="50px" width="50px" src="{{ $images['how_to_reg_icon'] }}" alt="i">-->
                                <h2>{{isset($response['status']) && $response['status'] != '' ? $response['status'] : 'No Data'}}</h2>
                                <span>Coverage Status</span>
                            </div>
                        </td>
                        <td>
                            <div class="border-redius">
                                <!--<img height="50px" width="50px" src="{{ $images['medical_services_icon'] }}" alt="i">-->
                                <h2>{{ isset($subscriberData['plan']) && isset($subscriberData['plan']['subscriberId']) && $subscriberData['plan']['subscriberId'] != "" ? $subscriberData['plan']['subscriberId'] : 'No Data' }}</h2>
                                <span>Subscriber ID</span>
                            </div>
                        </td>
                        <td>
                            <div class="border-redius">
                                <!--<img height="50px" width="50px" src="{{ $images['confirmation_number_icon'] }}" alt="i">-->
                                <h2>{{ isset($subscriberData['plan']) && isset($subscriberData['plan']['groupNumber']) && $subscriberData['plan']['groupNumber'] != "" ? $subscriberData['plan']['groupNumber'] : 'No Data' }}</h2>
                                <span>Group Number</span>
                            </div>
                        </td>
                        <td>
                            <div class="border-redius">
                                <!--<img height="50px" width="50px" src="{{ $images['date_range_icon'] }}" alt="i">-->
                                <h2>{{ isset($subscriberData['plan']) && isset($subscriberData['plan']['effectiveDateFrom']) && $subscriberData['plan']['effectiveDateFrom'] != "" ? $subscriberData['plan']['effectiveDateFrom'] : 'No Data' }}</h2>
                                <span>Effective Date From</span>
                            </div>
                        </td>
                    </tr>
                </table>
            @endif
            @if(!empty($response['subscriber']) && !empty($response['patient']))
                <table class="eligibility-list-area w-100" style="border-spacing: 0; border-collapse:collapse;" cellspacing="0" cellpadding="10" >
                    <tr>
                        @if(!empty($response['subscriber']))
                            <td style="padding-right:15px; border:0;">
                                <table class="table w-100 p-0" style="border-spacing: 0;  border-collapse: collapse;">
                                    <thead>
                                        <tr class="plan-area-detail">
                                            <th scope="col" class="border-0" colspan="2" align="left">Plan Detail</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="plan-area-detail">
                                            <td scope="col" >Member Name</td>
                                            <td scope="col" align="right"><strong>{{ data_get($response, 'subscriber.firstName', 'N/A') }} {{ data_get($response, 'subscriber.lastName', '') }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td scope="col" >Member Address</td>
                                            <td scope="col" align="right" ><strong>{{ !empty($response['subscriber']['address']) && is_array($response['subscriber']['address']) 
                                            ? implode(', ', array_filter($response['subscriber']['address'])) 
                                            : 'No Data' }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Date Of Birth</td>
                                            <td align="right"><strong>{{ !empty($response['subscriber']['dateOfBirth']) ? $response['subscriber']['dateOfBirth'] : 'No Data' }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Group Name</td>
                                            <td align="right"><strong>{{ !empty($response['subscriber']['plan']['groupName']) ? $response['subscriber']['plan']['groupName'] : 'No Data' }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Plan Name</td>
                                            <td align="right"><strong>{{ !empty($response['subscriber']['plan']['planName']) ? $response['subscriber']['plan']['planName'] : 'No Data' }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Plan Number</td>
                                            <td align="right"><strong>{{ data_get($response, 'subscriber.plan.planNumber', 'No Data') }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Coverage Indicator</td>
                                            <td align="right"><strong>{{ data_get($response, 'subscriber.plan.coverageIndicator', 'No Data') }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Service Type</td>
                                            <td align="right"><strong>{{ implode(', ', array_filter(data_get($response, 'planBenefits.serviceTypes', []))) ?: 'No Data' }}</strong></td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        @endif
                        <td style="vertical-align: top; border:0;">
                            @if(!empty($response['patient']))
                                <table class="table w-100 p-0 " style="border-spacing: 0;  border-collapse: collapse;">
                                    <thead>
                                        <tr class="plan-area-detail">
                                            <th scope="col" class="border-0" colspan="2" align="left">Patient Detail</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td scope="col" >Full Name</td>
                                            <td scope="col" align="right"><strong>{{ $response['patient']['firstName'] ?? 'No First Name' }} {{ $response['patient']['lastName'] ?? 'No Last Name' }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td scope="col" >Address</td>
                                            <td scope="col"align="right" ><strong>{{ !empty($response['patient']['address']) && is_array($response['patient']['address']) 
                                            ? implode(', ', array_filter($response['patient']['address'])) 
                                            : 'No Data' }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Date Of Birth</td>
                                            <td align="right"><strong>{{ $response['patient']['dateOfBirth'] ?? 'No Date of Birth' }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Relationship</td>
                                            <td align="right"><strong>{{ isset($response['patient']['relationship']) && $response['patient']['relationship'] ? $response['patient']['relationship'] : 'No Data' }}</strong></td>
                                        </tr>
                                    </tbody>
                                </table>
                            @endif
                            @if(!empty($response['payer']))
                                <table class="table w-100 p-0 " style="border-spacing: 0; margin-top: 20px; border-collapse: collapse; width:100%; white-space:nowrap;">
                                    <thead>
                                        <tr class="plan-area-detail">
                                            <th scope="col" class="border-0" colspan="2" align="left">Payer Detail</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td >Payer Id</td>
                                            <td align="right"><strong>{{ $response['payer']['id'] ?? 'No Data' }}</strong></td>
                                        </tr>
                                        <tr>
                                            <td >Payer Name</td>
                                            <td align="right"><strong>{{ $response['payer']['name'] ?? 'No Data' }}</strong></td>
                                        </tr>
                                    </tbody>
                                </table>
                            @endif
                        </td>
                    </tr>
                </table>
            @endif
            
            @php 
                $excludeKeys = ["subscriber", "status", "patient", "planBenefits", "activeCoverage", "payer"];
                // Function to format nested arrays/objects into a readable format
                function formatCell($cellValue) {
                    if (is_array($cellValue)) {
                        $output = '<ul style="padding-left: 0; margin: 0;">';
                        foreach ($cellValue as $subItem) {
                            if (is_array($subItem) || is_object($subItem)) {
                                foreach ((array) $subItem as $key => $val) {
                                    $formattedKey = ucwords(preg_replace('/([A-Z])/', ' $1', $key)); // Format key
                                    $output .= '<li><strong class="text-capitalize">' . $formattedKey . ':</strong> ' . ($val ?? '-') . '</li>';
                                }
                            } else {
                                $output .= '<li>' . ($subItem ?? '-') . '</li>';
                            }
                        }
                        return $output . '</ul>';
                    }
                    return $cellValue ?? "No Data";
                }
            @endphp
            @foreach($response as $key => $value)
                @continue(in_array($key, $excludeKeys))
                @php
                    $sectionTitle = ucwords(preg_replace('/([A-Z])/', ' $1', $key));
                    $tableId = "table_" . $key;
                    $headers = [];
            
                    // Collect headers from all rows to prevent column mismatches
                    if (is_array($value) && count($value) > 0) {
                        foreach ($value as $row) {
                            $headers = array_unique(array_merge($headers, array_keys((array) $row)));
                        }
                    } elseif (is_object($value)) {
                        $headers = array_keys((array) $value);
                    }
                @endphp
                <h2 class="eligibility-title">{{$sectionTitle}}</h2>
                <table class="eligibility-list-area other-list-tbl table " style="border:1px solid #e8ebeb; border-spacing: 0;  border-collapse: collapse; width:99%; " cellpadding="10">
                    <thead>
                        <tr style="color: #1c3d6e;  padding: 8px 10px;background: #34b6b340; border: 0px; line-height: 1.2; font-size: 16px;">
                            @foreach($headers as $header)
                                <th style="text-transform: uppercase;">{{ ucwords(preg_replace('/([A-Z])/', ' $1', $header)) }}</th>
                            @endforeach
                        </tr>
                    </thead>
                    <tbody>
                        @if(is_array($value) && count($value) > 0)
                            @foreach($value as $row)
                                <tr>
                                    @foreach($headers as $header)
                                        <td scope="col" style="padding: .75rem; vertical-align: top; border-top: 1px solid #dee2e6;">
                                            {!! formatCell($row[$header] ?? 'No Data') !!}
                                        </td>
                                    @endforeach
                                </tr>
                            @endforeach
                        @elseif(is_object($value))
                            <tr>
                                @foreach($headers as $header)
                                    <td scope="col" style="padding: .75rem; vertical-align: top; border-top: 1px solid #dee2e6;">
                                        {!! formatCell($value->$header ?? 'No Data') !!}
                                    </td>
                                @endforeach
                            </tr>
                        @endif
                    </tbody>
                </table>
            @endforeach
            @if(isset($parseResponse) && is_array($parseResponse) && count($parseResponse) > 0)
                <h2 class="eligibility-title">Next Eligible Dates</h2>
                <table class="eligibility-list-area other-list-tbl table w-100 p-0" style="border:1px solid #e8ebeb; border-spacing: 0;  border-collapse: collapse; width:99%;" cellpadding="10" id="CDTCodesTable">
                    <thead>
                        <tr style="color: #1c3d6e;  font-weight: 500; padding: 8px 10px;background: #34b6b340; border: 0px; line-height: 1.2; font-size: 16px;">
                            <th scope="col">Code</th>
                            <th scope="col">Last Service Date</th>
                            <th scope="col">Next Eligible Service Date</th>
                            <th scope="col">Authorization Required</th>
                            <th scope="col">Quick Notes</th>
                            <th scope="col">Coverage %</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($parseResponse as $parse)
                        <tr>
                            <td scope="col" style="padding: .75rem;    vertical-align: top;    border-top: 1px solid #dee2e6;">{{ $parse['Code'] ?? '-' }}</td>
                            <td scope="col" style="padding: .75rem;    vertical-align: top;    border-top: 1px solid #dee2e6;">{{ $parse['Lastservicedate'] }}</td>
                            <td scope="col" style="padding: .75rem;    vertical-align: top;    border-top: 1px solid #dee2e6;">{{ $parse['Nexteligibleservicedate'] }}</td>
                            <td scope="col" style="padding: .75rem;    vertical-align: top;    border-top: 1px solid #dee2e6;">{{ $parse['Authorizationrequired'] }}</td>
                            <td scope="col" style="padding: .75rem;    vertical-align: top;    border-top: 1px solid #dee2e6;">{{ $parse['Quicknotes'] ?? '-' }}</td>
                            <td scope="col" style="padding: .75rem;    vertical-align: top;    border-top: 1px solid #dee2e6;">{{ $parse['Coverage%'] }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            @endif
        </div>
        <div class="footer" style="padding-top:20px;">
            <p style='font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";'>Generated on {{ now()->format('d-m-Y h:i:s A') }} UTC</p>
        </div>
    </body>
</html>