<?php
	session_start();
	
	ini_set('session.gc_maxlifetime', 99999999); //设置时间 永不过期
	
	require_once("db/db.php");	
	require_once("common/common.php");
	
	$chainno="";
	$userno=$_REQUEST["userno"];
	$pswd=$_REQUEST["pwd"];
	$pswdnew=getpwd($pswd);
	$userno=strtoupper($userno);
	$_SESSION["main_defusernoflag"]="0";
	
	if($userno=="")
	{
		echo "<script>window.location.href='login.php?stext=';</script>";
		exit();
	}
		
	//开始搜索数据库是否可以登录
	$sql = sprintf("select [dbo].[TRF_AppUserLoginGL](GETDATE(),'%s','%s') as res;", $userno, $pswdnew);
	$result = querySql($sql);
	$row = fetch_array($result);
	if($row['res'] == 1)
	{
		//登录成功
		$_SESSION['ChainCount'] ="1";
		$_SESSION['ChainNo'] = "";  
		$_SESSION['ChainName'] = "";  
		$_SESSION['userno'] = $userno;  
		$_SESSION['username'] = "";  
		$_SESSION['pswd'] = $pswd;
		$_SESSION['logintime'] = time();
		$_SESSION['loginflag'] ="1";
		
		$_SESSION['hotelday'] ="";
		$_SESSION['updatetime'] ="";
		$_SESSION['updatestate'] ="";
		
		$_SESSION["main_defusernoflag"]="1";
		$_SESSION["main_defuserno"]=$userno;
		
		freeResult($result);
		
		//得到用户名称
		$sql = sprintf("Select Use_Name from User_QuanXian Where Use_No='%s'", $userno);
		$result = querySql($sql);
		if($row = fetch_array($result))
		{
			$_SESSION['username'] = trim($row['Use_Name']);
		}
		freeResult($result);
				
		$intRowCount=0;
		//得到该用户可以访问的分店信息
		$sql=sprintf("SELECT TOP 2 a.Chain_No,a.Chain_Name,a.Web_State,convert(varchar(19),a.Web_Date,120)AS WebTime,convert(varchar(10),a.HotelDate,120) AS HotelDay
			FROM Chain_Infor a,dbo.TRF_AppGetChainFromUser('%s') b WHERE a.Chain_No=b.Chain_No;",$userno);
			
		$result = querySql($sql);
		
		while($row = fetch_array($result))
		{
			if($intRowCount==0)
			{
				$_SESSION['ChainNo'] = $row['Chain_No'];  
				$_SESSION['ChainName'] = $row['Chain_Name']; 
				
				$_SESSION['hotelday'] = $row['HotelDay'];
				$_SESSION['updatetime'] = $row['WebTime'];
					
				$intWebState=$row['Web_State'];
				$swebstate="";
				switch($intWebState)
				{
					case 1:
						$swebstate="传输";
						break;
					case 2:
						$swebstate="完成";
						break;
					case 3:
						$swebstate="错误";
						break;
					default:
						$swebstate="停止";
						break;
				}
				
				$_SESSION['updatestate'] =$swebstate; 
			}
			
			$intRowCount=$intRowCount +1;
		}
		
				
		//判断是否有多条记录
		if($intRowCount>1)
		{
			$_SESSION['ChainCount'] ="2";
		}
		else
		{
			$_SESSION['ChainCount'] ="1";
		}

		//echo $intRowCount;
		echo "<script>window.location.href='index.php';</script>";
		
		freeResult($result);
		closeDb();
		exit();
	}
	else
	{					
		if($row['res'] == 0)
		{	//返回0无此工号,1登录成功,2密码错误,3无此权限
			$strmsg="登录失败,无此用户号.";
		}
		elseif($row['res'] == 2)
		{	//返回0无此工号,1登录成功,2密码错误,3无此权限
			$strmsg="登录失败,密码错误.";
		}
		elseif($row['res'] == 3)
		{	//返回0无此工号,1登录成功,2密码错误,3无此权限
			
			$strmsg="登录失败,您没有权限.";
		}
		else
		{
			$strmsg="登录失败！";
		}
		echo "<script>window.location.href='login.php?stext=".$strmsg."';</script>";
	}				
	//释放数据库
	freeResult($result);
	closeDb();
?>


