<!DOCTYPE html>
<html lang="ko">

<head>
    <?php $this->load->view('partial/head')?>
    <link rel="stylesheet" href="/ART/cmtv/css/default.css">
    <style>
    strong {
        text-decoration: underline;
        font-size: 14px
    }
    </style>
</head>

<body>
    <?php $this->load->view('partial/sidemenu')?>
    <?php $this->load->view('partial/header')?>
    <main>
        <section id="visual_slide">
            <div class="container">
                <?php if($searchTerm = $this->input->post('search')) {                        
                $searchResult = $this->db->like('b.content', $searchTerm, 'both')->from('cm_bible_korhrv b')->join('cm_bible_kor_chapter c', 'b.book=c.idx', 'inner')
                ->select('b.*, c.chapter chapterName')->limit(30)->get()->result();
                ?>
                <div class="row">
                    <ul class="search-result">
                        <?php foreach($searchResult as $r) {?>
                        <li class="reference">
                            <h3>
                                <a href="/ko/bible/88/jdg.12.11" target="_self"><?php echo $r->chapterName. ' '. $r->chapter.':'.$r->verse. ' (KRV)'?></a>
                            </h3>
                            <p>
                                <?php echo str_replace($searchTerm, '<strong>'.$searchTerm.'</strong>', $r->content)?>
                            </p>
                        </li>
                        <?php }?>
                    </ul>
                </div><?php } else { ?>
                <div class="row">
                    <form action="" method="post" class="cm-bible-form">                        
                        <div class="visual_slide">
                            <input type="search" class="search-input" value="" name="search" placeholder="검색 ...">
                            <input type="button" value="이전" class="cm-bible-nav cm-bible-prev">
                            <input type="button" value="다음" class="cm-bible-nav cm-bible-next">
                        </div>
                        <div>

                            <select class="cm-bible-book" name="book">
                                <?php foreach($this->db->group_by('book')->from('cm_bible_korhrv b')
                        ->join('cm_bible_kor_chapter c', 'b.book=c.idx', 'inner')
                        ->select('c.*')->get()
                        ->result() as $r) 
                        echo '<option value="'.$r->idx.'"' . ($this->input->post('book') == $r->idx ? ' selected':'') .'>'.$r->chapter.'</option>';?>
                            </select>
                            <select class="cm-bible-chapter" name="chapter" data-prev-lastchapter="<?php 
                            if($this->input->post('book') - 1 > 0) {
                                $chapterPrev = $this->db->where('idx', $this->input->post('book') - 1)->get('cm_bible_kor_chapter')->row();
                                echo $chapterPrev->cnt;
                            }
                            ?>">
                                <?php
                            $chapter = $this->db->where('idx', $this->input->post('book') ? $this->input->post('book') : 1)->get('cm_bible_kor_chapter')->row();
                            for($c=1;$c<=$chapter->cnt;$c++) {
                                echo '<option value="'.$c.'"' . ($this->input->post('chapter') == $c ? ' selected':'') .'>'.$c.'장</option>';
                            }
                            ?>
                            </select>
                            <select>
                                <option value="">개역한글</option>
                            </select>

                            <select>
                                <option value="">대역없음</option>
                            </select>

                        </div>
                        <div>
                            <?php
                            $bible = $this->db->where('book', $this->input->post('book') ? $this->input->post('book') : 1)->where('chapter', $this->input->post('chapter') ? $this->input->post('chapter') : 1)->get('cm_bible_korhrv')->result();
                            foreach($bible as $b) {
                                echo $b->verse. '. '. $b->content.'<br/>';
                            }
                        ?>
                        </div>
                    </form>
                </div>
                <?php }?>
            </div>
        </section>
    </main>
    <?php $this->load->view('partial/footer')?>
    <script src="/ART/cmtv/js/cmtv_slide.js"></script>
    <script>
    $(function() {
        $('.cm-bible-book, .cm-bible-chapter').on('change', function() {
            $('.cm-bible-form').submit();
        })
        $('.nav ul').append($('.nav ul li:last').clone().addClass('current')).find('li:last a').text('성경').attr('href', '/bible');

        $('.cm-bible-nav').on('click', function(){
            var book = $('.cm-bible-book')
            if($(this).hasClass('cm-bible-prev')) {
                var chapter = $('.cm-bible-chapter').get(0).selectedIndex;
                var opt = $('.cm-bible-chapter option')
                if(chapter) {
                    $('.cm-bible-chapter').get(0).selectedIndex = chapter - 1;                    
                }else {
                    book.get(0).selectedIndex = book.get(0).selectedIndex-1;
                    $('.cm-bible-chapter').prepend('<option value="'+$('.cm-bible-chapter').data('prev-lastchapter')+'" />').get(0).selectedIndex = 0
                }

            }else {
                var chapter = $('.cm-bible-chapter').get(0).selectedIndex;
                var opt = $('.cm-bible-chapter option')
                if(opt.length > chapter + 1) {
                    $('.cm-bible-chapter').get(0).selectedIndex = chapter + 1;
                }else {
                    book.get(0).selectedIndex = book.get(0).selectedIndex+1;
                    $('.cm-bible-chapter').get(0).selectedIndex = $('.cm-bible-chapter');
                }
            }
            $('.cm-bible-form').submit();
        })
    })
    </script>
</body>

</html>