<?php
$sub_menu = "777900";
include_once('./_common.php');
include_once(G5_ADMIN_PATH.'/admin.lib.php');
include_once(AMINA_PATH.'/lib/admin.lib.php');

auth_check($auth[$sub_menu], "r");

$token = get_token();

$sql_common = " from {$g5['amina_mileage']} ";

$sql_search = " where (1) ";
if ($stx) {
    $sql_search .= " and ( ";
    switch ($sfl) {
        case "mb_id" :
            $sql_search .= " ($sfl = '$stx') ";
            break;
        default : 
            $sql_search .= " ($sfl like '%$stx%') ";
            break;
    }
    $sql_search .= " ) ";
}

if (!$sst) {
    $sst  = "po_id";
    $sod = "desc";
}
$sql_order = " order by $sst $sod ";

$sql = " select count(*) as cnt
         $sql_common 
         $sql_search 
         $sql_order ";
$row = sql_fetch($sql);
$total_count = $row['cnt'];

$rows = $config['cf_page_rows'];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") $page = 1; // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

$sql = " select *
          $sql_common
          $sql_search
          $sql_order
          limit $from_record, $rows ";
$result = sql_query($sql);

$listall = "<a href='$_SERVER[PHP_SELF]'>처음</a>";

if ($sfl == "mb_id" && $stx)
    $mb = get_member($stx);

$g5['title'] = $g4['title'] = 'AP관리';
include_once (G5_ADMIN_PATH.'/admin.head.php');

$colspan = 7;

$amina_set_padding = '';
if(AMINA) {
	echo '<br /><br />'."\n";
	$amina_set_padding = ' style="padding:0px 15px;"';
}

?>

