<%
  edit_baseline = User.current.allowed_to?(:edit_baselines, @project)
  baselines = Project.where(easy_baseline_for: @project).reorder(:updated_on)
  earned_values = @project.easy_earned_values.preload(:baseline).reorder(:updated_at).reverse_order
  back_url = settings_project_path(@project, 'easy_earned_values')
%>

<br>
<br>

<table class="list">
  <thead>
    <tr>
      <th><%= l(:field_name) %></th>
      <th><%= l(:field_type) %></th>
      <th><%= l(:label_easy_baselines) %></th>
      <th><%= l(:label_default) %></th>
      <th><%= l(:field_created_on) %></th>
      <th style="width: 20%"></th>
    </tr>
  </thead>
  <tbody>
    <% earned_values.each do |earned_value| %>
      <tr>
        <td><%= earned_value.name %></td>
        <td><%= earned_value.type_name %></td>
        <td><%= earned_value.baseline.presence || '-' %></td>
        <td><%= checked_image earned_value.project_default? %></td>
        <td><%= format_date earned_value.created_at %></td>
        <td class="buttons">
          <% if !earned_value.planned_loaded %>
            <%= link_to l(:button_easy_earned_values_reload_planned), reload_planned_easy_earned_value_path(earned_value, back_url: back_url), class: 'icon icon-reload' %>
          <% end %>
          <%= link_to l(:button_edit), edit_easy_earned_value_path(earned_value), class: 'icon icon-edit' %>
          <%= delete_link easy_earned_value_path(earned_value) %>
        </td>
      </tr>
    <% end %>

    <tr>
      <td colspan="5"></td>
      <td class="buttons">
        <%= link_to l(:label_new), new_project_easy_earned_value_path(@project), class: 'icon icon-add' %>
      </td>
    </tr>
  </tbody>
</table>

<br>
<br>
<br>

<div class="splitcontent">
  <div class="splitcontentleft">
    <% if edit_baseline %>
      <h3><%= l(:label_create_easy_baseline) %></h3>

      <%= form_tag project_easy_baselines_path(@project) do %>
        <%= hidden_field_tag 'back_url', back_url %>

        <div class="tabular">
          <p>
            <%= label_tag 'easy_baseline[name]', l(:field_name), required: true %>
            <%= text_field_tag 'easy_baseline[name]', '', required: true %>
          </p>

          <p>
            <%= submit_tag l(:button_create) %>
          </p>
        </div>
      <% end %>
    <% end %>
  </div>

  <div class="splitcontentright">
    <h3><%= l(:label_easy_baselines) %></h3>

    <table class="list">
      <% baselines.each do |baseline| %>
        <tr>
          <td><%= baseline.name %></td>
          <td><%= format_time baseline.updated_on %></td>
          <td>
            <% if edit_baseline %>
              <%= delete_link project_easy_baseline_path(@project, baseline, back_url: back_url) %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </table>
  </div>
</div>