<cfparam name="url.ID" default="0">
<cfparam name="url.AID" default="0">
<cfparam name="url.type" default="diagnosis">
<cfparam name="url.report_type" default="teachplan">
<cfparam name="url.download" default="0">

<cfset TheFile="/var/www/datafiles/Agency_Logos/#session.AgencyID#.png">
<cfif isdefined('url.ID')>
	<cfoutput>	
		<cfset url.ID= decrypt(#url.ID#,#application.enckey#,"AES","Hex") />
		<cfset url.AID= decrypt(#url.AID#,#application.enckey#,"AES","Hex") />	
		<cfscript>
        	 careplandash = CreateObject("Component", "careplan");
       		 getproblem = careplandash.getproblems(#url.AID#,#url.Skill#,0,0,0,0); 
        </cfscript>
	</cfoutput> 

	<cfquery name="getdiag" dbtype="query">
            SELECT * FROM getproblem 
            WHERE (M0_No like 'M1021%' OR M0_No like 'M1023%')
            ORDER BY dx_order ASC
     </cfquery>

	<cfquery name="getoasis" dbtype="query">
               SELECT * FROM getproblem 
               WHERE Oasis_Deficits = 1
               ORDER BY dx_order ASC
  	 </cfquery>

	<cfquery name="GetP" datasource="#Application.DataSrc#">
	    SELECT * FROM  #Request.prefix_db_agency#.pPatients
	    WHERE Patient_Id = <cfqueryparam value = "#session.cs.patientid#" CFSQLType ="CF_SQL_INTEGER"> 
	    AND Status = 0
	</cfquery>
	<cfquery name="Getass" datasource="#Application.DataSrc#">
	    SELECT *  FROM  #Request.prefix_db_agency#.pAssessments 
	    JOIN #Request.prefix_db_lookup#.pEmployee on pEmployee.Emp_ID =  pAssessments.Record_Created_By	
	    WHERE pAssessments.Assmt_ID = <cfqueryparam value = "#url.AID#" CFSQLType ="CF_SQL_INTEGER"> 
	    AND pAssessments.Status = 0
	    ORDER BY pAssessments.AutoAssmt_ID DESC 
	</cfquery>

	<cfquery  name="getcareplans" datasource="#Application.DataSrc#">
		SELECT pCareplan.*,pPathway.Param,pPathway.Pathway_ID,pPathway.Type_Order,pPathway.Order_ID,pPathway.Type as pathwaytype , 
		pPathway.Header, pPathway.Header_No ,pPathway.Value as Measurevalue ,pPathway.Track  
	   FROM #Request.prefix_db_agency#.pCareplan  
	   LEFT OUTER JOIN #Request.prefix_db_lookup#.pPathway ON pPathway.Item_ID = pCareplan.Item_ID AND pPathway.Type = pCareplan.Type 
	   WHERE pCareplan.Assmt_ID = <cfqueryparam value=" #url.AID#"  cfsqltype="CF_SQL_INTEGER">
	   <cfif url.Skill NEQ 0>
	   		AND pCareplan.Skill = <cfqueryparam value="#url.Skill#"  cfsqltype="cf_sql_varchar">
	   </cfif>
	   AND pCareplan.Status = <cfqueryparam value="0"  cfsqltype="CF_SQL_INTEGER">
	   GROUP BY pCareplan.AutoCareplan_ID
	   ORDER BY Header_No ASC, pPathway.Type_Order,pCareplan.Date_Resolve,pPathway.Order_ID ASC
	</cfquery>

    <cfif url.download eq 1>
		 <cfset agency_name = rereplace(Session.AgencyName,"[^A-Za-z0-9]","","all") />
        <cfset coding_reports= #agency_name#&"_"&#Trim(GetP.Pt_Last)#&"_"&#DateFormat(now(),'yyyymmddhhmmss')#&"_"&#url.report_type#&"_report.pdf" />
        <cfheader name="Content-disposition" value="attachment;filename=#coding_reports#">
         <cfcontent  type="application/pdf">
     </cfif>
	<cfdocument format="pdf"   margintop=".75" marginbottom="1.25" orientation="landscape" overwrite="yes">	
		<cfdocumentsection>			
			<div class="row">		
				<div class="row">
					<table cellpadding="0" cellspacing="0" border="0" width="100%">
						<tr>
							<td align="left" width="70%">
								<table cellpadding="0" cellspacing="0" border="0" width="100%">
									<tr>
										<td style="padding-left:10px; font-family: Arial; font-size: 25px;font-style: italic;">Teaching Plan for</td>
									</tr>
						
									<tr>
										<td height="6px"></td>
									</tr>
									<tr>				
										<td style="padding-left:50px; font-family: Arial; font-size: 30px; font-weight: bold;"><cfoutput>#GetP.Pt_First# #GetP.Pt_Last#</cfoutput></td>
									</tr>
									<tr>
										<td height="6px"></td>
									</tr>
									<tr>	
										<cfoutput>
										<cfset strSomestring = #Getass.F26#>
										<cfset SOC_String = find( "Start of care", strSomestring )>	
										<cfset Recertification_String = find( "Recertification", strSomestring )>
										<cfset Resumption_String = find( "Resumption", strSomestring )>
																						
										<td style="padding-left:80px; font-family: Arial; font-size: 25px; font-weight: bold;"><cfif #SOC_String# neq 0>Start of Care<cfelseif #Recertification_String# neq 0>Recertification<cfelseif #Resumption_String# neq 0>Resumption<cfelse>#Getass.F26#</cfif> #DateFormat(Getass.F176,"mm/dd/yy")#</td>
										</cfoutput>
									</tr>
								</table>
							</td>
							<td align="right" width="30%">
								<table cellpadding="0" cellspacing="0" border="0" width="100%">
									<tr>
										<td>
										   <cfif fileExists(TheFile)> 	
												<img src="file:///<cfoutput>#TheFile#</cfoutput>" width="100%"  style="height:100px;">							
											<cfelse> 
												<img src="file:///var/www/img/MHCB_Logo.png" width="100%" style="height:100px;"> 
											</cfif>	
										</td>
									</tr>	
								</table>
							</td>	
						</tr>
					</table>		
				</div>		
				<br />		
				<div class="row">
					<div style="padding-right:10px; text-align:right; font-family: Arial; font-size: 25px;font-style: italic;">Prepared by <cfoutput>#Getass.Emp_First#&nbsp;#Getass.Emp_Last# </cfoutput></div>		
				</div>		
				<br />
				<div class="row">
					<div style="border-top:solid 1px blue;"></div>
				</div>
				<div class="row">	
					<div class="row">
						<div style="margin-top:10px;"></div>
						<div style="padding-left:20px; font-family: Arial; font-size: 20px;">TABLE OF CONTENTS</div>
						<div style="margin-top:5px;"></div>
					</div>	
					<cfloop list="getdiag" index="queries" >
	                    <cfoutput query="#queries#">
								<div class="row">
									<div style="margin-top:3px;"></div>
									<div style="padding-left:60px; font-family: Arial; font-size: 20px;">#Description#</div>
									<div style="margin-top:5px;"></div>
								</div>	
								 <cfquery  name="getteach" dbtype="query">
								    SELECT * FROM getcareplans 
									WHERE  pathwaytype = <cfqueryparam value="teach"  cfsqltype="cf_sql_varchar">
									AND Type = <cfqueryparam value="teach"  cfsqltype="cf_sql_varchar">
									AND Pathway_ID IN  (<cfqueryparam value="#Path_ID#"  cfsqltype="cf_sql_integer" list="yes">) 
								</cfquery>
								<cfloop query="getteach">					
										<div class="row">
											<div style="padding-left:120px;font-size:14px;font-family: Arial;">#getteach.Description#</div>
										</div>
								</cfloop>					
						</cfoutput>
					</cfloop>
					<cfdocumentitem type="pagebreak">
					</cfdocumentitem>
				</div>		
				
			</div>
			<cfdocumentitem type="footer"  evalatprint="true">
				<table cellpadding="0" align="center" style="border-top:: solid 1px black;" cellspacing="0" border="0" height="150px" width="100%">
					<tr>
					  <td><table align="center" cellpadding="0" cellspacing="0" border="0" width="100%" 
					  style="font-family: Arial; font-size: 22px; ">
						  <tr>
							<td height="3px"></td>
						  </tr>
						  <tr>
							<td align="center" style="font-family: Arial;font-size:22px;">Teaching Plan for <cfoutput>#GetP.Pt_First#&nbsp;#GetP.Pt_Last#</cfoutput></td>
						  </tr>
						  <tr>
							<td height="3px"></td>
						  </tr>
						  <tr>
							<td align="center" style="font-family: Arial;font-size: 22px;">prepared by <cfoutput>#Getass.Emp_First#&nbsp;#Getass.Emp_Last# </cfoutput></td>
						  </tr>
						<tr>
							<td height="3px"></td>
						  </tr>
						  <tr>
							<td align="center" style="font-family: Arial;font-size: 22px;"> page <cfoutput>#cfdocument.currentpagenumber#</cfoutput> </td>
						  </tr>
						  <tr>
							<td height="3px"></td>
						  </tr>
						</table></td>
					</tr>
				</table>
			</cfdocumentitem>  
		</cfdocumentsection>
	</cfdocument>

</cfif>