{% set absUrl = app.request.getSchemeAndHttpHost() ~ app.request.baseUrl|replace({'/app_dev.php': ''}) %}
{% set userData = app.session.get('userData') %}
<!DOCTYPE html>
<html class="no-js" lang="en" dir="ltr">
<head>
	<meta charset="utf-8">
	<meta http-equiv="x-ua-compatible" content="ie=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>DSRC Letter</title>
</head>
<body style="font-family:'Arial'; font-size:18px;">
{% set affiliatePhone = '' %}
{% if patientLetter.patientReferringProvider %}
    {% set rp = patientLetter.patientReferringProvider.referringProvider %}
    {% set patient = patientLetter.patient %}
    {% set receiverFullName = patientLetter.patientReferringProvider.referringProvider.honorific ~ ' ' ~ patientLetter.patientReferringProvider.referringProvider.fullName|title %}
    {% if patientLetter.patientReferringProvider.referringProvider.phoneNo is null or patientLetter.patientReferringProvider.referringProvider.phoneNo == '' %}
        {% set receiverPhone = '' %}
        {% for prhf in patient.patientReferringHealthFacilities %}
            {% for rhfp in rp.referringHealthFacilityProviders %}
                {% if prhf.referringHealthFacility.id == rhfp.referringHealthFacility.id %}
                    {% set receiverPhone = prhf.referringHealthFacility.phoneNo %}
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% else %}
        {% set receiverPhone = patientLetter.patientReferringProvider.referringProvider.phoneNo %}
    {% endif %}
    {% if patientLetter.patientReferringProvider.referringProvider.faxNo is null or patientLetter.patientReferringProvider.referringProvider.faxNo == '' %}
        {% set receiverFax = '' %}
        {% for prhf in patient.patientReferringHealthFacilities %}
            {% for rhfp in rp.referringHealthFacilityProviders %}
                {% if prhf.referringHealthFacility.id == rhfp.referringHealthFacility.id %}
                    {% set receiverFax = prhf.referringHealthFacility.faxNo %}
                {% endif %}
            {% endfor %}
        {% endfor %}
    {% else %}
        {% set receiverFax = patientLetter.patientReferringProvider.referringProvider.faxNo %}
    {% endif %}
{% endif %}
{% if patientLetter.patientReferringHealthFacility %}
	{% set patient = patientLetter.patient %}
	{% set receiverPhone = null %}
	{% set receiverFax = null %}
	{% set receiverFullName = null %}
	{% for rp in patientLetter.patient.patientReferringProviders %}
		{% if rp.endDate is null or rp.endDate|date('Y-m-d') >= 'now'|date('Y-m-d') %}
			{% set receiverFullName = rp.referringProvider.fullName %}
			{% set receiverPhone = rp.referringProvider.phoneNo %}
			{% set receiverFax = rp.referringProvider.faxNo %}
		{% endif %}
	{% endfor %}

    {#{% set receiverFullName = patientLetter.patientReferringHealthFacility.referringHealthFacility.description %}#}
    {#{% set receiverPhone = patientLetter.patientReferringHealthFacility.referringHealthFacility.phoneNo is null ? patientLetter.patientReferringHealthFacility.referringHealthFacility.phoneNoTwo : patientLetter.patientReferringHealthFacility.referringHealthFacility.phoneNo %}#}
    {#{% set receiverFax = patientLetter.patientReferringHealthFacility.referringHealthFacility.faxNo is null ? patientLetter.patientReferringHealthFacility.referringHealthFacility.faxNoTwo : patientLetter.patientReferringHealthFacility.referringHealthFacility.faxNo %}#}
{% endif %}
{% if patientLetter.patientInsurance %}
    {% set receiverFullName = patientLetter.patientInsurance.insurance.description %}
    {% set receiverPhone = patientLetter.patientInsurance.insurance.phoneNo is null ? patientLetter.patientInsurance.insurance.phoneNoTwo : patientLetter.patientInsurance.insurance.phoneNo %}
    {% set receiverFax = patientLetter.patientInsurance.insurance.faxNo is null ? patientLetter.patientInsurance.insurance.faxNoTwo : patientLetter.patientInsurance.insurance.faxNo %}
{% endif %}

{% if patientLetter.patient.patientReferringHealthFacilities %}
	{% for patientAffiliate in patientLetter.patient.patientReferringHealthFacilities %}
		{% if patientAffiliate.isDeleted == 0 %}
			{% set affiliatePhone = patientAffiliate.referringHealthFacility.phoneNo is null ? patientAffiliate.referringHealthFacility.phoneNoTwo : patientAffiliate.referringHealthFacility.phoneNo %}
		{% endif %}
	{% endfor %}
{% endif %}

{% set patientFullName = patientLetter.patient.fullName %}
{% set patientFirstName = patientLetter.patient.firstName %}
{% set patientLastName = patientLetter.patient.lastName %}
{% set patient = patientLetter.patient %}

{% set faxCoverCustomBody = patientLetter.faxCoverCustomBody|replace({
	'{{RECEVIER-FULLNAME}}' :  receiverFullName,
	'{{PATIENT-FULLNAME}}' : patientFullName,
	'{{PATIENT-FIRSTNAME}}' :  patientLetter.patient.firstName,
	'{{PATIENT-LASTNAME}}' :  patientLetter.patient.lastName,
	'{{USERDATA-FULLNAME}}' : userData.fullName,
	'{{DATE-TODAY}}' : 'now'|date('m/d/Y'),
	'{{DATE-CREATED}}' : patientLetter.createdAt|date('m/d/Y'),
	'{{PATIENT-PHN}}' : patientLetter.patient.personalHealthNo|default(''),
	'{{PATIENT-DOB}}' : patientLetter.patient.birthDate|date('m/d/Y'),
	'{{PATIENT-GENDER}}' : patientLetter.patient.gender == 'Male' ? 'he' : 'she',
	'{{PATIENT-GENDER2}}' : patientLetter.patient.gender == 'Male' ? 'him' : 'her',
	'{{PATIENT-GENDER3}}' : patientLetter.patient.gender == 'Male' ? 'his' : 'her',
	'{{PATIENT-GENDER4}}' : patientLetter.patient.gender == 'Male' ? 'He' : 'She'
}) %}

{% set customBody = patientLetter.customBody|replace({
	'{{RECEVIER-FULLNAME}}' :  receiverFullName,
	'{{PATIENT-FULLNAME}}' : patientFullName,
	'{{PATIENT-FIRSTNAME}}' :  patientLetter.patient.firstName,
	'{{PATIENT-LASTNAME}}' :  patientLetter.patient.lastName,
	'{{USERDATA-FULLNAME}}' : userData.fullName,
	'{{DATE-TODAY}}' : 'now'|date('m/d/Y'),
	'{{DATE-CREATED}}' : patientLetter.createdAt|date('m/d/Y'),
	'{{PATIENT-PHN}}' : patientLetter.patient.personalHealthNo|default(''),
	'{{PATIENT-DOB}}' : patientLetter.patient.birthDate|date('m/d/Y'),
	'{{PATIENT-GENDER}}' : patientLetter.patient.gender == 'Male' ? 'he' : 'she',
	'{{PATIENT-GENDER2}}' : patientLetter.patient.gender == 'Male' ? 'him' : 'her',
	'{{PATIENT-GENDER3}}' : patientLetter.patient.gender == 'Male' ? 'his' : 'her',
	'{{PATIENT-GENDER4}}' : patientLetter.patient.gender == 'Male' ? 'He' : 'She'
})
%}
<!------ 1ST PAGE ------>
{% include '@RxConnexion/Patient/FaxTemplate/FreedomTemplate/frsi_fax_cover.wkpdf.twig' with {patientLetter: patientLetter} %}
<div style="page-break-before: always"></div>
<!------ 2ND PAGE ------>
<div style="width:768px; position: relative; padding:0px; margin: 0 auto; box-shadow:3px 1px 10px 1px;">
	<div style="border: none;">
		<div style="width: 100%; height: 35px; background-color: #F0F0F0; box-sizing: border-box; padding:8px; position: relative;">
			<p style="margin:0; font-size: 18px; float: left;"><strong>PATIENT LETTER</strong></p>
			<p style="margin:0; font-size: 18px; float: right;"><strong></strong></p>
		</div>
		<hr style="margin: 0; height: 4px; background-color: #479085; margin-top:3px; margin-bottom: 20px;">
		<div style="margin-bottom: 40px;"></div>
		{% if patientLetter.patientReferringProvider %}
			{% set receiverClinic = '' %}
			{% set receiverClinicAddress = '' %}
			{% set receiverFullName = patientLetter.patientReferringProvider.referringProvider.fullName %}
			{% set receiverPhone = patientLetter.patientReferringProvider.referringProvider.phoneNo is null ? patientLetter.patientReferringProvider.referringProvider.phoneNoTwo : patientLetter.patientReferringProvider.referringProvider.phoneNo %}
			{% set receiverFax = patientLetter.patientReferringProvider.referringProvider.faxNo is null ? patientLetter.patientReferringProvider.referringProvider.faxNoTwo : patientLetter.patientReferringProvider.referringProvider.faxNo %}
			{% set receiverFullName = patientLetter.patientReferringProvider.referringProvider.honorific ~ ' ' ~ patientLetter.patientReferringProvider.referringProvider.fullName|title %}
			{% for referringClinic in patientLetter.patient.patientReferringHealthFacilities %}
				{% if (referringClinic.endDate == null and referringClinic.startDate != null) or referringClinic.endDate|date('m/d/Y') > 'now'|date('m/d/Y')  %}
					{% set receiverClinic = referringClinic.referringHealthFacility.description|default('') %}
					{% set receiverClinicAddress = referringClinic.referringHealthFacility.getReferringAffiliateAddress|default('') %}
				{% endif %}
			{% endfor %}
			<p style="margin:2px">{{ receiverFullName|default('') }} </p>
			<p style="margin:2px">{{ receiverClinic|default('') }} </p>
			<p style="margin:2px">{{ receiverClinicAddress|raw }}</p>
		{% endif %}
		{% if patientLetter.patientReferringHealthFacility %}
			{% set receiverClinic = null %}
			{% set receiverClinicAddress = null %}
			{% set receiverFullName = null %}
			{% for referringClinic in patientLetter.patient.patientReferringHealthFacilities %}
				{% if (referringClinic.endDate == null and referringClinic.startDate != null) or referringClinic.endDate|date('m/d/Y') > 'now'|date('m/d/Y')  %}
					{% set receiverClinic = referringClinic.referringHealthFacility.description|default('') %}
					{% set receiverClinicAddress = referringClinic.referringHealthFacility.getReferringAffiliateAddress|default('') %}
				{% endif %}
			{% endfor %}
			{% for rp in patientLetter.patient.patientReferringProviders %}
				{% if rp.endDate is null or rp.endDate|date('Y-m-d') >= 'now'|date('Y-m-d') %}
					{% set receiverFullName = rp.referringProvider.honorific ~ ' ' ~ rp.referringProvider.fullName|title %}
				{% endif %}
			{% endfor %}

			<p style="margin:2px">{{ receiverFullName|default('') }} </p>
			<p style="margin:2px">{{ receiverClinic|default('') }} </p>
			<p style="margin:2px">{{ receiverClinicAddress|raw }}</p>
		{% endif %}
		{% if patientLetter.patientInsurance %}
			{# {% set receiverFullName = patientLetter.patientInsurance.insurance.description %}
            {% set receiverPhone = patientLetter.patientInsurance.insurance.phoneNo is null ? patientLetter.patientInsurance.insurance.phoneNoTwo : patientLetter.patientInsurance.insurance.phoneNo %}
            {% set receiverFax = patientLetter.patientInsurance.insurance.faxNo is null ? patientLetter.patientInsurance.insurance.faxNoTwo : patientLetter.patientInsurance.insurance.faxNo %} #}
			{#<p style="margin:2px">{{ receiverFullName }} </p>#}
			{#<p style="margin:2px">{{ patientLetter.patientInsurance.insurance.address }}</p>#}
		{% endif %}
		{{ customBody|raw }}
		{#<br>#}
		{#<div style="width: 100%; height: auto; text-align:left;margin-top: 20px;">#}
			{#<p style="margin:2px">Sincerely,</p>#}
		{#</div>#}
		{#{% include '@RxConnexion/Patient/FaxTemplate/FreedomTemplate/frsi_signature.html.twig' with {patientLetter: patientLetter} %}#}

	</div>
</div>
<div style="page-break-before: always"></div>
<div style="width:768px; position: relative; padding:0px; margin: 0 auto; box-shadow:3px 1px 10px 1px;">
	<div style="margin-top:12px; height: 100px; width:100%; font-size: 17px;">
		<div style="float: left">
			<p>Patient Name: {{ patientLastName~' ,'~patientFirstName }}</p>
			<p>PHN Number: {{ patientLetter.patient.personalHealthNo }}</p>
		</div>
		<div style="float: right">
			<p>Date of Birth: {{ patientLetter.patient.birthDate|date('m/d/Y') }}</p>
			<p>Referring Doctor: {{ receiverFullName }}</p>
		</div>
	</div>
	<div style="text-align: center;">
		<img src="{{ app.request.getSchemeAndHttpHost() ~ asset('bundles/rxconnexion/admin/images/rx_cpap_machine.PNG') }}" style="width: 60%;"/>
	</div>
	<div style="width: 100%; height: 95px; border: 1px solid #479085; box-sizing: border-box; padding-top: 30px; float: left; font-size: 25px; padding-left: 10px; padding-right: 10px; margin-top: 10px;">
		<div style="text-align: center">
			<div>
				<strong><span style="color:#3d3d3d;">{{ aliceNightOne.osa|default('0') }}</span> Obstructive Sleep Apnea (AHI <span style="color: #3d3d3d;">{{ aliceNightOne.ahi|default('0') }}</span>)</strong>
			</div>
		</div>
	</div>
	<div style="width: 100%; text-align: center; float: left; margin-top: 20px; font-size: 17px;">
		<div>Auto CPAP Pressure <u>{{ aliceNightOne.cpapTheraphyOne|default('_________') }}</u> cmH<sub>2</sub>0 to <u>{{ aliceNightOne.cpapTheraphyTwo|default('_________') }}</u> cmH<sub>2</sub>0</div>
		<div>(Fixed) CPAP Pressure <u>{{ aliceNightOne.cpapTheraphyThree|default('______________') }}</u> cmH<sub>2</sub>0</div>
	</div>

	{% set recommendation = aliceNightOne.recommendation|replace({
		'{{RECEVIER-FULLNAME}}' :  receiverFullName,
		'{{PATIENT-FULLNAME}}' : patientFullName,
		'{{PATIENT-FIRSTNAME}}' :  patientLetter.patient.firstName,
		'{{PATIENT-LASTNAME}}' :  patientLetter.patient.lastName,
		'{{USERDATA-FULLNAME}}' : userData.fullName,
		'{{DATE-TODAY}}' : 'now'|date('m/d/Y'),
		'{{DATE-CREATED}}' : patientLetter.createdAt|date('m/d/Y'),
		'{{PATIENT-PHN}}' : patientLetter.patient.personalHealthNo|default(''),
		'{{PATIENT-DOB}}' : patientLetter.patient.birthDate|date('m/d/Y'),
		'{{PATIENT-GENDER}}' : patientLetter.patient.gender == 'Male' ? 'he' : 'she',
		'{{PATIENT-GENDER2}}' : patientLetter.patient.gender == 'Male' ? 'him' : 'her',
		'{{PATIENT-GENDER3}}' : patientLetter.patient.gender == 'Male' ? 'his' : 'her',
		'{{PATIENT-GENDER4}}' : patientLetter.patient.gender == 'Male' ? 'He' : 'She'
	})
	%}

	<div style="width: 100%; height: 350px; margin-top: 5px; padding-top:20px; float: left; font-size: 15px; padding-left: 10px; padding-right: 10px;">
		<div style="margin-bottom: 15px;">
			<div style="font-size: 15px;">
				<strong style="margin-bottom: 10px;">Recommendation</strong>
				<div style="height: 163px;">
					{{ recommendation|default('')|raw }}
				</div>
			</div>
		</div>
	</div>
	<div style="font-size: 15px;">Interpreted By: {{ aliceNightOne.interpretingPhysician|default('') }}</div>
	<div style="width: 100%; float: left;">
		<p>Other Recommendation:</p>
		<input type="checkbox" {{ aliceNightOne.isReferralToSleepSpecialist == 1 ? 'checked' : '' }}> Referral to Sleep Specialist
	</div>
	<div style="width: 100%; float: right;">
		<div style="width: 50%; float: right;">
			<p>Date: </p>
			<p>Referring Doctor's Signature:</p>
		</div>
	</div>
</div>
</body>
</html>