@extends ('layouts.patient')

@section('inner-content')
    <?php
    function toHumanReadable($name) {
        return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
    }
    function parseRender($_data) {
        if($_data) {
            $type = gettype($_data);
            if(is_string($_data) || is_numeric($_data)) {
                echo $_data;
            }
            else {
                echo "<table class='table table-sm border w-100'>";
                foreach($_data as $k => $v) {
                    echo "<tr>";
                    echo "<td><b class='text-secondary'>" . toHumanReadable($k) . "</b></td>";
                    echo "<td>";
                    if(is_object($v)) {
                        parseRender($v);
                    }
                    elseif(is_array($v)) {
                        foreach($v as $k2 => $v2) {
                            parseRender($v2);
                        }
                    }
                    else {
                        echo $v;
                    }
                    echo "</td>";
                    echo "</tr>";
                }
                echo "</table>";
            }
        }
    }
    ?>
    <style>
        .eligible-table td {
            white-space: nowrap;
        }
    </style>
<div>
    <div class="d-flex align-items-center">
        <h4 class="font-weight-bold m-0 font-size-16">Eligible Refreshes</h4>
        <div class="ml-3">
            <div moe relative>
                <a href="" start show class="btn btn-sm btn-primary text-white font-weight-bold small">Refresh Eligible Data</a>
                <form url="/api/client/refreshEligible" class="mcp-theme-1">
                    <input type="hidden" name="uid" value="{{$patient->uid}}">
                    <p class="text-nowrap">Refresh eligible data?</p>
                    <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>
    </div>

    <table class="table table-sm table-bordered mt-3 mb-0">
        <thead>
        <tr>
            <th class="border-bottom-0 w-25">Timestamp</th>
            <th class="border-bottom-0">Result</th>
        </tr>
        </thead>
        <tbody>
        @foreach($patient->eligibleRefreshes as $el)
            <tr>
                <td class="text-nowrap">{{friendly_date_time($el->ts)}}</td>
                <td>
                    <table class="table table-sm table-striped table-condensed eligible-table m-0">
                        <tbody>
                        <tr>
                            <td class="w-25">Was Successful</td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <span>{{$el->medicareResult->was_successful}}</span>
                                    <a href="#" class="ml-auto"
                                       onclick="$(this).closest('table').find('.collapsible-tbody').toggleClass('collapsed'); return">
                                        <i class="fa fa-sort"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        </tbody>
                        <tbody class="collapsible-tbody">
                        <tr>
                            <td>Reject Reason Code</td>
                            <td>{{$el->medicareResult->reject_reason_code}}</td>
                        </tr>
                        <tr>
                            <td>Reject Reason Description</td>
                            <td>{{$el->medicareResult->reject_reason_description}}</td>
                        </tr>
                        <tr>
                            <td>Follow Up Action Code</td>
                            <td>{{$el->medicareResult->follow_up_action_code}}</td>
                        </tr>
                        <tr>
                            <td>Follow Up Action Description</td>
                            <td>{{$el->medicareResult->follow_up_action_description}}</td>
                        </tr>
                        <tr>
                            <td>Details</td>
                            <td>{{$el->medicareResult->details}}</td>
                        </tr>
                        <tr>
                            <td>Is Mcn Valid Number</td>
                            <td>{{$el->medicareResult->is_mcn_valid_number}}</td>
                        </tr>
                        <tr>
                            <td>Address Street Line1</td>
                            <td>{{$el->medicareResult->address_street_line1}}</td>
                        </tr>
                        <tr>
                            <td>Address Street Line2</td>
                            <td>{{$el->medicareResult->address_street_line2}}</td>
                        </tr>
                        <tr>
                            <td>Address City</td>
                            <td>{{$el->medicareResult->address_city}}</td>
                        </tr>
                        <tr>
                            <td>Address State</td>
                            <td>{{$el->medicareResult->address_state}}</td>
                        </tr>
                        <tr>
                            <td>Address Zip</td>
                            <td>{{$el->medicareResult->address_zip}}</td>
                        </tr>
                        <tr>
                            <td>Is Partbprimary</td>
                            <td>{{$el->medicareResult->is_partbprimary}}</td>
                        </tr>
                        <tr>
                            <td>Is Hospice</td>
                            <td>{{$el->medicareResult->is_hospice}}</td>
                        </tr>
                        <tr>
                            <td>Mcn Response Detail</td>
                            <td>
                            @if($el->medicareResult->mcn_response_detail)
                                <?php $parsed = json_decode($el->medicareResult->mcn_response_detail);?>
                                <?php parseRender($parsed); ?>
                            @else
                                &hyphen;
                            @endif
                            </td>
                        </tr>
                        <tr>
                            <td>Billing Address</td>
                            <td>{{$el->medicareResult->billing_address}}</td>
                        </tr>
                        <tr>
                            <td>Reason Not Partb</td>
                            <td>{{$el->medicareResult->reason_not_partb}}</td>
                        </tr>
                        <tr>
                            <td>Reason Not Partbmemo</td>
                            <td>{{$el->medicareResult->reason_not_partbmemo}}</td>
                        </tr>
                        <tr>
                            <td>Is Medicare Advantage</td>
                            <td>{{$el->medicareResult->is_medicare_advantage}}</td>
                        </tr>
                        <tr>
                            <td>Medicare Advantage Plan</td>
                            <td>{{$el->medicareResult->medicare_advantage_plan}}</td>
                        </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>
@endsection
