<?php
define('YUKEY', true);
require(str_replace('sicms','',dirname(__FILE__)) . '/includes/init.php');
check_login();
require(ROOT_PATH . 'includes/lib_manage.php');
$active=!empty($_GET['active']) ? intval($_GET['active']) : 0;
$tab=!empty($_GET['tab']) ? $_GET['tab'] : 'user_list';
if($act=='ready')
{	
	if($tab=='user_list') $total=manage_count('users','tbCompanyID',$_SESSION['tbCompanyID'],''); //总记录条数
	if($tab=='company_list')$total=manage_count('companys','tbCompanyID',$_SESSION['tbCompanyID'],''); //总记录条数
	$page_num=20;  //每页显示的记录数
	$allpage=ceil($total/$page_num); //总页数
	$current_page=!empty($_GET['nowpage']) ? intval($_GET['nowpage']) : 1; //目前页数 
	$start_num=$page_num*($current_page-1); //$start_num 起始笔数
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('all_page',$allpage);
	$smarty->assign('all_pages',listpage($allpage));
	$smarty->assign('now_page',$current_page);
	if($tab=='user_list'){
		if($_SESSION['tbRank']>='70')$k=$v='';
		if($_SESSION['tbRank']<'70')$k='tbID';$v=$_SESSION['tbID'];
		$smarty->assign('user_list',manage_list('users',$start_num,$page_num,'tbUserName',$k,$v,''));
		$smarty->display('user_list.dwt');
	}elseif($tab=='company_list'){
		if($_SESSION['tbRank']>='70')$k=$v='';
		if($_SESSION['tbRank']<'70')$k='tbCompanyID';$v=$_SESSION['tbCompanyID'];
		$smarty->assign('list',manage_list('companys',$start_num,$page_num,'tbCompanyName',$k,$v,''));
		$smarty->display('company_list.dwt');
	}
}
elseif($act=='add_user')
{
	if($_SESSION['tbRank']>='70'){
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('allCompany',get_rows('si','*','','companys','tbNickName'));
	$smarty->display('user_add.dwt');
	}else{
	echo "<script language='javascript'>alert('Access Denied!')</script>";
	echo "<script language='javascript'>history.back();</script>";
	}
}

elseif($act=='add_company')
{
	if($_SESSION['tbRank']>='70'){
	$smarty->assign('active',$active);
	$smarty->display('company_add.dwt');
	}else{
	echo "<script language='javascript'>alert('Access Denied!')</script>";
	echo "<script language='javascript'>history.back();</script>";
	}
}

