<?php
	//Login System
	include ('/var/www/html/uclit/protected/common/login/check.php');
	include('/var/www/html/uclit/protected/connections/db_arlstats.php');

	//USE ARLSTATS DB
	$db = mysql_select_db('arlstats', $arlConn) or die ("Unable to select arlstats database.");

	$loggedinuser = $_SESSION['username'];
	/*
	//Material Types
		$sql_material = "SELECT MID, Name FROM Material_Types ORDER BY Name";
		$material_result = @mysql_query($sql_material, $sqlConn) or die ("Unable to query: $sql_material");
	//Calendar
		$sql_calendar = "SELECT * FROM Calendar ORDER BY Month";
		$calendar_result = @mysql_query($sql_calendar, $sqlConn) or die ("Unable to query: $sql_calendar");
	
	*/
	
	//Locations
	function location_query()
	{
		global $arlConn;
		global $location_result;
		$sql_location = "SELECT * from Location";
		$location_result = @mysql_query($sql_location, $arlConn) or die ("unable to query: $sql_location");
	}
	location_query();
	// Catalog Types
	function catalog_query() 
	{
		global $arlConn;
		global $catalog_result;
		$sql_location = "SELECT * from Catalog_Types";
		$catalog_result = @mysql_query($sql_location, $arlConn) or die ("unable to query: $sql_location");
    }
	catalog_query();

	// LOGIN UID RETRIEVAL
	//USE LOGIN DB
	//$db = mysql_select_db('login', $loginConn) or die ("Unable to select login database.");
	//$login_sql = "select personnel_id from users where username = '$loggedinuser'";
	//echo $login_sql;
	//$login_result = @mysql_query($login_sql, $loginConn) or die ("unable to query: $login_sql");
	//$login_row = @mysql_fetch_array($login_result);
    //$employee = $login_row['personnel_id'];

	//USE ARLSTATS DB
	$db = mysql_select_db('arlstats', $arlConn) or die ("Unable to select arlstats database.");


	// INSERT DATA INTO TABLE
	//outer loop for each row of the table
	while($row = @mysql_fetch_array($location_result))
	{
		$lid = $row['LID'];
	  	$location_name = $row['Name'];
	  	$location_description = $row['Description'];
	 	//echo $location_name;
	  	//inner loop for each column of the row
	  	while($row2 = @mysql_fetch_array($catalog_result))
	   	{
			//echo "boola";
			$cid=$row2['CID'];
			$input=$location_name;
			$input.=$cid;
			$data = $_POST[$location_name.$cid];
			$employee = $_POST['employee'];
			$month = $_POST['month'];
			$material = $_POST['material'];
			$stype = $_POST['stype'];
			//echo "cid = $cid, input = $input, data = $data, month = $month, material = $material, stype = $stype<br />";
			//SQL Statement
			$save_query = "INSERT into Current_Stats (Stat_ID, Date, LID, MID, CID, UID, QID, Value, SID) 
			VALUES ('',now(),'$lid','$material','$cid','$employee','$month','$data','$stype')";
			//	echo "$save_query <br>";
			if ($data > 0) 
				$save_result = @mysql_query($save_query, $arlConn) or die ("UNABLE TO SAVE DATA, Please Contact Libraries IT");
	  	}
		catalog_query();
	}
	header("Location: menu_entry.php?status=saved");

	//include('vars.php');
?>