<%= form_tag(url_for(:controller => 'pull_requests', :action => 'create', :project_id => @project.id, :forked_project_id => @forked_project.try(:gpid)), :id => 'pull_request_form', :method => "post", :remote => true) do %>
    <div class="new-merge-wrap">
      <div class="merge-option-name fl mt8"><span class="c_red">*</span>标题</div><input type="text" id="pr_name" name="title" class="merge-title-input fl ml30" />
      <p id ="pull_request_title" class="fl ml100 fontGrey2 mt5 c_red" style="display: none">标题不能为空</p>
      <div class="cl mb10"></div>
      <div class="merge-option-name fl">描述</div><textarea type="text" name="description" class="merge-description-input fl ml30"></textarea>
      <!--<p class="fl ml100 f12 mt5"><a href="javascript:void(0);" class="AnnexBtn fl mr10">上传附件</a></p>-->
      <div class="cl"></div>
    </div>
    <div class="new-merge-wrap borderBottomNone">
      <div class="merge-option-name fl" style="padding:5px 0">源分支</div>
      <%= select_tag :branch, options_for_select(@source_rev), :id => "source_branch", :name => "source_branch", :value => "source_branch", :class => "ml30 fontGrey3 fb fl", :style => "padding:5px 0 5px 5px;" %>
      <div class="cl mb10"></div>
      <div class="merge-option-name fl" style="padding:5px 0">目标分支</div>
      <% if @forked_project.nil? %>
          <%= select_tag :branch, options_for_select(@source_rev), :id => "pull_request_branch", :name => "target_branch", :value => "target_branch",:class => "ml30 fontGrey3 fb fl", :style => "padding:5px 0 5px 5px;" %>
      <% else %>
          <select onchange="choice_branch(this.value, document.getElementById('pull_request_branch'), <%= @source_rev %>, <%= @forked_rev %>);" id="pull_request_project" name="source_project" value="source_project" class="ml30 fontGrey3 fb fl" style = "padding:5px 0 5px 5px;">
            <option name="<%= @project.id %>" value="source_project_name" ><%= @source_project_name %></option>
            <option name="<%= @forked_project.id %>" value="forked_project_name" ><%= @forked_project_name %></option>
          </select>
          <select name="target_branch" id="pull_request_branch" class = "ml30 fontGrey3 fb fl" style = "width:140px; padding:5px 0 5px 5px;">
            <% @source_rev.each do |rev| %>
                <option value="<%= rev %>"><%= rev %></option>
            <% end %>
          </select>
      <% end %>
      <p id="pull_request_project_hidden" style="display: none"><%= @forked_project.nil? ? "" : @project.id %></p>
      <div class="cl"></div>
      <p id ="pull_request_branch_error" class="ml100 fontGrey2 mt5 c_red" style="display: none">没有内容可以合并，请切换一个不同的分支</p>
      <div class="cl"></div>
    </div>
    <div class="new-merge-row b_grey" style="border-top:1px solid #ddd;">
      <a href="javascript:void(0);" class="BlueCirBtn fl ml10" onclick="pull_request_commit()">提交请求</a>
      <%= link_to "返回", project_pull_requests_path(:project_id => @project.id), :class => "fr linkGrey2 mt5 mr10" %>
      <div class="cl"></div>
    </div>
<% end %>

<script>
    function regex_pr_name()
    {
        var name = $.trim($("#pr_name").val());
        if(name.length == 0)
        {
            $("#pull_request_title").show();
            return false;
        }
        else
        {
            $("#pull_request_title").hide();
            return true;
        }
    }

    function regex_branch()
    {
        var source_branch = $.trim($("#source_branch").val());
        var target_branch = $.trim($("#pull_request_branch").val());
        var target_project = $.trim($("#pull_request_project").children().attr("name"));
        var target_forked_project = $.trim($("#pull_request_project_hidden").text());
        if(target_project == target_forked_project && source_branch == target_branch)
        {
            $("#pull_request_branch_error").show();
            return false;
        }
        else
        {
            $("#pull_request_branch_error").hide();
            return true;
        }
    }

    //提交pull request
    function pull_request_commit()
    {
        if(regex_pr_name() && regex_branch())
        {
            $("#pull_request_form").submit();
        }
    }

     //切换项目时，替换分支
     $("#targetProject").change(function(){
        var defaultBranch = $("#targetBranch option:first-child").val();
        $("#targetBranch").val(defaultBranch);
    });
</script>