<?
include_once("./_common.php");
include_once("$g4[path]/head.sub.php");

if ($is_admin != "super") {
	alery("잘못된접근입니다","$g4[path]");	
}

$t_sql = mysql_query(" select * from g4_tag ");

//전체게시물 수
$total_tag = mysql_num_rows($t_sql);

if (!$page) $page = 0; //현제 페이지

$page_page = 10; //한화면에 표시할 페이지 수

$page_list = 10; //한화면에 표시할 게시물 수

$pages = $page_list * $page; // 한화면에 출력 되어지는 개시물

$total_page = ceil($total_tag / $page_list); //총페이지 수

$total_pp = ceil($total_page / $page_page); //총 다중 페이지 수

for ($now_pp=0; $now_pp<$total_pp; $now_pp++) {

	if ($now_pp * $page_page <= $page and ($now_pp+1) * $page_page > $page ) {		
		break;
	}
}

$first_page = ($now_pp * $page_page); //처음 다중 페이지

$first_page_next = $first_page + $page_page; //한 화면에 출력한 다중 페이지의 갯수

if ($first_page_next >= $total_page) $first_page_next = $total_page;

$sql = mysql_query(" select * from g4_tag order by date desc limit $pages , $page_list ");
for ($i=0; $row=mysql_fetch_array($sql); $i++) {
	$tag[$i] = $row;	
$sql1 = mysql_query(" select * from g4_popular where pp_word = '{$tag[$i][tag]}' ");
$tagsum[$i] = mysql_num_rows($sql1);
}
?>

<style>
#tag_admin a {
padding:6 6 6 6;
}

#tag_admin a:hover {
border:1px solid #555555;
background:#999999;
padding:5 5 5 5;
color:white;
text-decoration:none;
}
</style>

<table width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr><td>

<table style='padding:10 10 10 10' height=40 bgcolor='#dddddd' width='100%' cellpadding='0' cellspacing='0' border='0'>
<tr>
<td><b>:: 태그 관리</b></td>
<td align=right>총 태그 <?=$total_tag?> 개</td>
</tr>
<tr><td colspan=2 style="padding:10 10 10 10" id=tag_admin>
<A HREF="./admin.tag.php"><B>리스트</b></A>
</td></tr>
</table>

