<?php
!function_exists('html') && exit('ERR');
$gid=-8;
if($job=='list'&&$Apower[membermenu_list])
{
	$colordb[$gid]='red';
 
	$query = $db->query("SELECT * FROM {$pre}admin_menu WHERE groupid='$gid' AND fid=0 ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$listdb[]=$rs;
		$query2 = $db->query("SELECT * FROM {$pre}admin_menu WHERE fid='$rs[id]' ORDER BY list DESC");
		while($rs2 = $db->fetch_array($query2)){
			$rs2[icon]='&nbsp;&nbsp;&nbsp;&nbsp;|----';
			$listdb[]=$rs2;
		}
	}

	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/memberguidemenu/menu.htm");
	require(dirname(__FILE__)."/"."template/memberguidemenu/list.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($job=='edit'&&$Apower[membermenu_list])
{
	$atc="edit";
	$rsdb=$db->get_one("SELECT * FROM {$pre}admin_menu WHERE id='$id'");
	$gid=$rsdb[groupid];
	$target[$rsdb[target]]=' checked ';
	$iftier[$rsdb[iftier]]=' checked ';

	$selected=select_fupmenu('fid',$rsdb[fid]);
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/memberguidemenu/menu.htm");
	require(dirname(__FILE__)."/"."template/memberguidemenu/edit.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($action=='edit'&&$Apower[membermenu_list])
{
	if(!$postdb[name])
	{
		showmsg("名稱不能為空");
	}
	if(!$postdb['linkurl'])
	{
		//showmsg("鏈接地址不能為空");
	}
	$postdb[name]=filtrate($postdb[name]);
	$postdb[linkurl]=filtrate($postdb[linkurl]);

	$db->query("UPDATE {$pre}admin_menu SET fid='$fid',name='$postdb[name]',linkurl='$postdb[linkurl]',color='$postdb[color]',target='$postdb[target]',list='$postdb[list]',iftier='$postdb[iftier]' WHERE id='$id'");
	
	jump("修改succeed","?lfj=$lfj&job=list&gid=$gid",1);
}
elseif($job=='add'&&$Apower[membermenu_list])
{
	if(!$gid){
		showmsg("沒有指定的用戶組!");
	}
	$target[0]=' checked ';
	$iftier[0]=' checked ';
	$atc="add";
	$selected=select_fupmenu('fid',$rsdb[fid]);
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/memberguidemenu/menu.htm");
	require(dirname(__FILE__)."/"."template/memberguidemenu/edit.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($action=='add'&&$Apower[membermenu_list])
{
	if(!$postdb[name]){
		showmsg("名稱不能為空");
	}
	if($fid&&!$postdb['linkurl']){
		showmsg("鏈接地址不能為空");
	}
	if(!$addsort&&!$fid){
		showmsg("請選擇一個分類");
	}
	$postdb[name]=filtrate($postdb[name]);
	$postdb[linkurl]=filtrate($postdb[linkurl]);

	$db->query("INSERT INTO `{$pre}admin_menu` (`fid`, `name`, `linkurl`, `color`, `target`, `groupid`, `list`) VALUES ('$fid', '$postdb[name]', '$postdb[linkurl]', '$postdb[color]', '$postdb[target]','$gid', '$postdb[list]')");
	
	jump("添加succeed","?lfj=$lfj&job=list&gid=$gid",1);
}
elseif($action=='delete'&&$Apower[membermenu_list])
{
	$rs = $db->get_one("SELECT * FROM {$pre}admin_menu WHERE fid='$id'");
	if($rs){
		showmsg("請先刪除子菜單或者把子菜單移走.才能刪除此菜單");
	}

	$db->query("DELETE FROM `{$pre}admin_menu` WHERE id='$id'");
	
	jump("刪除succeed","?lfj=$lfj&job=list&gid=$gid",1);
}
elseif($action=="editlist"&&$Apower[membermenu_list])
{
	foreach( $order AS $key=>$value)
	{
		$db->query("UPDATE {$pre}admin_menu SET list='$value' WHERE id='$key'");
	}
	
	jump("修改succeed","?lfj=$lfj&job=list&gid=$gid",1);
}
elseif($job=="sysmenu"&&$Apower[membermenu_list])
{
	$Smenu=intval($Smenu);
	unset($menudb);
	if($Smenu){
		$_r=$db->get_one("SELECT * FROM {$pre}module WHERE id='$Smenu'");
		$_fname="$_r[name]-";
		$_r[admindir] || $_r[admindir]='admin';
		@include(PHP168_PATH."$_r[dirname]/$_r[admindir]/menu.php");
		foreach( $menudb AS $k1=>$v1){
			foreach($v1 AS $k2=>$v2){
				$v2["link"]="../$_r[dirname]/$_r[admindir]/".$v2["link"];
				$adminDB[$k1][$k2]=$v2;
			}
		}
	}else{
		require("menu.php");
		$adminDB=$menudb;
		$rsdb=$db->get_one("SELECT * FROM {$pre}group WHERE gid='$gid'");
		$grdb=unserialize($rsdb[allowadmindb]);
	}
	
	$colorDB[$Smenu]='red';
	$show="[<A HREF='index.php?lfj=$lfj&job=$job&Smenu=0' style='color:$colorDB[0];'>整站系統</A>] ";
	$query = $db->query("SELECT * FROM {$pre}module ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$show.=" [<A HREF='index.php?lfj=$lfj&job=$job&Smenu=$rs[id]' style='color:{$colorDB[$rs[id]]};'>$rs[name]</A>]";
	}

	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/memberguidemenu/sysmenu.htm");
	require(dirname(__FILE__)."/"."foot.php");
}



function select_fupmenu($name='fid',$id=0){
	global $db,$pre,$gid;
	$select="<select name='$name'><option value='0'>請選擇</option>";
	$query = $db->query("SELECT * FROM {$pre}admin_menu WHERE groupid='$gid' AND fid=0 ORDER BY list DESC");
	while($rs = $db->fetch_array($query)){
		$ckk=$id==$rs[id]?' selected ':'';
		$select.="<option value='$rs[id]' $ckk style='color:blue;'>$rs[name]</option>";
	}
	$select.="</select>";
	return $select;
}