<?php

include ("dbset.php");
$today = date("Y-m-d");
$username = $_GET['user_name_text'];
//$password = $_POST['password_text'];

// Include database connection settings
//include("db_connect.php");

$con = @mysql_connect("localhost",$dbu,$dbp);
if (!$con)
  {
  die('Could not connect to database: ' . mysql_error());
  }
   
mysql_select_db($dbs, $con);

function execsql($qry){
	$result = mysql_query($qry);
	return $result;
}

$login_query = "SELECT username FROM system_users WHERE username = '" . $username . "' ";

$login_result = mySQL_query($login_query);

$num = mysql_num_rows($login_result);
//echo "num:".$num;
// Check username and password match
if ($num == 1) {
// Inialize session
session_start();	
// Set username session variable

$access = "SELECT access_id, home_page FROM tlh_users WHERE (username = '" .$username. "') ";
//echo $access;
$accessResult = mySQL_query($access);
$_SESSION['access_id'] = mysql_result($accessResult, 0, "access_id");
$home_page = mysql_result($accessResult, 0, "home_page");
$_SESSION['username'] = $_GET['user_name_text'];
//echo "home:".$home_page;
date_default_timezone_set('Africa/Khartoum');
$entry_date = date("Y-m-d H:i:s",time()-3600); 

$active_query = "INSERT INTO tlh_active_users (username, log_in) VALUES ('".$_SESSION['username']."', '".$entry_date."' ) ";
execsql($active_query);


$table_name = "tlh_tracking_log_in";
$action_query = "INSERT INTO $table_name (entry_date, user, action) VALUES ('".$entry_date."' ,'".$_SESSION['username']."','log in' )";
execsql($action_query);



$login_check_query = "SELECT log_in_check_id FROM tlh_login_check WHERE log_in_date = '".$today."' ";
//echo $login_check_query;
$login_check_result = mySQL_query($login_check_query);
$login_check_num = mysql_num_rows($login_check_result);
if ($login_check_num == 0 ) {
	//echo 'make report';
//include('daily_report_sales.php');
}


if ($home_page=="hr") {$location = "hr_module/hr_main.php?date="; }
if ($home_page=="production") {$location = "production/production_main.php?"; }
if ($home_page=="stores") {$location = "store/store_main.php?"; }
if ($home_page=="sales") {$location = "sales/sales_main.php?date="; }
if ($home_page=="admin") {$location = "admin_module/user_list.php";  }
if ($home_page=="") {$location = "../mainmenu.php?log=1&e=2&cx=".$_SESSION['access_id']; }
$_SESSION['location'] = $location;


// Jump to secured page
 header('Location: '."../".$location );
}
else {
//Jump to login page
 ?>
  <script language="javascript" type="text/javascript">
        window.location="../mainmenu.php?log=1&e=0";
</script>
<?php
}

?>