Compile function OVERRIDE_REPORT_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler

equ crlf$$ to \0D0A\

#Window OVERRIDE_REPORT

$insert APP_INSERTS
$insert EVENT_SETUP
$insert MSG_EQUATES
$insert OVERRIDE_LOG_EQUATES



Declare subroutine SRP_Show_Window, Msg

Declare function   Utility, Datetime, SRP_DateTime, SRP_SORT_ARRAY

// Update the arguments so that the OpenInsight OLE event will treate the ActiveX event as a native event handler.
If Event EQ 'OLE' then
	Transfer Event to OIEvent
	Transfer Param1 to Event
	Transfer Param2 to Param1
	Transfer Param3 to Param2
	*    Transfer Param4 to Param3
	*    Transfer Param5 to Param4
	*    Transfer Param6 to Param5
	*    Transfer Param7 to Param6
	*    Transfer Param8 to Param7
end

GoToEvent Event for CtrlEntID

Return EventFlow else EVENT_CONTINUE$

Event WINDOW.CREATE(CreateParam)
	
	GoSub Setup_OLE_Controls
	FormSize = '' 
	SRP_Show_Window(@Window, '', 'C', 'C', 1, '', False$, False$, FormSize)
	
end event

Event OLE_DATEPICKER_FROM.OnSelChange(FirstDate)
	
	
	SelectedDate    = Get_Property(CtrlEntId, 'OLE.Selection')
	Set_Property(@Window : '.OLE_SCHEDULE', 'OLE.Date', SelectedDate)
	Set_Property(@Window : '.DATE_FROM','DEFPROP', OCONV(SelectedDate,'DT4/HS'))
	
end event

Event OLE_DATEPICKER_TO.OnSelChange(FirstDate)
	
	
	SelectedDate    = Get_Property(CtrlEntId, 'OLE.Selection')
	Set_Property(@Window : '.OLE_SCHEDULE', 'OLE.Date', SelectedDate)
	Set_Property(@Window : '.DATE_TO','DEFPROP', OCONV(SelectedDate,'DT4/HS'))
	
end event

Event PUB_DATE_FROM.CLICK:
	
	target = '.OLE_DATEPICKER_FROM'
	GoSub ToggleVisible
	
return

Event PUB_DATE_TO.CLICK:
	
	target = '.OLE_DATEPICKER_TO'
	GoSub ToggleVisible
	
return

Event PUB_DIR_BROWSE.CLICK
	value = "Select a directory" : @fm : "c:\temp"
	dir   = Utility( "CHOOSEDIR", @window, value)
	
	if dir NE '' then
		Set_Property(@Window : '.FILE_PATH', 'DEFPROP', dir: '\overrideexport.csv')	
	end
	
end event

Event PUB_EXPORT.CLICK
	tableLines = Get_Property(@WINDOW:'.OVERRIDE_TABLE','LIST')
	HeaderOut = Get_Property(@WINDOW:'.OVERRIDE_TABLE','LABEL') : crlf$$
	swap @FM with ',' in HeaderOut
	filepath = Get_Property(@Window : '.FILE_PATH', 'DEFPROP')
	DataOut = ''
	
	IF filepath NE '' then
		For Each line in tableLines using @FM
			For a = 1 to COUNT(line,@VM)
				line<1,a> = QUOTE(line<1,a>)
			Next a
			
			swap @VM with ',' in line
			DataOut := line: crlf$$
		Next line
	end else
		msg(@Window, 'No save path specified.')
		return
	end
	
	OSOPEN filepath TO hFilePath THEN
		OSWrite DataOut ON filepath			;* Clear file it was already there
	END ELSE
		OSWrite DataOut ON filepath			;* Create the file if it wasn't
		OSOPEN filepath TO hFilePath ELSE
			Msg(@Window, 'Unable to write the file!')
			RETURN ''
		END
	END  
	
	SWAP @FM WITH ',' IN HeaderOut
	
	FilePointer = 0
	
	OSBWrite HeaderOut ON filepath AT FilePointer
	FilePointer += LEN(HeaderOut)
	
	OSBWrite DataOut ON filepath AT FilePointer
	OSClose hFilePath
	Msg(@Window, 'Successfuly wrote file at ' : filepath : '.')
	
	
