<?php

  
include ("dbset.php");

$username = $_POST['username'];
$password = $_POST['password'];

$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;
}

//echo $password;


$login_query = "SELECT username FROM man_users WHERE (username = '" . $username . "') and (password = '".$password."')";
//echo $login_query;
$login_result = mySQL_query($login_query);
$num = mysql_num_rows($login_result);
//echo $num;
// Check username and password match
if ($num == 1) {
// Inialize session
session_start();  
// Set username session variable
$_SESSION['db_prefix']="man";
include('log_in_check.php');
$_SESSION['mail_num']= 0;

$access = "SELECT access_id, home_page FROM man_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'] = $_POST['username'];

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 


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


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


if ($home_page=="hr") {$location = "hr_module/hr_main.php?date="; }
if ($home_page=="sales") {$location = "sales/sales_main.php?date="; }
if ($home_page=="orders") {$location = "orders/orders_main.php"; }
if ($home_page=="maintenance") {$location = "maintenance/maintenance_main.php"; }
if ($home_page=="stores") {$location = "store/store_main.php"; }
if ($home_page=="production") {$location = "production/production_main.php"; }
if ($home_page=="transport") {$location = "transport/transport_main.php?date="; }
if ($home_page=="admin") {$location = "admin_module/user_list.php"; }
if ($home_page=="") {$location = "login.php?e=2"; }
$_SESSION['location'] = $location;


//$today = date('Y-m-d');
//$check_query = "SELECT check_id FROM man_system_check WHERE check_date_1 = '".$today."' ";
//$check_result = mySQL_query($check_query);
//$check_num = mysql_num_rows($check_result);

//if ($check_num==0) {
//   
//   include ("../include/reminder_check.php");
//   
//   
//   $table_name = $_SESSION['db_prefix']."_system_check";
//     $system_query = "INSERT INTO $table_name (check_date_1) VALUES ('".$today."' ) ";
//     execsql($system_query);
//}

// Jump to secured page
    header('Location: '.'../'.$location );
}
else {
//Jump to login page
   header('Location: ../login.php?e=1');
}

?>