<?php
define('EmpireCMSAdmin','1');
require("../../../e/class/connect.php");
require("../../../e/class/functions.php");
require("../../../e/member/class/user.php");
require "../../../e/data/".LoadLang("pub/fun.php");
$link=db_connect();
$empire=new mysqlquery();
$editor=1;
//驗證用戶
$lur=is_login();
$logininid=(int)$lur['userid'];
$loginin=$lur['username'];
$loginrnd=$lur['rnd'];
$loginlevel=(int)$lur['groupid'];
$loginadminstyleid=(int)$lur['adminstyleid'];
//ehash
$ecms_hashur=hReturnEcmsHashStrAll();
//驗證權限
CheckLevel($logininid,$loginin,$classid,"ingroup");

//增加會員內部組
function AddInGroup($add,$userid,$username){
	global $empire,$dbtbpre;
	if(empty($add['gname']))
	{
		printerror('EmptyInGroup','history.go(-1)');
	}
	$add['gname']=hRepPostStr($add['gname'],1);
	$add['myorder']=(int)$add['myorder'];
	$sql=$empire->updatesql("insert into {$dbtbpre}enewsingroup(gname,myorder) values('".$add['gname']."','".$add['myorder']."');","ins");
	//更新緩存
	GetMemberLevel();
	if($sql)
	{
		$gid=$empire->lastid($dbtbpre.'enewsingroup','gid');
		insert_dolog("gid=$gid&gname=".$add['gname']);//操作日誌
		printerror("AddInGroupSuccess","AddInGroup.php?enews=AddInGroup".hReturnEcmsHashStrHref2(0));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//修改會員內部組
function EditInGroup($add,$userid,$username){
	global $empire,$dbtbpre;
	$gid=intval($add['gid']);
	if(empty($add['gname'])||!$gid)
	{
		printerror('EmptyInGroup','history.go(-1)');
	}
	$add['gname']=hRepPostStr($add['gname'],1);
	$add['myorder']=(int)$add['myorder'];
	$sql=$empire->query("update {$dbtbpre}enewsingroup set gname='".$add['gname']."',myorder='".$add['myorder']."' where gid='$gid'");
	//更新緩存
	GetMemberLevel();
	if($sql)
	{
		insert_dolog("gid=$gid&gname=".$add['gname']);//操作日誌
		printerror("EditInGroupSuccess","ListInGroup.php".hReturnEcmsHashStrHref2(1));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

//刪除會員內部組
function DelInGroup($add,$userid,$username){
	global $empire,$dbtbpre;
	$gid=intval($add['gid']);
	if(!$gid)
	{
		printerror('EmptyInGroupid','history.go(-1)');
	}
	$r=$empire->fetch1("select gid,gname from {$dbtbpre}enewsingroup where gid='$gid'");
	if(!$r['gid'])
	{
		printerror('EmptyInGroupid','history.go(-1)');
	}
	$sql=$empire->query("delete from {$dbtbpre}enewsingroup where gid='$gid'");
	//更新緩存
	GetMemberLevel();
	if($sql)
	{
		insert_dolog("gid=$gid&gname=".$r['gname']);//操作日誌
		printerror("DelInGroupSuccess","ListInGroup.php".hReturnEcmsHashStrHref2(1));
	}
	else
	{
		printerror("DbError","history.go(-1)");
	}
}

$enews=$_POST['enews'];
if(empty($enews))
{$enews=$_GET['enews'];}
if($enews)
{
	hCheckEcmsRHash();
}
if($enews=="AddInGroup")
{
	AddInGroup($_POST,$logininid,$loginin);
}
elseif($enews=="EditInGroup")
{
	EditInGroup($_POST,$logininid,$loginin);
}
elseif($enews=="DelInGroup")
{
	DelInGroup($_GET,$logininid,$loginin);
}


$search=$ecms_hashur['ehref'];
$page=(int)$_GET['page'];
$page=RepPIntvar($page);
$start=0;
$line=16;//每頁顯示條數
$page_line=25;//每頁顯示鏈接數
$offset=$page*$line;//總偏移量
$query="select * from {$dbtbpre}enewsingroup";
$totalquery="select count(*) as total from {$dbtbpre}enewsingroup";
$num=$empire->gettotal($totalquery);//取得總條數
$query=$query." order by gid desc".do_dblimit($line,$offset);
$sql=$empire->query($query);
$returnpage=page2($num,$line,$page_line,$start,$page,$search);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="../adminstyle/<?=$loginadminstyleid?>/adminstyle.css" rel="stylesheet" type="text/css">
<title>會員內部組</title>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
  <tr> 
    <td width="50%" height="32">位置：<a href="ListInGroup.php<?=$ecms_hashur['whehref']?>">管理會員內部組</a></td>
    <td><div align="right" class="emenubutton">
        <input type="button" name="Submit5" value="增加會員內部組" onclick="self.location.href='AddInGroup.php?enews=AddInGroup<?=$ecms_hashur['ehref']?>';">
      </div></td>
  </tr>
</table>

<br>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
  <tr class="header"> 
    <td width="6%" height="25"> <div align="center">ID</div></td>
    <td width="49%" height="25"> <div align="center">組名稱</div></td>
    <td width="24%"><div align="center">會員數</div></td>
    <td width="21%" height="25"> <div align="center">操作</div></td>
  </tr>
  <?php
  //formhash
  $efh=heformhash_get('DelInGroup',1);
  
  while($r=$empire->fetch($sql))
  {
  	$color="#ffffff";
	$movejs=' onmouseout="this.style.backgroundColor=\'#ffffff\'" onmouseover="this.style.backgroundColor=\'#C3EFFF\'"';
	$membernum=$empire->gettotal("select count(*) as total from ".eReturnMemberTable()." where ".egetmf('ingid')."='".$r['gid']."'");
  ?>
  <tr bgcolor="<?=$color?>"<?=$movejs?>> 
    <td height="25"> <div align="center"> 
        <?=$r['gid']?>
      </div></td>
    <td height="25"> <div align="center"> 
        <?=$r['gname']?>
      </div></td>
    <td><div align="center"><a href="ListMember.php?sear=1&ingid=<?=$r['gid']?><?=$ecms_hashur['ehref']?>" target="_blank" title="點擊查看列表"><?=$membernum?></a></div></td>
    <td height="25"> <div align="center"> [<a href="AddInGroup.php?enews=EditInGroup&gid=<?=$r['gid']?><?=$ecms_hashur['ehref']?>">修改</a>]&nbsp;[<a href="ListInGroup.php?enews=DelInGroup&gid=<?=$r['gid']?><?=$ecms_hashur['href'].$efh?>" onclick="return confirm('確認要刪除？');">刪除</a>]</div></td>
  </tr>
  <?php
  }
  ?>
  <tr bgcolor="#FFFFFF"> 
    <td height="25" colspan="4">&nbsp;&nbsp;&nbsp; 
      <?=$returnpage?>    </td>
  </tr>
</table>
<br>
</body>
</html>
<?php
db_close();
$empire=null;
?>
