<?php
  $coverage = $patient->getPrimaryCoverage();
?>

<div class="table-responsive">
   @if(!$coverage)
      <table class="table table-sm table-bordered mb-0">
          <tr>
            <td>
              <b>No coverage info!</b>
            </td>
          </tr>
        </table>
  @else
  <table class="table table-sm table-bordered mb-0">
    <tr>
      <td class="border-right-0">
          <b>{{ $coverage->plan_type }}</b>
          @if($coverage->is_covered == 'YES')
            <i class="ml-1 fas fa-check-circle text-success"></i>
          @else
          <i class="ml-1 far fa-times-circle text-danger"></i>
          @endif
      </td>
      <td class="border-left-0 text-right">
        <b>(<i class="ml-1 fas fa-map-pin"></i>
          @if($coverage->creation_strategy == 'NEW' || $coverage->creation_strategy == 'AUTO_REFRESH')
          Auto
          @else
          Manual
          @endif)
        </b>
      </td>
    </tr>
    @if($coverage->plan_type === 'MEDICARE')
        <!-- Auto -->
        @if($coverage->creation_strategy == 'NEW' || $coverage->creation_strategy == 'AUTO_REFRESH')
          <tr>
            <td class="text-secondary">Matched</td>
            <td>{{ $coverage->auto_medicare_is_match_found ? 'YES':'NO' }}</td>
          </tr>
          <tr class="{{!$coverage->auto_medicare_is_partbactive ? 'd-none' : ''}}">
            <td class="text-secondary">Part B Active</td>
            <td>{{ $coverage->auto_medicare_is_partbactive }}</td>
          </tr>
          <tr class="{{!$coverage->auto_medicare_is_partbprimary ? 'd-none' : ''}}">
            <td class="text-secondary">Part B Primary</td>
            <td>{{ $coverage->auto_medicare_is_partbprimary }}</td>
          </tr>
          <tr class="{{!$coverage->auto_medicare_is_partcactive ? 'd-none' : ''}}">
            <td class="text-secondary">Part C Active</td>
            <td>{{ $coverage->auto_medicare_is_partcactive }}</td>
          </tr>
          @if($coverage->auto_medicare_is_partcactive)
            <?php   $detailJsonArray = json_decode($coverage->auto_medicare_detail_json, true); ?>
            @if(isset($detailJsonArray['plan_details']['MC']))
              <tr>
                  <td class="text-secondary">MC Payer</td>
                  <td><span class="coverage-payer-name" title="{{@$detailJsonArray['plan_details']['MC']['payer_name']}}">{{ @$detailJsonArray['plan_details']['MC']['payer_name'] }}</span></td>
              </tr>
            @endif
          @endif
        @else
        <!-- Manual -->
          <tr>
            <td class="text-secondary">Matched</td>
            <td>{{ $coverage->manual_medicare_is_matched ?? 'UNKNOWN' }}</td>
          </tr>
          <tr>
            <td class="text-secondary">Category</td>
            <td>{{ $coverage->manual_determination_category }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_is_partbactive ? 'd-none' : ''}}">
            <td class="text-secondary">Part B Active</td>
            <td>{{ $coverage->manual_medicare_is_partbactive }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_is_partbprimary ? 'd-none' : ''}}">
            <td class="text-secondary">Part B Primary</td>
            <td>{{ $coverage->manual_medicare_is_partbprimary }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_is_partcactive ? 'd-none' : ''}}">
            <td class="text-secondary">Part C Active</td>
            <td>{{ $coverage->manual_medicare_is_partcactive }}</td>
          </tr>         
          <tr class="{{!$coverage->manual_medicare_is_msp ? 'd-none' : ''}}">
            <td class="text-secondary">Msp</td>
            <td>{{ $coverage->manual_medicare_is_msp }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_msp_memo ? 'd-none' : ''}}">
            <td class="text-secondary">Msp Memo</td>
            <td>{{ $coverage->manual_medicare_msp_memo }}</td>
          </tr>
        @endif
    @elseif($coverage->plan_type === 'MEDICAID')
        <tr>
          <td class="text-secondary">Medicaid Payer</td>
          <td><span class="coverage-payer-name" title="{{$coverage->mcdPayer ? $coverage->mcdPayer->name :'--'}}">{{ $coverage->mcdPayer ? $coverage->mcdPayer->name :'--' }}</span></td>
        </tr>
        <!-- Auto -->
        @if($coverage->creation_strategy == 'NEW' || $coverage->creation_strategy == 'AUTO_REFRESH')
            <tr>
              <td class="text-secondary">Matched</td>
              <td>{{ $coverage->auto_medicaid_is_match_found ? 'YES':'NO' }}</td>
            </tr>
        @else
        <!-- Manual -->
              <tr>
                <td class="text-secondary">Category</td>
                <td>{{ $coverage->manual_determination_category }}</td>
              </tr>
              <tr>
                <td class="text-secondary">Matched</td>
                <td>{{ $coverage->manual_medicare_is_matched }}</td>
              </tr>
        @endif
    @elseif($coverage->plan_type === 'COMMERCIAL')
        <tr>
          <td colspan="2"><span>{{ $coverage->payer ? $coverage->payer->name : ($coverage->carrier_free_text ?: '--') }}</span></td>
        </tr>
        @if($coverage->mcdPayer)
        <tr>
          <td class="text-secondary">Medicaid Payer</td>
          <td><span class="coverage-payer-name" title="{{$coverage->mcdPayer ? $coverage->mcdPayer->name :'--'}}">{{ $coverage->mcdPayer ? $coverage->mcdPayer->name :'--' }}</span></td>
        </tr>
        @endif
        <!-- Auto -->
        @if($coverage->creation_strategy == 'NEW' || $coverage->creation_strategy == 'AUTO_REFRESH')
          <tr>
            <td class="text-secondary">Commercial Matched</td>
            <td>{{ $coverage->auto_commercial_is_match_found ? 'YES':'NO' }}</td>
          </tr>
          @if($coverage->autoCommercialMoreSpecificPayer)
              <tr>
                <td colspan="2"><span>{{ $coverage->autoCommercialMoreSpecificPayer->name }}</span></td>
              </tr>
          @endif
          @if($coverage->mcr_number)
            <tr>
              <td class="text-secondary">Medicare Matched</td>
              <td>{{ $coverage->auto_medicare_is_match_found ? 'YES':'NO' }}</td>
            </tr>
            <tr class="{{!$coverage->auto_medicare_is_partbactive ? 'd-none' : ''}}">
              <td class="text-secondary">Part B Active</td>
              <td>{{ $coverage->auto_medicare_is_partbactive }}</td>
            </tr>
            <tr class="{{!$coverage->auto_medicare_is_partbprimary ? 'd-none' : ''}}">
              <td class="text-secondary">Part B Primary</td>
              <td>{{ $coverage->auto_medicare_is_partbprimary }}</td>
            </tr>
            <tr class="{{!$coverage->auto_medicare_is_partcactive ? 'd-none' : ''}}">
              <td class="text-secondary">Part C Active</td>
              <td>{{ $coverage->auto_medicare_is_partcactive }}</td>
            </tr>
            @if($coverage->auto_medicare_is_partcactive)
              <?php   $detailJsonArray = json_decode($coverage->auto_medicare_detail_json, true); ?>
              @if(isset($detailJsonArray['plan_details']['MC']))
                <tr>
                    <td class="text-secondary">MC Payer Name</td>
                    <td><span class="coverage-payer-name" title="{{@$detailJsonArray['plan_details']['MC']['payer_name']}}">{{ @$detailJsonArray['plan_details']['MC']['payer_name'] }}</span></td>
                </tr>
              @endif
            @endif
          @endif
          @if($coverage->mcdPayer)
            <tr>
              <td class="text-secondary">Medicaid Matched</td>
              <td>{{ $coverage->auto_medicaid_is_match_found ? 'YES':'NO' }}</td>
            </tr>
            <tr>
              <td class="text-secondary">MCD Payer</td>
              <td><span class="coverage-payer-name" title="{{$coverage->mcdPayer->name}}">{{ $coverage->mcdPayer->name }}</span></td>
            </tr>
          @endif
      @else
      <!-- Manual -->
        <tr>
          <td class="text-secondary">Category</td>
          <td>{{ $coverage->manual_determination_category }}</td>
        </tr>
        <tr>
          <td class="text-secondary">Commercial Matched</td>
          <td>{{ $coverage->manual_commercial_is_matched }}</td>
        </tr>
        @if($coverage->mcr_number)
          <tr>
            <td class="text-secondary">Medicare Matched</td>
            <td>{{ $coverage->manual_medicare_is_matched }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_is_partbactive ? 'd-none' : ''}}">
            <td class="text-secondary">Part B Active</td>
            <td>{{ $coverage->manual_medicare_is_partbactive }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_is_partbprimary ? 'd-none' : ''}}">
            <td class="text-secondary">Part B Primary</td>
            <td>{{ $coverage->manual_medicare_is_partbprimary }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_is_partcactive ? 'd-none' : ''}}">
            <td class="text-secondary">Part C Active</td>
            <td>{{ $coverage->manual_medicare_is_partcactive }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_is_msp ? 'd-none' : ''}}">
            <td class="text-secondary">Msp</td>
            <td>{{ $coverage->manual_medicare_is_msp }}</td>
          </tr>
          <tr class="{{!$coverage->manual_medicare_msp_memo ? 'd-none' : ''}}">
            <td class="text-secondary">Msp Memo</td>
            <td>{{ $coverage->manual_medicare_msp_memo }}</td>
          </tr>
        @endif
        @if($coverage->mcdPayer)
          <tr>
            <td class="text-secondary">Medicaid Matched</td>
            <td>{{ $coverage->manual_medicaid_is_matched }}</td>
          </tr>
          <tr>
              <td class="text-secondary">MCD Payer</td>
              <td><span class="coverage-payer-name" title="{{$coverage->mcdPayer->name}}">{{ $coverage->mcdPayer->name }}</span></td>
            </tr>
        @endif
      @endif
    @endif
  </table>
   @endif
</div>
