<p>
<%
if current_user.role == "admin" 
  advancedView = true
else
  advancedView = false
end
if @project.run_stat.nil? 
  busy = false
else
  busy = true
end
if @project.user_id == @current_user.id or current_user.role == "admin"
  authorized = true
else
  authorized = false
end

if [
"approved",
"displayed",
"released",
"reviewing",
"revoked"
].any? {|nonActiveStatus| @project.status == nonActiveStatus }
  nonActive = true
else
  nonActive = false
end

# handle freeze deadline
if (@deadline)
  if Time.now > @deadline
    nonActive = true
    flash[:notice] = "Pipeline not available due to post-freeze processing<br>(as of #{@deadline.strftime('%Y-%m-%d %H:%M')})."
  end
end

%>
<% if @project.project_archives.blank? and @project.status=="new" %>
   <b>Submit data for this project:</b>
<% end %>

<table style="margin-top:10px;" cellpadding=1 cellspacing=1>

<tr>
<td>Submission: </td><td><%= @project.name -%></td><td>&nbsp;&nbsp;&nbsp;</td>
<td>Created: </td><td><%= @project.created_at.strftime("%Y-%m-%d %H:%M"); -%></td>
  <td>&nbsp;&nbsp;</td>
  <td><%= distance_of_time_in_words_to_now(@project.created_at)+" ago" -%></td>
</tr>

<tr>
<td>DB: </td><td><%= @project.db -%></td><td>&nbsp;&nbsp;&nbsp;</td>
<td>Updated: </td><td><%= @project.updated_at.strftime("%Y-%m-%d %H:%M"); -%></td>
  <td>&nbsp;&nbsp;</td>
  <td><%= distance_of_time_in_words_to_now(@project.updated_at)+" ago" -%></td>
</tr>

<tr>
<td>Status: </td><td><%= @project.status -%></td>
</tr>

<tr>
  <td>&nbsp;</td>
</tr>

<% if advancedView %>
<tr><td>Submission ID: &nbsp;&nbsp;&nbsp;</td><td><%= @project.id -%></td></tr>
<tr><td>Type: </td><td> <%= @project.project_type.short_label -%></td></tr>
<tr><td>Active: </td><td><%= @project.archives_active -%></td></tr>
<% end %>
</table>

<% if @aheadOfYou -%>
<strong>Number of Waiting Jobs Ahead of Yours: </strong> <%= @aheadOfYou -%> <br/>
<% end %>

<% if @uploadText -%>
<strong>Progress uploading: </strong> <%= @uploadText -%> <br/>
<% end %>

<%
needLoadLink = false
needValidateLink = false
needUploadLink = false
needDeleteLink = false
needRenameLink = false
needUnloadLink = false
needReexpandAllLink = false
needShowDafLink = false
needShowDdfLink = false


if (not busy) and authorized and ((not nonActive) or advancedView)


  if @project.status == "validated" 
    needLoadLink = true
  end

  if @project.status == "expanded" \
  or @project.status == "validate failed" 
    needValidateLink = true
  end

  if @project.status == "new" \
  or @project.status == "expanded" \
  or @project.status == "upload failed" \
  or @project.status == "validate failed" \
  or @project.status == "load failed" \
  or advancedView
     needUploadLink = true
  end

  needDeleteLink = true

  needRenameLink = true

  if (advancedView and @project_status != "new") or @project.status == "loaded"
    needUnloadLink = true
  end

  if (@project.status == "expand failed") \
  or (@project.status == "uploaded") \
  or (advancedView and @project_status != "new")
    needReexpandAllLink = true
  end

end 

if authorized  
  if @dafText != ""
    needShowDafLink = true
  end

  if @ddfText != ""
    needShowDdfLink = true
  end
end 

