<?php

//include "common/top_admin_before.php";
include "common/config.php";
include "common/function.php";

$username = val_post('username', 'text');
$password = md5(val_post('password', 'text'));
$statuslogin = val_post('status', 'int');

//echo $username."<br><br>";
//echo $password."<br><br>";
//exit; 

$query="SELECT * FROM tbl_admin WHERE user_id='$username' AND status='$statuslogin'";
$result=mysql_query($query);
$data=mysql_fetch_array($result);
$USERNAME=$data["user_id"];
$PASSWORD=$data["password"];
//$NAMA=$data["nama"];
$STATUS=$data["status"];
$id_branch=$data["id_branch"];

$time1 = mysql_fetch_array(mysql_query("select * from tbl_workingtime where id_branch = '$id_branch'"));
$timestart = $time1['timestart'].':00:00';
$timestart = strtotime($timestart);   
$timefinish = $time1['timefinish'].":00:00";
$timefinish = strtotime($timefinish); 
$timenow = date('H:i:s'); 
$timenow = strtotime($timenow); 

//exit;

//echo date('H:i:s')."<br><br>";
//echo $timenow."<br>".$timestart."<br>".$timefinish; exit;

		if (($username==$USERNAME)&&($password==$PASSWORD))
		{
			if(($timenow >= $timestart) && ($timenow <= $timefinish))
			{
				session_name("AUTHENUSER");
				session_start();
				$_SESSION['SES_USERNAME']=$USERNAME;
				$_SESSION['SES_PASSWORD']=$PASSWORD;
				  //$_SESSION['SES_NAMA']=$NAMA;
				$_SESSION['SES_STATUS']=$STATUS;
		
		
		  $query = 'UPDATE tbl_admin SET `lastlogin`=\''.date('Y-m-d H:i:s').'\' WHERE user_id=\''.$username.'\' ';
		  $result = mysql_query($query);
			//echo $query; exit();
		  ?>
				<center>
				<br /><Br /><Br />
		  <table width="800px" height="300px" cellpadding="10px" cellspacing="1px" border="0">
			<tr>
				<td align="center" valign="middle" width="100%"><big>SIGN IN SUCCESFULLY</big></td>
			</tr>
		  </table>
		  <script language="JavaScript">
			var start=new Date();
			start=Date.parse(start)/1000;
			var counts=4;
			function CountDown()
			{
			  var now=new Date();
			  now=Date.parse(now)/1000;
			  var x=parseInt(counts-(now-start), 10);
			  if(x>0)
			  {
				timerID=setTimeout("CountDown()", 100)
			  }
			  else
			  {
				document.location="dashboard.php"; //ini nama file yang dituju oleh admin
			  }
			}
			window.setTimeout('CountDown()', 20);
			//  End -->
		  </script>
		  <?php
		
			}
			else
			{
				?>
				  <script language="javascript">
				  alert("Sorry, it's not working hour.")
					document.location="index.php";
				  </script>
				  <?php
			}
		
		}
		else
		{
		  ?>
		  <script language="javascript">
			document.location="index.php?err=1";
		  </script>
		  <?php
		}

mysql_close();
?>

