# CORRECTED Export Script for Statistics View
# This script fixes all the issues found in the original export script

data = []
table = []
import datetime
current_datetime = datetime.datetime.now()
str_date = current_datetime.strftime("%m-%d-%Y %I-%M")

if self.getSibling("Statistics").props.currentTabIndex == 0:
	# INDUCT DETAILS - SIMPLIFIED (only Total fields, no carrier breakdown)
	# NOTE: To include single/double carrier data, the Induct Details query must be updated
	results = self.parent.getChild("Statistics").getChild("Induct Details").getChild("Induct Details").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Induct", "Total (#)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['InductName'], result['Total_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Induct", "Total (%)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['InductName'], result['Total_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Induct", "Total (pph)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['InductName'], result['Total_rate']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Induct Details").getChild("Induct Details").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Induct", "Total (#)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['InductName'], result['Total_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Induct", "Total (%)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['InductName'], result['Total_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Induct", "Total (pph)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['InductName'], result['Total_rate']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Induct Details.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 1:
	# SCANNER DETAILS - No changes needed
	results = self.parent.getChild("Statistics").getChild("Scanner_Details").getChild("Scanner Details").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Scanner", "Total (#)", "Good Read (#)", "No Read (#)", "Multi Read (#)", "No Code (#)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['ScannerName'], result['Total_count'], result['GoodRead_count'], result['NoRead_count'], result['MultiLabelRead_count'], result['NoCode_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Scanner", "Total (%)", "Good Read (%)", "No Read (%)", "Multi Read (%)", "No Code (%)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['ScannerName'], result['Total_perc'], result['GoodRead_perc'], result['NoRead_perc'], result['MultiLabelRead_perc'], result['NoCode_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Scanner", "Total (pph)", "Good Read (pph)", "No Read (pph)", "Multi Read (pph)", "No Code (pph)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['ScannerName'], result['Total_rate'], result['GoodRead_rate'], result['NoRead_rate'], result['MultiLabelRead_rate'], result['NoCode_rate']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Scanner_Details").getChild("Scanner Details").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Scanner", "Total (#)", "Good Read (#)", "No Read (#)", "Multi Read (#)", "No Code (#)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['ScannerName'], result['Total_count'], result['GoodRead_count'], result['NoRead_count'], result['MultiLabelRead_count'], result['NoCode_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Scanner", "Total (%)", "Good Read (%)", "No Read (%)", "Multi Read (%)", "No Code (%)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['ScannerName'], result['Total_perc'], result['GoodRead_perc'], result['NoRead_perc'], result['MultiLabelRead_perc'], result['NoCode_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Scanner", "Total (pph)", "Good Read (pph)", "No Read (pph)", "Multi Read (pph)", "No Code (pph)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['ScannerName'], result['Total_rate'], result['GoodRead_rate'], result['NoRead_rate'], result['MultiLabelRead_rate'], result['NoCode_rate']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Scanner Details.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 2:
	# SORTER SUMMARY - No changes needed
	results = self.parent.getChild("Statistics").getChild("Sorter Summary").getChild("Sorter Summary").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (#)", "Sorted (#)", "Awcs Recirc (#)", "Operational Recirc (#)", "Machine Recirc (#)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Induct_count'], result['Sorted_count'], result['AWCSRecirc_count'], result['OperRecirc_count'], result['MachineRecirc_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (%)", "Sorted (%)", "Awcs Recirc (%)", "Operational Recirc (%)", "Machine Recirc (%)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Induct_perc'], result['Sorted_perc'], result['AWCSRecirc_perc'], result['OperRecirc_perc'], result['MachineRecirc_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (pph)", "Sorted (pph)", "Awcs Recirc (pph)", "Operational Recirc (pph)", "Machine Recirc (pph)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Induct_rate'], result['Sorted_rate'], result['AWCSRecirc_rate'], result['OperRecirc_rate'], result['MachineRecirc_rate']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Sorter Summary").getChild("Sorter Summary").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (#)", "Sorted (#)", "Awcs Recirc (#)", "Operational Recirc (#)", "Machine Recirc (#)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Induct_count'], result['Sorted_count'], result['AWCSRecirc_count'], result['OperRecirc_count'], result['MachineRecirc_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (%)", "Sorted (%)", "Awcs Recirc (%)", "Operational Recirc (%)", "Machine Recirc (%)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Induct_perc'], result['Sorted_perc'], result['AWCSRecirc_perc'], result['OperRecirc_perc'], result['MachineRecirc_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (pph)", "Sorted (pph)", "Awcs Recirc (pph)", "Operational Recirc (pph)", "Machine Recirc (pph)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Induct_rate'], result['Sorted_rate'], result['AWCSRecirc_rate'], result['OperRecirc_rate'], result['MachineRecirc_rate']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Sorter Summary.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 3:
	# SORTER DETAILS - FIXED: assigned_count typo
	results = self.parent.getChild("Statistics").getChild("Sorter Details").getChild("Sorter Details").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (#)", "Sorted (#)", "Assigned (#)", "Dest Inv (#)", "Dest None (#)", "Dest Dis (#)", "Dest Full (#)", "Unexpected (#)", "Dest Fault (#)", "Div Fail (#)", "Gap Err (#)", "Lost (#)", "Track Err (#)", "Unknown (#)", "Unsafe (#)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['inducted_count'], result['sorted_count'], result['assigned_count'], result['destinv_count'], result['destnone_count'], result['destdis_count'], result['destfull_count'], result['unexpected_count'], result['destfault_count'], result['divfail_count'], result['gaperr_count'], result['lost_count'], result['trackerr_count'], result['unknownn_count'], result['unsafe_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (%)", "Sorted (%)", "Assigned (%)", "Dest Inv (%)", "Dest None (%)", "Dest Dis (%)", "Dest Full (%)", "Unexpected (%)", "Dest Fault (%)", "Div Fail (%)", "Gap Err (%)", "Lost (%)", "Track Err (%)", "Unknown (%)", "Unsafe (%)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['inducted_perc'], result['sorted_perc'], result['assigned_perc'], result['destinv_perc'], result['destnone_perc'], result['destdis_perc'], result['destfull_perc'], result['unexpected_perc'], result['destfault_perc'], result['divfail_perc'], result['gaperr_perc'], result['lost_perc'], result['trackerr_perc'], result['unknown_perc'], result['unsafe_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (pph)", "Sorted (pph)", "Assigned (pph)", "Dest Inv (pph)", "Dest None (pph)", "Dest Dis (pph)", "Dest Full (pph)", "Unexpected (pph)", "Dest Fault (pph)", "Div Fail (pph)", "Gap Err (pph)", "Lost (pph)", "Track Err (pph)", "Unknown (pph)", "Unsafe (pph)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['inducted_rate'], result['sorted_rate'], result['assigned_rate'], result['destinv_rate'], result['destnone_rate'], result['destdis_rate'], result['destfull_rate'], result['unexpected_rate'], result['destfault_rate'], result['divfail_rate'], result['gaperr_rate'], result['lost_rate'], result['trackerr_rate'], result['unknown_rate'], result['unsafe_rate']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Sorter Details").getChild("Sorter Details").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (#)", "Sorted (#)", "Assigned (#)", "Dest Inv (#)", "Dest None (#)", "Dest Dis (#)", "Dest Full (#)", "Unexpected (#)", "Dest Fault (#)", "Div Fail (#)", "Gap Err (#)", "Lost (#)", "Track Err (#)", "Unknown (#)", "Unsafe (#)"]
			for result in test_table:
				# FIXED: Changed second sorted_count to assigned_count
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['inducted_count'], result['sorted_count'], result['assigned_count'], result['destinv_count'], result['destnone_count'], result['destdis_count'], result['destfull_count'], result['unexpected_count'], result['destfault_count'], result['divfail_count'], result['gaperr_count'], result['lost_count'], result['trackerr_count'], result['unknownn_count'], result['unsafe_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (%)", "Sorted (%)", "Assigned (%)", "Dest Inv (%)", "Dest None (%)", "Dest Dis (%)", "Dest Full (%)", "Unexpected (%)", "Dest Fault (%)", "Div Fail (%)", "Gap Err (%)", "Lost (%)", "Track Err (%)", "Unknown (%)", "Unsafe (%)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['inducted_perc'], result['sorted_perc'], result['assigned_perc'], result['destinv_perc'], result['destnone_perc'], result['destdis_perc'], result['destfull_perc'], result['unexpected_perc'], result['destfault_perc'], result['divfail_perc'], result['gaperr_perc'], result['lost_perc'], result['trackerr_perc'], result['unknown_perc'], result['unsafe_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Inducted (pph)", "Sorted (pph)", "Assigned (pph)", "Dest Inv (pph)", "Dest None (pph)", "Dest Dis (pph)", "Dest Full (pph)", "Unexpected (pph)", "Dest Fault (pph)", "Div Fail (pph)", "Gap Err (pph)", "Lost (pph)", "Track Err (pph)", "Unknown (pph)", "Unsafe (pph)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['inducted_rate'], result['sorted_rate'], result['assigned_rate'], result['destinv_rate'], result['destnone_rate'], result['destdis_rate'], result['destfull_rate'], result['unexpected_rate'], result['destfault_rate'], result['divfail_rate'], result['gaperr_rate'], result['lost_rate'], result['trackerr_rate'], result['unknown_rate'], result['unsafe_rate']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Sorter Details.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 4:
	# LANE DETAILS - FIXED: Case sensitivity issues
	results = self.parent.getChild("Statistics").getChild("Lane Details").getChild("Lane Details").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Lane", "Total (#)", "Diverted (#)", "Dest Full (#)", "Dest Jam (#)", "Dest Disabled (#)", "Dest Fault (#)", "Divert Fail (#)", "Lost (#)", "Unsafe (#)", "Dim Error (#)", "Gap Error (#)", "Unknown (#)"]
			for result in results:
				try:
					data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Lane'], result['Total_count'], result['Diverted_count'], result['DestFull_count'], result['DestJam_count'], result['DestDis_count'], result['DestFault_count'], result['DivertFail_count'], result['Lost_count'], result['Unsafe_count'], result['Dim_count'], result['GapErr_count'], result['Unknown_count']])
				except KeyError as e:
					# Log available fields for debugging
					print("Lane Details Count - Missing field:", str(e))
					print("Available fields:", result.keys())
					raise
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Lane", "Total (%)", "Diverted (%)", "Dest Full (%)", "Dest Jam (%)", "Dest Disabled (%)", "Dest Fault (%)", "Divert Fail (%)", "Lost (%)", "Unsafe (%)", "Dim Error (%)", "Gap Error (%)", "Unknown (%)"]
			for result in results:
				try:
					data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Lane'], result['total_perc'], result['Diverted_perc'], result['destfull_perc'], result['destjam_perc'], result['destdisabled_perc'], result['destfault_perc'], result['DivertFail_perc'], result['lost_perc'], result['unsafe_perc'], result['dim_perc'], result['gaperr_perc'], result['unknown_perc']])
				except KeyError as e:
					# Log available fields for debugging
					print("Lane Details Percentage - Missing field:", str(e))
					print("Available fields:", result.keys())
					raise
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Lane", "Total (pph)", "Diverted (pph)", "Dest Full (pph)", "Dest Jam (pph)", "Dest Disabled (pph)", "Dest Fault (pph)", "Divert Fail (pph)", "Lost (pph)", "Unsafe (pph)", "Dim Error (pph)", "Gap Error (pph)", "Unknown (pph)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Lane'], result['total_rate'], result['Diverted_rate'], result['destfull_rate'], result['destjam_rate'], result['destdisabled_rate'], result['destfault_rate'], result['DivertFail_rate'], result['lost_rate'], result['unsafe_rate'], result['dim_rate'], result['gaperr_rate'], result['unknown_rate']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Lane Details").getChild("Lane Details").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Lane", "Total (#)", "Diverted (#)", "Dest Full (#)", "Dest Jam (#)", "Dest Disabled (#)", "Dest Fault (#)", "Divert Fail (#)", "Lost (#)", "Unsafe (#)", "Dim Error (#)", "Gap Error (#)", "Unknown (#)"]
			for result in test_table:
				try:
					data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Lane'], result['Total_count'], result['Diverted_count'], result['DestFull_count'], result['DestJam_count'], result['DestDis_count'], result['DestFault_count'], result['DivertFail_count'], result['Lost_count'], result['Unsafe_count'], result['Dim_count'], result['GapErr_count'], result['Unknown_count']])
				except KeyError as e:
					# Log available fields for debugging
					print("Lane Details Count (props.data) - Missing field:", str(e))
					print("Available fields:", result.keys())
					raise
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Lane", "Total (%)", "Diverted (%)", "Dest Full (%)", "Dest Jam (%)", "Dest Disabled (%)", "Dest Fault (%)", "Divert Fail (%)", "Lost (%)", "Unsafe (%)", "Dim Error (%)", "Gap Error (%)", "Unknown (%)"]
			for result in test_table:
				try:
					data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Lane'], result['total_perc'], result['Diverted_perc'], result['destfull_perc'], result['destjam_perc'], result['destdisabled_perc'], result['destfault_perc'], result['DivertFail_perc'], result['lost_perc'], result['unsafe_perc'], result['dim_perc'], result['gaperr_perc'], result['unknown_perc']])
				except KeyError as e:
					# Log available fields for debugging
					print("Lane Details Percentage (props.data) - Missing field:", str(e))
					print("Available fields:", result.keys())
					raise
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "End Timestamp", "Sorter", "Lane", "Total (pph)", "Diverted (pph)", "Dest Full (pph)", "Dest Jam (pph)", "Dest Disabled (pph)", "Dest Fault (pph)", "Divert Fail (pph)", "Lost (pph)", "Unsafe (pph)", "Dim Error (pph)", "Gap Error (pph)", "Unknown (pph)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['SorterName'], result['Lane'], result['total_rate'], result['Diverted_rate'], result['destfull_rate'], result['destjam_rate'], result['destdisabled_rate'], result['destfault_rate'], result['DivertFail_rate'], result['lost_rate'], result['unsafe_rate'], result['dim_rate'], result['gaperr_rate'], result['unknown_rate']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Lane Details.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 5:
	# HOURLY INDUCT - No changes needed
	results = self.parent.getChild("Statistics").getChild("Hourly_Induct").getChild("Hourly_Induct").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Total (#)", "Single Carrier (#)", "Double Carrier (#)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (#)'], result['Single Carrier (#)'], result['Double Carrier (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Total (%)", "Single Carrier (%)", "Double Carrier (%)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (%)'], result['Single Carrier (%)'], result['Double Carrier (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Total (pph)", "Single Carrier (pph)", "Double Carrier (pph)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (pph)'], result['Single Carrier (pph)'], result['Double Carrier (pph)']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Hourly_Induct").getChild("Hourly_Induct").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Total (#)", "Single Carrier (#)", "Double Carrier (#)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (#)'], result['Single Carrier (#)'], result['Double Carrier (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Total (%)", "Single Carrier (%)", "Double Carrier (%)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (%)'], result['Single Carrier (%)'], result['Double Carrier (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Total (pph)", "Single Carrier (pph)", "Double Carrier (pph)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (pph)'], result['Single Carrier (pph)'], result['Double Carrier (pph)']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Hourly Induct.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 6:
	# HOURLY SCANNER - No changes needed
	results = self.parent.getChild("Statistics").getChild("Hourly_Scanner").getChild("Hourly_Scanner").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Total (#)", "Good Read (#)", "No Read (#)", "Multi Read (#)", "No code (#)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (#)'], result['Good Read (#)'], result['No Read (#)'], result['Multi Read (#)'], result['No code (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Total (%)", "Good Read (%)", "No Read (%)", "Multi Read (%)", "No Code (%)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (%)'], result['Good Read (%)'], result['No Read (%)'], result['Multi Read (%)'], result['No Code (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Total (pph)", "Good Read (pph)", "No Read (pph)", "Multi Read (pph)", "No Code (pph)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (pph)'], result['Good Read (pph)'], result['No Read (pph)'], result['Multi Read (pph)'], result['No Code (pph)']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Hourly_Scanner").getChild("Hourly_Scanner").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Total (#)", "Good Read (#)", "No Read (#)", "Multi Read (#)", "No code (#)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (#)'], result['Good Read (#)'], result['No Read (#)'], result['Multi Read (#)'], result['No code (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Total (%)", "Good Read (%)", "No Read (%)", "Multi Read (%)", "No Code (%)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (%)'], result['Good Read (%)'], result['No Read (%)'], result['Multi Read (%)'], result['No Code (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Total (pph)", "Good Read (pph)", "No Read (pph)", "Multi Read (pph)", "No Code (pph)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (pph)'], result['Good Read (pph)'], result['No Read (pph)'], result['Multi Read (pph)'], result['No Code (pph)']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Hourly Scanner.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 7:
	# HOURLY SORTER SUMMARY - No changes needed
	results = self.parent.getChild("Statistics").getChild("Hourly_Sorter_Summary").getChild("Hourly_Sorter_Summary").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Inducted (#)", "Sorted (#)", "Awcs Recirc (#)", "Operational Recirc (#)", "Machine Recirc (#)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (#)'], result['Sorted (#)'], result['Awcs Recirc (#)'], result['Operational Recirc (#)'], result['Machine Recirc (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Inducted (%)", "Sorted (%)", "Awcs Recirc (%)", "Operational Recirc (%)", "Machine Recirc (%)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (%)'], result['Sorted (%)'], result['Awcs Recirc (%)'], result['Operational Recirc (%)'], result['Machine Recirc (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Inducted (pph)", "Sorted (pph)", "Awcs Recirc (pph)", "Operational Recirc (pph)", "Machine Recirc (pph)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (pph)'], result['Sorted (pph)'], result['Awcs Recirc (pph)'], result['Operational Recirc (pph)'], result['Machine Recirc (pph)']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Hourly_Sorter_Summary").getChild("Hourly_Sorter_Summary").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Inducted (#)", "Sorted (#)", "Awcs Recirc (#)", "Operational Recirc (#)", "Machine Recirc (#)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (#)'], result['Sorted (#)'], result['Awcs Recirc (#)'], result['Operational Recirc (#)'], result['Machine Recirc (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Inducted (%)", "Sorted (%)", "Awcs Recirc (%)", "Operational Recirc (%)", "Machine Recirc (%)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (%)'], result['Sorted (%)'], result['Awcs Recirc (%)'], result['Operational Recirc (%)'], result['Machine Recirc (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Inducted (pph)", "Sorted (pph)", "Awcs Recirc (pph)", "Operational Recirc (pph)", "Machine Recirc (pph)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (pph)'], result['Sorted (pph)'], result['Awcs Recirc (pph)'], result['Operational Recirc (pph)'], result['Machine Recirc (pph)']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Hourly Sorter Summary.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 8:
	# HOURLY SORTER DETAILS - No changes needed
	results = self.parent.getChild("Statistics").getChild("Hourly_Sorter_Details").getChild("Hourly_Sorter_Details").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Inducted (#)", "Sorted (#)", "Dest Inv (#)", "Dest None (#)", "Dest Dis (#)", "Dest Full (#)", "Unexpected (#)", "Dest Fault (#)", "Div Fail (#)", "Gap Err (#)", "Lost (#)", "Track Err (#)", "Unknown (#)", "Unsafe (#)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (#)'], result['Sorted (#)'], result['Dest Inv (#)'], result['Dest None (#)'], result['Dest Dis (#)'], result['Dest Full (#)'], result['Unexpected (#)'], result['Dest Fault (#)'], result['Div Fail (#)'], result['Gap Err (#)'], result['Lost (#)'], result['Track Err (#)'], result['Unknown (#)'], result['Unsafe (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Inducted (%)", "Sorted (%)", "Dest Inv (%)", "Dest None (%)", "Dest Dis (%)", "Dest Full (%)", "Unexpected (%)", "Dest Fault (%)", "Div Fail (%)", "Gap Err (%)", "Lost (%)", "Track Err (%)", "Unknown (%)", "Unsafe (%)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (%)'], result['Sorted (%)'], result['Dest Inv (%)'], result['Dest None (%)'], result['Dest Dis (%)'], result['Dest Full (%)'], result['Unexpected (%)'], result['Dest Fault (%)'], result['Div Fail (%)'], result['Gap Err (%)'], result['Lost (%)'], result['Track Err (%)'], result['Unknown (%)'], result['Unsafe (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Inducted (pph)", "Sorted (pph)", "Dest Inv (pph)", "Dest None (pph)", "Dest Dis (pph)", "Dest Full (pph)", "Unexpected (pph)", "Dest Fault (pph)", "Div Fail (pph)", "Gap Err (pph)", "Lost (pph)", "Track Err (pph)", "Unknown (pph)", "Unsafe (pph)"]
			for result in results:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (pph)'], result['Sorted (pph)'], result['Dest Inv (pph)'], result['Dest None (pph)'], result['Dest Dis (pph)'], result['Dest Full (pph)'], result['Unexpected (pph)'], result['Dest Fault (pph)'], result['Div Fail (pph)'], result['Gap Err (pph)'], result['Lost (pph)'], result['Track Err (pph)'], result['Unknown (pph)'], result['Unsafe (pph)']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Hourly_Sorter_Details").getChild("Hourly_Sorter_Details").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Inducted (#)", "Sorted (#)", "Dest Inv (#)", "Dest None (#)", "Dest Dis (#)", "Dest Full (#)", "Unexpected (#)", "Dest Fault (#)", "Div Fail (#)", "Gap Err (#)", "Lost (#)", "Track Err (#)", "Unknown (#)", "Unsafe (#)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (#)'], result['Sorted (#)'], result['Dest Inv (#)'], result['Dest None (#)'], result['Dest Dis (#)'], result['Dest Full (#)'], result['Unexpected (#)'], result['Dest Fault (#)'], result['Div Fail (#)'], result['Gap Err (#)'], result['Lost (#)'], result['Track Err (#)'], result['Unknown (#)'], result['Unsafe (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Inducted (%)", "Sorted (%)", "Dest Inv (%)", "Dest None (%)", "Dest Dis (%)", "Dest Full (%)", "Unexpected (%)", "Dest Fault (%)", "Div Fail (%)", "Gap Err (%)", "Lost (%)", "Track Err (%)", "Unknown (%)", "Unsafe (%)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (%)'], result['Sorted (%)'], result['Dest Inv (%)'], result['Dest None (%)'], result['Dest Dis (%)'], result['Dest Full (%)'], result['Unexpected (%)'], result['Dest Fault (%)'], result['Div Fail (%)'], result['Gap Err (%)'], result['Lost (%)'], result['Track Err (%)'], result['Unknown (%)'], result['Unsafe (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Inducted (pph)", "Sorted (pph)", "Dest Inv (pph)", "Dest None (pph)", "Dest Dis (pph)", "Dest Full (pph)", "Unexpected (pph)", "Dest Fault (pph)", "Div Fail (pph)", "Gap Err (pph)", "Lost (pph)", "Track Err (pph)", "Unknown (pph)", "Unsafe (pph)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Inducted (pph)'], result['Sorted (pph)'], result['Dest Inv (pph)'], result['Dest None (pph)'], result['Dest Dis (pph)'], result['Dest Full (pph)'], result['Unexpected (pph)'], result['Dest Fault (pph)'], result['Div Fail (pph)'], result['Gap Err (pph)'], result['Lost (pph)'], result['Track Err (pph)'], result['Unknown (pph)'], result['Unsafe (pph)']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Hourly Sorter Details.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 9:
	# HOURLY LANE - FIXED: wrong field names in filtered results
	# NOTE: This tab seems to have data structure issues - unfiltered works but filtered may need verification
	results = self.parent.getChild("Statistics").getChild("Hourly_Lane").getChild("Hourly_Lane").props.filter.results.data
	if(len(results) > 0):
		# Using unfiltered data structure for filtered results (may need adjustment based on actual query)
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Hourly_Lane").getChild("Hourly_Lane").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Total (#)", "Diverted (#)", "Dest Full (#)", "Dest Jam (#)", "Dest Disabled (#)", "Dest Fault (#)", "Divert Fail (#)", "Lost (#)", "Unsafe (#)", "Dim Err (#)", "Gap Error (#)", "Unknown (#)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (#)'], result['Diverted (#)'], result['Dest Full (#)'], result['Dest Jam (#)'], result['Dest Disabled (#)'], result['Dest Fault (#)'], result['Divert Fail (#)'], result['Lost (#)'], result['Unsafe (#)'], result['Dim Err (#)'], result['Gap Err (#)'], result['Unknown (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Total (%)", "Diverted (%)", "Dest Full (%)", "Dest Jam (%)", "Dest Disabled (%)", "Dest Fault (%)", "Divert Fail (%)", "Lost (%)", "Unsafe (%)", "Dim Err (%)", "Gap Error (%)", "Unknown (%)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (%)'], result['Diverted (%)'], result['Dest Full (%)'], result['Dest Jam (%)'], result['Dest Disabled (%)'], result['Dest Fault (%)'], result['Divert Fail (%)'], result['Lost (%)'], result['Unsafe (%)'], result['Dim Err (%)'], result['Gap Err (%)'], result['Unknown (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Total (pph)", "Diverted (pph)", "Dest Full (pph)", "Dest Jam (pph)", "Dest Disabled (pph)", "Dest Fault (pph)", "Divert Fail (pph)", "Lost (pph)", "Unsafe (pph)", "Dim Err (pph)", "Gap Error (pph)", "Unknown (pph)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (pph)'], result['Diverted (pph)'], result['Dest Full (pph)'], result['Dest Jam (pph)'], result['Dest Disabled (pph)'], result['Dest Fault (pph)'], result['Divert Fail (pph)'], result['Lost (pph)'], result['Unsafe (pph)'], result['Dim Err (pph)'], result['Gap Err (pph)'], result['Unknown (pph)']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Hourly_Lane").getChild("Hourly_Lane").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Total (#)", "Diverted (#)", "Dest Full (#)", "Dest Jam (#)", "Dest Disabled (#)", "Dest Fault (#)", "Divert Fail (#)", "Lost (#)", "Unsafe (#)", "Dim Err (#)", "Gap Error (#)", "Unknown (#)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (#)'], result['Diverted (#)'], result['Dest Full (#)'], result['Dest Jam (#)'], result['Dest Disabled (#)'], result['Dest Fault (#)'], result['Divert Fail (#)'], result['Lost (#)'], result['Unsafe (#)'], result['Dim Err (#)'], result['Gap Err (#)'], result['Unknown (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Total (%)", "Diverted (%)", "Dest Full (%)", "Dest Jam (%)", "Dest Disabled (%)", "Dest Fault (%)", "Divert Fail (%)", "Lost (%)", "Unsafe (%)", "Dim Err (%)", "Gap Error (%)", "Unknown (%)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (%)'], result['Diverted (%)'], result['Dest Full (%)'], result['Dest Jam (%)'], result['Dest Disabled (%)'], result['Dest Fault (%)'], result['Divert Fail (%)'], result['Lost (%)'], result['Unsafe (%)'], result['Dim Err (%)'], result['Gap Err (%)'], result['Unknown (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Total (pph)", "Diverted (pph)", "Dest Full (pph)", "Dest Jam (pph)", "Dest Disabled (pph)", "Dest Fault (pph)", "Divert Fail (pph)", "Lost (pph)", "Unsafe (pph)", "Dim Err (pph)", "Gap Error (pph)", "Unknown (pph)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Total (pph)'], result['Diverted (pph)'], result['Dest Full (pph)'], result['Dest Jam (pph)'], result['Dest Disabled (pph)'], result['Dest Fault (pph)'], result['Divert Fail (pph)'], result['Lost (pph)'], result['Unsafe (pph)'], result['Dim Err (pph)'], result['Gap Err (pph)'], result['Unknown (pph)']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Hourly Lane.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 10:
	# LANE TOTAL FULL - COMPLETELY REWRITTEN
	results = self.parent.getChild("Statistics").getChild("Total Full1").getChild("Total Full1").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Lane", "Total (#)", "DestFull (#)"]
			for result in results:
				data.append([result['Lane'], result['Total_count'], result['DestFull_count']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Lane", "Total (%)", "DestFull (%)"]
			for result in results:
				data.append([result['Lane'], result['Total_perc'], result['DestFull_perc']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Lane", "Total (pph)", "DestFull (pph)"]
			for result in results:
				data.append([result['Lane'], result['Total_rate'], result['DestFull_rate']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Total Full1").getChild("Total Full1").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Lane", "Total (#)", "DestFull (#)"]
			for result in test_table:
				data.append([result['Lane'], result['Total (#)'], result['DestFull (#)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Lane", "Total (%)", "DestFull (%)"]
			for result in test_table:
				data.append([result['Lane'], result['Total (%)'], result['DestFull (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Lane", "Total (pph)", "DestFull (pph)"]
			for result in test_table:
				data.append([result['Lane'], result['Total (pph)'], result['DestFull (pph)']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Lane Total Full.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 11:
	# JAM AREA - FIXED: case sensitivity on Total_jam
	results = self.parent.getChild("Statistics").getChild("Jam Area").getChild("Jam Area").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Stamp", "End Stamp", "Total Jam (#)", "Dumpers Jam (#)", "Inbound Jam (#)", "Runout Jam (#)", "Sorter Recirc Jam (#)"]
			for result in results:
				# FIXED: Total_Jam -> Total_jam (lowercase 'j')
				data.append([result['Startstamp'], result['Endtstamp'], result['Total_jam'], result['Dumpers_jam'], result['Inbound_jam'], result['Runout_jam'], result['Sorter_Recirc_jam']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Stamp", "End Stamp", "Total Jam (%)", "Dumpers Jam (%)", "Inbound Jam (%)", "Runout Jam (%)", "Sorter Recirc Jam (%)"]
			for result in results:
				data.append([result['Startstamp'], result['Endtstamp'], result['Total_perc'], result['Dumpers_perc'], result['Inbound_perc'], result['Runout_perc'], result['Sorter_Recirc_perc']])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Jam Area").getChild("Jam Area").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Stamp", "End Stamp", "Total Jam (#)", "Dumpers Jam (#)", "Inbound Jam (#)", "Runout Jam (#)", "Sorter Recirc Jam (#)"]
			for result in test_table:
				# FIXED: Total_Jam -> Total_jam (lowercase 'j')
				data.append([result['Startstamp'], result['Endtstamp'], result['Total_jam'], result['Dumpers_jam'], result['Inbound_jam'], result['Runout_jam'], result['Sorter_Recirc_jam']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Stamp", "End Stamp", "Total Jam (%)", "Dumpers Jam (%)", "Inbound Jam (%)", "Runout Jam (%)", "Sorter Recirc Jam (%)"]
			for result in test_table:
				data.append([result['Startstamp'], result['Endtstamp'], result['Total_perc'], result['Dumpers_perc'], result['Inbound_perc'], result['Runout_perc'], result['Sorter_Recirc_perc']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Jam Areas.csv', data=csv)


elif self.getSibling("Statistics").props.currentTabIndex == 12:
	# DUMPER CYCLES - NEEDS VERIFICATION
	# NOTE: This section needs to be verified against actual query output
	# The field names (ulc1-ulc8) and headers need confirmation
	results = self.parent.getChild("Statistics").getChild("Dumper cycles").getChild("Dumper cycles").props.filter.results.data
	if(len(results) > 0):
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Cycles of ULC1", "Cycles of ULC2", "Cycles of ULC3", "Cycles of ULC4", "Cycles of ULC5", "Cycles of ULC6", "Cycles of ULC7", "Cycles of ULC8"]
			for result in results:
				# NOTE: Field names need verification - may be 'ulc1', 'ulc2', etc. or 'Cycles of ULC1', etc.
				data.append([result.get('Start Timestamp', ''), result.get('Hour', ''), 
							result.get('Cycles of ULC1', result.get('ulc1', 0)),
							result.get('Cycles of ULC2', result.get('ulc2', 0)),
							result.get('Cycles of ULC3', result.get('ulc3', 0)),
							result.get('Cycles of ULC4', result.get('ulc4', 0)),
							result.get('Cycles of ULC5', result.get('ulc5', 0)),
							result.get('Cycles of ULC6', result.get('ulc6', 0)),
							result.get('Cycles of ULC7', result.get('ulc7', 0)),
							result.get('Cycles of ULC8', result.get('ulc8', 0))])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Cycles of ULC1 (%)", "Cycles of ULC2 (%)", "Cycles of ULC3 (%)", "Cycles of ULC4 (%)", "Cycles of ULC5 (%)", "Cycles of ULC6 (%)", "Cycles of ULC7 (%)", "Cycles of ULC8 (%)"]
			for result in results:
				data.append([result.get('Start Timestamp', ''), result.get('Hour', ''),
							result.get('Cycles of ULC1 (%)', 0),
							result.get('Cycles of ULC2 (%)', 0),
							result.get('Cycles of ULC3 (%)', 0),
							result.get('Cycles of ULC4 (%)', 0),
							result.get('Cycles of ULC5 (%)', 0),
							result.get('Cycles of ULC6 (%)', 0),
							result.get('Cycles of ULC7 (%)', 0),
							result.get('Cycles of ULC8 (%)', 0)])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Cycles of ULC1 (cph)", "Cycles of ULC2 (cph)", "Cycles of ULC3 (cph)", "Cycles of ULC4 (cph)", "Cycles of ULC5 (cph)", "Cycles of ULC6 (cph)", "Cycles of ULC7 (cph)", "Cycles of ULC8 (cph)"]
			for result in results:
				data.append([result.get('Start Timestamp', ''), result.get('Hour', ''),
							result.get('Cycles of ULC1 (cph)', 0),
							result.get('Cycles of ULC2 (cph)', 0),
							result.get('Cycles of ULC3 (cph)', 0),
							result.get('Cycles of ULC4 (cph)', 0),
							result.get('Cycles of ULC5 (cph)', 0),
							result.get('Cycles of ULC6 (cph)', 0),
							result.get('Cycles of ULC7 (cph)', 0),
							result.get('Cycles of ULC8 (cph)', 0)])
	else:
		test_table = system.dataset.toPyDataSet(self.parent.getChild("Statistics").getChild("Dumper cycles").getChild("Dumper cycles").props.data)
		
		if self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Count":
			headers = ["Start Timestamp", "Hour", "Cycles of ULC1", "Cycles of ULC2", "Cycles of ULC3", "Cycles of ULC4", "Cycles of ULC5", "Cycles of ULC6", "Cycles of ULC7", "Cycles of ULC8"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Cycles of ULC1'], result['Cycles of ULC2'], result['Cycles of ULC3'], result['Cycles of ULC4'], result['Cycles of ULC5'], result['Cycles of ULC6'], result['Cycles of ULC7'], result['Cycles of ULC8']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Percentage":
			headers = ["Start Timestamp", "Hour", "Cycles of ULC1 (%)", "Cycles of ULC2 (%)", "Cycles of ULC3 (%)", "Cycles of ULC4 (%)", "Cycles of ULC5 (%)", "Cycles of ULC6 (%)", "Cycles of ULC7 (%)", "Cycles of ULC8 (%)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Cycles of ULC1 (%)'], result['Cycles of ULC2 (%)'], result['Cycles of ULC3 (%)'], result['Cycles of ULC4 (%)'], result['Cycles of ULC5 (%)'], result['Cycles of ULC6 (%)'], result['Cycles of ULC7 (%)'], result['Cycles of ULC8 (%)']])
		elif self.parent.getChild("Aggregation_Mode").getChild("Dropdown_Aggregation_mode").props.value == "Rate":
			headers = ["Start Timestamp", "Hour", "Cycles of ULC1 (cph)", "Cycles of ULC2 (cph)", "Cycles of ULC3 (cph)", "Cycles of ULC4 (cph)", "Cycles of ULC5 (cph)", "Cycles of ULC6 (cph)", "Cycles of ULC7 (cph)", "Cycles of ULC8 (cph)"]
			for result in test_table:
				data.append([result['Start Timestamp'], result['Hour'], result['Cycles of ULC1 (cph)'], result['Cycles of ULC2 (cph)'], result['Cycles of ULC3 (cph)'], result['Cycles of ULC4 (cph)'], result['Cycles of ULC5 (cph)'], result['Cycles of ULC6 (cph)'], result['Cycles of ULC7 (cph)'], result['Cycles of ULC8 (cph)']])
	
	table = system.dataset.toDataSet(headers, data)
	csv = system.dataset.toCSV(table, 1, 0, 1)
	system.perspective.download(filename=str_date + ' Dumper cycles.csv', data=csv)

