<% case @ticket.status 
  when "open"
    badge_class = "tk-open"
  when "closed"
    badge_class = "tk-close"
  when "commenced"
    badge_class = "tk-processing"
  end
%>
<% content_for :page_menu do %>
 	<ul class="list-sidebar">
    <li class="item-sidebar"><a class="btn-sidebar" href="<%= sites_path %>"><i class="fa fa-home" aria-hidden="true"></i><%= t("client_management.my_sites") %></a></li>
    <li class="item-sidebar active"><a class="btn-sidebar" href="/cpanel/site/<%= @site_construct.uid %>"><i class="fa fa-envelope" aria-hidden="true"></i><%= t("client_management.tickets") %></a></li>
    <li class="item-sidebar"><a class="btn-sidebar" href="<%= cpanel_add_new_site_path(:site => @site_construct.id) %>"><i class="fa fa-server" aria-hidden="true"></i><%= t("client_management.services") %></a></li>
    <li class="item-sidebar"><a class="btn-sidebar" href="#"><i class="fa fa fa-credit-card-alt" aria-hidden="true"></i><%= t("client_management.payments") %></a></li>
  </ul>
<% end %>
<section class="cp-content col-md-10">
  <div class="cp-bread">
   	<ol class="breadcrumb">
      <li><a href="/cpanel/sites">Sites</a></li>
      <li><a href="/cpanel/site/<%= @site_construct.uid %>"><%= @site_construct.title %></a></li>
      <li><a href="/cpanel/site/<%= @site_construct.uid %>">Tickets</a></li>
      <li class="active"><%= @ticket.uid %></li>
    </ol>
  </div>
  <h2 class="cp-page-title">
    Ticket number : #<%= @ticket.uid %>
  </h2>
  <% if @ticket.status != "commenced" %>
    <div class="cp-function-box clear">
	    <% case @ticket.status %>
	      <% when "open" %>
	        <a href="/cpanel/ticket/<%= @ticket.id.to_s %>/close" data-confirm="Are you sure?" class="btn btn-warning pull-right ticket-action-btn">Close</a>
	        <% if !@ticket.reopened? %>
	        	<a data-method="delete" data-confirm="Are you sure?" href="/cpanel/ticket/<%= @ticket.id.to_s %>/delete" class="btn btn-danger pull-right ticket-action-btn">Delete</a>
	        <% end %>
	      <% when "closed" %>
	        <% if @ticket.can_be_reopened? %>
	          <a href="/cpanel/ticket/<%= @ticket.id.to_s %>/reopen" class="btn btn-info pull-right">Reopen</a>
	        <% end %>
	    <% end %>
    </div>
  <% end %>
  <div id="error-msg-area" class="cp-message-box hide">
    <div class="message-text">
      Sorry, we found nothing.
    </div>
  </div>
  <div class="cp-comments-ticket">
    <div class="header-comments">
      <span class="subject-ticket"><%= @ticket.subject %></span>
      <div class="info-ticket">
      	<span class="type-ticket"><i class="fa fa-user" area-hidden="true"></i><%= @ticket.author %></span>
        <span class="type-ticket"><i class="fa fa-file" aria-hidden="true"></i>Type : <%= @ticket.category.title %></span>
        <span class="time-ticket"><i class="fa fa-clock-o" aria-hidden="true"></i>Time : <%= @ticket.created_at.strftime("%d %B %y %H:%M") %></span>
        <span class="number-ticket"><i class="fa fa-tags" aria-hidden="true"></i>Number : <%= @ticket.uid %></span>
      </div>
      <span class="status-ticket <%= badge_class %>"><%= @ticket.status.capitalize %></span>
    </div>

    <!-- lopp here -->
    <% @ticket.ticket_queries.asc(:created_at).each do |query| %>
	    <div class="comments-group">
	      <div class="post-comments">
	      	<p><%= query.query.html_safe %></p>
	      </div>
	      <% responses = query.ticket_query_responses.for_clients.asc(:created_at) %>
	      <% if responses.count > 0 %>
	      	<div class="talk-comments">
		        <div class="box-talk">
		          <div class="header-talk">
		            <h3 class="talk-title"><i class="fa fa-commenting-o" aria-hidden="true"></i>Response</h3>
		          </div>
		          <% responses.each do |response| %>
			          <div class="response-talk">
			            <div class="author-talk">
			              <div class="pic-author">
			                <img src="<%= response.author.member_profile.avatar.thumb.url %>" alt="author">
			              </div>
			              <span class="name-author"><%= response.author.name %></span>
			              <span class="time-author"><%= response.created_at.strftime("%d %B %Y - %H:%M") %></span>
			            </div>
			            <div class="text-talk">
			              <%= response.response.html_safe %>
			            </div>
			          </div>
			        <% end %>
		        </div>
	      	</div>
	       <% end %>
	    </div>
	  <% end %>
  </div>
</section>
<script type="text/javascript">
	$(document).bind("ajaxSend", function(elm, xhr, s){
    if ((s.type == "POST") || (s.type == "DELETE")) {
      xhr.setRequestHeader('X-CSRF-Token', '<%= form_authenticity_token %>');
    }
  });
  $(document).on("click","a.ticket-action-btn",function(){
    var el = $(this),
        actionType = "get";
    if(el.hasClass("btn-danger")){
      actionType = "delete";
    }
    if(confirm("Are you sure?")){
      $.ajax({
        url : el.attr("href"),
        type : actionType,
        dataType : "json"
      }).done(function(data){
        if(data.success){
         window.location.reload();
        }else{
          $("#error-msg-area").find(".message-text").text("Sorry, we were unable to perform the requested action.");
          $("#error-msg-area").removeClass("hide");
          $("*").animate({scrollTop:0}, '300');
        }
      })
    }
    return false;
  })
  <% if !@reopen_error.blank? %>
    $("#error-msg-area").find(".message-text").text("<%= @reopen_error %>");
    $("#error-msg-area").removeClass("hide");
  <% end %>
</script>