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

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

// 체크된 자료 삭제
if (is_array($_POST['chk'])) {
    for ($i = 0; $i < count($chk); $i++) {
        // 실제 번호를 넘김
        $k = $chk[$i];

        sql_query(" delete from $g4[popular_table] where pp_id = '{$_POST['pp_id'][$k]}' ", true);
    }
}

$sql_common = " from $g4[popular_table] a ";
$sql_search = " where (1) ";

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

if (!$sst) {
    $sst = "pp_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>";

$g4[title] = "인기검색어관리";
include_once("./admin.head.php");

$colspan = 4;
?>

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

<div class='row 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'>
                <select name='sfl' class='form-control'>
                    <option value='pp_word'>검색어</option>
                    <option value='pp_date'>등록일</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>
        </form>
    </div>
</div>
<div class="row">
    <div class='col-sm-12'>
        <form name='fpopularlist' method='post' class='form-inline' 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 ?>">
            <table class='table table-striped'>
                <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("pp_word") ?>검색어</a></th>
                        <th>등록일</th>
                        <th>등록IP</th>
                    </tr>
                </thead>
                <?php
                for ($i = 0; $row = sql_fetch_array($result); $i++) {

                    $word = get_text($row[pp_word]);

                    $list = $i % 2;
                    echo "<input type='hidden' name='pp_id[$i]' value='{$row['pp_id']}'>";
                    echo "<tr>";
                    echo "<td><input type='checkbox' name=chk[] value='$i'></td>";
                    echo "<td align='left'>&nbsp; <a href='$_SERVER[PHP_SELF]?sfl=pp_word&stx=$word'>$word</a></td>";
                    echo "<td>$row[pp_date]</td>";
                    echo "<td>$row[pp_ip]</td>";
                    echo "</tr>";
                    echo "<tr>";
                    echo "</tr>\n";
                }

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

    ?>
            </table>
                <?php
                $pagelist = get_paging($config['cf_m_write_pages'], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&page=");

                if ($is_admin == "super")
                    echo "<div class='col-sm-12'><input type=button class='btn1' value='선택삭제' onclick=\"btn_check(this.form, 'delete')\"></div>";

                echo "<div class='col-sm-12 page'>{$pagelist}</div>";
                if ($stx)
                    echo "<script>document.fsearch.sfl.value = '$sfl';</script>";
                ?>
        </form>
    </div>
</div>
<?php
include_once("./admin.tail.php");
?>
