<?php
/*
 * 회원목록
 */
$sub_menu = "200100";
include_once("./_common.php");

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

$token = get_token();

$sql_common = " from {$g4['member_table']} ";

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

//if ($is_admin == 'group') $sql_search .= " and mb_level = '$member[mb_level]' ";
if ($is_admin != 'super')
    $sql_search .= " and mb_level <= '$member[mb_level]' ";

if (!$sst) {
    $sst = "mb_datetime";
    $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 count(*) as cnt {$sql_common} {$sql_search} and mb_leave_date <> '' {$sql_order} ";
$row = sql_fetch($sql);
$leave_count = $row['cnt'];

// 차단회원수
$sql = " select count(*) as cnt {$sql_common} {$sql_search} and mb_intercept_date <> '' {$sql_order} ";
$row = sql_fetch($sql);
$intercept_count = $row['cnt'];

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

$g4[title] = "회원관리";
include_once("./admin.head.php");

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

$colspan = 15;
?>

<script type="text/javascript">
    var list_update_php = "member_list_update.php";
    var list_delete_php = "member_list_delete.php";
</script>
<style type='text/css'>
    .mb_sublist li{min-height: 40px; display: inline-block; margin: 0 10px 0 0}
</style>
<div class='row round-box'>
    <div class="col-sm-12">
        <div class='col-sm-3' style="line-height: 34px">
            <?php echo $listall ?> 
            (총회원수 : <?php echo number_format($total_count) ?>, 
            <a href='?sst=mb_intercept_date&sod=desc&sfl=<?php echo $sfl ?>&stx=<?php echo $stx ?>' title='차단된 회원부터 출력'><font color=orange>차단 : <?php echo number_format($intercept_count) ?></font></a>, 
            <a href='?sst=mb_leave_date&sod=desc&sfl=<?php echo $sfl ?>&stx=<?php echo $stx ?>' title='탈퇴한 회원부터 출력'><font color=crimson>탈퇴 : <?php echo number_format($leave_count) ?></font></a>)
        </div>
        <div class='ad_search col-sm-9' 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='mb_name'>이름</option>
                        <option value='mb_nick'>별명</option>
                        <option value='mb_level'>권한</option>
                        <option value='mb_email'>E-MAIL</option>
                        <option value='mb_tel'>전화번호</option>
                        <option value='mb_hp'>핸드폰번호</option>
                        <option value='mb_point'>포인트</option>
                        <option value='mb_datetime'>가입일시</option>
                        <option value='mb_ip'>IP</option>
                        <option value='mb_recommend'>추천인</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>


<form name='fmemberlist' method='post' class='form-horizontal' role='form'>
    <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 ?>'/>
    <!-- 768px 이상이면 테이블로 출력한다. -->
    <table class="table table-striped hide-sm">
        <thead>
            <tr>
                <th style="width: 24px;"><input type='checkbox' name='chkall' value='1' onclick='check_all(this.form)'></th>
                <th><?php echo subject_sort_link('mb_id') ?>회원아이디</a></th>
                <th><?php echo subject_sort_link('mb_name') ?>이름</a></th>
                <th><?php echo subject_sort_link('mb_nick') ?>별명</a></th>
                <th><?php echo subject_sort_link('mb_level', '', 'desc') ?>권한</a></th>
                <th><?php echo subject_sort_link('mb_point', '', 'desc') ?>포인트</a></th>
                <th><?php echo subject_sort_link('mb_today_login', '', 'desc') ?>최종접속</a></th>
                <th title='메일수신허용여부'><?php echo subject_sort_link('mb_mailling', '', 'desc') ?>수신</a></th>
                <th title='정보공개여부'><?php echo subject_sort_link('mb_open', '', 'desc') ?>공개</a></th>
                <!-- <th><?php echo subject_sort_link('mb_leave_date', '', 'desc') ?>탈퇴</a></th> -->
                <th><?php echo subject_sort_link('mb_email_certify', '', 'desc') ?>인증</a></th>
                <th><?php echo subject_sort_link('mb_intercept_date', '', 'desc') ?>차단</a></th>
                <th title='접근가능한 그룹수'>그룹</th>
                <th style="width: 100px;"><a href="./member_form.php" class="btn btn-info">추가</a></th>
            </tr>
        </thead>
        <tbody>
            <?php
            $show_sm = "<div class='panel panel-default show-sm'><div class='panel-heading'>회원목록</div><div class='panel-body'><a href='./member_form.php' class='btn btn-info'>회원추가</a></div>";
            $show_sm .= "<ul class='list-group'>";
            for ($i = 0; $row = sql_fetch_array($result); $i++) {
                // 접근가능한 그룹수
                $sql2 = " select count(*) as cnt from $g4[group_member_table] where mb_id = '$row[mb_id]' ";
                $row2 = sql_fetch($sql2);
                $group = "";
                if ($row2[cnt])
                    $group = "<a href='./boardgroupmember_form.php?mb_id=$row[mb_id]'>$row2[cnt]</a>";

                if ($is_admin == 'group') {
                    $s_mod = "";
                    $s_del = "";
                } else {
                    $s_mod = "<a href=\"./member_form.php?$qstr&w=u&mb_id=$row[mb_id]\"><img src='img/icon_modify.gif' border=0 title='수정'></a>";
                    //$s_del = "<a href=\"javascript:del('./member_delete.php?$qstr&w=d&mb_id=$row[mb_id]');\"><img src='img/icon_delete.gif' border=0 title='삭제'></a>";
                    $s_del = "<a href=\"javascript:post_delete('member_delete.php', '$row[mb_id]');\"><img src='img/icon_delete.gif' border=0 title='삭제'></a>";
                }
                $s_grp = "<a href='./boardgroupmember_form.php?mb_id=$row[mb_id]'><img src='img/icon_group.gif' border=0 title='그룹'></a>";

                $leave_date = $row[mb_leave_date] ? $row[mb_leave_date] : date("Ymd", $g4[server_time]);
                $intercept_date = $row[mb_intercept_date] ? $row[mb_intercept_date] : date("Ymd", $g4[server_time]);

                $mb_nick = get_m_sideview($row['mb_id'], $row['mb_nick'], $row['mb_email'], $row['mb_homepage']);
                $mb_nick_sm = get_sm_sideview($row['mb_id'], $row['mb_nick'], $row['mb_email'], $row['mb_homepage']);
                $mb_id = $row['mb_id'];
                if ($row['mb_leave_date'])
                    $mb_id = "<font color=crimson>{$mb_id}</font>";
                else if ($row['mb_intercept_date'])
                    $mb_id = "<font color=orange>{$mb_id}</font>";

                $list = $i % 2;
                echo "
                
                <tr>
                    <td><input type='hidden' name=mb_id[{$i}] value='{$row['mb_id']}'><input type=checkbox name=chk[] value='{$i}'></td>
                    <td title='{$row[mb_id]}'><nobr style='display:block; overflow:hidden; width:90;'>&nbsp;{$mb_id}</nobr></td>
                    <td><nobr style='display:block; overflow:hidden; width:90px;'>{$row[mb_name]}</nobr></td>
                    <td> {$mb_nick} </td>
                    <td>" . get_member_level_select("mb_level[$i]", 1, $member[mb_level], $row[mb_level]) . "</td>
                    <td align=right><a href='point_list.php?sfl=mb_id&stx=$row[mb_id]' class=tt>" . number_format($row[mb_point]) . "</a>&nbsp;</td>
                    <td>" . substr($row[mb_today_login], 2, 8) . "</td>
                    <td>" . ($row[mb_mailling] ? '&radic;' : '&nbsp;') . "</td>
                    <td>" . ($row[mb_open] ? '&radic;' : '&nbsp;') . "</td>
                    <!-- <td title='$row[mb_leave_date]'>" . ($row[mb_leave_date] ? '&radic;' : '&nbsp;') . "</td> -->
                    <td title='$row[mb_email_certify]'>" . (preg_match('/[1-9]/', $row[mb_email_certify]) ? '&radic;' : '&nbsp;') . "</td>
                    <td title='$row[mb_intercept_date]'><input type=checkbox name=mb_intercept_date[$i] " . ($row[mb_intercept_date] ? 'checked' : '') . " value='$intercept_date'></td>
                    <td>$group</td>               
                    <td>$s_mod $s_del $s_grp</td>
                </tr>";
                $show_sm .= "<li class='list-group-item'><ul class='mb_sublist'>";
                $show_sm .= "<li><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 glyphicon glyphicon-user'>{$mb_id}</span> </li>";
                $show_sm .= "<li><span class='mb_name glyphicon glyphicon-tag'>{$row['mb_name']}</li>";
                $show_sm .= "<li> {$mb_nick_sm}</li>";
                $show_sm .= "<li> ".get_member_level_select("mb_level[$i]", 1, $member['mb_level'], $row['mb_level'])."</li>";
                $show_sm .= "<li><span class='mb_point glyphicon glyphicon-usd'>".number_format($row['mb_point'])."</span></li>";
                $show_sm .= "<li><span class='mb_login glyphicon glyphicon-certificate'>".substr($row['mb_today_login'], 2, 8)."</span></li>";
                $show_sm .= "<li>{$s_mod} {$s_del} {$s_grp}</li>";
                $show_sm .= "";
                $show_sm .= "</ul></li>";
            }
            $show_sm .= "</ul></div>";
            if ($i == 0) {
                echo "<tr><td colspan='{$colspan}' align='center' height='100' class='contentbg'>자료가 없습니다.</td></tr>";
            }
            ?>
        </tbody>
    </table>
    <div class="alert alert-info">
        * 회원자료 삭제시 다른 회원이 기존 회원아이디를 사용하지 못하도록 회원아이디, 이름, 별명은 삭제하지 않고 영구 보관합니다.
    </div>
    
    <?php
    echo $show_sm;
    $pagelist = get_paging($config['cf_m_write_pages'], $page, $total_page, "?{$qstr}&page=");
    ?>
    <div class="form-action col-sm-6">
        <input type='button' class='btn btn-primary' value='선택수정' onclick='btn_check(this.form, "update")'/>
    </div>
    <div class="form-action col-sm-6" style="text-align: right">
        <input type='button' class='btn btn-danger' value='선택삭제' onclick='btn_check(this.form, "delete")'/>        
    </div>

    <?php
    if($pagelist)
        echo "<div class='col-sm-12 page'>" . $pagelist . "</div>";

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



<script type="text/javascript">
    // POST 방식으로 삭제
    function post_delete(action_url, val) {
        var f = document.fpost;

        if (confirm("한번 삭제한 자료는 복구할 방법이 없습니다.\n\n정말 삭제하시겠습니까?")) {
            f.mb_id.value = val;
            f.action = action_url;
            f.submit();
        }
    }
</script>

<form name='fpost' 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 ?>'/>
    <input type='hidden' name='mb_id' />
</form>



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