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

if ($is_admin != "super")
    alert("최고관리자만 접근 가능합니다.");

$token = get_token();

$sql_common = " from {$g4['auth_table']} a left join {$g4['member_table']} b on (a.mb_id=b.mb_id) ";

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

if (!$sst) {
    $sst = "a.mb_id, au_menu";
    $sod = "";
}
$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']}' class=tt>처음</a>";

$g4[title] = "관리권한설정";
include_once("./admin.head.php");

$colspan = 5;
?>

<script type="text/javascript">
    var list_update_php = "";
    var list_delete_php = "auth_list_delete.php";
</script>
<div class='row'>
    <div class="col-sm-12 round-box">
        <div class='col-sm-3' style="line-height:34px;">
            <?php echo $listall ?> (건수 : <?php echo number_format($total_count) ?>)
        </div>
        <div class='col-sm-9' style="text-align: right">
            <form name='fsearch' method='get' class="form-inline" role="form">
                <div class='form-group'>
                    <input type='hidden' name='sfl' value="a.mb_id" />
                    <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='fauthlist' 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">
                <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('a.mb_id') ?>회원아이디</a></th>
                        <th><?php echo subject_sort_link('mb_nick') ?>별명</a></th>
                        <th>메뉴</th>
                        <th>권한</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    for ($i = 0; $row = sql_fetch_array($result); $i++) {
                        $mb_nick = get_m_sideview($row[mb_id], $row[mb_nick], $row[mb_email], $row[mb_homepage]);

                        // 메뉴번호가 바뀌는 경우에 현재 없는 저장된 메뉴는 삭제함
                        if (!isset($auth_menu[$row[au_menu]])) {
                            sql_query(" delete from $g4[auth_table] where au_menu = '$row[au_menu]' ");
                            continue;
                        }

                        $list = $i % 2;
                        echo "
                    <input type='hidden' name=mb_id[$i] value='$row[mb_id]'>
                    <input type='hidden' name=au_menu[$i] value='$row[au_menu]'>
                    <tr class='list$list col1 ht center'>
                        <td><input type=checkbox name=chk[] value='$i'></td>
                        <td><a href='?sfl=a.mb_id&stx=$row[mb_id]'>$row[mb_id]</a></td>
                        <td>{$mb_nick}</td>
                        <td align=left>&nbsp; [{$row['au_menu']}] {$auth_menu[$row[au_menu]]}</td>
                        <td>{$row['au_auth']}</td>
                    </tr>";
                    }

                    if ($i == 0) {
                        echo "<tr><td colspan='{$colspan}' height=100 align=center bgcolor='#FFFFFF'>자료가 없습니다.</td></tr>";
                    }
                    ?>
                </tbody>
            </table>
            <?php
            $pagelist = "<div style='padding:5px; text-align:center; margin:0 auto;'>" . get_paging($config['cf_m_write_pages'], $page, $total_page, "{$_SERVER['PHP_SELF']}?$qstr&page=") . "</div>";
            ?>

            <input type='button' class='btn btn-danger btn-100' value='선택삭제' onclick="btn_check(this.form, 'delete')">

            <?php echo $pagelist ?>

            <?php
            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>

        <?php $colspan = 5 ?>
        <p>

        <form name='fauthlist2' method='post' onsubmit="return fauthlist2_submit(this);" autocomplete="off" class="form-horizontal">
            <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 ?>'/>

            <table class="table table-bordered ">
                <thead>
                    <tr>
                        <th>회원아이디</th>
                        <th>접근가능메뉴</th>
                        <th>권한</th>
                        <th>관리자패스워드</th>
                        <th>입력</th>
                    </tr>
                </thead>
                <tr>
                    <td><input type='text' class='form-control' name=mb_id required itemname='회원아이디' value='<?php echo $mb_id ?>'></td>
                    <td>
                        <select name='au_menu' class="form-control" required itemname='접근가능메뉴'>
                            <option value=''>-- 선택하세요
                                <?php
                                foreach ($auth_menu as $key => $value) {
                                    if (!(substr($key, -3) == "000" || $key == "-" || !$key))
                                        echo "<option value='$key'>[$key] $value</option>";
                                }
                                ?>
                        </select>
                    </td>
                    <td>
                        <div class='pull-left' style="margin-right: 5px">
                            <label class='control-label'><input type='checkbox' name='r' value='r' checked='checked'/>r(읽기)</label>
                        </div>
                        <div class='pull-left' style="margin-right: 5px">
                            <label class='control-label'><input type='checkbox' name='w' value='w'>w(입력,수정)</label>
                        </div>
                        <div class='pull-left'>
                            <label class='control-label'><input type='checkbox' name='d' value='d'>d(삭제)</label>
                        </div>
                    </td>
                    <td><input type='password' class='form-control' name=admin_password required itemname='관리자 패스워드' placeholder="관리자 패스워드"/></td>
                    <td><input type='submit' class='btn btn-primary' value='  확  인  '></td>
                </tr>
            </table>

        </form>

        <script type="text/javascript">
            function fauthlist2_submit(f)
            {
                f.action = "./auth_update.php";
                return true;
            }
        </script>
    </div>
</div>
<?php
include_once ("./admin.tail.php");
?>
