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

<head>
    <?php $this->load->view('adm/layout/head')?>
</head>

<body>
    <!-- start wrapper -->
    <div class="wrapper">
        <!-- start nav -->
        <?php $this->load->view('adm/layout/nav')?>
        <!-- end nav -->
        <!-- start content_wrapper -->
        <div class="content_wrapper">
            <!-- start header -->
            <?php $this->load->view('adm/layout/header')?>
            <!-- end header -->
            <div class="contents">
                <h3 style="font-size:17px;font-weight:bold;color:#aaa">페이지설정 > 크맵TV 카테고리 최신영상</h3>
                <!-- 여기까지 레이아웃 상단입니다 -->
                <div class="box">
                    <div class="box-table">
                        <form method="post" action="" enctype="multipart/form-data" class="cm-pageset-form">
                            <input type="hidden" name="redirect" value="<?php echo $this->input->server('REQUEST_URI') ?>">
                            <?php foreach ($posts as $post) {?>
                            <table class="table table-bordered cm-hometopnews-setbox">
                                <thead>
                                <tbody>
                                    <tr>
                                        <th>검색</th>
                                        <td>
                                            <select name="category" class="form-control category" style="width:200px">
                                                <option value="">카테고리</option>
                                                <?php foreach ($lifeCategories as $category) {echo '<option value="' . $category->idx . '" ' . ($categoryNo == $category->idx ? 'selected' : '') . '>' . $category->category_name . '</option>';}?>
                                            </select>
                                            <select name="editor" class="form-control editor" style="width:200px;display:none">
                                                <option value="">에디터</option>
                                                <?php foreach ($graceEditors as $editor) {echo '<option value="' . $editor->editorId . '" ' . (param('editor') == $editor->editorId ? 'selected' : '') . '>' . $editor->editorName . '</option>';}?>
                                            </select>
                                            <select name="limit" class="form-control limit" style="width:100px">
                                                <option value="">검색결과수</option>
                                                <option value="5" selected>5</option>
                                                <option value="10">10</option>
                                                <option value="30">30</option>
                                                <option value="50">50</option>
                                            </select>
                                            <input type="text" name="keyword" class="form-control keyword" style="width:200px">
                                            <a class="btn btn-info btn-sm cm-pageset-searchbtn">검색</a>
                                            <a class="btn btn-warning btn-sm cm-pageset-addsetbox">추가</a>
                                            <div class="cm-search-result" style="margin-top:3px">

                                            </div>
                                        </td>
                                    </tr>
                                    </thead>
                                <tbody>
                                    <tr>
                                        <th>아이디</th>
                                        <td>
                                            <input type="text" name="postid[]" class="form-control postid" value="<?php if (isset($post)) {
    echo $post->idx;
}
    ?>">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>제목</th>
                                        <td>
                                            <input type="text" name="title[]" class="form-control title" value="<?php if (isset($post)) {
        echo convertEmoji($post->title);
    }
    ?>">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>메인이미지</th>
                                        <td>
                                            <img src="<?php if (isset($post)) {
        echo imgSrc($post->main_img);
    }
    ?>" width="150" name="main_img[]" class="main_img">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>순서</th>
                                        <td>
                                            <input type="number" name="display_sort[]" class="form-control display_sort" value="<?php if (isset($post)) {
        echo $post->display_sort;
    }
    ?>">
                                        </td>
                                    </tr>
                                </tbody>
                            </table><?php }?>
                            <div style="text-align:center">
                                <a class="btn btn-danger" href="<?php echo $this->input->server('HTTP_REFERER') ?>">취소</a>
                                <button type="submit" class="btn btn-success">저장</button>
                            </div>
                        </form>
                    </div>
                </div>
                <!-- 여기부터 레이아웃 하단입니다 -->
            </div>
        </div>
        <!-- end content_wrapper -->
    </div>
    <!-- end wrapper -->
    <?php $this->load->view('adm/layout/footer')?>
    <script src="/assets/adm/js/tinymce/tinymce.min.js"></script>
    <script>
    var result;
    $(function() {

        $('.cm-pageset-addsetbox').on('click', function() {
            var dd = $('.cm-hometopnews-setbox');
            dd.last().after(dd.eq(0).clone(true));

        })

        $('.cm-pageset-rmsetbox').on('click', function(){
            $(this).parent().parent().parent().parent().remove();
        })

        $('.category').change('change', function() {
            location.href = '/adm/setcmtv/categorylatest/' + this.value;
        })

        $('.cm-pageset-searchbtn').on('click', function(e) {
            var searchBtn = $(this);
            var frm = searchBtn.parent()
            var params = {
                category: frm.find('.category').val(),
                editor: frm.find('.editor').val(),
                term: frm.find('.keyword').val(),
                pageset: 1,
                limit: frm.find('.limit').val()
            }


            $.get('/adm/content', params, function(res) {
                result = res.data.rows
                var resultBox = searchBtn.parent().find('.cm-search-result').empty();
                for (var i = 0; i < result.length; i++) {
                    resultBox.append('<a class="btn btn-sm form-control cm-result-item" style="margin-bottom:3px" tabindex="' + i + '">' + result[i].title + '</a>')
                }
                $('.cm-result-item').on('click', function() {
                    var item = result[$(this).attr('tabindex')]
                    var frm = searchBtn.parent().parent().parent().next();
                    console.log(frm)
                    frm.find('.postid').val(item.idx)
                    frm.find('.title').val(item.title)
                    frm.find('.main_img').attr('src', item.mainImg)
                    $('.cm-result-item').remove();
                })
            }, 'json')
        })
    })
    </script>
</body>

</html>