<?php
include('init.php');
include('libs/functions.php');

if (checklogin($userID, "menu.php") && $status == 1) {

	$p = 1;
if (isset($_REQUEST['p'])) {
	$p = scrub_white_list($_REQUEST['p'], 'NUMBERONLY');
	if(!is_numeric($p)) {
		$p = 1;
	}
} else {$p = 1;}

print_header();

$sql = "SELECT account_type FROM tb_user WHERE user_id=$userID";
$rs = $db->Execute($sql);
$account_type = $rs->fields('account_type');

?>

<style type="text/css">
.auto-style1 {
	
	border: 1px solid #999999;
	border-style: inset;
	border-color: gray;
	background-color: white;
	-moz-border-radius: ;
}
</style>

<style type="text/css">
#manage {
	width:755px;
	border-width: 1px;
	border-spacing: ;
	border-style: outset;
	border-color: gray;
	border-collapse: separate;
	background-color: white;
}
#manage th {
	border-width: 1px;
	padding: 1px;
	border-style: inset;
	border-color: gray;
	background-color: white;
	-moz-border-radius: ;
}
#manage td {
	border-width: 1px;
	padding: 1px;
	border-style: inset;
	border-color: gray;
	background-color: white;
	-moz-border-radius: ;
}
</style>

<H1 align=center>Upload History</H1>
<CENTER><A HREF="apps/index.php">Return to Main Menu</A></CENTER>
<HR>

<? if ($account_type == 'CF') { ?>
<center>
	<a href="upload_history.php">Federal & State Uploads</a> | <b>County & Municipal Uploads</b>
</center>
<br>

<? } ?>
<TABLE ALIGN="CENTER">



</TD></TR>
</TABLE>

<TABLE ALIGN="CENTER" NAME="manage" id="manage" cellspacing="0" class="auto-style1" style="width: 100%; float: left">
<TR>
	<TH class='auto-style1'>File ID</TH>
	<!-- <TH class='auto-style1'>Uploader</TH> -->
	<?if ($account_type == 'CF') { ?>
	<TH class='auto-style1'>Correctional Facility</TH>
	<TH class='auto-style1'>Correctional System Type</TH>
<?} else if ($account_type == 'CS') { ?>
	<TH class='auto-style1' align='center'>Court System Name </TH>
	<TH class='auto-style1' align='center'>Court System Type</TH>
<?}?>
	<TH class='auto-style1'>File Name</TH>
	<TH class='auto-style1'>Upload Date (EST)</TH>
	<TH class='auto-style1'>File Status</TH>
	<TH class='auto-style1'>Records Processed</TH>
</TR>

<?

$results = 0;
$start = 1+($p-1)*20;
$finish = $start+19;

if ($status == 1) {
	$where_sql = 'hf.user_id = ' . $userID . ' and ';
} else if ($status == 2) {
	$where_sql = '';
}


