<!---symptoms.cfc--->
<cfcomponent displayname="Symptoms"  hint="ColdFusion Component forSymptoms">
    
    <cffunction name="show_diagnosis_edit"  access="remote" output="Yes" returnFormat= "plain">   
        <cfargument name="Problem_ID" required="yes" default="0">
        <cfargument name="CM_ids" required="no" default="">
        
        <cfquery  datasource="#Application.DataSrc#" name="getSymp">
            SELECT pOrphan.*,pProblem.Description from #Request.prefix_db_agency#.pOrphan
            JOIN #Request.prefix_db_agency#.pProblem ON pProblem.Problem_ID = pOrphan.Problem_ID
            AND pOrphan.Assmt_ID = pProblem.Assmt_ID
            AND pProblem.status = 0
            where pOrphan.status = 0
            AND pOrphan.Problem_Dx_ID = '#Problem_ID#'
           <!----  <cfif len(arguments.CM_ids) gt 0 >
                AND pOrphan.Problem_ID NOT IN (#arguments.CM_ids#)
            </cfif> --->
            AND  pProblem.M0_No  NOT IN ('CM')
            AND  pProblem.Description  NOT like 'M18%'
             AND pProblem.Description NOT LIKE 'GG0%' AND Trim(pProblem.Description) NOT LIKE '(%'
                                                    ORDER BY pProblem.Problem_ID ASC

        </cfquery>
        <cfquery name="getProblem" datasource="#Application.DataSrc#">
            SELECT * FROM  #Request.prefix_db_agency#.pProblem 
            WHERE  Problem_ID = <cfqueryparam value="#arguments.Problem_ID#" cfsqltype="CF_SQL_INTEGER">
            AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
            AND  pProblem.Assmt_ID = '#getSymp.Assmt_ID#'
            AND M0_No like 'M102%'
        </cfquery>
         <!--- <cfif session.Display EQ 'Velmurugan G' OR CGI.REMOTE_ADDR EQ '103.143.7.161'>
            <cfdump var="#getProblem#">
              <cfdump var="#getSymp#">
        </cfif>--->  
        <cfsavecontent  variable = "html_diagnosis">           
            <cfoutput>
                <form name="editDiag_form" id="editDiag_form_id" action="" method="post">                     
                    <input type="hidden" name="Diagnosis_AutoProblem_ID" id="Diagnosis_AutoProblem_ID"  value="#getProblem.AutoProblem_ID#"/>            
                    <h3 style="font-weight:bold;">#getProblem.Description#</h3>
                    <div class="form-body">
                        <div class="form-group">
                            <label>Severity</label>
                            <SELECT id='Answer#getProblem.AutoProblem_ID#' name="Answer" class="form-control"  >
                                    <OPTION VALUE="0-Asymptomatic, no treatment needed at this time" <cfif #getProblem.Answer# EQ "0-Asymptomatic, no treatment needed at this time">SELECTED</cfif>>0-Asymptomatic, no treatment needed at this time</OPTION>
                                    <OPTION VALUE="1-Symptoms well controlled with current therapy" <cfif #getProblem.Answer# EQ "1-Symptoms well controlled with current therapy">SELECTED</cfif>>1-Symptoms well controlled with current therapy</OPTION>
                                    <OPTION VALUE="2-Symptoms controlled with difficulty, affecting daily functioning; patient needs ongoing monitoring" <cfif #getProblem.Answer# EQ "2-Symptoms controlled with difficulty, affecting daily functioning; patient needs ongoing monitoring">SELECTED</cfif>>2-Symptoms controlled with difficulty, affecting daily functioning; patient needs ongoing monitoring</OPTION>
                                    <OPTION VALUE="3-Symptoms poorly controlled, patient needs frequent adjustment in treatment and dose monitoring" <cfif #getProblem.Answer# EQ "3-Symptoms poorly controlled, patient needs frequent adjustment in treatment and dose monitoring">SELECTED</cfif>>3-Symptoms poorly controlled, patient needs frequent adjustment in treatment and dose monitoring</OPTION>
                                    <OPTION VALUE="4-Symptoms poorly controlled, history of re-hospitalizations" <cfif #getProblem.Answer# EQ "4-Symptoms poorly controlled, history of re-hospitalizations">SELECTED</cfif>>4-Symptoms poorly controlled, history of re-hospitalizations</OPTION>
                                </SELECT>
                        </div>
                        <div class="form-group">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>Symptoms</th>
                                        <th>Paired</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <cfset Ids = "" />
                                    <cfloop query="getSymp" group="Problem_ID">
                                        <tr>
                                            <td>#getSymp.Description#</td>
                                            <td>
                                                <span><input type="radio" id="symptoms_#Orphan_ID#"  name="symptoms_#Orphan_ID#" value="Y" <cfif Paired EQ 'Y'> checked</cfif> > Yes</span>
                                                <span><input type="radio"  id="symptoms_#Orphan_ID#" name="symptoms_#Orphan_ID#" value="N" <cfif Paired EQ 'N'> checked</cfif>> No</span>
                                            </td>
                                            <cfset Ids = listAppend(Ids, Orphan_ID) />
                                        </tr>
                                    </cfloop>
                                        <input type="hidden" name="symp_OrphanId" id="symp_OrphanId" value="#Ids#" />
                                </tbody>
                            </table>
                        </div>
                    </div>

                </form>     
            </cfoutput> 
        </cfsavecontent>    
        <cfreturn html_diagnosis />    
    </cffunction>

    <cffunction name="updateProblems"  access="remote" output="Yes" returnFormat= "plain"> 
        <cfargument name="Answer" required="yes" default="" />
        <cfargument name="Diagnosis_AutoProblem_ID" required="yes" default="" />
        <cfargument name="symptoms" required="yes" default="[]" />

        <cfset Severity = listFirst(arguments.Answer,'-') />
        <cfset Careplan = 1 >
        <cfif LEFT(Severity,1) LT 2>
            <cfset Careplan = 0 >
        </cfif>

        <cfquery name="updateProbs" datasource="#Application.DataSrc#">
           UPDATE #Request.prefix_db_agency#.pProblem 
           SET Answer = <cfqueryparam value="#arguments.Answer#" cfsqltype="cf_sql_varchar">,
           Severity =  <cfqueryparam value="#Severity#" cfsqltype="cf_sql_varchar">,
           Careplan =  '#Careplan#'
           WHERE  AutoProblem_ID = <cfqueryparam value="#arguments.Diagnosis_AutoProblem_ID#" cfsqltype="CF_SQL_INTEGER">
            AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
        </cfquery>
        <cfset Orphan_ID  = 0 >
        <cfset Orphan_IDs_List = "" >
        <cfset symptomsArray = deserializeJSON(arguments.symptoms) >
        <cfif arrayLen(symptomsArray) GT 0 >
            <!--- Build lists for batch updates --->
            <cfset paired_Y_ids = "" >
            <cfset paired_N_ids = "" >
            <cfloop array="#symptomsArray#" index="i" >
                <cfset orphanId = listLast(i,'|') >
                <cfset pairedValue = listFirst(i,'|') >
                <cfif pairedValue EQ 'Y'>
                    <cfset paired_Y_ids = listAppend(paired_Y_ids, orphanId) >
                    <cfif Orphan_ID EQ 0>
                        <cfset Orphan_ID = orphanId />
                    </cfif>
                <cfelse>
                    <cfset paired_N_ids = listAppend(paired_N_ids, orphanId) >
                </cfif>
            </cfloop>
            
            <!--- Batch update for 'Y' values --->
            <cfif len(paired_Y_ids) GT 0>
                <cfquery name="updateOrphanY" datasource="#Application.DataSrc#">
                   UPDATE #Request.prefix_db_agency#.pOrphan 
                   SET Paired = <cfqueryparam value="Y" cfsqltype="cf_sql_varchar">
                   WHERE  Orphan_ID IN (<cfqueryparam value="#paired_Y_ids#" cfsqltype="CF_SQL_INTEGER" list="yes">)
                    AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
                </cfquery>
            </cfif>
            
            <!--- Batch update for 'N' values --->
            <cfif len(paired_N_ids) GT 0>
                <cfquery name="updateOrphanN" datasource="#Application.DataSrc#">
                   UPDATE #Request.prefix_db_agency#.pOrphan 
                   SET Paired = <cfqueryparam value="N" cfsqltype="cf_sql_varchar">
                   WHERE  Orphan_ID IN (<cfqueryparam value="#paired_N_ids#" cfsqltype="CF_SQL_INTEGER" list="yes">)
                    AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
                </cfquery>
            </cfif>
        </cfif>

        <cfif Orphan_ID NEQ 0 >
            <!--- Get Assmt_IDs from orphan records to update assessments --->
            <cfquery name="getAssmtIDs" datasource="#Application.DataSrc#">
              SELECT DISTINCT Assmt_ID FROM #Request.prefix_db_agency#.pOrphan 
              WHERE Orphan_ID IN (<cfqueryparam value="#paired_Y_ids#" cfsqltype="CF_SQL_INTEGER" list="yes">)
              AND status = 0
            </cfquery>
            <cfif getAssmtIDs.recordcount GT 0>
                <cfset assmtIDsList = ValueList(getAssmtIDs.Assmt_ID) >
                <cfquery name="Update_ASS" datasource="#Application.DataSrc#">
                  UPDATE  #Request.prefix_db_agency#.pAssessments
                    SET F320 = 0
                    WHERE  Status  = 0
                    AND Assmt_ID IN (<cfqueryparam value="#assmtIDsList#" cfsqltype="CF_SQL_INTEGER" list="yes">)
                </cfquery>
            </cfif>
        </cfif>



    </cffunction>


    <cffunction name="show_medsdiag_edit"  access="remote" output="Yes" returnFormat= "plain">   
        <cfargument name="Problem_ID" required="yes" default="0">
        <cfargument name="Assmt_ID" required="no" default="">

         <cfquery name="getProblem" datasource="#Application.DataSrc#">
            SELECT * FROM  #Request.prefix_db_agency#.pProblem 
            WHERE  Problem_ID = <cfqueryparam value="#arguments.Problem_ID#" cfsqltype="CF_SQL_INTEGER">
            AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
        </cfquery>

        <cfset targetAssmtID = arguments.Assmt_ID >
        <cfif NOT len(trim(targetAssmtID)) AND getProblem.recordcount GT 0>
            <cfset targetAssmtID = getProblem.Assmt_ID >
        </cfif>

        <cfif len(trim(targetAssmtID))>
            <cfquery name="getAssmtPatient" datasource="#Application.DataSrc#">
                SELECT Patient_ID
                FROM #Request.prefix_db_agency#.pAssessments
                WHERE Assmt_ID = <cfqueryparam value="#targetAssmtID#" cfsqltype="CF_SQL_INTEGER">
                AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
            </cfquery>

            <cfif getAssmtPatient.recordcount GT 0>
                <cfquery name="getMissingPtMeds" datasource="#Application.DataSrc#">
                    SELECT pPtmeds.PtMeds_ID, pPtmeds.Drug_Name
                    FROM #Request.prefix_db_agency#.pPtmeds
                    LEFT JOIN #Request.prefix_db_agency#.pMed_Recon ON pMed_Recon.PtMeds_ID = pPtmeds.PtMeds_ID
                        AND pMed_Recon.Assmt_ID = <cfqueryparam value="#targetAssmtID#" cfsqltype="CF_SQL_INTEGER">
                        AND pMed_Recon.Med_Dx_ID = <cfqueryparam value="#arguments.Problem_ID#" cfsqltype="CF_SQL_INTEGER">
                        AND pMed_Recon.Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
                    WHERE pPtmeds.Patient_ID = <cfqueryparam value="#getAssmtPatient.Patient_ID#" cfsqltype="CF_SQL_INTEGER">
                    AND pPtmeds.Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
                    AND pMed_Recon.PtMeds_ID IS NULL
                    <cfif structKeyExists(session, "discont_meds") AND len(trim(session.discont_meds)) GT 0>
                        AND pPtmeds.PtMeds_ID NOT IN (<cfqueryparam value="#session.discont_meds#" cfsqltype="CF_SQL_INTEGER" list="yes">)
                    </cfif>
                    GROUP BY pPtmeds.PtMeds_ID
                </cfquery>

                <cfif getMissingPtMeds.recordcount GT 0>
                    <cfquery name="getMaxOrphanID" datasource="#Application.DataSrc#">
                        SELECT IFNULL(MAX(Orphan_ID),0) AS Max_Orphan_ID
                        FROM #Request.prefix_db_agency#.pMed_Recon
                    </cfquery>
                    <cfset nextOrphanID = getMaxOrphanID.Max_Orphan_ID >
                    <cfloop query="getMissingPtMeds">
                        <cfset nextOrphanID = nextOrphanID + 1>
                        <cfquery name="insertMissingMedRecon" datasource="#Application.DataSrc#">
                            INSERT INTO #Request.prefix_db_agency#.pMed_Recon
                            (Orphan_ID, Assmt_ID, PtMeds_ID, Drug_Name, Med_Dx_ID, Diagnosis, Paired, automatic_paired, Date_Created, Created_By)
                            VALUES (
                                <cfqueryparam value="#nextOrphanID#" cfsqltype="CF_SQL_INTEGER">,
                                <cfqueryparam value="#targetAssmtID#" cfsqltype="CF_SQL_INTEGER">,
                                <cfqueryparam value="#getMissingPtMeds.PtMeds_ID#" cfsqltype="CF_SQL_INTEGER">,
                                <cfqueryparam value="#getMissingPtMeds.Drug_Name#" cfsqltype="CF_SQL_VARCHAR">,
                                <cfqueryparam value="#arguments.Problem_ID#" cfsqltype="CF_SQL_INTEGER">,
                                <cfqueryparam value="#getProblem.Description#" cfsqltype="CF_SQL_VARCHAR">,
                                <cfqueryparam value="N" cfsqltype="CF_SQL_VARCHAR">,
                                <cfqueryparam value="N" cfsqltype="CF_SQL_VARCHAR">,
                                <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP">,
                                <cfqueryparam value="#session.employeeid#" cfsqltype="CF_SQL_INTEGER">
                            )
                        </cfquery>
                    </cfloop>
                </cfif>
            </cfif>
        </cfif>

        <cfquery  datasource="#Application.DataSrc#" name="getspMed_Recon">
            SELECT pMed_Recon.* from #Request.prefix_db_agency#.pMed_Recon
            JOIN #Request.prefix_db_agency#.pPtmeds ON pPtmeds.PtMeds_ID = pMed_Recon.PtMeds_ID
            AND pPtmeds.status = 0
            where pMed_Recon.status = 0
            AND pMed_Recon.Med_Dx_ID = <cfqueryparam value="#arguments.Problem_ID#" cfsqltype="CF_SQL_INTEGER">
            <cfif len(trim(targetAssmtID))>
                AND pMed_Recon.Assmt_ID = <cfqueryparam value="#targetAssmtID#" cfsqltype="CF_SQL_INTEGER">
            </cfif>
            <cfif structKeyExists(session, "discont_meds") AND len(trim(session.discont_meds)) GT 0>
                AND pMed_Recon.PtMeds_ID NOT IN (<cfqueryparam value="#session.discont_meds#" cfsqltype="CF_SQL_INTEGER" list="yes">)
            </cfif>
            ORDER BY pMed_Recon.Drug_Name ASC
        </cfquery>   

        <cfsavecontent  variable = "html_diagnosis">           
            <cfoutput>
                <form name="editDiag_form" id="editDiag_form_id" action="" method="post">    
                    <input type="hidden" name="Diagnosis_AutoProblem_ID" id="Diagnosis_AutoProblem_ID"  value="#getProblem.AutoProblem_ID#"/>                         
                    <h3 style="font-weight:bold;">#getspMed_Recon.Diagnosis#</h3>
                    <div class="form-body">
                        <div class="form-group">
                            <label>Severity</label>
                            <SELECT id='Answer#getProblem.AutoProblem_ID#' name="Answer" class="form-control"  >
                                    <OPTION VALUE="0-Asymptomatic, no treatment needed at this time" <cfif #getProblem.Answer# EQ "0-Asymptomatic, no treatment needed at this time">SELECTED</cfif>>0-Asymptomatic, no treatment needed at this time</OPTION>
                                    <OPTION VALUE="1-Symptoms well controlled with current therapy" <cfif #getProblem.Answer# EQ "1-Symptoms well controlled with current therapy">SELECTED</cfif>>1-Symptoms well controlled with current therapy</OPTION>
                                    <OPTION VALUE="2-Symptoms controlled with difficulty, affecting daily functioning; patient needs ongoing monitoring" <cfif #getProblem.Answer# EQ "2-Symptoms controlled with difficulty, affecting daily functioning; patient needs ongoing monitoring">SELECTED</cfif>>2-Symptoms controlled with difficulty, affecting daily functioning; patient needs ongoing monitoring</OPTION>
                                    <OPTION VALUE="3-Symptoms poorly controlled, patient needs frequent adjustment in treatment and dose monitoring" <cfif #getProblem.Answer# EQ "3-Symptoms poorly controlled, patient needs frequent adjustment in treatment and dose monitoring">SELECTED</cfif>>3-Symptoms poorly controlled, patient needs frequent adjustment in treatment and dose monitoring</OPTION>
                                    <OPTION VALUE="4-Symptoms poorly controlled, history of re-hospitalizations" <cfif #getProblem.Answer# EQ "4-Symptoms poorly controlled, history of re-hospitalizations">SELECTED</cfif>>4-Symptoms poorly controlled, history of re-hospitalizations</OPTION>
                                </SELECT>
                        </div>
                         <div class="form-group">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>Medications</th>
                                        <th>Paired</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <cfset Ids = "" />
                                    <cfloop query="getspMed_Recon">
                                        <tr>
                                            <td>#Drug_Name#</td>
                                            <td>
                                                <span><input type="radio" id="medsdiagnosis_#Orphan_ID#"  name="medsdiagnosis_#Orphan_ID#" value="Y" <cfif Paired EQ 'Y'> checked</cfif> > Yes</span>
                                                <span><input type="radio"  id="medsdiagnosis_#Orphan_ID#" name="medsdiagnosis_#Orphan_ID#" value="N" <cfif Paired EQ 'N'> checked</cfif>> No</span>
                                            </td>
                                            <cfset Ids = listAppend(Ids, Orphan_ID) />
                                        </tr>
                                    </cfloop>
                                    <input type="hidden" name="diagnsis_OrphanId" id="diagnsis_OrphanId" value="#Ids#" />
                                </tbody>
                            </table>
                        </div>
                    </div>

                </form>     
            </cfoutput> 
        </cfsavecontent>    
        <cfreturn html_diagnosis />    
    </cffunction>


    <cffunction name="updateMedsDiagnosis"  access="remote" output="Yes" returnFormat= "plain"> 
        <cfargument name="Answer" required="yes" default="" />
        <cfargument name="Diagnosis_AutoProblem_ID" required="yes" default="" />
        <cfargument name="meds" required="yes" default="[]" />
        <cfquery name="updateProbs" datasource="#Application.DataSrc#">
           UPDATE #Request.prefix_db_agency#.pProblem 
           SET Answer = <cfqueryparam value="#arguments.Answer#" cfsqltype="cf_sql_varchar">,
           Severity =  <cfqueryparam value="#(listFirst(arguments.Answer,'-'))#" cfsqltype="cf_sql_varchar">
           WHERE  AutoProblem_ID = <cfqueryparam value="#arguments.Diagnosis_AutoProblem_ID#" cfsqltype="CF_SQL_INTEGER">
            AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
        </cfquery>

        <cfset Orphan_ID  = 0 >
        <cfset medsArray = deserializeJSON(arguments.meds) >
        <cfif arrayLen(medsArray) GT 0 >
            <!--- Build lists for batch updates --->
            <cfset paired_Y_ids = "" >
            <cfset paired_N_ids = "" >
            <cfloop array="#medsArray#" index="i" >
                <cfset orphanId = listLast(i,'|') >
                <cfset pairedValue = listFirst(i,'|') >
                <cfif pairedValue EQ 'Y'>
                    <cfset paired_Y_ids = listAppend(paired_Y_ids, orphanId) >
                    <cfif Orphan_ID EQ 0>
                        <cfset Orphan_ID = orphanId />
                    </cfif>
                <cfelse>
                    <cfset paired_N_ids = listAppend(paired_N_ids, orphanId) >
                </cfif>
            </cfloop>
            
            <!--- Batch update for 'Y' values --->
            <cfif len(paired_Y_ids) GT 0>
                <cfquery name="updatepMed_ReconY" datasource="#Application.DataSrc#">
                   UPDATE #Request.prefix_db_agency#.pMed_Recon 
                   SET Paired = <cfqueryparam value="Y" cfsqltype="cf_sql_varchar">
                   WHERE  Orphan_ID IN (<cfqueryparam value="#paired_Y_ids#" cfsqltype="CF_SQL_INTEGER" list="yes">)
                    AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
                </cfquery>
            </cfif>
            
            <!--- Batch update for 'N' values --->
            <cfif len(paired_N_ids) GT 0>
                <cfquery name="updatepMed_ReconN" datasource="#Application.DataSrc#">
                   UPDATE #Request.prefix_db_agency#.pMed_Recon 
                   SET Paired = <cfqueryparam value="N" cfsqltype="cf_sql_varchar">
                   WHERE  Orphan_ID IN (<cfqueryparam value="#paired_N_ids#" cfsqltype="CF_SQL_INTEGER" list="yes">)
                    AND Status = <cfqueryparam value="0" cfsqltype="CF_SQL_INTEGER">
                </cfquery>
            </cfif>
        </cfif>

         <cfif Orphan_ID NEQ 0 >
            <!--- Get Patient_ID and Assmt_IDs from orphan records --->
            <cfquery name="getMedReconInfo" datasource="#Application.DataSrc#">
              SELECT DISTINCT Patient_ID, Assmt_ID FROM #Request.prefix_db_agency#.pMed_Recon 
              JOIN #Request.prefix_db_agency#.pPtmeds ON pPtmeds.PtMeds_ID = pMed_Recon.PtMeds_ID
              WHERE Orphan_ID IN (<cfqueryparam value="#paired_Y_ids#" cfsqltype="CF_SQL_INTEGER" list="yes">)
              AND pMed_Recon.status = 0
            </cfquery>
            <cfif getMedReconInfo.recordcount GT 0>
                <cfset patientIDList = ValueList(getMedReconInfo.Patient_ID) >
                <cfset assmtIDList = ValueList(getMedReconInfo.Assmt_ID) >
                <cfquery name="Update_ASS" datasource="#Application.DataSrc#">
                  UPDATE  #Request.prefix_db_agency#.pPatients
                    SET Med_Approve = 2,
                    Med_Recon_Field = 2 
                    WHERE  Status  < 2
                    AND Patient_ID IN (<cfqueryparam value="#patientIDList#" cfsqltype="CF_SQL_INTEGER" list="yes">)
                </cfquery>
            </cfif>
        </cfif>


    </cffunction>
               
  </cfcomponent>