elseif($act=='edit_user')
{
	$user_id=!empty($_GET['user_id']) ? $_GET['user_id'] : '';
	if($_SESSION['tbRank']>='70' || $_SESSION['tbID']==$user_id){
	$smarty->assign('active',$active);
    $smarty->assign('get_man',get_row('si','*','tbID',$user_id,'users'));
	$smarty->assign('allCompany',get_rows('si','*','','companys','tbNickName'));
	$smarty->display('user_edit.dwt');
	}else{
	echo "<script language='javascript'>alert('Access Denied!')</script>";
	echo "<script language='javascript'>history.back();</script>";
	}
}
elseif($act=='edit_company')
{
	$id=!empty($_GET['id']) ? $_GET['id'] : '';
	if($_SESSION['tbRank']>='70'){
	$smarty->assign('active',$active);
	$smarty->assign('pre',$pre);
	$smarty->assign('com',get_row('si','*','tbCompanyID',$id,'companys'));
	$smarty->display('company_edit.dwt');
	}else{
	echo "<script language='javascript'>alert('Access Denied!')</script>";
	echo "<script language='javascript'>history.back();</script>";
	}
}
elseif($act=='save_user')
{
    $tbID=!empty($_POST['tbID']) ? $_POST['tbID'] : '';
	$tbUserName=!empty($_POST['tbUserName']) ? $_POST['tbUserName'] : '';
	$tbPassword=!empty($_POST['tbPassword']) ? $_POST['tbPassword'] : '';
    $tbName=!empty($_POST['tbName']) ? $_POST['tbName'] : '';
	$tbCnName=!empty($_POST['tbCnName']) ? $_POST['tbCnName'] : '';
	$tbRank=!empty($_POST['tbRank']) ? $_POST['tbRank'] : '';
	$tbCompanyID=!empty($_POST['tbCompanyID']) ? $_POST['tbCompanyID'] : '';
	$tbOpen=!empty($_POST['tbOpen']) ? $_POST['tbOpen'] : '';
	$tbMail=!empty($_POST['tbMail']) ? $_POST['tbMail'] : '';
	$tbTitle=!empty($_POST['tbTitle']) ? $_POST['tbTitle'] : '';
	$tbDepartment=!empty($_POST['tbDepartment']) ? $_POST['tbDepartment'] : '';
	$tbPhoneNumber=!empty($_POST['tbPhoneNumber']) ? $_POST['tbPhoneNumber'] : '';
	$tbCellNumber=!empty($_POST['tbCellNumber']) ? $_POST['tbCellNumber'] : '';
	$tbSentMail=$_POST['tbSentMail'];
	if($tbID!=''){
	$sql="UPDATE " .$GLOBALS['si']->table('users'). " SET". " tbName='$tbName',tbCnName='$tbCnName',tbOpen='$tbOpen',tbRank='$tbRank',tbMail='$tbMail',tbTitle='$tbTitle',tbCompanyID='$tbCompanyID',tbDepartment='$tbDepartment',tbPhoneNumber='$tbPhoneNumber',tbCellNumber='$tbCellNumber',tbSentMail='$tbSentMail',tbCnSent='$tbCnSent' WHERE tbID='$tbID'";
	}else{//尽管有失焦事件控制输入重名用户名，但失焦过后还是可能产生提交重名并保存，所以采取以下程加以控制
			if(get_row('si','tbID,tbUserName','tbUserName',trim($tbUserName),'users') != false){
			 echo "<script language='javascript'>alert('User Name Already Exist!');history.back();</script>";
			 exit;
			 }else{
					$sql="INSERT INTO " .$GLOBALS['si']->table('users'). " (tbUserName,tbPassword,tbName,tbCnName,tbOpen,tbRank,tbMail,tbTitle,tbDepartment,tbCompanyID,tbPhoneNumber,tbCellNumber,tbSentMail,tbCnSent) VALUES ('$tbUserName','$tbPassword','$tbName','$tbCnName','$tbOpen','$tbRank','$tbMail','$tbTitle','$tbDepartment','$tbCompanyID','$tbPhoneNumber','$tbCellNumber','$tbSentMail','$tbCnSent')"; }
		//var_dump($sql);
	}
	 if($GLOBALS['db']->query($sql)){
	 echo "<script language='javascript'>alert('Save Successful!')</script>";
	 ecs_header("Location:management.php?tab=user_list");
	 }else{
	 echo "<script language='javascript'>alert('Error!Save Failure!')</script>";
	 echo "<script language='javascript'>history.back();</script>";
	 }
}