<table width='96%' align=center cellpadding='0' cellspacing='0' border='0'>
<tr><td height=10></td></tr>
<tr><td>
<? if ($status==delok) { // 삭제 하는 부분 

$sql = " delete from g4_tag where tag = '$tags' and ip = '$ip' and date = '$date' and memberid = '$mb_id' ";
mysql_query($sql);

$sql = mysql_query(" select * from g4_tag where tag = '$tags' and ip = '$ip' and date = '$date'  and memberid = '$mb_id' ");
$row8 = mysql_num_rows($sql);
if ($row8==0) {


alert("삭제되었습니다","./admin.tag.php?page=$page");

}else{

alert("삭제할 대상이 없거나 삭제처리 되지 않았습니다","./admin.tag.php?page=$page");

}

} elseif ($status==del) { // 삭제 할꺼냐 확인 하는 부분

$sql = mysql_query(" select * from g4_tag where tag = '$tags' and ip = '$ip' and date = '$date' and memberid = '$mb_id' ");
$row9 = mysql_num_rows($sql);

?>
<script>
if (confirm("정말로 삭제 하시겠습니까?")) {
location.href("./admin.tag.php?status=delok&tags=<?=$tags?>&ip=<?=$ip?>&date=<?=$date?>&mb_id=<?=$mb_id?>&page=<?=$page?>");
}else{
location.href("./admin.tag.php");
}
</script>
<?
} elseif ($status == modify) { // 수정하는 부분 ?>


<table width='100%' cellpadding='0' cellspacing='0' style="padding:5 5 5 5;" border='0'>
<form method='post' action='./admin.tag.php?status=modifyok'>
<input type="hidden" name="page" value="<?=$page?>">
<input type="hidden" name="mb_id" value="<?=$mb_id?>">
<input type="hidden" name="ip" value="<?=$ip?>">
<input type="hidden" name="date" value="<?=$date?>">
<tr height=30>
<td bgcolor=dddddd width=50 align=center>번호</td>
<td bgcolor=dddddd align=center>태그</td>
<td bgcolor=dddddd align=center>Hit</td>
<td bgcolor=dddddd width=100 align=center>아이디</td>
<td bgcolor=dddddd width=120 align=center>IP</td>
<td bgcolor=dddddd width=150 align=center>날짜</td>
</tr>
<tr><td height=5></td></tr>

<tr height=30>
<td width=50 align=center><?=$no?></td>
<td><input type="text" name="tags" value="<?=$tags?>"></td>
<td align=center width=60><?=$hit?></td>
<td align=center width=100><?=$mb_id?></td>
<td align=center width=120><?=$ip?></td>
<td align=center width=150><?=date("Y-m-d H:i:s",$date)?></td>
</tr>
<tr><td height=1 bgcolor=eeeeee colspan=6></td></tr>
<tr><td height=30></td></tr>
<tr><td align=center colspan=6>
<input type="submit" value=" 저 장 ">
<input type="button" value=" 목 록 " onclick="javascript:location.href('./admin.tag.php');">
</td></tr>
</form>
</table>


<? } elseif ($status == modifyok) { 

$sql = " update g4_tag set tag = '$tags' where memberid = '$mb_id' and ip = '$ip' and date = '$date' ";
mysql_query($sql);

alert("수정되었습니다","./admin.tag.php?page=$page");

} else { ?>

<table width='100%' cellpadding='0' cellspacing='0' style="padding:5 5 5 5;" border='0'>
<tr height=30>
<td bgcolor=dddddd width=50 align=center>번호</td>
<td bgcolor=dddddd align=center>태그</td>
<td bgcolor=dddddd align=center>Hit</td>
<td bgcolor=dddddd width=100 align=center>아이디</td>
<td bgcolor=dddddd width=120 align=center>IP</td>
<td bgcolor=dddddd width=150 align=center>날짜</td>
<td bgcolor=dddddd width=30 align=center>수정</td>
<td bgcolor=dddddd width=30 align=center>삭제</td>
</tr>
<tr><td height=5></td></tr>

<? for ($i=0; $i<count($tag); $i++) { 
$num++;

$no = $total_tag - $page * $page_list - $num + 1;
?>
<tr height=30>
<td width=50 align=center><?=$no?></td>
<td><?=$tag[$i][tag]?></td>
<td align=center width=60><?=$tagsum[$i]?></td>
<td align=center width=100><?=$tag[$i][memberid]?></td>
<td align=center width=120><?=$tag[$i][ip]?></td>
<td align=center width=150><?=date("Y-m-d H:i:s",$tag[$i][date])?></td>
<td width=30 align=center><a href="./admin.tag.php?status=modify&tags=<?=$tag[$i][tag]?>&ip=<?=$tag[$i][ip]?>&date=<?=$tag[$i][date]?>&mb_id=<?=$tag[$i][memberid]?>&page=<?=$page?>&no=<?=$no?>&hit=<?=$tagsum[$i]?>">수정</a></td>

<td width=30 align=center><a href="./admin.tag.php?status=del&tags=<?=$tag[$i][tag]?>&ip=<?=$tag[$i][ip]?>&date=<?=$tag[$i][date]?>&mb_id=<?=$tag[$i][memberid]?>&page=<?=$page?>">삭제</a></td>
</tr>
<tr><td height=1 bgcolor=eeeeee colspan=8></td></tr>

<? } ?>

<tr><td height=20></td></tr>

<tr><td align=center colspan=8>

<table align=center cellpadding='0' cellspacing='0' border='0'>
<tr><td>
<?
$first = 0;
//처음 버튼
if ($now_pp != 0) {
	echo "<td width=30><a href='$php_self?page=$first'><b><<</b></a></td>";
}

$prev = ($now_pp-1)*$page_page;
//이전 버튼
if ($now_pp != 0) {
	echo "<td align=center width=50><a href='$php_self?page=$prev'><B><</B></a></td>";
}

for ($i=$first_page; $i<$first_page_next; $i++) {

	if ($i==$page) {
	echo "<td align=center width=30><span style='text-decoration:underline;font:bold;font-size:12pt;'>".($i+1)."</span></td>";
	}else{
	echo "<td align=center width=30><a href='$php_self?page=$i'><span style='color:#999999;font:bold;font-size:11pt;'>".($i+1)."</span></a></td>";
	}

}

$next = ($now_pp+1)*$page_page;
//다음 버튼
if ($now_pp!=$total_pp-1) {
	echo "<td align=center width=50><a href='$php_self?page=$next'><B>></B></a></td>";
}

$end = $total_page-1;
//마지막 버튼
if ($now_pp!=$total_pp-1) {
	echo "<td width=30><a href='$php_self?page=$end'><B>>></B></a></td>";
}

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

</td></tr>

</table>

<? } ?>

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

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