@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">Insurance Coverage</h4>
        <div class="ml-3">
            <div moe relative>
                <a href="" start show class="btn btn-sm btn-primary text-white font-weight-bold small">Add</a>
                <form url="/api/client/validateAgainstMBPayer" class="mcp-theme-1">
                    <input type="hidden" name="uid" value="{{$patient->uid}}">
                    <div class="form-group">
                        <select name="mbPayerUid"  class="form-control" select2>
                            <option value=""></option>
                            @foreach($mbPayers as $mbPayer)
                            <option value="{{$mbPayer->uid}}">{{$mbPayer->name()}}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <label class="control-label">Member Id</label>
                        <input type="text" name="memberId" class="form-control">
                    </div>
                    <div class="form-group">
                        <label class="control-label">Member First Name</label>
                        <input type="text" name="memberFirstName" class="form-control">
                    </div>
                    <div class="form-group">
                        <label class="control-label">Member Last Name</label>
                        <input type="text" name="memberLastName" class="form-control">
                    </div>
                    <div class="form-group">
                        <label class="control-label">Member Dob</label>
                        <input type="date" name="memberDOB" class="form-control">
                    </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>
    </div>

    <table class="table table-sm table-bordered mt-3 mb-0">
        <thead>
        <tr>
            <th>Created At</th>
            <th class="border-bottom-0 w-25">Payer Name </th>
            <th class="border-bottom-0 w-25">Eligible Payer ID</th>
            <th class="border-bottom-0 w-25">Member Details</th>
            <th class="border-bottom-0">Result</th>
        </tr>
        </thead>
        <tbody>
        @foreach($patient->mbPayerValidationResults as $mbpvResult)
            <tr>
                <td class="text-nowrap">{{friendly_date_time($mbpvResult->created_at)}}</td>
                <td>{{$mbpvResult->mbPayer->vendor_identifier}}</td>
                <td>{{$mbpvResult->mbPayer->name()}}</td>
                <td># {{$mbpvResult->member_id}} | {{$mbpvResult->member_first_name}} | {{$mbpvResult->member_last_name}} | {{$mbpvResult->member_dob}}</td>
                <td class="">
                    <pre>
                    {{json_encode(json_decode($mbpvResult->validation_result_json), JSON_PRETTY_PRINT)}}</td>
                    </pre>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>
@endsection