elseif($act=='save_company')
{
    $id=!empty($_POST['id']) ? $_POST['id'] : '';
	$tbCompanyName=!empty($_POST['tbCompanyName']) ? $_POST['tbCompanyName'] : '';
	$tbCountry=!empty($_POST['tbCountry']) ? $_POST['tbCountry'] : '';
    $tbShortName=!empty($_POST['tbShortName']) ? $_POST['tbShortName'] : '';
	$tbTel=!empty($_POST['tbTel']) ? $_POST['tbTel'] : '';
	$tbNickName=!empty($_POST['tbNickName']) ? $_POST['tbNickName'] : '';
	$tbContactPerson=!empty($_POST['tbContactPerson']) ? $_POST['tbContactPerson'] : '';
	$tbAddress=!empty($_POST['tbAddress']) ? $_POST['tbAddress'] : '';
	$tbType=!empty($_POST['tbType']) ? $_POST['tbType'] : '';
	$tbCurrent=!empty($_POST['tbCurrent']) ? $_POST['tbCurrent'] : '';
	$tbDayCharge=!empty($_POST['tbDayCharge']) ? $_POST['tbDayCharge'] : '';
	$tbHourCharge=!empty($_POST['tbHourCharge']) ? $_POST['tbHourCharge'] : '';
	if($id!=''){
	$sql="UPDATE " .$GLOBALS['si']->table('companys'). " SET". " tbCompanyName='$tbCompanyName',tbCountry='$tbCountry',tbShortName='$tbShortName',tbTel='$tbTel',tbNickName='$tbNickName',tbContactPerson='$tbContactPerson',tbAddress='$tbAddress',tbType='$tbType',tbCurrent='$tbCurrent',tbDayCharge='$tbDayCharge',tbHourCharge='$tbHourCharge' WHERE tbCompanyID='$id'";
	}else{
	$sql="INSERT INTO " .$GLOBALS['si']->table('companys'). " (tbCompanyName,tbCountry,tbShortName,tbTel,tbNickName,tbContactPerson,tbAddress,tbType,tbCurrent,tbDayCharge,tbHourCharge) VALUES ('$tbCompanyName','$tbCountry','$tbShortName','$tbTel','$tbNickName','$tbContactPerson','$tbAddress','$tbType','$tbCurrent','$tbDayCharge','$tbHourCharge')";
		//var_dump($sql);
	}
	 if($GLOBALS['db']->query($sql)){
	 echo "<script language='javascript'>alert('Save Successful!')</script>";
	 ecs_header("Location:management.php?tab=company_list");
	 }else{
	 echo "<script language='javascript'>alert('Error!Save Failure!')</script>";
	 echo "<script language='javascript'>history.back();</script>";
	 }
}

elseif($act=='ckExist')
{
    $tbUserName=!empty($_POST['tbUserName']) ? $_POST['tbUserName'] : '';
	if(trim($tbUserName)!=''){
			if(get_row('si','tbID,tbUserName','tbUserName',trim($tbUserName),'users')!= false){
			 echo "User Name Already Exist!";
			 }else{
			 echo "NoExist!";
			 }
	}else{
			 echo "User Name Empty!";
	}
}
elseif($act=='cpForm')
{
       $tbID=!empty($_GET['tbID']) ? intval($_GET['tbID']) : '';
	if($_SESSION['tbID']==$tbID){
	   $smarty->assign('tbID',$tbID);
	   $smarty->display('user_pwd.dwt');
	 }else{
	   echo "<script language='javascript'>alert('You are not allow to change others password!')</script>";
	   echo "<script language='javascript'>window.close();</script>";
	 }
}
elseif($act=='check')
{
    $tbID=!empty($_POST['tbID']) ? intval($_POST['tbID']) : '';
	$o_pass=!empty($_POST['o_pass'])? $_POST['o_pass'] : '';
	$sql = "SELECT * FROM " . $GLOBALS['si']->table('users'). " WHERE tbID ='" . $tbID . "' AND tbPassword ='" . $o_pass . "'";
	if ($GLOBALS['db']->getOne($sql)){
		echo "<img src='pics/right.gif' />";
	}else{
		echo "<img src='pics/wrong.gif' />";
	}
}
elseif($act=='change')
{
    $tbID=!empty($_POST['tbID']) ? intval($_POST['tbID']) : '';
	$n_pass=!empty($_POST['n_pass'])? $_POST['n_pass'] : '';
	$sql = "UPDATE " . $GLOBALS['si']->table('users'). " SET tbPassword='" .$n_pass. "' WHERE tbID ='" . $tbID . "'";
	if ($GLOBALS['db']->query($sql)){
		echo "Changed";
	}else{
		echo "Error";
	}
}
?>