{% set absUrl = app.request.getSchemeAndHttpHost() ~ app.request.baseUrl|replace({'/app_dev.php': ''}) %}
{% set customBody = patientLetter.customBody|replace({'{{PATIENT-FIRSTNAME}}' :  patientLetter.patient.firstName, '{{PATIENT-FULLNAME}}' : patientLetter.patient.fullName, '{{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 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>Freedom Letter</title>
</head>
<body style="font-family:'Arial'; font-size:18px;">
{% 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 %}
{% 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, '{{USERDATA-FULLNAME}}' : userData.fullName}) %}

{% set receiverClinic = '' %}
{% set receiverClinicAddress = '' %}
{% if patientLetter.patientReferringProvider %}
	{% 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 rp in patientLetter.patient.patientReferringProviders %}
		{% if rp.endDate is null or rp.endDate|date('Y-m-d') >= 'now'|date('Y-m-d') %}
			{% set receiverClinic = rp.referringProvider.referringHealthFacility.description|default('') %}
			{% set receiverClinicAddress = rp.referringHealthFacility.getReferringAffiliateAddress|default('') %}
			{% set receiverFullName = rp.referringProvider.fullName %}
		{% 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 %}
<!------ 1ST PAGE ------>
{% include '@RxConnexion/Patient/FaxTemplate/FreedomTemplate/frsi_fax_cover.wkpdf.twig' with {patientLetter: patientLetter} %}
<div style="page-break-before: always"></div>
<div style="width:1024px; position: relative; padding:0px; margin: 0 auto; box-shadow:3px 1px 10px 1px;">
	<div style="font-size: 22px; float: left; width: 100%; margin-top: 20px;">
		<div>{{ receiverFullName|default('') }}</div>
		<div>{{ receiverClinicAddress|raw }}</div>
		{#<br>#}
		{#<div>CITY, PROVINCE, POSTAL CODE</div>#}
		<br>
		<div>DATE: {{ 'now'|date('m/d/Y') }}</div>
	</div>
	<div style="font-size: 22px; float: left; width: 100%">
		<div>Patient Name: {{ patientLetter.patient.fullName|default('') }}</div>
		<div>Date of Birth: {{ patientLetter.patient.birthDate|date('m/d/Y') }}</div>
		<div>PHN: {{ patientLetter.patient.personalHealthNo }}</div>
	</div>
	<div style="font-size: 22px; float: left; width: 100%">
		<div>Dear {{ receiverFullName|default('') }}</div>
		<br>
		<div>Thank you for referring your patient {{ patientFirstName }},{{ patientLastName }} to Freedom Respiratory and Sleep Centre INC.</div>
		<br>
		<div>We Regret to inform you that we are no longer accepting referrals for Overnight Oximetry (Home Sleep Apnea Testing for patients 17 years old and below).</div>
		<br>
		<div>Should you have questions or if we can be of any further assistance, please do not hesitate to contact us</div>
		<br>
		<div>Thank you for your continued support</div>
		<br>
		<div>Sincerely,</div>
		<br>
		{% include '@RxConnexion/Patient/FaxTemplate/FreedomTemplate/frsi_signature.html.twig' with {patientLetter: patientLetter} %}
	</div>
</div>
</body>
</html>