<cfscript>
    request.general = CreateObject("component","components.general.general").init();
    request.ass     = CreateObject("component","components.general.assessments").init();
    get_ass = request.general.get_answers('assessment_type','assessment_form');
    getmess = request.general.get_messages('assessment_form');
</cfscript>


<cfquery name="AssForm" datasource="#Application.DataSrc#">    
    SELECT  pAssessments.*,pPatients.*, pAdmit.*, pEmployee.*, 
     CASE 
            WHEN  F26 =  '1 - Start of care - further visits planned' THEN "1 - SOC - SN"
            WHEN  F26 =  '1 - Start of care - further visits planned - Physical Therapy'  THEN  "1 - SOC - PT"
            WHEN  F26 =  '3 - Resumption of care (after inpatient stay)'  THEN  "3 - ROC - SN" 
            WHEN F26 =  '4 - Recertification (follow-up) reassessment' THEN  '4 - Recertification'
            WHEN  F26 =  'PT Adult Evaluation' THEN "PT Eval" 
            WHEN  F26 =  'OT Adult Evaluation' THEN "OT Eval"
            WHEN  F26 =  'ST Adult Evaluation' THEN "ST Eval" 
            WHEN  F26 =  'SN Adult Evaluation' THEN "SN Eval" 
            WHEN  F26 =  'MSW Evaluation' THEN "MSW Eval" 
            WHEN  F26 =  'PT Followup' THEN "PT FU Eval" 
            WHEN  F26 =  'OT Followup' THEN "OT FU Eval"
            WHEN  F26 =  'ST Followup' THEN "ST FU Eval"
            WHEN F26 =  'MSW Followup' THEN "MSW FU Eval" 
            WHEN F26 =  'SN Followup' THEN "SN FU Eval" 
            WHEN F26 =  'Hospice Comprehensive' THEN "Hospice Comprehensive" 
            WHEN F26 =  'SN Hospice' THEN "SN Hospice"  
            WHEN F26 =  'Chaplain Comprehensive Evaluation' THEN "Chaplain Comprehensive Evaluation" 
            WHEN F26 =  'Hospice Discharge' THEN "Hospice Discharge"  
            WHEN F26 =  '5 - Other follow-up' THEN "5 - Other follow-up" 
            WHEN F26 =  '6 - Transferred to an inpatient facility - patient not discharged from agency' THEN "6 - Transfer no discharge" 
            WHEN F26 =  '7 - Transferred to an inpatient facility - patient discharged from agency' THEN "7 - Transfer discharged" 
            WHEN F26 =  '8 - Death at home' THEN "8 - Death" 
            WHEN F26 =  '9 - Discharge from agency' THEN "9 - Discharge from agency" 
            ELSE 0
        END AS assesments
      FROM  #Request.prefix_db_agency#.pAssessments
    INNER join #Request.prefix_db_agency#.pPatients ON pAssessments.patient_id = pPatients.patient_id
    INNER join #Request.prefix_db_agency#.pAdmit ON pAdmit.Admit_ID = pAssessments.Admit_ID
    LEFT OUTER join #Request.prefix_db_lookup#.pEmployee ON pEmployee.Emp_ID = pAdmit.Clinical_Manager
    WHERE pAssessments.Assmt_ID =  <cfqueryparam cfsqltype="cf_sql_integer" value="#session.cs.ASSESSMENTID#">
    AND pAssessments.Patient_ID =    <cfqueryparam cfsqltype="cf_sql_integer" value="#session.cs.PATIENTID#">
    AND pPatients.status < 2 
    AND  pAdmit.status  = 0 
     AND  pAssessments.status  = 0 
     GROUP BY pAssessments.Assmt_ID
</cfquery> 

<cfquery  name="GetVitals" datasource="#Application.DataSrc#">
    SELECT pWoundsVitals.*,pWoundsVitals.ID AS woundsid
    FROM  #Request.prefix_db_agency#.pWoundsVitals
    WHERE  pWoundsVitals.Assmt_ID = <cfqueryparam value = "#AssForm.Assmt_ID#" CFSQLType ="CF_SQL_INTEGER">
    AND pWoundsVitals.Status = 0
    AND VitalOrWound =  'vital'
    ORDER BY pWoundsVitals.Item_ID ASC
