{layout name="public/layout_main"}

<section class="wrapper">
    <h3>
        <i class="fa fa-angle-right"></i> 小程序列表
    </h3>

    <hr>
    <div class="row margin-bottom">
        <div class="col-md-12">
            <form class="form-inline" method="GET" id="search_from">
                <div class="form-group">
                    <input type="text" class="form-control" name="name" id="name" placeholder="名称" value="<?php echo input('get.name');?>">
                </div>
                <button type="submit" class="btn btn-primary margin-left">搜索</button>
                <a href="<?=url('admin/Miniapp/create')?>" class="btn btn-success margin-left">创建小程序</a>
            </form>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="table-responsive">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>名称</th>
                            <th>简介</th>
                            <th>封面图</th>
                            <th>按钮颜色</th>
                            <th>标题颜色</th>
                            <th>图标</th>
                            <th>小程序码</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($items as $item):?>
                            <tr id="tr_<?php echo $item->id;?>">
                                <td><?php echo $item->id;?></td>
                                <td>
                                    <?php echo $item->name;?>
                                </td>
                                <td>
                                   <?php echo $item->subject;?>
                                </td>
                                <td>
                                    <a href="<?=$item->poster?>" target="_blank" data-fancybox="images">
                                        <img src="<?=$item->poster?>" width='50' height='50' />
                                    </a>
                                </td>
                                <td>
                                    <?php echo $item->btn_color;?>
                                </td>
                                <td>
                                    <?php echo $item->title_color;?>
                                </td>
                                <td>
                                    <a href="<?=$item->icon?>" target="_blank" data-fancybox="images">
                                        <img src="<?=$item->icon?>" width='50' height='50' />
                                    </a>
                                </td>
                                <td>
                                    <a href="<?=$item->qrcode?>" target="_blank" data-fancybox="images">
                                        <img src="<?=$item->qrcode?>" width='50' height='50' />
                                    </a>
                                </td>
                                <td>
                                    <a href="<?=url('@admin/Miniapp/update', ['id'=>$item->id])?>" type="button" class="btn btn-primary btn-xs  btn-delete" oid="<?=$item['id']?>"><i class="fa fa-pencil"></i></a>
                                    /
                                    <a href="javascript:;" type="button" class="btn btn-danger btn-xs btn-delete" oid="<?=$item['id']?>"><i class="fa fa-trash"></i></a>
                                </td>
                            </tr>
                        <?php endforeach;?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            {$items->render()}
        </div>
    </div>
</section>

{include file="public/course_detail" /}

<script type="text/javascript">
$(function(){
    $('#sel_type').change(function(){
        $("#search_from").submit();
    });
    $('#sel_catalog').change(function(){
        $("#search_from").submit();
    });

    // 课程分类
    $.post('/admin/Course/ajax_course_catalogs', {}, function(res){
        res = $.parseJSON(res);
        if (res.code==0) {
            var html = '';
            var catalog = "<?=input('catalog')?>";
            var selected;
            for (var i = 0; i < res.data.length; i++) {
                if (res.data[i].id==catalog) {
                    selected = "selected='selected'";
                }else{
                    selected = "";
                }
                html += "<option "+selected+ "value="+res.data[i].id+">"+res.data[i].title+"</option>";
            }
            $('#sel_catalog').append(html);
        }else{
            alert(res.msg);
        }
    });

    // 右侧详情
    $('.btn-detail').unbind('click').bind('click', function (event) {
        event.stopPropagation();
        event.preventDefault();

        document.getElementById('right-detail-content').innerHTML = "";
        var url = "/admin/Course/read";
        var oid = $(this).attr('oid');
        showRightDetail(function () {
            $.get(url + "/id/" + oid, function (res) {
                var res = JSON.parse(res);
                if (res.error) {
                    new PNotify({
                        title: '提示',
                        text: res.msg,
                        'type': 'error'
                    });
                } else {
                    var html = template('course', res.data);
                    document.getElementById('right-detail-content').innerHTML = html;
                }
            });
        });
    });

    $(".btn-delete").click(function(){
        var _this = $(this);
        var url = "ajax_delete";
        var oid = _this.attr('oid');
        showDialog("提示", "确定删除此小程序吗？", function () {
            // ajax post 方法
            $.post(url, {id:oid}, function (res) {
                console.log(res);
                var res = JSON.parse(res);
                if (res.error == 1) {
                    // javascript 的默认弹出提示方法
                    alert(res.msg);
                } else {
                    _this.remove();
                    $("#tr_"+oid).fadeOut();
                }
            });
        });
    });

});
</script>






