<%
# Code Review plugin for Redmine
# Copyright (C) 2010-2012  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 id="code_review_settings">
  <script type="text/javascript">
  function setAutoAssignSettingFormEnable() {
    var form = $('#cr_auto_assign_settings');
    var flag = $('#auto_assign_enabled').is(':checked');
    if (flag) {
      form.show('blind');
    }
    else {
      form.hide('blind');
    }
  }

  function setAutoAssignSettingFiltersEnable() {
    var form = $('#auto_assignment_filter_form');
    var flag = $('#auto_assign_filter_enabled').is(':checked');
    if (flag) {
      form.show('blind');
    }
    else {
      form.hide('blind');
    }
  }

  function is_valid_expression(field) {
    var text = field[0].value;
    if (text == '') {
      return false;
    }

    try {
      new RegExp(text);
    }
    catch (e) {
      alert(e);
      return false;
    }

    return true;
  }
  
  function add_assign_filter() {
    if (!is_valid_expression($('#auto_assign_add_filter_expression'))) {
      return false;
    }
    var url = '<%=url_for(:controller => 'code_review_settings', :action => 'add_filter', :id => @project) %>';
    var args = $('#code_review_form').serialize2json();
    $('#auto_assignment_filters').load(url, args);
  }
  
  function update_assign_filter(num) {
    if (!is_valid_expression($('#auto_assign_edit_filter_' + num + '_expression'))) {
      return false;
    }
    var url = '<%=url_for(:controller => 'code_review_settings', :action => 'edit_filter', :id => @project) %>';
    var args = $('#code_review_form').serialize2json();
    args['num'] = num;
    $('#auto_assignment_filters').load(url, args);
  }
</script>
  
  <%
  @code_review_setting = CodeReviewProjectSetting.find_or_create(@project)
%>

  <%= labelled_form_for :setting, @code_review_setting,
    :url => {:controller => 'code_review_settings',
    :action => 'update', :id => @project, :tab => 'code_review',
    :partial => 'code_review_settings/update',
    :setting_id => @code_review_setting.id}, :html => {:id => 'code_review_form'} do |f| %>
    <%= error_messages_for 'code_review_setting' %>
    <div class="box">
      <%= f.hidden_field :lock_version %>
      <p><%= f.check_box :tracker_in_review_dialog %></p>

      <b><%=h l(:select_tracker_for_code_reviews)%>:</b>
      <p><%= f.select :tracker_id, @project.trackers.collect {|t| [t.name, t.id]}, :required => true %></p>

      <b><%=h l(:select_tracker_for_code_review_assignment)%>:</b>
      <p><%= f.select :assignment_tracker_id, @project.trackers.collect {|t| [t.name, t.id]}, :required => true %></p>

      <p>
        <%= f.check_box :hide_code_review_tab %>
      </p>

      <p>
        <label><%=h l(:label_if_revision_has_issues)%>:</label>
        <%= f.radio_button(:auto_relation, CodeReviewProjectSetting::AUTORELATION_TYPE_RELATES) %>
        <%=h l(:label_review_issue_relates) %>

        <%= f.radio_button(:auto_relation, CodeReviewProjectSetting::AUTORELATION_TYPE_BLOCKS) %>
        <%=h l(:label_review_issue_blocks) %>

        <%= f.radio_button(:auto_relation, CodeReviewProjectSetting::AUTORELATION_TYPE_NONE) %>
        <%=h l(:label_review_issue_do_nothing) %>

      </p>
      <hr/>

      <p>
        <%
        @auto_assign = @code_review_setting.auto_assign_settings
        @auto_assign.subject = l(:code_review_requrest) if @auto_assign.subject.blank?
      -%>
        <label><%=h l(:label_auto_assign_settings)%></label>
        <%= check_box_tag "auto_assign[enabled]", true, @auto_assign.enabled?, :onchange => 'setAutoAssignSettingFormEnable();'%><%=h l(:button_activate)%>
      </p>
      <div id="cr_auto_assign_settings" style="display: <%=  @auto_assign.enabled? ? 'true' : 'none'%>;">
        <p>
          <label><%=h l(:field_author)%></label>
          <%= select :auto_assign, :author_id,
            (@project.users.collect {|user|
              [user.name, user.id]
            }), :selected => @auto_assign.author_id, :required => true %>
        </p>
        <p>
          <label><%=h l(:label_candidates_of_reviewer)%></label>
          <% @project.users.each do |user| %>
            <label class="floating"><%= check_box_tag 'auto_assign[assignable_list][]', user.id, @auto_assign.assignable?(user) %> <%=h user %></label>
          <% end %>
        </p>
        <p>
          <label><%=h l(:field_subject)%></label>
          <%= text_field(:auto_assign, :subject, :size => 70) %>
        </p>
        <p>
          <label><%=h l(:field_description) %></label>
          <%= text_area :auto_assign, :description,
                   :cols => 30,
                   :rows => 12,
                   :accesskey => accesskey(:edit),
                   :class => 'wiki-edit' %>
        </p>
        <p>
        <div id="auto_assignment_filters">
          <%= render :partial => 'code_review_settings/filters' %>
        </div>
        </p>
      </div>
      
    </div>
    <%= submit_tag l(:button_update) %>
  <% end %>
</div>
<%= wikitoolbar_for 'auto_assign_description' %>
