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

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

if (empty($fr_date))
    $fr_date = $g4[time_ymd];
if (empty($to_date))
    $to_date = $g4[time_ymd];

$qstr = "fr_date=$fr_date&to_date=$to_date";

$sql_common = " from $g4[popular_table] a ";
$sql_search = " where trim(pp_word) <> '' and pp_date between '$fr_date' and '$to_date' ";
$sql_group = " group by pp_word ";
$sql_order = " order by cnt desc ";

$sql = " select pp_word
         $sql_common
         $sql_search
         $sql_group ";
$result = sql_query($sql);
$total_count = mysql_num_rows($result);

$rows = $config[cf_page_rows];
$total_page = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") {
    $page = 1;
} // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

$sql = " select pp_word, count(*) as cnt 
          $sql_common
          $sql_search
          $sql_group
          $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 = 3;
?>
<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'>
                <input type='date' id='fr_date' name='fr_date' size=11 maxlength=10 value='<?php echo $fr_date ?>' class='form-control'>
            </div>
            <div class='form-group'>
                <input type='date' id='to_date' name='to_date' size=11 maxlength=10 value='<?php echo $to_date ?>' class='form-control'>
            </div>
            <button type='submit' class='btn btn-primary'>검색</button>
        </form>
    </div>
</div>


<form name=fpopularrank 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-striped'>
        <thead>
            <tr>
                <th>검색어</th>
                <th>검색회수</th>
                <th>순위</th>
            </tr>
        </thead>
        <?php
        for ($i = 0; $row = sql_fetch_array($result); $i++) {

            $word = get_text($row[pp_word]);
            $rank = ($i + 1 + ($rows * ($page - 1)));

            $list = $i % 2;
            echo "<tr>";
            echo "<td align='left'>&nbsp; $word</td>";
            echo "<td>$row[cnt]</td>";
            echo "<td>$rank</td>";
            echo "</tr>";
        }

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

    </table>
    <?php

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

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