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

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

$token = get_token();

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

$sql_search = " where (1) ";
if ($is_admin != "super")
    $sql_search .= " and (gr_admin = '{$member['mb_id']}') ";

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

if ($sst)
    $sql_order = " order by {$sst} {$sod} ";
else
    $sql_order = " order by gr_id asc ";

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

$g4['title'] = "게시판그룹설정";
include_once("./admin.head.php");

$colspan = 8;
?>

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

<div class='row'>
    <div class="round-box">
        <form name='fsearch' method='get' class="form-inline" role='form'>
            <div class='col-sm-3' style="line-height: 32px">
                <?php echo $listall ?> (그룹수 : <?php echo number_format($total_count) ?>개)
            </div>
            <div class='col-sm-9' style="text-align: right">
                <div class='form-group'>
                    <select name='sfl' class="form-control">
                        <option value="gr_subject">제목</option>
                        <option value="gr_id">ID</option>
                        <option value="gr_admin">그룹관리자</option>
                    </select>
                </div>
                <div class='form-group'>
                    <input type='text' name='stx' class='form-control' required itemname='검색어' value='<?php echo $stx ?>' placeholder="검색어"/>
                </div>
                <button type='submit' class='btn btn-primary'>검색</button>
            </div>
        </form>
    </div>
    <div class='col-sm-12'>



        <form name='fboardgrouplist' 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 class='table table-bordered hide-sm'>
                <thead>
                    <tr>
                        <th><input type=checkbox name=chkall value="1" onclick="check_all(this.form)"></th>
                        <th><?php echo subject_sort_link("gr_id") ?>그룹아이디</a></th>
                        <th><?php echo subject_sort_link("gr_subject") ?>제목</a></th>
                        <th><?php echo subject_sort_link("gr_admin") ?>그룹관리자</a></th>
                        <th>게시판</th>
                        <th>접근사용</th>
                        <th>접근회원수</th>
                        <th>
                            <?php
                            if ($is_admin == "super") {
                                echo "<a href='./boardgroup_form.php' class='btn btn-info'>생성</a>";
                            }
                            ?>
                        </th>
                    </tr>
                </thead>
                <?php
                $show_sm = '<div class="panel panel-default show-sm">
                            <div class="panel-heading">그룹목록</div>
                            <div class="panel-body">
                            <a href="./boardgroup_form.php" class="btn btn-info">생성</a>
                            </div><ul class="list-group">';
                for ($i = 0; $row = sql_fetch_array($result); $i++) {
                    // 접근회원수
                    $sql1 = " select count(*) as cnt from $g4[group_member_table] where gr_id = '$row[gr_id]' ";
                    $row1 = sql_fetch($sql1);

                    // 게시판수
                    $sql2 = " select count(*) as cnt from $g4[board_table] where gr_id = '$row[gr_id]' ";
                    $row2 = sql_fetch($sql2);

                    $s_upd = "<a href='./boardgroup_form.php?$qstr&w=u&gr_id=$row[gr_id]'><img src='img/icon_modify.gif' border=0 title='수정'></a>";
                    $s_del = "";
                    if ($is_admin == "super") {
                        //$s_del = "<a href=\"javascript:del('./boardgroup_delete.php?$qstr&gr_id=$row[gr_id]');\"><img src='img/icon_delete.gif' border=0 title='삭제'></a>";
                        $s_del = "<a href=\"javascript:post_delete('boardgroup_delete.php', '$row[gr_id]');\"><img src='img/icon_delete.gif' border=0 title='삭제'></a>";
                    }

                    $list = $i % 2;
                    echo "<input type='hidden' name=gr_id[{$i}] value='{$row['gr_id']}'>";
                    echo "<tr class='list$list' onmouseover=\"this.className='mouseover';\" onmouseout=\"this.className='list$list';\" height=27 align=center>";
                    echo "<td><input type=checkbox name=chk[] value='$i'></td>";
                    echo "<td><a href='{$g4['g4m_bbs_path']}/group.php?gr_id=$row[gr_id]'><b>$row[gr_id]</b></a></td>";
                    echo "<td><input type='text' class='form-control' name='gr_subject[$i]' value='" . get_text($row[gr_subject]) . "' size=30></td>";

                    if ($is_admin == "super")
                    //echo "<td>".get_member_id_select("gr_admin[$i]", 9, $row[gr_admin])."</td>";
                        echo "<td><input type=text class='form-control' name=gr_admin[$i] value='{$row['gr_admin']}' maxlength=20></td>";
                    else
                        echo "<input type='hidden' name='gr_admin[$i]' value='{$row['gr_admin']}'><td>{$row['gr_admin']}</td>";

                    echo "<td><a href='./board_list.php?sfl=a.gr_id&stx={$row['gr_id']}'>{$row2['cnt']}</a></td>";
                    echo "<td><input type=checkbox name=gr_use_access[$i] " . ($row['gr_use_access'] ? 'checked' : '') . " value='1'></td>";
                    echo "<td><a href='./boardgroupmember_list.php?gr_id={$row['gr_id']}'>{$row1['cnt']}</a></td>";
                    echo "<td>{$s_upd} {$s_del}</td>";
                    echo "</tr>\n";
                    $show_sm .= "<li class='list-group-item'>";
                    $show_sm .= "<input type='hidden' name=gr_id[{$i}] value='{$row['gr_id']}'>";
                    $show_sm .= "<input type='checkbox' name='chk[]' value='$i'>";
                    $show_sm .= "<a href='{$g4['g4m_bbs_path']}/group.php?gr_id={$row['gr_id']}'>{$row['gr_id']}</a>";
                    $show_sm .= "<input type='text' class='form-control' name='gr_subject[$i]' value='" . get_text($row['gr_subject']) . "' size=30>";
                    if ($is_admin == "super")
                    //echo "<td>".get_member_id_select("gr_admin[$i]", 9, $row[gr_admin])."</td>";
                        $show_sm .= "<input type='text' class='form-control' name=gr_admin[$i] value='{$row['gr_admin']}' maxlength=20 placeholder='그룹관리자'>";
                    else
                        $show_sm .= "<input type='hidden' name='gr_admin[$i]' value='{$row['gr_admin']}'><td>{$row['gr_admin']}";
                    $show_sm .= "(게시판 : <a href='./board_list.php?sfl=a.gr_id&stx={$row['gr_id']}'>{$row2['cnt']}</a>)";
                    $show_sm .= "<label>접근사용<input type='checkbox' name='gr_use_access[$i]' " . ($row['gr_use_access'] ? 'checked' : '') . " value='1'></label>";
                    $show_sm .= "(접근회원수 : <a href='./boardgroupmember_list.php?gr_id={$row['gr_id']}'>{$row1['cnt']}</a>)";
                    $show_sm .= "{$s_upd} {$s_del}";
                    $show_sm .= "</li>";
                }

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

            <input type='button' class='btn btn-primary' value='선택수정' onclick="btn_check(this.form, 'update')">


            <div class="col-sm-12">
                <?php echo $pagelist ?>
            </div>
            <?php
            if ($stx)
                echo "<script>document.fsearch.sfl.value = '$sfl';</script>";
            ?>
        </form>
    </div>
</div>
<script>
// POST 방식으로 삭제
    function post_delete(action_url, val) {
        var f = document.fpost;

        if (confirm("한번 삭제한 자료는 복구할 방법이 없습니다.\n\n정말 삭제하시겠습니까?")) {
            f.gr_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='gr_id'>
</form>

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