
<h2><%= I18n.t(:project_user_stories) %>:
  <% if current_user && current_user.is_admin? %>
	<a href="/projects/<%= @project.id %>/edit" id="project_name"><span id="project_name_display"><%= @project.name %></span></a>
  <% else %>
		<span id="project_name_display"><%= @project.name %></span>	
	<% end%>
	
  <span style="font-size: 15px;">(<%= @project.identifier %>)</span>
  

  <button type="button" id="upload-user-stories-btn" class="btn btn-primary btn-sm"  data-bs-toggle="modal" data-bs-target="#uploadModal">
    Upload User Stories
  </button>  
  
  <% if @project.support_traceability?%>
  <button class="btn btn-info btn-sm" id="refresh-traceability-matrix-btn" data-bs-toggle="modal" data-bs-target="#traceabilityModal">Refresh Traceability Matrix</button>
  <% end %>

</h2>


<!-- The Modal -->
<div class="modal" id="uploadModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Upload User Stories</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<%= I18n.t(:close) %>"></button>>
      </div>
      
      <form action="/projects/<%= @project.id %>/upload_user_stories" method="post" enctype="multipart/form-data">      
      
        <div class="modal-body">
          
          <div class="alert alert-info">
            Existing user stories will be replaced.
          </div>
          
          <input type="file" name="user_stories_csv" id="user_stories_csv"/>
          
          <div style="margin-top: 0.6rem; padding-left: 0.3rem;"><a href="/sample-user-stories.csv">Sample CSV</a></div>
          
        </div>
        
        <div class="modal-footer">
          <button type="cancel" class="btn btn-secondary" data-dismiss="modal" data-bs-dismiss="modal"><%= t(:cancel) %></button>
          <button type="submit" id="upload-stories-btn" class="btn btn-primary"><%= t(:upload) %></button>
        </div>
      </form>
      
    </div>
  </div>
</div>

<% if @traceability_lookups%>

    <table class="table">
      <tr>
        <td style="text-align:center">
					<div style="margin-bottom: 0.4rem;">Story count</div>
          <span class="stats_number"><%= @project.user_stories.count %></span>
        </td>
        <td style="text-align:center">
					<div style="margin-bottom: 0.4rem;">Covered count:</div>
          <span id='requirement_covered_count' class="stats_number"><%=@traceability_lookups[:requirement_tests].keys.uniq.size rescue 0 %></td>
        <td style="text-align:center">
					<div style="margin-bottom: 0.4rem;">Test Scripts</div>
            <span id='test_files_count' class="stats_number"><%= @traceability_lookups[:test_files].size rescue 0%></span>
          </td>          
        <td style="text-align:center">
					<div style="margin-bottom: 0.4rem;">Coverage</div>
          <span id="coverage_percentage" class="stats_number"><%= number_to_percentage(@traceability_lookups[:coverage] || 0, :precision => 0)%></td>
        <td style="text-align:center">
          <form action="/projects/<%= @project.id %>/export-traceability-matrix-spreadseet" method="post">                          
            <button type="submit" id="export-excel-btn" class="btn btn-success">Export Excel</button>
          </form>
        </td>
      </tr>
    </table>

<% end %>

<div class="modal fade" id="traceabilityModal" tabindex="-1" role="dialog" aria-labelledby="traceabilityModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Generate Requirement Traceability Matrix</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="<%= I18n.t(:close) %>"></button>
      </div>
      
      <form action="/projects/<%= @project.id %>/generate-traceability-matrix" method="post">      
      
        <div class="modal-body">
          
          <div class="" style="margin-bttom: 0.8rem;">
            <div><i>BuildWise will scan RSpec scripts under functional test directory with matching User Story ID as below:</i></div>
            <img src="/images/add-story-id.png" style="width: 100%;">
          </div>
          
          <div style="margin-top: 0.8rem;">
            <table class="table tabel-strip" >
              <tbody>
                <tr>
                  <td>User stories:</td>
                  <td><b><%= @project.user_stories.count %></b></td>
                </tr>
                <tr>
                  <td>Functional test dir:</td>
                  <td>
                    <code style="font-size: 105%;"><%= @project.config.ui_tests_dir %></code> <br/>
                     <i><%= Dir[File.join(@project.working_dir, "sources", @project.config.ui_tests_dir) + "/*_spec.rb"].count  rescue nil %> test script files</i> 
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
                    
        </div>
        
        <div class="modal-footer">
          <button type="cancel" class="btn btn-secondary" data-dismiss="modal" data-bs-dismiss="modal"><%= t(:cancel) %></button>
          <button type="submit" id="generate-matrix-btn" class="btn btn-primary"><%= t(:generate) %></button>
        </div>
      </form>
      
    </div>
  </div>
</div>

<p>Upload your user stories (e.g. from JIRA) here to generate requirement traceability matrix, based on <code>External ID</code> in RSpec test case names.</p>


<div style="margin-right: 1rem;">
<table class="table table-bordered  table-striped table-hover" border="0" cellspacing="0" align="center" id="user_stories_table" style="">
  <thead class="">
    <th width="5%" nowrap="true">External ID</th>
    <th width="93%">Title</th>
    <th>Pts</th>
  </thead>
  <% if @user_stories && @user_stories.any? %>
  <tbody>
    <% @user_stories.each do |us|%>
    <tr>
      <td>
        <% if !@project.config.user_story_prefix_url.blank? %>
          <%= link_to us.external_id, "#{@project.config.user_story_prefix_url.strip}#{us.external_id}", :target => "_blank" %>
        <% else  %>
          <%= us.external_id %>
        <% end %>
      </td>
      <td>
        <% story_tests = @traceability_lookups[:requirement_tests][us.external_id.to_s] rescue [] %>
        <% story_test_count = story_tests.count rescue 0 %>
        <button type="button" class="btn <% if story_test_count < 1 %>btn-warning<% else %>btn-success<% end %> btn-xs" <% if story_test_count > 0 %>onclick="$('#story-<%= us.id %>-tests').toggle();"<% end%> >
          <span class="badge badge-light"><%= story_test_count %></span>
        </button>
        
        <span style="font-weight: 550;"><%= us.title %></span>
        
        <div class="story-test" style="display:none;" id="story-<%= us.id%>-tests">
         <% if story_tests && story_tests.size() > 0%>
         <ol style="">
         	<% story_tests.each do |spec_behaviour| %> 	
         	  <li>Script: <i><a href="testwise://<%= spec_behaviour.file_path%>"><%= File.basename(spec_behaviour.file_path) %></a></i>
              <p><%= raw highlight_code(spec_behaviour.lines.join("\n"), :line_number_start => spec_behaviour.start_line) %></p>	
            </li>
         	<% end %>  	
         </ol>
         <% end %>
       </div>              
      </td>
      <td><%= us.points %></td>
    </tr>
    <% end %>
  </tbody>
  <% end %>
</table>              
</div>