{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" action="<?php echo url('@admin/Post/index')?>">
                 <div class="form-group margin-right">
                    <label for="title">文章标题</label>
                    <input class="form-control" type="text" name="title" id="title" value="<?php echo input('title');?>" />
                </div>
                <button type="submit" class="btn btn-primary">搜索</button>
                <a href='<?php echo url("@admin/Post/create")?>' type="button" class="btn btn-success" style="margin-left:30px;">
                    发布文章
                </a>
            </form>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>文章标题</th>
                            <th>封面</th>
                            <th>发布人</th>
                            <th>发布时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($articles as $k => $article):?>
                            <tr id="tr_<?php echo $article->id;?>">
                                <td><?php echo $article->id;?></td>
                                <td style="max-width:450px;">
                                    {if condition="$article.type == 1"}
                                    <a href="<?php echo url('@mob/Post/read', 'id='.$article->id);?>/" target='_blank'>
                                        <?php echo $article->title;?>
                                    </a>
                                    {else/}
                                        <a href="{$article->content}" target='_blank'>
                                            <?php echo $article->title;?>
                                        </a>
                                    {/if}
                                </td>
                                <td><img style="width: 170px" src="{$article->poster}" alt=""></td>
                                <td><?php echo $article->admin->username;?></td>
                                <td><?php echo $article->created_at;?></td>
                                <td style="min-width:100px;">
                                    <a href="<?php echo url('@admin/Post/update', 'id='.$article->id);?>" type="button" class="btn btn-primary btn-xs" data-toggle="tooltip" data-placement="top" data-original-title="编辑">
                                        <i class="fa fa-pencil"></i>
                                    </a>
                                    <button oid="<?php echo $article->id;?>" type="button" class="btn btn-danger btn-xs btn-delete" data-toggle="tooltip" data-placement="top" data-original-title="删除">
                                        <i class="fa fa-trash-o"></i>
                                    </button>
                                </td>
                            </tr>
                        <?php endforeach;?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            {$articles->render()}
        </div>
    </div>

</section>

<script type="text/javascript">
    // 文档加载完毕之后，会进入该方法
    $(function(){
        $(".btn-delete").click(function(){
            var _this = $(this);
            var url = "delete";
            var oid = _this.attr('oid');
            showDialog("提示", "确定删除这篇文章吗？", function () {
                // ajax post 方法
                $.post(url, {id:oid}, function (res) {
                    var res = JSON.parse(res);
                    if (res.error == 1) {
                        // javascript 的默认弹出提示方法
                        alert(res.msg);
                    } else {
                        _this.remove();
                        $("#tr_"+oid).fadeOut();
                    }
                });
            });
        });
    });
</script>
