<cfparam name="Form.inactivation_hidden" default="0" >
<cfparam name="Form.activation_hidden" default="0" >
<cfif form.inactivation_hidden EQ 1 >
	<cfquery  name="GetSchedules"  datasource="#Application.DataSrc#">
		SELECT pPatients.Patient_ID, pPatients.Pt_Last AS PatientLast,pPatients.Pt_Middle, pPatients.Pt_First AS PatientFirst,pPatients.Pt_Agy_ID,pPatients.Status AS clientStatus,pPatients.Pt_SSN,pPatients.Pt_Street,pPatients.Pt_City,pPatients.Pt_State,pPatients.Pt_Zip,pPatients.Pt_Longitude,pPatients.Pt_Latitude,pPatients.Pt_Phone,
		pSchedules.Schedule_ID, pSchedules.Visit_Date, pSchedules.Skill AS Type, pSchedules.Visit_Type,
		pSchedules.Progress_Note, pSchedules.pgnotesdraft, pSchedules.Pay_ID, 0 AS No_Days_Progress,  "" AS  Note_QA , pSchedules.Notes AS Memo,pSchedules.Sch_Latitude,pSchedules.Sch_Longitude,pSchedules.StartTime,pSchedules.EndTime,pSchedules.Date_Create AS CallDateTime,
		pEmployee.*, pAssessments.Assmt_ID,pAuths.*,pPayer.*,pPtPayer.*,Agency.*,pRevenue_Code.HCPCS,pPhysicians.Phys_NPI,pRevenue_Code.HCPCS
		FROM #Request.prefix_db_agency#.pSchedules
		LEFT OUTER JOIN #Request.prefix_db_agency#.pAssessments ON pAssessments.Assmt_ID = pSchedules.Assmt_ID AND pAssessments.status = 0
		LEFT OUTER JOIN #Request.prefix_db_agency#.pAdmit ON pAdmit.Admit_ID = pAssessments.Admit_ID
		JOIN #Request.prefix_db_agency#.pPatients ON pPatients.Patient_ID = pSchedules.Patient_ID AND	pPatients.Status  < 2
		JOIN #Request.prefix_db_lookup#.pEmployee ON pSchedules.Emp_ID = pEmployee.Emp_ID AND pEmployee.Status < 2
		LEFT OUTER JOIN #Request.prefix_db_agency#.pAuths ON pAuths.Patient_ID = pSchedules.Patient_ID AND	pAuths.Status  =0
		LEFT OUTER JOIN #Request.prefix_db_agency#.pPayer ON pPayer.Pay_ID = pSchedules.Pay_ID AND pPayer.Status = 0
		LEFT OUTER JOIN #Request.prefix_db_agency#.pPtPayer ON pPtPayer.Patient_ID = pPatients.Patient_ID AND pPtPayer.Status = 0
		LEFT OUTER JOIN #Request.prefix_db_lookup#.Agency ON Agency.Agency_ID = pPatients.Loc_ID AND Agency.Status = 'active'
		LEFT OUTER JOIN #Request.prefix_db_agency#.pRevenue_Code ON pRevenue_Code.Rev_CodeID = pSchedules.HCPCS_ID AND pRevenue_Code.Status = 0
		LEFT OUTER JOIN #Request.prefix_db_lookup#.pPhysicians ON pPhysicians.Emp_ID = pEmployee.Emp_ID AND pPhysicians.Status = 0
		WHERE pSchedules.status= 0
		AND pSchedules.Emp_ID != 0
		AND pSchedules.Missed = '0'
		AND pSchedules.Visit_Date >= '2024-07-31'
	  	AND pSchedules.EVV_Export = 1
	  	AND pSchedules.Schedule_ID IN  (<cfqueryparam cfsqltype="cf_sql_integer" value="#Form.Inactivation#" list="yes">)   
		GROUP BY pSchedules.Schedule_ID
		ORDER BY pPatients.Pt_Last ASC, pPatients.Pt_First ASC, pEmployee.Emp_Last ASC, pEmployee.Emp_First ASC,
			 pSchedules.Skill ASC, pSchedules.Visit_Date ASC 
	</cfquery>
	<cfquery  name="GetContacts" datasource="#Application.DataSrc#">
		SELECT *  
		FROM agency_179.pPtContacts
		WHERE Patient_ID IN  (<cfqueryparam cfsqltype="cf_sql_integer" value="#GetSchedules.Patient_ID#" list="yes">)   
		AND status < 2
		Order by Emergency DESC, status ASC
	</cfquery>
	<cfif GetSchedules.recordcount gt 0> 
		<cfquery  name="UpdateSchedule" datasource="#Application.DataSrc#">
		  UPDATE #Request.prefix_db_agency#.pSchedules 
		  SET EVV_Export = 0
		  WHERE Schedule_ID = <cfqueryparam value="#GetSchedules.Schedule_ID#" cfsqltype="cf_sql_integer" />
		  AND Patient_ID =  <cfqueryparam value="#GetSchedules.Patient_ID#" cfsqltype="cf_sql_integer" />
		  AND Status = <cfqueryparam value="0" cfsqltype="cf_sql_integer" />
		</cfquery>
	</cfif>
	<cfif CGI.REMOTE_ADDR EQ '121.200.49.65'>
		<cfinclude template="evv_dataset.cfm" />
	<cfelse>
		<cfinclude template="evv_dataset.cfm" />
	</cfif>
	<cfoutput><p><a href='index.cfm?page=dashboard&cat=dashboard&rpt=evv_visits&sID=#GetSchedules.Schedule_ID#'>Click Here</a> go to EVV Visits Ready For Export.</p></cfoutput>
<cfelseif form.activation_hidden EQ 1 >
	<cfquery  name="UpdateSchedule" datasource="#Application.DataSrc#">
	  UPDATE #Request.prefix_db_agency#.pSchedules 
	  SET EVV_Export = 0
	  WHERE Schedule_ID IN (<cfqueryparam value="#form.Schedule_ID#" cfsqltype="cf_sql_integer" list="yes">)
	  AND Status = <cfqueryparam value="0" cfsqltype="cf_sql_integer" />
	</cfquery>	
	<cflocation url="../dashboard/index.cfm?page=dashboard&cat&rpt=evv_transmitted_datasets" addtoken="no">
</cfif>