if needLoadLink \
or needValidateLink \
or needUploadLink \
or needDeleteLink \
or needRenameLink \
or needUnloadLink \
or needReexpandAllLink \
or needShowDafLink \
or needShowDdfLink
%>
  <table style="margin-top:10px;" cellpadding=1 cellspacing=1>
    <tr>

    <% if needLoadLink %> 
      <td><%= ' [ ' -%> 
      <%= link_to 'Load', {:action => 'db_load', :id => @project.id} -%>
      <%= ' ] ' -%> </td>
    <% end %>

    <% if needValidateLink %>
      <td><%= ' [ ' -%> 
      <%= link_to 'Validate', {:action => 'validate', :id => @project.id} -%>
      <%= ' ] ' -%> </td>
    <% end %>

    <% if needUploadLink %>
      <td><%= ' [ ' -%> 
      <%= link_to 'Submit Data', {:action => 'upload', :id => @project.id} -%>
      <%= ' ] ' -%> </td>
    <% end %>

    <% if needDeleteLink %>
      <td><%= ' [ ' -%> 
      <%= link_to 'Delete Submission', {:action => 'delete', :id => @project.id},
	:confirm => "Are you sure you want to delete this submission?" -%>
      <%= ' ] ' -%> </td>
    <% end %>

    <% if needRenameLink %>
      <td><%= ' [ ' -%> 
      <%= link_to 'Rename Submission', {:action => 'edit', :id => @project.id} -%>
      <%= ' ] ' -%> </td>
    <% end %>

    <% if needUnloadLink %>
      <td><%= ' [ ' -%> 
      <%= link_to 'Unload', {:action => 'unload', :id => @project.id},
	:confirm => "Are you sure you want to unload this submission?" -%>
      <%= ' ] ' -%> </td>
    <% end %>

    <% if needReexpandAllLink %>
      <td><%= ' [ ' -%> 
      <%= link_to 'Expand', {:action => 'reexpand_all', :id => @project.id} -%>
      <%= ' ] ' -%> </td>
    <% end %>

    <% if needShowDafLink %>
      <td><%= ' [ ' -%> 
      <%= link_to 'Show DAF', {:action => 'show_daf', :id => @project.id} -%>
      <%= ' ] ' -%> </td>
    <% end %>

    <% if needShowDdfLink %>
      <td><%= ' [ ' -%> 
      <%= link_to 'Show DDF', {:action => 'show_ddf', :id => @project.id} -%>
      <%= ' ] ' -%> </td>
    <% end %>

    </tr>

  </table>
<% end %>


<p>
<% if !@project.project_archives.blank? && @project.status != "new"  %>
<table cellspacing=2 cellpadding=2>
<tr><td></td>
<td align="left"><b>File</b></td>
<td align="left"><b>Size</b></td>
<td align="left"><b>Updated</b></td>
<% if advancedView %>
  <td align="left"><b>Status</b></td>
<% end %>
<td></td></tr>
<% @project.project_archives.reverse_each do |a| %>
  <% n = a.archive_no-1 %>
  <% c = @project.archives_active[n..n] %>
  <% if c == "0" && !advancedView %> 
    <% next %>
  <% end %>

  <tr style="margin: 10;">
  <td><b>Archive</b></td><td><%= a.file_name -%></td>
  <td align="right"><%= a.file_size -%></td>
  <td><%= a.file_date.strftime("%Y-%m-%d %H:%M") -%></td>

  <% if advancedView %>
    <td><%= 
    if a.status == "schedule uploading" then "schedule uploading"
    elsif a.status == "schedule expanding" then "schedule expanding"
    elsif a.status == "see current" then @project.status
    else a.status
    end
    -%></td>
    <td><%= a.archives_active -%></td>
  <% end %>

  <% if (not busy) and authorized and advancedView %>
    <% if c == "1" %>
      <td><%= link_to 'Delete', {:action => 'delete_archive', :id => a.id} -%></td>
    <% else %>
      <td><%= link_to 'Un-Delete', {:action => 'delete_archive', :id => a.id} -%></td>
    <% end %>
  <% end %>
  </tr>

  <% a.project_files.each do |f| %>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<%= f.file_name -%></td>
      <td align=right><%= f.file_size -%></td>
      <td><%= f.file_date.strftime("%Y-%m-%d %H:%M") -%></td>
    </tr>
  <% end %>

<% end %>
</table>
<p>
<% end %>

<% if @errText != "" -%> 
  <hr />
  Error:<br>
<pre>
<%= @errText -%>
</pre>
  <br>
<% end %>
