<?php
if (!defined("_GNUBOARD_"))
    exit; // 개별 페이지 접근 불가 
?>
<div class="g4search_basic">
    <div class="page-header">
        <h1>통합검색</h1>
    </div>

    <div class="search_box well well-sm">
        <form name='fsearch' method=get onsubmit="return fsearch_submit(this);" class="form-inline" role='form'>
            <input type="hidden" name="srows" value="<?php echo $srows ?>"/>
            <div class='form-group'>
                <?php echo $group_select ?>
            </div>
            <script type="text/javascript">document.getElementById("gr_id").value = "<?php echo $gr_id ?>";</script>
            <div class='form-group'>
                <select name="sfl" class='form-control'>
                    <option value="wr_subject||wr_content" selected="selected" class="form-control">제목+내용</option>
                    <option value="wr_subject">제목</option>
                    <option value="wr_content">내용</option>
                    <option value="mb_id">회원아이디</option>
                    <option value="wr_name">이름</option>
                </select>
            </div>
            <div class='form-group'>
                <input type="text" name="stx" maxlength="20" required itemname="검색어" value='<?php echo $text_stx ?>' class="form-control" placeholder="검색어 입력"/>
                <input type="submit" value=" 검 색 " class="btn btn-primary btn-100"/>
            </div>
            <div class="form-group">
                연산자 &nbsp; 
                <label for="option_or"><input type="radio" name="sop" value="or" <?php echo ($sop == "or") ? "checked" : ""; ?> id="option_or"/>OR</label> &nbsp;
                <label for="option_and"><input type="radio" name="sop" value="and" <?php echo ($sop == "and") ? "checked" : ""; ?> id="option_and"/>AND</label>
            </div>

        </form>
    </div>

    <?php
    if ($stx) {
        echo "<div class='panel panel-default'><div class='panel-heading'>검색된 게시판</div> <div class='panel-body'> 게시판<span class='badge'>{$board_count}</span>, 게시글<span class='badge'>" . number_format($total_count) . "</span>, " . number_format($page) . "/" . number_format($total_page) . " 페이지</div>";
        if ($board_count) {
            echo "<ul class='list-group'>";
            if ($onetable) {
                echo "<li class='list-group-item'><a href='?{$search_query}&gr_id={$gr_id}'>전체게시판 검색</a>";
            }
            echo $str_board_list;
            echo "</ul>";
        } else {
            echo "<div class='search_result'>검색된 자료가 없습니다.</div>";
        }
        echo "</div>";
    }

    $k = 0;
    $search_count = count($search_table);
    if ($search_count > 0) {
        echo '<div class="search_result">';
    }
    for ($idx = $table_index, $k = 0; $idx < $search_count && $k < $rows; $idx++) {
        echo "<div class='sr_title'><h3>{$bo_subject[$idx]} 검색결과 </h3><a href='./board.php?bo_table={$search_table[$idx]}&{$search_query}' class='btn btn-info pull-right'>게시판 바로가기</a></div>";
        $comment_href = "";
        $list_count = count($list[$idx]);
        echo "<ul>";
        for ($i = 0; $i < $list_count && $k < $rows; $i++, $k++) {
            echo "<li class='well'>";
            $is_cmt = "";
            if ($list[$idx][$i]['wr_is_comment']) {
                $is_cmt = "<span style='color:#999'>[댓글]</span> ";
                $comment_href = "#c_" . $list[$idx][$i]['wr_id'];
            }
            echo "<a href='{$list[$idx][$i]['href']}{$comment_href}'>";
            echo $is_cmt . $list[$idx][$i]['subject'];
            echo "</a><br/>";
            echo "<span class='sr_content'>" . $list[$idx][$i]['content'] . " <em>{$list[$idx][$i]['wr_datetime']}</em></span>";
            echo "<p>{$list[$idx][$i]['name']}</p>";
            echo "</li>";
        }
        echo "</ul>";
    }
    if ($search_count > 0) {
        echo '</div>';
    }

    echo "<div id='paging' style='text-align:center'>" . $write_pages . "</div>";
    ?>
</div>
<script type="text/javascript">
    document.fsearch.sfl.value = "<?php echo $sfl ?>";
    
    function fsearch_submit(f){
        if (f.stx.value.length < 2) {
            alert("검색어는 두글자 이상 입력하십시오.");
            f.stx.select();
            f.stx.focus();
            return false;
        }
        
        // 검색에 많은 부하가 걸리는 경우 이 주석을 제거하세요.
        var cnt = 0;
        for (var i=0; i<f.stx.value.length; i++) {
            if (f.stx.value.charAt(i) == ' ')
                cnt++;
        }
        
        if (cnt > 1) {
            alert("빠른 검색을 위하여 검색어에 공백은 한개만 입력할 수 있습니다.");
            f.stx.select();
            f.stx.focus();
            return false;
        }
        
        f.action = "";
        return true;
    }
</script>