end event

Event SEARCH.CLICK
	Set_Property(@WINDOW:'.OVERRIDE_TABLE','LIST','')
	Set_Property(@WINDOW:'.OVERRIDE_TABLE','LABEL','')
	DynamicFields = 'RDS':@FM:'REACTOR'
	SearchType = Get_Property(@WINDOW:'.REPORT_BY','DEFPROP')
	Filter = Get_Property(@WINDOW:'.ITEM_FILTER','DEFPROP')
	DateFrom = Get_Property(@WINDOW:'.DATE_FROM','DEFPROP')
	DateFrom = ICONV(DateFrom,'DT')
	DateTo = Get_Property(@WINDOW:'.DATE_TO','DEFPROP')
	DateTo = ICONV(DateTo,'DT')
	
	IF DateTo EQ '' then
		DateTo = DateTime()
	end
	IF DateFrom EQ '' then
		DateFrom = SRP_DateTime('AddDays', DateTo, -7)
	end
	
	dataarray = ''
	open "OVERRIDE_LOG" To OverRideTable else
		Status = Set_FSError()
		return
		
	end
	
	select OverRideTable
	Begin Case
		
		Case SearchType = 'ALL'
			
			Done = 0
			Loop
				ReadNext @ID else Done = 1
			Until Done Do
				read @Record from OverRideTable, @ID else
					status = Set_FSError()
					
					return
				end
				RecordTime = @RECORD<OVERRIDE_LOG_DTM$>
				IF RecordTime GE DateFrom AND RecordTime LE DateTo Then
					RecToWrite = @Record
					DynamicValues = ''
					User = @Record<OVERRIDE_LOG_USER$>
					Comment = @Record<OVERRIDE_LOG_COMMENT$>
					LogTime = OCONV(@Record<OVERRIDE_LOG_DTM$>,'DT4/HS')
					Category = @Record<OVERRIDE_LOG_CATEGORY$>
					Cause = @Record<OVERRIDE_LOG_CAUSE$>
					keys = @Record<OVERRIDE_LOG_KEY$>
					for i = 1 to DCount(keys,@VM)
						pairedTable = @RECORD<OVERRIDE_LOG_TABLE$, i>
						Locate pairedTable in DynamicFields using @FM setting fPos then
							DynamicValues<1,fPos> = keys<1,i>
						end
						
						
					Next i
					dataarray:= User :@VM:Comment:@VM:LogTime:@VM:Category:@VM:Cause:@VM:DynamicValues :@FM
					
				end
			Repeat
			
			
			swap @VM with @FM in DynamicFields
			HeaderText = Get_Property(@WINDOW:'.OVERRIDE_TABLE','LABEL')
			HeaderText<6> = DynamicFields
			
			//////////////////////			
		Case SearchType = 'RDS' Or SearchType = 'REACTOR'
			if Filter Eq '' then
				Msg(@Window, 'No search string entered. Please enter a valid ' : SearchType)
				return
			end
			Done = 0
			Loop
				ReadNext @ID else Done = 1
			Until Done Do
				read @Record from OverRideTable, @ID else
					status = Set_FSError()
					
					return
				end
				RecordTables = @RECORD<OVERRIDE_LOG_TABLE$>
				RecordKeys = @RECORD<OVERRIDE_LOG_KEY$>
				Locate SearchType in RecordTables using @VM setting tPos then
					
					RecordTime = @RECORD<OVERRIDE_LOG_DTM$>
					IF RecordTime GE DateFrom AND RecordTime LE DateTo AND RecordKeys<1,tPos> EQ Filter Then
						RecToWrite = @Record
						DynamicValues = ''
						User = @Record<OVERRIDE_LOG_USER$>
						Comment = @Record<OVERRIDE_LOG_COMMENT$>
						LogTime = OCONV(@Record<OVERRIDE_LOG_DTM$>,'DT4/HS')
						Category = @Record<OVERRIDE_LOG_CATEGORY$>
						Cause = @Record<OVERRIDE_LOG_CAUSE$>
						keys = @Record<OVERRIDE_LOG_KEY$>
						for i = 1 to DCount(keys,@VM)
							pairedTable = @RECORD<OVERRIDE_LOG_TABLE$, i>
							Locate pairedTable in DynamicFields using @FM setting fPos then
								DynamicValues<1,fPos> = keys<1,i>
							end
							
							
						Next i
						dataarray:=  User :@VM:Comment:@VM:LogTime:@VM:Category:@VM:Cause:@VM:DynamicValues :@FM
						
						//swap @VM with @FM in DynamicFields
					end
				end
				
			Repeat
			////////////////////
		Case SearchType = 'OVERRIDE'
			
			OverrideFilter = Get_Property(@WINDOW:'.OVERRIDE_TYPE','DEFPROP')
			Done = 0
			Loop
				ReadNext @ID else Done = 1
			Until Done Do
				read @Record from OverRideTable, @ID else
					status = Set_FSError()
					
					return
				end
				
				RecordTime = @RECORD<OVERRIDE_LOG_DTM$>
				IF RecordTime GE DateFrom AND RecordTime LE DateTo AND @RECORD<OVERRIDE_LOG_CATEGORY$> EQ OverrideFilter Then
					RecToWrite = @Record
					DynamicValues = ''
					User = @Record<OVERRIDE_LOG_USER$>
					Comment = @Record<OVERRIDE_LOG_COMMENT$>
					LogTime = OCONV(@Record<OVERRIDE_LOG_DTM$>,'DT4/HS')
					Category = @Record<OVERRIDE_LOG_CATEGORY$>
					Cause = @Record<OVERRIDE_LOG_CAUSE$>
					keys = @Record<OVERRIDE_LOG_KEY$>
					for i = 1 to DCount(keys,@VM)
						pairedTable = @RECORD<OVERRIDE_LOG_TABLE$, i>
						Locate pairedTable in DynamicFields using @FM setting fPos then
							DynamicValues<1,fPos> = keys<1,i>
						end
						
						
					Next i
					dataarray:=  User :@VM:Comment:@VM:LogTime:@VM:Category:@VM:Cause:@VM:DynamicValues :@FM
					
					swap @VM with @FM in DynamicFields
				end
				
				
			Repeat
			
		Case SearchType = 'USER'
			
			
			Done = 0
			Loop
				ReadNext @ID else Done = 1
			Until Done Do
				read @Record from OverRideTable, @ID else
					status = Set_FSError()
					
					return
				end
				
				RecordTime = @RECORD<OVERRIDE_LOG_DTM$>
				IF RecordTime GE DateFrom AND RecordTime LE DateTo AND @RECORD<OVERRIDE_LOG_USER$> EQ Filter Then
					RecToWrite = @Record
					DynamicValues = ''
					User = @Record<OVERRIDE_LOG_USER$>
					Comment = @Record<OVERRIDE_LOG_COMMENT$>
					LogTime = OCONV(@Record<OVERRIDE_LOG_DTM$>,'DT4/HS')
					Category = @Record<OVERRIDE_LOG_CATEGORY$>
					Cause = @Record<OVERRIDE_LOG_CAUSE$>
					keys = @Record<OVERRIDE_LOG_KEY$>
					for i = 1 to DCount(keys,@VM)
						pairedTable = @RECORD<OVERRIDE_LOG_TABLE$, i>
						Locate pairedTable in DynamicFields using @FM setting fPos then
							DynamicValues<1,fPos> = keys<1,i>
						end
						
						
					Next i
					dataarray:=  User :@VM:Comment:@VM:LogTime:@VM:Category:@VM:Cause:@VM:DynamicValues :@FM
					
					swap @VM with @FM in DynamicFields
				end
				
				
			Repeat
	End Case
	
	dataarray =  SRP_SORT_Array(dataarray, "DR3", 1)
	
	Set_Property(@WINDOW:'.OVERRIDE_TABLE','LIST',dataarray)
	HeaderText = 'USER':@FM:'COMMENT':@FM:'DATE/TIME':@FM:'OVERRIDE TYPE':@FM:'CAUSE':@FM:DynamicFields
	Set_Property(@WINDOW:'.OVERRIDE_TABLE','LABEL',HeaderText)
	
	* HeaderText<COL$REC_NO> = HeaderText<COL$REC_NO>:CHAR(13):'<dc>'
	* HeaderText<COL$THK_UNITS> = HeaderText<COL$THK_UNITS>:CHAR(13):'<dc>'
	* HeaderText<COL$RES_UNITS> = HeaderText<COL$RES_UNITS>:CHAR(13):'<dc>'
	* HeaderText<COL$CON_UNITS> = HeaderText<COL$CON_UNITS>:CHAR(13):'<dc>'
	* HeaderText<COL$CRES_UNITS> = HeaderText<COL$CRES_UNITS>:CHAR(13):'<dc>'
	* 
	* Set_Property(@WINDOW:'.LS_TABLE','LABEL',HeaderText)
	//NewList = SRP_Sort_Array(List, "AL2", 1)
	//Set_Property(@Window:".EDT_TEST", "LIST", NewList)
	
	
	
