<%
# Code Review plugin for Redmine
# Copyright (C) 2009-2017  Haruyuki Iida
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-%>
<div class="code-review-dialog" id="code-review-dialog-<%= @review.id%>">

  <div class="code_review_body">

    <!-- flash -->
    <%= error_messages_for 'review' -%>
    <%= error_messages_for 'reply' -%>
    <% if @notice -%>
      <div class="flash notice"><%= @notice -%></div>
    <% end -%>
    <% if @error -%>
      <div class="flash error"><%= @error -%></div>
    <% end -%>

    <!-- code review view -->
    <div class="code_review_viewer issue">

      <!-- toolbar -->
      <div class="contextual">
        <% if authorize_for('code_review', 'update') -%>
          <%= link_to_function  l(:button_update), "$('#update-form-#{@review.id}').show();return false;", :class => 'icon icon-edit' %>
        <% end %>
        <% if authorize_for('code_review', 'destroy') -%>
          <%= link_to(l(:button_delete),
            {:controller => 'code_review',
              :action => 'destroy',
              :id => @project,
              :review_id => @review},
            :update => "show_review_#{@review.id}",
            :remote => true,
            :data => {:confirm => l(:text_are_you_sure)},
            :class => 'icon icon-del', :id => 'delete-review-link') %>
        <% end -%>
        <script>
          $('#delete-review-link').bind('ajax:complete', function() {
            location.reload();
          });
        </script>
      </div>

      <!-- title -->
      <h2>
        <%= link_to h(@issue.tracker.name) + ' #' +@issue.id.to_s + ' (' + h(@review.issue.status) + ')',
          :controller => 'issues', :action => 'show', :id => @review.issue.id %>:
        <br />
        <%=h @review.subject %>
      </h2>

      <!-- author and creation time -->
      <p class="author">
        <%= avatar(@review.user, :size => "64") %>
        <br />
        <%= authoring @review.created_at, @review.user %>.
        <%= l(:label_updated_time, distance_of_time_in_words(Time.now, @review.updated_at)) + '.' if @review.created_at != @review.updated_at %>
      </p>
      <hr />

      <!-- review comment -->
      <div class="wiki">
        <%= textilizable @review, :comment %>
      </div>

      <!-- review comment edit form -->
      <div class="box" id="update-form-<%= @review.id %>" style="display:none;">
        <%
        review_form_id = "review_form_#{@review.id}"
        -%>
        <%= form_for :review,
          :url => {:controller => 'code_review',
          :action => 'update',
          :id => @project},
          :remote => true,
          :authenticity_token => true,
          :html => {:id => review_form_id} do |f|  %>
          <%= f.hidden_field :lock_version %>
          <%= hidden_field :issue, :lock_version%>
          <%= hidden_field_tag :review_id, @review.id %>
          <p>
            <label><b><%=h l(:field_subject)%>:</b></label>
            <%= f.text_field :subject, :size => 70, :value => @review.subject%>
          </p>
          <% if @allowed_statuses and @allowed_statuses.any? %>
            <p>
              <label><b><%=h l(:field_status)%>:</b></label>
              <%= f.select :status_id, (@allowed_statuses.collect {|p| [p.name, p.id]}), :required => true %>
            </p>
          <% end %>
          <p>
            <%= f.text_area :comment,
              :cols => 30,
              :rows => 10,
              :accesskey => accesskey(:edit),
              :class => 'wiki-edit',
              :id => 'review_comment_' + @review.id.to_s %>
          </p>
          <p>
            <%
            submit_url = url_for(:controller => 'code_review', :action => 'update', :id => @project)
            -%>
            <%= button_tag l(:button_apply), type: 'button', onclick: "$('#show_review_#{@review.id}').load('#{submit_url}', $('##{review_form_id}').serialize())" %>
            <input type="button" value="<%=h l(:button_cancel) %>" onclick='$("#review_form_<%= @review.id %>").hide();' />
          </p>
          <%= wikitoolbar_for 'review_comment_' + @review.id.to_s %>
        <% end %>
      </div>
    </div>

    <!-- review replies -->
    <% journals = @review.issue.journals.sort {|a, b| a.id <=> b.id } %>
    <% if respond_to?('render_journal') %>
      <% # ChiliProject -%>
      <% if journals.length > 0 %>
        <div id="history">
          <h3 class="rounded-background"><%= l(:label_history) %></h3>
          <%= render :partial => 'history', :locals => { :issue => @review.issue, :journals => journals } %>
        </div>
      <% end %>
    <% else %>
      <% # Redmine -%>
      <% if journals.length > 0 %>
        <h3><%= l(:label_history) %></h3>
      <% end %>
      <%= render :partial => 'reply', :collection => journals %>
    <% end %>

    <!-- reply form -->
    <% if authorize_for('code_review', 'reply') -%>
      <p>
        <%= toggle_link l(:button_reply), "reply_#{@review.id}", :focus => 'reply_comment_' + @review.id.to_s %>
      </p>
      <%  
      message_form_id = "message-form-#{@review.id}"
      -%>
      <div id="reply_<%= @review.id  %>" style="display:none;" class="box">
        <%= form_tag({:action => 'reply', :id => @project, :review_id => @review.id}, :id => message_form_id) do |f| %>
          <%= hidden_field_tag :review_id, @review.id %>
          <%= hidden_field :review,  :lock_version%>
          <%= hidden_field :issue,  :lock_version%>
          <% if @allowed_statuses and @allowed_statuses.any? %>
            <p>
              <label><%=h l(:field_status) %>:</label>
              <%  # select :review, :status, {l(:label_review_closed) => CodeReview::STATUS_CLOSED, l(:label_review_open) => CodeReview::STATUS_OPEN} %>
              <%= select :review, :status_id, (@allowed_statuses.collect {|p| [p.name, p.id]}), :required => true %>
            </p>
          <% end %>
          <p>
            <%= text_area_tag 'reply[comment]', '',
              :cols => 30,
              :rows => 10,
              :accesskey => accesskey(:edit),
              :class => 'wiki-edit',
              :id => 'reply_comment_' + @review.id.to_s %>
          </p>
          <p>
            <%
            submit_url = url_for(:controller => 'code_review', :action => 'reply', :id => @project)
            -%>
            <%= button_tag l(:button_apply), type: 'button', onclick: "$('#show_review_#{@review.id}').load('#{submit_url}', $('##{message_form_id}').serialize())" %>
            <%
            reply_preview_id = "reply_preview_#{@review.id}"
            -%>
          </p>
          <%= wikitoolbar_for 'reply_comment_' + @review.id.to_s %>
        <% end %>
      </div>
    <% end %>

  </div>

</div>