if ($account_type == 'CF') {
$sql = 'Select id, file_id, username, cs_type, correctional_name, dataset_type, dataset_version, name, upload_date, status, num_data
From (
		Select hf.file_id, hf.user_id, hf.id, hf.num_data, tu.username, sg.cs_type, sp.correctional_name, di.dataset_type, sc.status, di.dataset_version, hf.name, hf.upload_date, ROW_NUMBER() OVER(ORDER BY hf.upload_date desc) [rowNumber]
		From APP_Files hf, tb_cf_system sp, tb_correctional_system_type sg, tb_dataset_info di, tb_user tu, tb_file_status_codes sc
		Where ' . $where_sql . ' hf.user_id = sp.user_id and hf.cs_id = 0 and sp.cs_type = sg.cs_type_id and hf.dataset_id = di.dataset_id and hf.user_id = tu.user_id and hf.dataset_id != 0 and hf.status = sc.id
	)q
		
	WHERE rowNumber BETWEEN ' . $start . ' and ' . $finish . '
	';

//echo $sql;
$rs = $db->Execute($sql);

while (!$rs->EOF) { 	
	$data_id = $rs->fields('id');
	$file_id = $rs->fields('file_id');
	//$user_id = $rs->fields('user_id');
	//$user_name = $rs->fields('username');
	$correctional_type = $rs->fields('cs_type');
	$correctional_name = $rs->fields('correctional_name');
	$file_name = $rs->fields('name');
	$dataset_type = $rs->fields('dataset_type');
	$dataset_version = $rs->fields('dataset_version');
	$upload_date = $rs->fields('upload_date');
	$file_status = $rs->fields('status');
	$newDate = date("m/d/Y  g:i:s  a", strtotime($upload_date));
	$record_process = $rs->fields('num_data');
		
	print ("
		<tr>
			<td class='auto-style1'>$data_id</td>
			<td class='auto-style1'>$correctional_name</td>
			<td class='auto-style1'>$correctional_type</td>
			<td class='auto-style1'>$file_name</td>
			<td class='auto-style1'>$newDate</td>
			<td class='auto-style1'>$file_status</td>
			<td class='auto-style1'>$record_process</td>			
		</tr>
	");
	
	$results = $results + 1;    
	$rs->MoveNext();
  }

 
 } else if ($account_type == 'CS') {

$sql = 'Select id, file_id, username, court_type, court_name, dataset_type, dataset_version, name, upload_date, status, num_data
From (
		Select hf.file_id, hf.user_id, hf.id, hf.num_data, tu.username, sg.court_type, sp.court_name, di.dataset_type, sc.status, di.dataset_version, hf.name, hf.upload_date, ROW_NUMBER() OVER(ORDER BY hf.upload_date desc) [rowNumber]
		From APP_Files hf, tb_court_system sp, tb_court_system_type sg, tb_dataset_info di, tb_user tu, tb_file_status_codes sc
		Where ' . $where_sql . ' sg.court_type_id = sp.court_type_id and hf.dataset_id = di.dataset_id and hf.user_id = tu.user_id and hf.dataset_id != 0 and hf.status = sc.id and tu.user_id = sp.user_id
	)q
		
	WHERE rowNumber BETWEEN ' . $start . ' and ' . $finish . '
	';

$rs = $db->Execute($sql);

while (!$rs->EOF) { 	
	$data_id = $rs->fields('id');
	$file_id = $rs->fields('file_id');
	//$correctional_type = $rs->fields('cs_type');
	//$correctional_system = $rs->fields('correctional_system');
	$court_name = $rs->fields('court_name');
	$court_system = $rs->fields('court_type');
	
	$file_name = $rs->fields('name');
	$dataset_type = $rs->fields('dataset_type');
	$dataset_version = $rs->fields('dataset_version');
	$upload_date = $rs->fields('upload_date');
	$file_status = $rs->fields('status');
	$newDate = date("m/d/Y  g:s  a", strtotime($upload_date));
	$record_process = $rs->fields('num_data');
 	
	print ("
		<tr>
			<td class='auto-style1'>$data_id</td>
			<td class='auto-style1'>$court_name</td>
			<td class='auto-style1'>$court_system</td>
			<td class='auto-style1'>$file_name</td>
			<td class='auto-style1'>$newDate</td>
			<td class='auto-style1'>$file_status</td>
			<td class='auto-style1'>$record_process</td>			
		</tr>
	");
	
	$results = $results + 1;    
	$rs->MoveNext();
  }

}
  
?>

</TABLE>
<p><b>*** Note: The File ID is randomly generated in the system to help in identifying the file in the database. ***</b></p>

<?
	print '<center>';
	if ($p > 1) {
		$lastp=$p-1;
		print "<a href=upload_history.php?p=$lastp><< Previous Page</a> ";
	}

	if (20 == $results) {
		$nextp=$p+1;
		print "<a href=upload_history.php?p=$nextp> Next Page >></a>";
	}
	//print '</center> <br><br><CENTER><A HREF="menu.php">Return to Main Menu</A></CENTER>';

	print_footer();

} // end check login

?>