<!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">페이지설정 > 오늘의말씀</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 $dailyqt) {?>
                            <table class="table table-bordered cm-hometopnews-setbox">
                                <tbody> 
                                    <tr><td colspan="2"><a class="btn btn-warning btn-sm cm-pageset-addsetbox">추가</a>
                                            <a class="btn btn-danger btn-sm cm-pageset-rmsetbox">삭제</a></td></tr>                                                                       
                                    <tr>
                                        <th>말씀</th>
                                        <td>
                                            <input type="text" name="title[]" class="form-control" value="<?php if(isset($dailyqt)) echo $dailyqt->Title?>">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>구절</th>
                                        <td>
                                        <input type="text" name="headline[]" class="form-control" value="<?php if(isset($dailyqt)) echo $dailyqt->Headline?>">
                                        </td>
                                    </tr>
                                    <tr>
                                        <th>날짜</th>
                                        <td>
                                        <input type="text" name="dptime[]" class="form-control datetimepicker" value="<?php if(isset($dailyqt->DpTime)) echo $dailyqt->DpTime; else echo date('Y-m-d H:i:s');?>">
                                        </td>
                                    </tr>
                                    <tr class="cm-sample" style="display:none">
                                        <th>날짜</th>
                                        <td>
                                        <input type="text" class="form-control">
                                        </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(){

        $('.datetimepicker').datetimepicker({ format: 'YYYY-MM-DD HH:mm:ss' });

        $('.cm-pageset-addsetbox').on('click', function(){
            var dd = $('.cm-hometopnews-setbox');
            var b = dd.eq(0).clone(true);
            b.find('.datetimepicker').parent().parent().remove();
            dd.last().after(b);            
            var s = b.find('.cm-sample').clone();
            b.find('tbody').append(s);
            s.find('input').attr('name', 'dptime[]')
            s.find('input').datetimepicker({ format: 'YYYY-MM-DD HH:mm:ss' });
            s.show();

        })

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

        $('.cm-pageset-searchbtn').on('click', function(e){
            var frm = $('.cm-pageset-form')[0]
            var params = {category: frm.category.value,
            editor: frm.editor.value,
            term:frm.keyword.value, pageset: 1, limit: frm.limit.value}
            $.get('/adm/content', params, function(res){
                result = res.data.rows
                var resultBox = $('.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 = $('.cm-pageset-form')[0]
                    frm.postid.value = item.idx
                    frm.title.value = item.title
                    frm.main_img.src = item.mainImg
                    console.log(item)
                })
            }, 'json')
        })
    })
    </script>
</body>

</html>