return
end event

Event REPORT_BY.ALL.CLICK
Set_Property(@Window:'.ITEM_FILTER', 'ENABLED', -1)
Set_Property(@WINDOW:'.LABEL_FILTER','TEXT','')
Set_Property(@Window:'.OVERRIDE_TYPE', 'ENABLED', 0)
return
end event

Event REPORT_BY.RDS.CLICK
Set_Property(@Window:'.ITEM_FILTER', 'ENABLED', 1)
Set_Property(@WINDOW:'.LABEL_FILTER','TEXT', 'RDS No:')
Set_Property(@Window:'.OVERRIDE_TYPE', 'ENABLED', 0)
end event

Event REPORT_BY.REACTOR.CLICK
Set_Property(@Window:'.ITEM_FILTER', 'ENABLED', 1)
Set_Property(@WINDOW:'.LABEL_FILTER','TEXT', 'Reactor No:')
Set_Property(@Window:'.OVERRIDE_TYPE', 'ENABLED', 0)
end event

Event REPORT_BY.OVERRIDE.CLICK
Set_Property(@Window:'.OVERRIDE_TYPE', 'ENABLED', 1)
Set_Property(@Window:'.ITEM_FILTER', 'ENABLED', -1)
Set_Property(@WINDOW:'.LABEL_FILTER','TEXT','')
end event

Event REPORT_BY.USER.CLICK
Set_Property(@Window:'.ITEM_FILTER', 'ENABLED', 1)
Set_Property(@WINDOW:'.LABEL_FILTER','TEXT', 'User:')
Set_Property(@Window:'.OVERRIDE_TYPE', 'ENABLED', 0)
end event

Setup_OLE_Controls:

Ctrl = @Window : '.OLE_DATEPICKER_FROM'
Set_Property(Ctrl, 'OLE.Border', 'XP Flat')
Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : '9')
Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnSelChange', 1)

Ctrl = @Window : '.OLE_DATEPICKER_TO'
Set_Property(Ctrl, 'OLE.Border', 'XP Flat')
Set_Property(Ctrl, 'OLE.Font', 'Segoe UI' : @SVM : '9')
Send_Message(Ctrl, 'QUALIFY_EVENT', 'OLE.OnSelChange', 1)

return

* * * * * * * * 
ToggleVisible:
* * * * * * * *

visible = Get_Property(@Window : target , 'VISIBLE')

if visible then
Set_Property(@Window : target, 'VISIBLE', 0)
end else
Set_Property(@Window : target, 'VISIBLE', 1)
end
target = ''
return