<link rel="stylesheet" href="<?php echo AMINA_URL; ?>/css/admin.css" />
<style>
	.ap-tbl { width:100%; border:0px; }
	.ap-tbl th, .ap-tbl td { border:0px; padding:6px; }
	.set_tbl.ap-line { border-top:4px solid #333; border-bottom:1px solid #333; }
	.ap-btn { height:24px; line-height:24px; padding:0px 15px; font-weight:normal; color:#fff;}
</style>
<script>
	var amina_url = "<?php echo AMINA_URL; ?>";
	var gnu_url = "<?php echo AMINA_URL; ?>";
	var bo_table  = "<?php echo $bo_table; ?>";
	var list_update_php = "";
	var list_delete_php = "point.delete.php";
</script>

<script src="<?php echo AMINA_URL; ?>/js/admin.js"></script>

<div id="amina_setup"<?php echo $amina_set_padding; ?>>

<form id="fsearch" name="fsearch" method="get">
<table border=0 class="ap-tbl">
<tr>
	<td width=50% align=left>
        <?php echo $listall; ?> (건수 : <?php echo number_format($total_count); ?>)
        <?php
	        if ($mb['mb_id']) 
		        echo "&nbsp;(" . $mb['mb_id'] ." 님 AP 합계 : " . number_format($mb['mb_7']) . "점)";
			else {
	            $row2 = sql_fetch(" select sum(po_point) as sum_point from {$g5['amina_mileage']} ");
		        echo "&nbsp;(전체 AP 합계 : " . number_format($row2['sum_point']) . "점)";
			}
        ?>
    </td>
    <td width=50% align=right>
        <select name=sfl>
            <option value='mb_id'>회원아이디</option>
            <option value='po_content'>내용</option>
        </select>
        <input type=text name=stx required itemname='검색어' value="<?php echo $stx; ?>">
		<input type="submit" accesskey="s" value=" 검색 " class="btn_black ap-btn">
</tr>
</table>
</form>

<form id="fpointlist" name="fpointlist" method="post">
<input type="hidden" name="sst"   value="<?php echo $sst; ?>">
<input type="hidden" name="sod"   value="<?php echo $sod; ?>">
<input type="hidden" name="sfl"   value="<?php echo $sfl; ?>">
<input type="hidden" name="stx"   value="<?php echo $stx; ?>">
<input type="hidden" name="page"  value="<?php echo $page; ?>">
<input type="hidden" name="token" value="<?php echo $token; ?>">

<table border=0 class="set_tbl ap-line">
<colgroup width=30>
<colgroup width=100>
<colgroup width=140>
<colgroup width=140>
<colgroup width="">
<colgroup width=50>
<colgroup width=80>
<tr class="head_gray">
    <th><input type=checkbox name=chkall value='1' onclick='check_all(this.form)'></th>
    <th style="text-align:left;padding-left:10px;"><?php echo subject_sort_link('mb_id'); ?>회원아이디</a></th>
    <th style="text-align:left;padding-left:10px;">별명(이름)</th>
    <th><?php echo subject_sort_link('po_datetime'); ?>일시</a></th>
    <th><?php echo subject_sort_link('po_content'); ?>AP 내용</a></th>
    <th><?php echo subject_sort_link('po_point'); ?>AP</a></th>
    <th>AP합</th>
</tr>
<?php
for ($i=0; $row=sql_fetch_array($result); $i++) {
    if ($row2['mb_id'] != $row['mb_id']) {
        $sql2 = " select mb_id, mb_name, mb_nick, mb_email, mb_homepage, mb_point, mb_7 from {$g5['member_table']} where mb_id = '{$row['mb_id']}' ";
        $row2 = sql_fetch($sql2);
    }

    $mb_nick = get_sideview($row['mb_id'], $row2['mb_nick'], $row2['mb_email'], $row2['mb_homepage']);

    $link1 = $link2 = '';
    if (!preg_match("/^\@/", $row['po_rel_table']) && $row['po_rel_table']) {
        $link1 = '<a href="'.G5_BBS_URL.'/board.php?bo_table='.$row['po_rel_table'].'&wr_id='.$row['po_rel_id'].'" target="_blank">';
        $link2 = '</a>';
    }

	$bg = $i%2 ? ' class="bg_gray"' : '';
    echo '
    <input type="hidden" name="po_id['.$i.']" value="'.$row['po_id'].'">
    <input type="hidden" name="mb_id['.$i.']" value="'.$row['mb_id'].'">
    <tr'.$bg.'>
        <td><input type="checkbox" name="chk[]" value="'.$i.'"></td>
        <td><a href="?sfl=mb_id&stx='.$row['mb_id'].'">'.$row['mb_id'].'</a></td>
        <td>'.$mb_nick.'('.$row2['mb_name'].')</td>
		<td>'.$row['po_datetime'].'</td>
        <td align=left>&nbsp;'.$link1.$row['po_content'].$link2.'</td>
        <td align=right>'.number_format($row['po_point']).'&nbsp;</td>
        <td align=right>'.number_format($row2['mb_7']).'&nbsp;</td>
    </tr> ';
} 

if ($i == 0)
    echo '<tr><td colspan="'.$colspan.'" align=center height=200>자료가 없습니다.</td></tr>';

echo '</table>';

$pagelist = get_paging($config['cf_write_pages'], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&page=");
echo '<table class="ap-tbl">';
echo '<tr><td width=50%>';
echo '<input type="button" class="btn_red ap-btn" value="선택삭제" onclick="btn_check(this.form, \'delete\');">';
echo '</td>';
echo '<td width=50% align=right>'.$pagelist.'</td></tr></table>'."\n";

if ($stx)
    echo '<script type="text/javascript">document.fsearch.sfl.value = "'.$sfl.'";</script>'."\n";

if (strstr($sfl, "mb_id"))
    $mb_id = $stx;
else
    $mb_id = '';
?>
</form>

<script type="text/javascript"> document.fsearch.stx.focus(); </script>

<br />

<form id="fpointlist2" name="fpointlist2" method="post" onsubmit="return fpointlist2_submit(this);" autocomplete="off">
<input type="hidden" name="sst"   value="<?php echo $sst; ?>">
<input type="hidden" name="sod"   value="<?php echo $sod; ?>">
<input type="hidden" name="sfl"   value="<?php echo $sfl; ?>">
<input type="hidden" name="stx"   value="<?php echo $stx; ?>">
<input type="hidden" name="page"  value="<?php echo $page; ?>">
<input type="hidden" name="token" value="<?php echo $token; ?>">

<table class="set_tbl ap-line">
<colgroup width=120>
<colgroup width=''>
<colgroup width=100>
<colgroup width=100>
<tr class="head_gray">
    <th>회원아이디</th>
    <th>AP 내용</th>
    <th>AP</th>
    <th>입력</th>
</tr>
<tr>
    <td><input type=text name=mb_id required itemname='회원아이디' value='<?=$mb_id?>'></td>
    <td><input type=text name=po_content required itemname='내용' style='width:99%;'></td>
    <td><input type=text name=po_point required itemname='포인트' size=15></td>
    <td><input type=submit class="btn_blue ap-btn" value="  확  인  "></td>
</tr>
</form>
</table>

</div>

<script type="text/javascript">
function fpointlist2_submit(f)
{
    f.action = "./point.update.php";
    return true;
}
</script>

<?
include_once (G5_ADMIN_PATH.'/admin.tail.php');
?>
