<?php
//====================================
if(!empty($_GET['action'])&&$_GET["action"]=="del")
{
	if(isset($_GET['groupid'])&&$_GET['groupid']>3)
	  $deletegroupid=$_GET['groupid'];
	else
	  $deletegroupid=0;
	$sql="delete from ".USERGROUP." where group_id='".$deletegroupid."'"; 
	$db->query($sql);
	msg("module.php?m=company&s=membergroup.php");
}
//=======================================
$group_array=array('groupname','groupstatu','groupdes','groupdes','grouplogo','groupid','minilogo','groupfee');
if($_POST["submit"]==lang_show('addtype')&&!empty($_POST['groupname']))
{
	$creattime=date("Y-m-d h:i:s");
	$groupn=$_POST['groupname'];
	$fee=$_POST['groupfee']*1;
	$minilogo=$_POST['minilogo'];
	$groupd=$_POST['groupdes'];
	$groupl=$_POST['grouplogo'];
	
    $sql="insert into ".USERGROUP." (name,des,logo,statu,creat_time,minilogo,groupfee,con) 
	      values ('$groupn','$groupd','$groupl','1','$creattime','$minilogo','$fee','$_POST[con]')";
    $db->query($sql);

	creat_file();
	msg("module.php?m=company&s=membergroup.php");
}

