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

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

$search_sort = $_GET['search_sort'];

$g4['title'] = "접속자검색";
include_once("./admin.head.php");

include_once "{$g4['path']}/lib/visit.lib.php";

$qstr = "search_word=$search_word&search_sort=$search_sort"; //페이징 처리관련 변수

$colspan = 5;

$listall = "<a href='{$_SERVER['PHP_SELF']}' class=tt>처음</a>"; //페이지 처음으로 (초기화용도)
?>
<style type='text/css'>
    .link{word-break: break-all}
</style>
<div class='row round-box'>
    <div class='col-sm-12'>
        <div class="col-sm-2" style="line-height: 34px;">
            <?php echo $listall ?>
        </div>
        <div class="col-sm-10" style="text-align:right">
            <form name="fvisit" method="get" class='form-inline' role='form'>

                <div class="form-group">
                    <label class="sr-only">검색분류</label>
                    <select name="search_sort" id="sch_sort" class="search_sort form-control">
                        <?php
                        //echo '<option value="vi_ip" '.($search_sort=='vi_ip'?'selected="selected"':'').'>IP</option>'; //selected 추가
                        if ($search_sort == 'vi_ip') { //select 안의 옵셥값이 vi_ip면
                            echo '<option value="vi_ip" selected="selected">IP</option>'; //selected 추가
                        } else {
                            echo '<option value="vi_ip">IP</option>';
                        }
                        if ($search_sort == 'vi_referer') { //select 안의 옵셥값이 vi_referer면
                            echo '<option value="vi_referer" selected="selected">접속경로</option>'; //selected 추가
                        } else {
                            echo '<option value="vi_referer">접속경로</option>';
                        }
                        if ($search_sort == 'vi_date') { //select 안의 옵셥값이 vi_date면
                            echo '<option value="vi_date" selected="selected">날짜</option>'; //selected 추가
                        } else {
                            echo '<option value="vi_date">날짜</option>';
                        }
                        ?>
                    </select>
                </div>
                <div class='form-group'>
                    <input type="text" name="search_word" size="20" value="<?php echo $search_word ?>" id="sch_word" class="form-control">
                </div>
                <div class='form-group'>
                    <input type="submit" alt="검색" align="absmiddle" onclick="fvisit_submit('visit_search.php');" class='btn btn-sm btn-primary' value="검색"/>
                </div>
            </form>
        </div>
    </div>
</div>
<div class='row'>
    <div class='col-sm-12'>
        <table class='table table-striped'>
            <thead>
                <tr>
                    <td>IP</td>
                    <td>접속 경로</td>
                    <td class='hide-sm'>브라우저</td>
                    <td class='hide-sm'>OS</td>
                    <td class='hide-sm'>일시</td>
                </tr>

            </thead>
            <?php
            $sql_common = " from {$g4['visit_table']} ";
            if ($search_sort) {
                if ($search_sort == 'vi_ip' || $search_sort == 'vi_date') {
                    $sql_search = " where $search_sort like '$search_word%' ";
                } else {
                    $sql_search = " where $search_sort like '%$search_word%' ";
                }
            }
            $sql = " select count(*) as cnt
         $sql_common 
         $sql_search ";
            $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
          order by vi_id desc
          limit $from_record, $rows ";
            $result = sql_query($sql);

            for ($i = 0; $row = sql_fetch_array($result); $i++) {
                $brow = get_brow($row['vi_agent']);
                $os = get_os($row['vi_agent']);

                $link = "";
                $referer = "";
                $title = "";
                if ($row['vi_referer']) {

                    /*
                      $referer = $row['vi_referer'];
                      $referer = htmlspecialchars($referer);
                     */
                    $referer = get_text(cut_str($row[vi_referer], 255, ""));
                    $referer = urldecode($referer);

                    if (strtolower($g4['charset']) == 'utf-8') {
                        if (!is_utf8($referer)) {
                            $referer = iconv('euc-kr', 'utf-8', $referer);
                        }
                    } else {
                        if (is_utf8($referer)) {
                            $referer = iconv('utf-8', 'euc-kr', $referer);
                        }
                    }

                    $title = str_replace(array("<", ">"), array("&lt;", "&gt;"), $referer);
                    $link = "<a href='$row[vi_referer]' target=_blank title='$title '>{$title}</a>";
                }

                if ($is_admin == 'super')
                    $ip = $row['vi_ip'];
                else
                    $ip = preg_replace("/([0-9]+).([0-9]+).([0-9]+).([0-9]+)/", "\\1.♡.\\3.\\4", $row['vi_ip']);

                if ($brow == '기타') {
                    $brow = "<span title='$row[vi_agent]'>$brow</span>";
                }
                if ($os == '기타') {
                    $os = "<span title='$row[vi_agent]'>$os</span>";
                }

                $list = ($i % 2);
                echo "
                <tr>
                    <td align='left'>&nbsp;<a href='{$_SERVER['PHP_SELF']}?search_sort=vi_ip&amp;search_word=$ip'>$ip</a></td>
                    <td align='left' class='link'>{$link}<span class='show-sm'>{$brow} {$os} <a href='{$_SERVER['PHP_SELF']}?search_sort=vi_date&amp;search_word={$row['vi_date']}'>{$row['vi_date']}</a> {$row['vi_time']}</span></td>
                    <td class='hide-sm'>{$brow}</td>
                    <td class='hide-sm'>{$os}</td>
                    <td class='hide-sm'><a href='{$_SERVER['PHP_SELF']}?search_sort=vi_date&amp;search_word={$row['vi_date']}'>{$row['vi_date']}</a> {$row['vi_time']}</td>
                </tr>";
            }

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

        <?php
        $page = get_paging($config['cf_m_write_pages'], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&domain=$domain&page=");
        if ($page) {
            echo "<div class='page col-sm-12'>{$page}</div>";
        }
        ?>
    </div>
</div>
<script type='text/javascript'>
    $(function() {
        $("#sch_sort").change(function() { // select #sch_sort의 옵션이 바뀔때
            if ($(this).val() == "vi_date") { // 해당 value 값이 vi_date이면
                $("#sch_word").datepicker({changeMonth: true, changeYear: true, dateFormat: "yy-mm-dd", showButtonPanel: true, yearRange: "c-99:c+99", maxDate: "+0d"}); // datepicker 실행
            } else { // 아니라면
                $("#sch_word").datepicker("destroy"); // datepicker 미실행
            }
        });
        if ($("#sch_sort option:selected").val() == "vi_date") { // select #sch_sort 의 옵션중 selected 된것의 값이 vi_date라면
            $("#sch_word").datepicker({changeMonth: true, changeYear: true, dateFormat: "yy-mm-dd", showButtonPanel: true, yearRange: "c-99:c+99", maxDate: "+0d"}); // datepicker 실행
        }
    });

    function fvisit_submit(act)
    {
        var f = document.fvisit;
        f.action = act;
        f.submit();
    }
</script>
<?php
include_once("./admin.tail.php");
?>