</cfquery>

<cfquery  name="GetWounds" datasource="#Application.DataSrc#">
    SELECT pWoundsVitals.*,pWoundsVitals.ID AS woundsid 
    FROM  #Request.prefix_db_agency#.pWoundsVitals 
    JOIN #Request.prefix_db_agency#.pAssessments ON pAssessments.Assmt_ID = pWoundsVitals.Assmt_ID AND pAssessments.Status = 0
    WHERE  pAssessments.Admit_ID = <cfqueryparam value = "#AssForm.Admit_ID#" CFSQLType ="CF_SQL_INTEGER"> 
    AND pWoundsVitals.Status = 0
    AND VitalOrWound =  'wound' 
    ORDER BY pWoundsVitals.Item_ID ASC
</cfquery>


<cfquery name="get_initial" datasource="#Application.DataSrc#">    
    SELECT *  FROM  #Request.prefix_db_agency#.pSchedules
    INNER JOIN #Request.prefix_db_lookup#.pEmployee ON pEmployee.Emp_ID = pSchedules.Emp_ID
    WHERE pSchedules.Assmt_ID =  <cfqueryparam cfsqltype="cf_sql_integer" value="#session.cs.ASSESSMENTID#">
    AND  pSchedules.status =    <cfqueryparam cfsqltype="cf_sql_integer" value="0">
    AND pSchedules.Initial_visit =  1
</cfquery> 

<cfif get_initial.recordcount gt 0 >
    <cfset completed_by =  #get_initial.Emp_First#  & " "& #get_initial.Emp_Last# />
<cfelse>
    <cfset completed_by =  #AssForm.Emp_First#  & " "& #AssForm.Emp_Last# />
</cfif>

 <cfset assessmentreason = AssForm.F26 />
 <cfset assmids  = AssForm.Assmt_ID />
 <cfquery name="getempass" datasource="#Application.DataSrc#">
    SELECT  pSchedules.* , pEmployee.Emp_First,  pEmployee.Emp_Last 
    FROM  #Request.prefix_db_agency#.pSchedules  
    JOIN   #Request.prefix_db_lookup#.pEmployee ON pEmployee.Emp_ID = pSchedules.Emp_ID
    WHERE pSchedules.Status = <cfqueryparam cfsqltype="cf_sql_integer"  value="0"> 
    AND pSchedules.Patient_ID = '#session.cs.PATIENTID#'
    <!--- AND pSchedules.Assmt_ID = '#AssForm.Assmt_ID#' --->
    AND pSchedules.Visit_Type =  '#AssForm.assesments#'
    AND pSchedules.Followup_visit = 1
    ORDER BY pSchedules.Visit_Date DESC
</cfquery> 
<cfset completed_by =  #getempass.Emp_First#  & " "& #getempass.Emp_Last# />


<cfquery name="GetE" datasource="#Application.DataSrc#">
    select Emp_Last, Emp_First from #Request.prefix_db_lookup#.pEmployee where Emp_ID = '#Assform.Record_Created_By#'
</cfquery>

<cfquery name="get_cg_questions" datasource="#Application.DataSrc#">
    SELECT * FROM  #Request.prefix_db_lookup#.assessment_questions_lookup
    WHERE  Deleted <> 1  
    AND  assessment_reason = '1 - Start of care - further visits planned'
    ORDER BY wizard,order_id
</cfquery>

<cfoutput query="get_cg_questions" group="ques_id">
    <cfset  Variables.checked_value = "AssForm."&answer_label />
    <cfset answer_value = #Evaluate(Variables.checked_value)# />
</cfoutput>

<cfquery name="get_patient_details" datasource="#Application.DataSrc#">
    SELECT * FROM  #Request.prefix_db_agency#.pPatients
    WHERE  patient_id =  #AssForm.patient_id# 
</cfquery>


<cfquery datasource="#Application.DataSrc#" name="getproblem">
    SELECT * FROM  #Request.prefix_db_agency#.pProblem
    WHERE Status = 0
    AND Assmt_ID =  <cfqueryparam value = "#AssForm.Assmt_ID#" CFSQLType ="CF_SQL_INTEGER">
    AND M0_No like 'M102%'
    ORDER BY dx_order ASC
