<?php
$sub_menu = "200200";
include_once("./_common.php");

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

$token = get_token();

$sql_common = " from $g4[point_table] ";

$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);

$g4[title] = "포인트관리";
include_once ("./admin.head.php");

$colspan = 8;
?>
<style type='text/css'>
    .mb_sublist li{min-height: 30px; display: inline-block}
    .mb_content{font-size: .9em; letter-spacing: -1px; color: #666}
    .po_datetime{font-size: .9em; letter-spacing: -1px; color: #666; min-width: 140px; text-align: right ; padding-right: 5px}
    .po_point{color: #ff0000}
</style>

<script type="text/javascript">
    var list_update_php = "";
    var list_delete_php = "point_list_delete.php";

    function point_clear(){
        if (confirm("포인트 정리를 하시면 최근 50건 이전의 포인트 부여 내역을 삭제하므로\n\n포인트 부여 내역을 필요로 할때 찾지 못할 수도 있습니다.\n\n\n그래도 진행하시겠습니까?")){
            document.location.href = "./point_clear.php?ok=1";
        }
    }
</script>
<div class='row round-box'>
    <div class="col-sm-12">
        <div class='col-sm-4' style="line-height: 34px; letter-spacing: -1px">
            <?php echo $listall ?> (건수 : <?php echo number_format($total_count) ?>)
            <?php
            if ($mb[mb_id])
                echo "&nbsp;(" . $mb[mb_id] . " 님 포인트 합계 : " . number_format($mb[mb_point]) . "점)";
            else {
                $row2 = sql_fetch(" select sum(po_point) as sum_point from $g4[point_table] ");
                echo "&nbsp;(전체 포인트 합계 : " . number_format($row2[sum_point]) . "점)";
            }
            ?>
            <?php if ($is_admin == "super") { ?><!-- <a href="javascript:point_clear();">포인트정리</a> --><?php } ?>
        </div>
        <div class='ad_search col-sm-8' style="text-align: right">
            <form name='fsearch' method='get' class="form-inline" role="form">
                <div class="form-group">
                    <select name='sfl' class='form-control'>
                        <option value='mb_id'>회원아이디</option>
                        <option value='po_content'>내용</option>
                    </select>
                </div>
                <div class='form-group'>
                    <label class="sr-only">검색어</label>                    
                    <input type='text' name='stx' class='form-control' required itemname='검색어' value='<?php echo $stx ?>' placeholder="검색어"/>
                </div>
                <button type="submit" class="btn btn-primary">찾기</button>
            </form>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-sm-12">


        <form name='fpointlist' method='post' role='form' class='form-horizontal'>
            <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='table table-striped hide-sm'>
                <thead>
                    <tr>
                        <th><input type=checkbox name=chkall value='1' onclick='check_all(this.form)'></th>
                        <th><?php echo subject_sort_link('mb_id') ?>회원아이디</a></th>
                        <th>이름</th>
                        <th>별명</th>
                        <th><?php echo subject_sort_link('po_datetime') ?>일시</a></th>
                        <th><?php echo subject_sort_link('po_content') ?>포인트 내용</a></th>
                        <th><?php echo subject_sort_link('po_point') ?>포인트</a></th>
                        <th>포인트합</th>
                    </tr>
                </thead>
                <?php
                $show_sm = "<ul class='list-group show-sm'>";
                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 from $g4[member_table] where mb_id = '$row[mb_id]' ";
                        $row2 = sql_fetch($sql2);
                    }

                    $mb_nick = get_m_sideview($row[mb_id], $row2[mb_nick], $row2[mb_email], $row2[mb_homepage]);
                    $mb_nick_sm = get_sm_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='$g4[bbs_path]/board.php?bo_table={$row[po_rel_table]}&wr_id={$row[po_rel_id]}' target=_blank>";
                        $link2 = "</a>";
                    }

                    $list = $i % 2;
                    echo "

                    <tr class='list$list col1 ht center'>
                        <td>    <input type='hidden' name=po_id[$i] value='$row[po_id]'>
                        <input type='hidden' name=mb_id[$i] value='$row[mb_id]'><input type=checkbox name=chk[] value='$i'></td>
                        <td><a href='?sfl=mb_id&stx=$row[mb_id]'>$row[mb_id]</a></td>
                        <td>$row2[mb_name]</td>
                        <td>$mb_nick</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_point]) . "&nbsp;</td>
                    </tr> ";
                    $show_sm .= "<li class='list-group-item'><ul class='mb_sublist'>";
                    $show_sm .= "<li><input type='hidden' name='po_id[{$i}]' value='{$row['po_id']}'><input type='hidden' name=mb_id[{$i}] value='{$row['mb_id']}'><input type='checkbox' name='chk[]' value='{$i}'></li>";
                    $show_sm .= "<li><span class='mb_id'><a href='?sfl=mb_id&stx={$row['mb_id']}'>{$row['mb_id']}</a></span> </li>";
                    $show_sm .= "<li style='margin:0 0 0 5px;'>{$mb_nick_sm} </li>";
                    $show_sm .= "<li class='po_datetime'> {$row['po_datetime']} </li>";
                    $show_sm .= "<li> <span class='po_point'>P:" . number_format($row['po_point']) . "</span> ";
                    $show_sm .= " (합:" . number_format($row2['mb_point']) . ")";
                    $show_sm .= " <span class='mb_content'>{$link1}{$row['po_content']}{$link2}</span>";
                    $show_sm .= "</li>";
                    $show_sm .= "";
                    $show_sm .= "</ul></li>";
                }
                $show_sm .= "</ul>";

                if ($i == 0)
                    echo "<tr><td colspan='{$colspan}' align=center height=100 bgcolor=#ffffff>자료가 없습니다.</td></tr>";
                ?>
            </table>
            <?php
            echo $show_sm;
            $pagelist = get_paging($config['cf_m_write_pages'], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&page=");

            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 = "";
            ?>
            <div class='form-action'>
                <input type='button' class='btn btn-primary' value='선택삭제' onclick="btn_check(this.form, 'delete')">
            </div>
            <?php
            if ($pagelist) {
                echo "<div class='col-sm-12 page'" . $pagelist . "</div>";
            }
            ?>
        </form>

        <script type='text/javascript'> document.fsearch.stx.focus();</script>
    </div>
</div>
<div class="row round-box">
    <div class="col-sm-12">

        <?php $colspan = 5 ?>
        <?php echo subtitle('포인트추가') ?>
        <form name='fpointlist2' method='post' onsubmit="return fpointlist2_submit(this);" autocomplete="off" role='form' class='form-inline'>
            <input type='hidden' name=sfl   value='<?php echo $sfl ?>'>
            <input type='hidden' name=stx   value='<?php echo $stx ?>'>
            <input type='hidden' name=sst   value='<?php echo $sst ?>'>
            <input type='hidden' name=sod   value='<?php echo $sod ?>'>
            <input type='hidden' name=page  value='<?php echo $page ?>'>
            <input type='hidden' name=token value='<?php echo $token ?>'>

            <div class="form-group">
                <label class='sr-only'>
                    회원아이디
                </label>
                <input type=text class='form-control' name=mb_id required itemname='회원아이디' placeholder="회원아이디" value='<?php echo $mb_id ?>'>
            </div>
            <div class="form-group">
                <label class='sr-only'>
                    포인트 내용
                </label>
                <input type=text class='form-control' name=po_content required itemname='내용' placeholder="내용" >
            </div>
            <div class="form-group">
                <label class='sr-only'>
                    포인트
                </label>
                <input type=text class='form-control' name=po_point required itemname='포인트' size=10 placeholder="포인트">
            </div>
            <div class="form-group">
                <label class='sr-only'>
                    관리자패스워드
                </label>
                <input type=password class='form-control' name=admin_password required itemname='관리자 패스워드' placeholder="관리자비밀번호">
            </div>
            <div class="form-group">
                <label class='sr-only'>
                    입력 
                </label>
                <button type='submit' class='btn btn-primary'>확인</button>
            </div>
        </form>

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

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