if(!empty($_POST['edit'])&&!empty($_POST['groupid']))
{
    unset($grpid);
	$grpid=$_POST['groupid'];  
	$groupn=$_POST['groupname'];
	$groups=$_POST['groupstatu'];
	$minilogo=$_POST['minilogo'];
	$fee=$_POST['groupfee']*1;
	$groupd=$_POST['groupdes'];
	$groupl=$_POST['grouplogo'];
	$creattime=date("Y-m-d h:i:s");	
	if(!empty($groupn))
		$sql=",name='$groupn'";
	else
		$sql=NULL;
	$sql="update ".USERGROUP." set 
		des='$groupd',logo='$groupl',con='$_POST[con]',minilogo='$minilogo',statu='$groups',creat_time='$creattime' ,groupfee='$fee' $sql
    	where group_id='$grpid'";
	$db->query($sql);
	creat_file();
	msg('module.php?m=company&s=membergroup.php');
}
//==================================
function creat_file()
{
	$write_group_data=get_user_group();//从库里取出数据生成数组
	$write_str=serialize($write_group_data);//将数组序列化后生成字符串
	$write_str='<?php $group = unserialize(\''.$write_str.'\');?>';//生成要写的内容
	$fp=fopen('../config/usergroup.php','w');
	fwrite($fp,$write_str,strlen($write_str));//将内容写入文件．
	fclose($fp);
}
function get_user_group()
{
	global $db;
	$sql="select group_id,name,des,logo,statu,creat_time,minilogo,groupfee from ".USERGROUP;
	$db->query($sql);
	$re=$db->getRows();
	foreach($re as $key=>$v)
	{
		$gid=$v['group_id'];
		$sre[$gid]=$v;
	}
	return $sre;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="main.css" rel="stylesheet" type="text/css" />
<TITLE> <?php echo lang_show('admin_system');?></TITLE>
<script src="../script/my_lightbox.js" language="javascript"></script>
<script type="text/javascript" src="../script/jquery-1.4.4.min.js"></script>
</head>
<body>
<?php
if(empty($_GET['action'])&&$_GET['action']!="add")
{
?>
<div class="bigbox">
	<div class="bigboxhead tab">
		<span class="cbox on"><a href="module.php?m=company&s=membergroup.php"><?php echo lang_show('allmembergroup');?></a></span>
		<span class="cbox"><a href="module.php?m=company&s=membergroup.php&action=add"><?php echo lang_show('addgroup');?></a></span>
	</div>
    <div class="bigboxbody">
    <form name="groupuser" method="get" action="">
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
	    <tr class="theader">
          <td height="31"><?php echo lang_show('add_nav_name');?></td>
          <td><?php echo lang_show('number');?></td>
          <td><?php echo lang_show('statu');?></td>
          <td><?php echo lang_show('icon');?></td>
          <td><?php echo lang_show('gpfee');?></td>
          <td><?php echo lang_show('description');?></td>
          <td width="186"><?php echo lang_show('act');?></td>
        </tr>
        <?php
        $sql="select * from ".USERGROUP." order by group_id asc";
        $db->query($sql);
        $re=$db->getRows();
        foreach($re as $v)
	    {
        ?>
        <tr>
          <td width="127" height="31"><?php echo $v['name'];?></td>
          <td width="64" height="31"><?php echo $v['group_id'];?></td>
          <td width="65"><?php if($v['statu']==1) echo lang_show('qiyong'); else echo lang_show('tingyong');?>&nbsp;</td>
          <td width="77"><?php echo "<img src='".$v['minilogo']."' >";?>&nbsp;</td>
          <td width="66"><?php echo $config['money'].$v['groupfee'];?>&nbsp;</td>
          <td width="398"><?php echo $v['des'];?>&nbsp;</td>
          <td><a href="module.php?m=company&s=membergroup.php&action=modify&groupid=<?php echo $v['group_id']; ?>"><?php echo $editimg;?></a>
		   <?php
			if($v['group_id']>3)
			{
		   ?>
			<a href="module.php?m=company&s=membergroup.php&action=del&groupid=<?php echo $v['group_id'] ;?>" onClick="javascript:return confirm('<?php echo lang_show('suredelugroup');?>')"><?php echo $delimg;?></a>
		   <?php
			}
		   ?>
		   &nbsp;</td>
        </tr>
        <?php
          }
         ?>
		 <tr>
          <td height="31" colspan="7" style="color:#666666">
		  1.<?php echo lang_show('3def_user_group');?>
		  <br>
		  2.<?php echo lang_show('add_user_group');?>
		  </td>
        </tr>
      </table>
    </form>
    </div>
</div>
<?php
}
?>

<?php
if(!empty($_GET['action'])&&$_GET["action"]=="modify"&&empty($_POST['submit']))
{
   $groupid=$_GET['groupid'];
  $sql="select * from ".USERGROUP." where group_id='".$groupid."'";
  $db->query($sql);
  $rs=$db->fetchRow();
?>
<form method="post" action="">
<div class="bigbox">
<div class="bigboxhead"><?php echo lang_show('groupxiugai');?></div>
<div class="bigboxbody">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr class="theader">
    <td colspan="2"><?php echo lang_show('shuxing');?></td>
    </tr>
      <tr>
        <td height="40" ><?php echo lang_show('groupzt');?></td>
        <td width="858">
          <input type="radio" class="radio" name="groupstatu" value="1" <?php if($rs['statu']==1) echo "checked"; ?>>
          <?php echo lang_show('qiyong');?>
          <input type="radio" class="radio" name="groupstatu" value="0" <?php if($rs['statu']==0) echo "checked"; ?>>
          <?php echo lang_show('tingyong');?>		  </td>
        </tr>
      <tr>
        <td width="146" ><?php echo lang_show('groupname');?></td>
        <td><input name="groupname" type="text" id="groupname" class="text" maxlength="30" value="<?php echo $rs['name']; ?>"></td>
        </tr>
      <tr>
        <td><?php echo lang_show('gpfee');?></td>
        <td><input name="groupfee" type="text" id="groupfee" class="text" value="<?php echo $rs['groupfee'];?>"></td>
      </tr>
      <tr>
        <td><?php echo lang_show('grouplogo');?></td>
        <td>
          <input name="grouplogo" type="text" id="grouplogo" class="text" value="<?php echo $rs['logo']; ?>">
		 [<a href="javascript:uploadfile('<?php echo lang_show('upload_logo');?>','grouplogo',180,60)"><?php echo lang_show('upload');?></a>] 
		 [<a href="javascript:preview('grouplogo');"><?php echo lang_show('review');?></a>]
		 [<a onclick="javascript:$('#grouplogo').val('');" href="#"><?php echo lang_show('del');?></a>]		  </td>
        </tr>
      <tr>
        <td><?php echo lang_show('minilogo');?></td>
        <td>
		 <input type="hidden" name="groupid" id="groupid" value="<?php echo $rs['group_id']; ?>">
         <input name="minilogo" id="minilogo" type="text" class="text" value="<?php if(!empty($rs['minilogo'])) echo $rs['minilogo'];?>" >
		 [<a href="javascript:uploadfile('<?php echo lang_show('upload_logo');?>','minilogo',180,60)"><?php echo lang_show('upload');?></a>] 
		 [<a href="javascript:preview('minilogo');"><?php echo lang_show('review');?></a>]
		 [<a onclick="javascript:$('#minilogo').val('');" href="#"><?php echo lang_show('del');?></a>]
         </td>
      </tr>
	  <tr>
        <td><?php echo lang_show('groupdes');?></td>
        <td>
          <textarea name="groupdes" id="groupdes" class="text" style="height:70px;"><?php echo $rs['des']; ?></textarea><br />
		  <span class="bz"><?php echo lang_show('brief_tips');?></span>
		  </td>
        </tr>
          <tr>
            <td height="256" align="left"><?php echo lang_show('deta_des');?></td>
            <td align="left">
				
            <script charset="utf-8" src="../lib/kindeditor/kindeditor-min.js"></script>             
	<script>
    var editor;
    KindEditor.ready(function(K) {
        editor = K.create('textarea[name="con"]', {
        });
    });
    </script>
     <textarea name="con" style="width:90%; height:400px;"><?php echo $rs["con"] ?></textarea>
     
			<br /><span class="bz"><?php echo lang_show('user_group_desc');?></span>
			</td>
          </tr>
        <tr>
  	<td align="center">&nbsp;</td>
    <td align="left">
    <input class="btn" type="submit" name="edit" value="<?php echo lang_show('submit');?>"/>&nbsp;&nbsp;
     <input class="btn" type="button" name="cancel" id="cancel" value="<?php echo lang_show('cancel');?>" onclick='javascript:window.location="module.php?m=company&s=membergroup.php"' />	 </td>
  </tr>
</table>
</div>
</div>
</form>
<?php } ?>






<?php
if(!empty($_GET['action'])&&$_GET["action"]=="add"&&empty($_POST['submit']))
{
?>
<form method="post" action="">
<div class="bigbox">
<div class="bigboxhead tab">
	<span class="cbox"><a href="module.php?m=company&s=membergroup.php"><?php echo lang_show('allmembergroup');?></a></span>
	<span class="cbox on"><a href="module.php?m=company&s=membergroup.php&action=add"><?php echo lang_show('addgroup');?></a></span>
</div>
<div class="bigboxbody">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
	<td width="135"><?php echo lang_show('groupname');?></td>
	<td><input name="groupname" type="text" id="groupname" class="text" ></td>
	</tr>
  <tr>
	<td><?php echo lang_show('gpfee');?></td>
	<td><input name="groupfee" type="text" id="groupfee" class="text" ></td>
  </tr>
  <tr>
	<td><?php echo lang_show('groupdes');?></td>
	<td><textarea name="groupdes" id="groupdes" class="text" style="height:70px;"></textarea></td>
	</tr>
  <tr>
	<td><?php echo lang_show('grouplogo');?></td>
	<td><input name="grouplogo" type="text" id="grouplogo" class="text"  /></td>
	</tr>
  <tr>
	<td><?php echo lang_show('minilogo');?></td>
	<td><input name="minilogo" type="text" id="minilogo" class="text" /></td>
	</tr>
  <tr>
	<td>&nbsp;</td>
	<td>
	<input class="btn" type="submit" name="submit" value="<?php echo lang_show('addtype');?>"/>
	  &nbsp;&nbsp;
	  <input class="btn" type="button" name="cancel2" id="cancel2" value="<?php echo lang_show('cancel');?>" onClick='javascript:window.location="module.php?m=company&s=membergroup.php"' /></td>
  </tr>
</table>
</div>
</div>
</form>
<?php
}
?>
</body>
</html>