<?php
$addressParts = [];
if (!!$patient->mailing_address_line1) $addressParts[] = $patient->mailing_address_line1;
if (!!$patient->mailing_address_line2) $addressParts[] = $patient->mailing_address_line2;
$addressParts = implode(", ", $addressParts) . ", ";
$addressPart2 = [];
if (!!$patient->mailing_address_city) $addressPart2[] = $patient->mailing_address_city;
if (!!$patient->mailing_address_state) $addressPart2[] = $patient->mailing_address_state;
$addressPart2 = implode(", ", $addressPart2);

$coverage = $patient->effectiveClientPrimaryCoverage;
$payerName = null;
$memberID = null;
if($coverage) {
  if ($coverage->plan_type == 'MEDICARE') {
      $payerName = 'Medicare';
      $memberID = $coverage->mcr_number;
  };
  if ($coverage->plan_type == 'MEDICAID') {
      $payerName = $coverage->mcd_payer_name;
      $memberID = $coverage->mcd_number;
  };
  if ($coverage->plan_type == 'COMMERCIAL') {
      $payerName = $coverage->commercial_payer_name;
      $memberID = $coverage->commercial_member_identifier;
  }
}
?>
<section style="padding-top: 0.1rem;">
    <table style="width:100%;font-size:14px; margin-bottom:1rem;border-collapse: collapse;table-layout:fixed;">
      <tr>
        <td style="vertical-align:top;padding: 5px;">
          <h4 style="line-height:20px;margin:0;text-transform:uppercase;">{{ $patient->displayName() }}</h4>
          @if($addressParts)<span style="font-size:13px;line-height:20px;display:block">{{$addressParts ?: ''}}</span>@endif
          @if($addressPart2)<span style="font-size:13px;line-height:20px;display:block">{{$addressPart2 ?: ''}} {{$patient->mailing_address_zip}}</span>@endif
          @if($patient->cell_number)<span style="font-size:13px;line-height:20px;display:block">Phone: {{$patient->cell_number}}</span>@endif
        </td>
        <td style="vertical-align:top;padding: 5px;">
          <h4 style="line-height:20px;margin:0;">PATIENT INFORMATION</h4>
          <span style="font-size:13px;line-height:20px;display:block">DOB: {{friendlier_date($patient->dob)}}</span>
          <span style="font-size:13px;line-height:20px;display:block">Age: {{$patient->age_in_years}} years old</span>
          <span style="font-size:13px;line-height:20px;display:block">Gender: {{$patient->sex}}</span>
        </td>
        <td style="vertical-align:top;padding: 5px;">
          <h4 style="line-height:20px;margin:0;">INSURANCE</h4>
          <span style="font-size:13px;line-height:20px;display:block">{{$payerName}}</span>
          <span style="font-size:13px;line-height:20px;display:block">Member ID: {{$memberID}}</span>
          <span style="font-size:13px;line-height:20px;display:block">Insurance Contact:</span>
        </td>
      </tr>
    </table>
</section>

<hr style="margin: 0.5rem 0; border-top-width: 0; border-bottom-width: 1px; border-color: #bbb">

<?php $clinical = json_decode($prescription->clinical_detail_json); ?>
<?php if($clinical && @$clinical->items): ?>
    <div style="margin: 1rem 0; font-weight: bold;"><span style="font-size: 28px">R</span><span style="font-size: 20px">x</span></div>
    <div style="margin: 1rem 0; font-size:14px;">Date: {{friendlier_date(date('Y-m-d'))}}</div>
    <section style="font-size: 15px;">
        <?php $count = 0; ?>
        <?php foreach ($clinical->items as $item): ?>
        <table cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin-bottom: 5pt">
            <tr>
                <td style="width: 20px; vertical-align: top; opacity: 0.75; font-size: 14px">
                    <span>{{ ++$count }}. </span>
                </td>
                <td style="vertical-align: top">
                    <div>
                        <p style="margin: 0">
                            <b>{{ $item->medication }}</b>
                        </p>
                        <div style="">
                            @if(@$item->frequency)
                                <p style="margin: 0.5rem 0;">Frequency: {{ $item->frequency }}</p>
                            @endif
                            @if(@$item->instructions)
                                <p style="margin: 0.5rem 0;">Instructions: {{ $item->instructions }}</p>
                            @endif
                            @if(isset($item->dispense))
                                <p style="margin: 0.5rem 0;">Dispense: {{ $item->dispense }}</p>
                            @endif
                            @if(isset($item->refills))
                                <p style="margin: 0.5rem 0;">Refills: {{ $item->refills }}</p>
                            @endif
                        </div>
                    </div>
                </td>
            </tr>
        </table>
        <?php endforeach; ?>
    </section>
<?php endif; ?>
