{% extends request.ajax ? "layout_blank.phtml" : "layout_default.phtml" %}
{% import "macro_functions.phtml" as mf %}
{% block meta_title %}{{ticket.subject}}{% endblock %}
{% set active_menu = 'support' %}
{% block content %}

<div class="widget simpleTabs tabsRight first">
    <div class="head">
        <h5 class="iSpeech">{{ ticket.subject }}</h5>
    </div>

    <ul class="tabs">
        <li><a href="#tab-index">Ticket</a></li>
        <li><a href="#tab-manage">Manage</a></li>
    </ul>

    <div class="tabs_container">
        <div class="fix"></div>
        <div class="tab_content nopadding" id="tab-index">
            <table class="tableStatic wide">
                <tbody>
                    <tr class="noborder">
                        <td>{% trans 'Ticket ID' %}</td>
                        <td><b>#{{ticket.id}}</b></td>
                    </tr>

                    <tr>
                        <td>{% trans 'Author' %}</td>
                        <td>{{ ticket.author_name }}</td>
                    </tr>

                    <tr>
                        <td>{% trans 'Email' %}</td>
                        <td>{{ ticket.author_email }}</td>
                    </tr>

                    <tr>
                        <td>{% trans 'Status' %}</td>
                        <td>{{ mf.status_name(ticket.status) }}</td>
                    </tr>

                    <tr>
                        <td>{% trans 'Time opened' %}</td>
                        <td>{{ticket.created_at|date('l, d F Y')}}</td>
                    </tr>
                    
                    {% if ticket.created_at != ticket.updated_at %}
                    <tr>
                        <td>{% trans 'Last updated' %}</td>
                        <td>{{ticket.updated_at|timeago}} ago</td>
                    </tr>
                    {% endif %}
                 </tbody>

                 <tfoot>
                     <tr>
                         <td colspan="2">
                            <div class="aligncenter">
                                {% if ticket.status != 'closed' %}
                                <a href="admin/support/public_ticket_close?id={{ticket.id}}" data-api-redirect="support/public-tickets" class="btn55 mr10 api-link" ><img src="images/icons/middlenav/stop.png" alt=""><span>Close ticket</span></a>
                                {% endif %}
                                <a href="admin/support/public_ticket_delete?id={{ticket.id}}" data-api-confirm="Are you sure?" data-api-redirect="support/public-tickets" class="btn55 mr10 api-link"><img src="images/icons/middlenav/trash.png" alt=""><span>Delete</span></a>
                                <a href="{{ 'contact-us/conversation'|link }}/{{ ticket.hash }}" class="btn55 mr10" target="_blank"><img src="images/icons/middlenav/userComment.png" alt=""><span>Open as client</span></a>
                            </div>
                         </td>
                     </tr>
                 </tfoot>
            </table>
        </div>

        <div class="tab_content nopadding" id="tab-manage">
            <form method="post" action="admin/support/public_ticket_update" class="mainForm api-form" data-api-reload="1">
                <fieldset>
                    <div class="rowElem">
                        <label>{% trans 'Subject' %}</label>
                        <div class="formRight noborder">
                            <input type="text" name="subject" value="{{ticket.subject}}" required="required"/>
                        </div>
                        <div class="fix"></div>
                    </div>

                    <div class="rowElem">
                        <label>{% trans 'Status' %}</label>
                        <div class="formRight">
                            {{ mf.selectbox('status', admin.support_ticket_get_statuses({"titles":1}), ticket.status, 1) }}
                        </div>
                        <div class="fix"></div>
                    </div>

                     <input type="submit" value="{% trans 'Update' %}" class="greyishBtn submitForm" />
                </fieldset>
                <input type="hidden" name="id" value="{{ ticket.id }}">
            </form>
        </div>

    </div>
</div>

{% set canned_response_selector %}
<div class="canned_response">
    <select name="canned_response" class="canned">
    {% for response in admin.support_canned_get_list({"per_page":90}).list %}
        <option value="{{ response.id }}">{{ response.title }}</option>
    {% endfor %}
    </select>
</div>
{% endset %}

<div class="conversation">
{% for i, message in ticket.messages %}
<div class="widget simpleTabs tabsRight">
    <div class="head">
        <h5 class="iSpeech">{{ message.author.name }} - {{ message.created_at|date("l, d F Y") }}</h5>
    </div>
    <ul class="tabs">
        <li><a href="#m-{{message.id}}"><img src="{{ message.author.email|gravatar }}?size=20" alt="{{ message.author.name }}" class="gravatar"/>#{{ i+1 }}</a></li>
        <li><a href="#reply-{{message.id}}"><span class="iSpeech"></span>{% trans 'Reply' %}</a></li>
        <li><a href="#quote-{{message.id}}"><span class="iBubbles"></span>{% trans 'Quote' %}</a></li>
    </ul>
    <div class="tabs_container">
        <div class="fix"></div>
        <div class="tab_content" id="m-{{message.id}}">
            <div class="block message">
                <div class="body">
                    {{ message.content|bbmd }}
                    <div class="clear"></div>
                </div>
            </div>
            <div class="clear"></div>
        </div>
        <div class="tab_content" id="reply-{{message.id}}">
            <form method="post" action="admin/support/public_ticket_reply" class="mainForm api-form" data-api-redirect="support/public-tickets?status=open">
                <fieldset>
                    {{ canned_response_selector }}
                    <textarea name="content" cols="5" rows="20" required="required" id="msg-area-{{message.id}}">
Hello {{ ticket.author.name }},



{{ profile.signature }}
{{ ticket.helpdesk.signature }}
                    </textarea>
                    <input type="hidden" name="id" value="{{ ticket.id }}">
                    <input type="submit" value="{% trans 'Post' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>
        </div>

        <div class="tab_content" id="quote-{{message.id}}">
            <form method="post" action="admin/support/public_ticket_reply" class="mainForm api-form" data-api-redirect="support/public-tickets?status=open">
                <fieldset>
                    {{ canned_response_selector }}
                    <textarea name="content" cols="5" rows="20" required="required" id="quote-area-{{message.id}}">
Hello {{ ticket.author.name }},


{{ profile.signature }}

{{ ticket.helpdesk.signature }}

{{ mf.markdown_quote(message.content) }}
                    </textarea>
                    <input type="hidden" name="id" value="{{ ticket.id }}">
                    <input type="submit" value="{% trans 'Post' %}" class="greyishBtn submitForm" />
                </fieldset>
            </form>
        </div>
    </div>
</div>
{% endfor %}
</div>

{% endblock %}

{% block js%}
<script type="text/javascript">
$(function() {

    $('.canned_response div.jqTransformSelectWrapper ul li a').click(function(){
        var value = $(this).attr('index');
        var select = $(this).parents('div.canned_response').find('select');
        var textarea = select.parent().parent().siblings('textarea');
        var id = $(select).find('option:eq('+value+')').val();
        api.get('admin/support/canned_get', {id:id}, function(result){ bb.insertToTextarea(textarea.attr('id'), result.content) });
        return false;
    });

});
</script>
{% endblock %}