<?php
require_once("checkstate.php");
?>

<?php
require("connect.php");
?>



<?php    //本程式碼是處理班級通訊錄之新增、刪除、修改

//刪除
if ($_GET['action']=="delete") {
//	echo "here";
	$schnum=$_GET['id'];
	$sql="delete from bkstage_user where schnum='$schnum'";
//echo $sql; exit();
	mysql_query($sql) or die("delete error!");
}

//修改
if ($_POST['action']=="modify") {
	
	$id = $_POST['id'];
	$account = $_POST['account'];
	$pass1 = sha1($_POST['pass1']);
	$level = $_POST['level'];

	$sql="UPDATE bkstage_user SET account='$account',password='$pass1',level='$level'";
	$sql.=" WHERE id='$id'";

//echo $sql; exit();

	mysql_query($sql) or die("ERROR insert");
	//echo "新增成功!";
}

//新增
if ($_POST['action']=="add_new") {
//	$schnum = $_POST['id'];
	$account = $_POST['account'];
	$pass1 = $_POST['pass1'];
	$level = $_POST['level'];

	$sql="INSERT INTO bkstage_user(account,pass1,level) VALUES ('$account','$pass1','$level')";
//echo $sql; exit();
	mysql_query($sql) or die("ERROR insert");
}
?>



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
<style type="text/css">

/* unvisited link */
a:link {
	color: #00C;
	text-decoration: none;
}

/* visited link */
a:visited {
	color: green;
	text-decoration: none;
}

/* mouse over link */
a:hover {
	color: #D5313E;
	text-decoration: none;
}

/* selected link */
a:active {
	color: #090;
	text-decoration: none;
}

.aaa {
	font-size: 36px;
	color: #00F;
}
.bbb {
	font-size: 20px;
	color: #093;
	font-weight: bold;
}
</style></head>

<body>
<table width="980" height="818" border="1" align="center">
  <tr>
    <td height="112" colspan="2" align="center" class="aaa">後台管理程式</td>
  </tr>
  <tr>
    <td width="151" valign="baseline">&nbsp;<?php require_once("left_menu.php");?></td>
    <td width="813" align="center" valign="top"><p class="bbb">&nbsp;</p>
      <p class="bbb">後台管理員 清單</p>
<?php 
if ($_GET['page']=="") $page=1;
else $page=$_GET['page'];

$sql="select * from bkstage_user";
//echo $sql;
$result=mysql_query($sql);
$totalrec=mysql_num_rows($result);

?>

      <table width="657" border="0" cellpadding="3" cellspacing="3">
        <tr>
          <td width="324">&nbsp;</td>
          <td width="323" align="right">　<a href="bkstage_add.php">新增</a></td>
        </tr>
      </table>
      <table width="657" height="78" border="1" align="center">
        <tr>
        <th width="53" height="34">id</th>
        <th width="122">帳號</th>
        <th width="301">密碼(hash)</th>
        <th width="73">權限等級</th>
        <th width="74">功能</th>
      </tr>

<?php
$pagecount=10;  //一頁秀幾筆資料
$totalpage=ceil($totalrec/$pagecount);      //總頁數
if ($page==$totalpage) $lastnum=$totalrec-($page-1)*$pagecount;
else $lastnum=10;

$sql=$sql." limit ".($page-1)*$pagecount.",".$lastnum;
//echo $sql; exit();
$pageresult=mysql_query($sql);


while ($object=mysql_fetch_object($pageresult)) {
?>
      <tr>
        <td align="center"><?php echo $object->id;?></td>
        <td align="center"><?php echo $object->account;?></td>
        <td align="center"><?php echo $object->password;?></td>
        <td align="center"><?php echo $object->level;?></td>
        <td align="center"><a href="bkstage_modify.php?id=<?php echo $object->id;?>"><img src="images/b_edit.png" border=0 title='修改'></a> <a href="<?php echo $_SERVER['PHP_SELF'];?>?action=delete&id=<?php echo $object->id; ?>"><img src="images/b_drop.png" border=0 title='立即刪除'></a></td>
      </tr>
<?php
} //while ($object=mysql_fetch_object($pageresult)) 
?>
  </table>
      <br>
      <table border=0 align=center>
        <tr height=100><td>&nbsp;
<?php
        echo "<p><center><font color=blue>資料筆數：<font color=red>".$totalrec;

        echo "<p><center><font color=blue>共 <font color=red>".$totalpage." <font color=blue>頁，現在第 <font color=red>".$page." <font color=blue>頁　<br>";

        if ($page != 1) echo " <a href='" . $_SERVER['PHP_SELF'] . "?&page=1'>第一頁</a>　";
        else echo "<font color=#cccccc>第一頁</font>　";

        if ($page > 1) echo " <a href='" . $_SERVER['PHP_SELF'] . "?page=" . ($page-1) . "'>上一頁</a>　";
        else echo "<font color=#cccccc>上一頁</font>　";

        if ($page < $totalpage) echo " <a href='". $_SERVER['PHP_SELF'] . "?page=" . ($page+1) ."'>下一頁</a>　";
        else echo "<font color=#cccccc>下一頁</font>　";

        if ($page != $totalpage) echo " <a href='" . $_SERVER['PHP_SELF'] . "?page=$totalpage'>最後頁</a>　";
        else echo "<font color=#cccccc>最後頁</font>　";
        echo "</center>";
?>

      </td>
     </tr>
	</table>

  
    </td>
  </tr>
</table>
</body>
</html>