</cfquery>



<cfset page_num = 10 >
<cfset next_page = 1> 
<cfset question_count =  1 >
<cfset current_page = 1 >
<cfset diagnosis_printed = false >
<cfdocument format="pdf" orientation="portrait" overwrite="yes">
<cfset max_rows = ceiling(get_cg_questions.recordcount/10) >
        <table border="0" height="100%" style="border-left: solid 1px ##CCCCCC; border-right: solid 1px ##CCCCCC; border-top: ##CCCCCC solid 1px; border-bottom: solid 1px ##CCCCCC;" width="100%"  cellpadding="0" cellspacing="0">
          <tr valign="top">
                <td >
                    <table border="0" width="100%" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td style="height: 5px"></td>
                                </tr>
                                <cfoutput>
                                    <tr>
                                        <td style="padding-left: 5px; font-family: Arial; font-size: 14px; font-weight: bold; font-style: italic;">#Assform.Pt_First#  #Assform.Pt_Last# - DOB: #DateFormat(Assform.Pt_DOB,"mm/dd/yyyy")#</td>
                                        <td width="50%" align="right" style="padding-right: 30px; font-family: Arial; font-size: 14px; font-weight: bold; font-style: italic;"> <cfif LEFT(AssForm.F26,1) EQ 1>1 - Start Of Care<cfelse>#AssForm.F26#</cfif><br>#DateFormat(AssForm.F177,"mm/dd/yyyy")# - #DateFormat(AssForm.F178,"mm/dd/yyyy")#<br>completed #DateFormat(AssForm.F176,"mm/dd/yyyy")#</td>
                                    </tr>
                                </cfoutput>    
                                <tr>
                                    <td style="height: 5px"></td>
                                </tr>
                                <tr>
                                    <td style="border: solid 1px ##CCCCCC" colspan="5"></td>
                                </tr>
                                <tr>
                                    <td style="height: 5px"></td>
                                </tr>
                    </table>
                 
                    <table border="0" width="100%" height="95%" cellpadding="0" cellspacing="0">
                        <cfoutput query="get_cg_questions" group="ques_id">
                            <!--- START pProblems start  --->
                                <cfif   current_page EQ 1 AND diagnosis_printed EQ false>
                                     <tr>
                                        <td>
                                            <div class="row" style="margin:10px;"><b>DIAGNOSIS</b></div>
                                        </td>  
                                    </tr>
                                    <cfloop query="getproblem">
                                         <tr>
                                            <td style="font-family: Arial; font-size: 12px; padding-top: 5px; width: 50%; padding-left: 8px;">#getproblem.Description#</td>
                                            <td colspan="0" style="font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 5px;width: 50%; padding-left: 8px;"><cfif getproblem.Answer neq ''>#getproblem.Answer#<cfelse> - </cfif></td>
                                        </tr>
                                    </cfloop>
                                    <tr>
                                        <td style="height: 5px">
                                        </td>
                                    </tr>
                                    <cfset diagnosis_printed = true>
                                </cfif>
                             <!--- End pProblems End  --->

                            <tr>
                                 <cfif trim(title) neq ''>
                                        <cfif ques_id EQ '20' OR ques_id EQ '24' OR ques_id EQ '70' OR ques_id EQ '159'  OR ques_id EQ '50'  >
                                           <td style="font-size: 12px; margin-left:10px;"> #title#</td>

                                        <cfelse>
                                           <td> <div class="row" style="margin:10px;">  #title# </div></td>  
                                        </cfif>
                                </cfif>
                            </tr>

                            <tr>
                                <td style="font-family: Arial; font-size: 12px; padding-top: 5px; width: 50%; padding-left: 8px;"><cfif  ques_identifier NEQ ''>(#ques_identifier#)</cfif> #questions#
                                </td>
                                <cfset  cheked_value = "Assform."&answer_label />
                                <td colspan="0" style="font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 5px;width: 50%; padding-left: 8px;"><cfif #Evaluate(Variables.cheked_value)# neq ''>#Evaluate(Variables.cheked_value)#<cfelse> - </cfif></td>
                            </tr>
                            <tr>
                                <td style="height: 5px">
                                </td>
                            </tr>
                            <cfset question_count = question_count + 1>
                            <cfif question_count GT 10>
                                <cfset current_page = current_page + 1>
                                <cfset question_count = 1>
                            </cfif>
                            <cfif get_cg_questions.currentrow EQ get_cg_questions.recordcount>
                                     <tr>
                                        <td style="height: 10px"> </td>
                                    </tr>
                                    <cfif #GetVitals.recordcount# GT 0>
                                        <tr>
                                            <td style="font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 5px;width: 50%; padding-left: 8px;"> VITAL SIGNS</td>  
                                        </tr>
                                        <cfloop query="GetVitals">
                                            <tr>
                                                <td style="font-family: Arial; font-size: 12px; padding-top: 5px; width: 50%; padding-left: 8px;">
                                                    #(GetVitals.Description)#
                                                </td>
                                                <td style="font-family: Arial; font-size: 12px; padding-top: 5px; width: 50%; padding-left: 8px;">#GetVitals.Reading1# - #GetVitals.Notes#</td>
                                            </tr>
                                        </cfloop>
                                    <cfelse>
                                        <tr>
                                          <td style="font-family: Arial; font-size: 12px; padding-top: 5px; width: 50%; padding-left: 8px;"> No Vital Sign Records were Found </td>
                                        </tr>
                                    </cfif>
                                    <tr>
                                        <td style="height: 10px"> </td>
                                    </tr>
                                    <tr>
                                        <td style="font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 5px;width: 50%; padding-left: 8px;"> WOUNDS</td>  
                                    </tr>
                                        <cfif #GetWounds.recordcount# GT 0>
                                            <cfloop query="GetWounds">
                                                <tr>
                                                    <td style="font-family: Arial; font-size: 12px; padding-top: 5px; width: 50%; padding-left: 8px;">#GetWounds.WoundType#</td>
                                                    <td style="font-family: Arial; font-size: 12px; padding-top: 5px; width: 50%; padding-left: 8px;">
                                                        Length: #Length# cm, Width:#Width# cm, Depth: #Depth# cm, <br>Severity: #HealingStage#,#GetWounds.Notes#
                                                        <cfif Location NEQ ''><br>Location: #Location#<br> </cfif>
                                                    </td>
                                                </tr>
                                            </cfloop>
                                        <cfelse>
                                            <tr style="font-family: Arial; font-size: 12px; padding-top: 5px; width: 50%; padding-left: 8px;">
                                                <td colspan="3"> No Wound Records were Found </td>
                                            </tr>
                                        </cfif>
                                    <tr>
                                        <td style="height: 10px">
                                        </td>
                                    </tr>

                                    <tr><td style="border: solid 1px ##CCCCCC" colspan="2"></td>   </tr>
                                   <table border="0" cellpadding="0" cellspacing="0">
                                    
                                         <tr>
                                            <td style="font-family: Arial; font-size: 14px; padding-top: 5px; padding-left: 8px;">Completed & Digitally Signed by #completed_by# on #DateFormat(AssForm.F176,"mm/dd/yyyy")#
                                            </td>
                                        </tr>
                                         <tr>
                                        <td style="height: 5px">
                                        </td>
                                     </tr>

                                    </table>
                                   <!---  <tr><td style="border: solid 1px ##CCCCCC" colspan="2"></td>   </tr>
                                      <tr><td style="height: 8px"></td></tr>
                                     <tr><td style="border: solid 1px ##CCCCCC" colspan="2"></td>    </tr> --->
                            </cfif>
                            </cfoutput>
                     
                            
                           
                           

                    </table>

                   
                            
                    <!--- <table border="0" cellpadding="0" cellspacing="0">
                        <tr><td style="page-break-after: always"></td></tr>
                    </table> --->

                    
                </td>
            </tr>
        </table>
        <cfdocumentitem type="footer">
            <table width="100%" cellpadding="0" cellpadding="0" border="0" align="center">
            <tr>
            <td align="center" style="font-family: Arial; font-size: 13px;">
            <cfoutput>Page #cfdocument.currentpagenumber# SOC for <cfoutput>#AssForm.Pt_First# #AssForm.Pt_Last#</cfoutput></cfoutput>
            </td>
            </tr>
            </table>        
        </cfdocumentitem>
           


</cfdocument>


