{{extend "forums_layout.html"}}

{{block forum_nav}}
    <div class="nav">
        <a href="/forum" class="home" title="首页">论坛首页</a>
        <em>»</em>
        <span><a href="/forum/{{=forum.id}}">{{=forum.name}}</a></span>
        <em>»</em>
        <span><a href="/forum/{{=forum.id}}/{{=topic.id}}">{{=topic.subject}}</a></span>
    </div>
{{end}}

{{block content_main}}
{{use "jquery"}}
{{use "jqutils"}}
{{use "xheditor"}}
{{use "jqimgzoom"}}
<script type="text/html" id="postTemplate">
    <a name="${id}"></a>
    <li class="post" id="post_${id}">
        <div class="user_image">
            <img title="${username}" src="${userimage}">
        </div>
        <div class="post_body">
            <div class="header">
                ${floor}楼 由 <span class="user">${posted_by}</span> 发表于 
                <span class="date">${created_on}</span>
            </div>
            <div class="body">
            ${content}
            ${updated}
            </div>
            <div class="footer">
                <span class="actions" style="display:none">
                    ${actions}
                </span>
            </div>
        </div>
        <div class="clear"></div>
    </li>
</script>

<a name="a_topic"></a>
<div class="topic">
    <h2>{{=topic.subject}}</h2>
    <div class="topic_msg">
        共有{{=topic.num_replies}}篇评论
        分类: <span>{{=topic.get_display_value('topic_type')}}</span>
    </div>
</div>
<ul id="posts">
    <li class="post">正在装入...</li>
</ul>
<div id="paginate"></div>
{{include "inc_syntaxhighlighter.html"}}
{{if request.user:}}
    {{if not topic.closed:}}
        <div class="replay">
            <h3>发表评论</h3>
            <form action="/forum/{{=forum.id}}/{{=topic.id}}/new_post" method="POST">
                <div class="type-text">
                    <textarea name="content" class="field" cols="75" rows="10"></textarea>
                </div>
                <input type="hidden" name="slug" value="{{=slug}}"></input>
                <div class="line">
                    <input type="checkbox" name="reply_email" id="field_reply_email" checked>有回复时邮件通知我</input>
                </div>
                <div class="line">
                    <input type="submit" value="发表评论"></input>
                </div>
            </form>
            <div>
                <a href="#a_topic">回页面顶部</a> 
            </div>
        </div>
    {{else:}}
        <div class="replay">
            <b>此主题已经被关闭，无法继续评论！</b>
            <div>
                <a href="#a_topic">回页面顶部</a> 
            </div>
        </div>
    {{pass}}
{{else:}}
    <div class="replay">
        <b>你尚未登录，无法评论！</b>
        <div>
            <a href="#a_topic">回页面顶部</a> 
        </div>
    </div>
{{pass}}
{{include "inc_xheditor_plugins.html"}}
{{include "inc_paginate.html"}}
{{use "poshytip"}}
<script type="text/javascript">
    var bind_action = function(el, action){
        el.click(function(event){
            event.preventDefault();
            var post_id = el.attr('rel')
            $.ajax({
                type: "POST",
                data: {post_id:post_id, action:action},
                url: "{{=url_for('plugs.forums.views.ForumView.post_actions')}}",
                success: function(data){
                    show_message(data.msg);
                    if(data.txt){
                        el.html(data.txt);
                    }
                }
            });
        });
    }
    var load_posts = function(page){
        page = page || 1;
        $.ajax({
            type: "POST",
            dataType: 'json',
            data: {data:'', page:page},
            url: "{{=url_for('plugs.forums.views.ForumView.topic_view', forum_id=forum.id, topic_id=topic.id)}}",
            success: function(data){
                var posts = $('#posts')
                posts.empty();
                $.each(data.rows, function(index, value){
                    var post = template($('#postTemplate').html(), value).appendTo(posts);
                    post.hover(
                        function(){
                            post.find('span.actions').slideDown();
                        },
                        function(){
                            post.find('span.actions').slideUp();
                        }
                    );
                    $.each(['close', 'hidden', 'delete', 'top', 'essence', 'homepage', 'email'], function(index, value){
                            bind_action(post.find('a.'+value), value);
                            bind_action(post.find('a.un'+value), 'un'+value);
                        }
                    );
                });
                do_syntax();
                create_pagination('#paginate', data.total, {{=settings.get_var('PARA/FORUM_PAGE_NUMS')}}, page, load_posts);
                $('.post_body img').imgZoom({loadingImg:"{{=url_for_static('jqimgzoom/css/imgzoom-loading.gif')}}"});
                {{if not has_email:}}
                    //设置poshytip关闭图片
                    $.fn.poshytip.defaults.closeImage = '{{=url_for_static("jqutils/cross_grey_small.png")}}';
                    var el = $('#field_reply_email').poshytip({
                        className: 'tip-yellowsimple',
                        content: '您尚未设置邮箱，将无法处理邮件',
                        showOn: 'none',
                        alignTo: 'target',
                        alignX: 'inner-left',
                        offsetX: 0,
                        offsetY: 5,
                        closeButton: true
                    });
                    el.poshytip('show');
                {{pass}}
            }
        });
        return true;
    }
    $(function(){
        $('textarea').xheditor({
            tools:'Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,SelectAll,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Img,Hr,Emot,Table,Code,Quote,|,About',
            skin:'vista',
            upLinkUrl:"!/forum/upload_file?forum_id={{=forum.id}}&slug={{=slug}}",
            upImgUrl:"!/forum/upload_image?forum_id={{=forum.id}}&slug={{=slug}}",
            plugins:plugins
        });
        load_posts();
    });
</script>
{{end}}
