<div class="page-header">
  <h3>Subscriptions</h3>
</div>
<table class="table table-condensed table-striped data-table">
  <thead>
    <tr>
      <th>Product category</th>
      <th>Product name</th>
      <th>Billing period</th>
      <th>Start date</th>
      <th>Charged through date</th>
      <th>Notes</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    <% if subscriptions.present? && !subscriptions.nil? %>
      <% subscriptions.each do |sub| %>
        <tr>
          <td><%= sub.product_category.downcase.capitalize if sub.product_category.present? %></td>
          <td><%= sub.product_name.downcase.capitalize if sub.product_name.present? %></td>
          <td><%= sub.billing_period.downcase.capitalize if sub.billing_period.present? %></td>
          <td><%= format_date(sub.start_date).html_safe  if sub.start_date.present? %></td>
          <td><%= format_date(sub.charged_through_date).html_safe if sub.charged_through_date.present? %></td>
          <td>
            <% if sub.canceled_date.present? && !sub.canceled_date.nil? %>
              <% if Time.parse(sub.canceled_date) > Time.now %> <%= "Pending cancellation on " %> <% else %> <%= "Canceled on " %> <% end %> <%= format_date(sub.canceled_date).html_safe %>
            <% end %>
          </td>
          <td>
            <nobr>
              <% if sub.product_category == 'BASE' %>
                <%= link_to "Change", kaui_engine.edit_subscription_path(sub.subscription_id), :class => "btn btn-mini" unless sub.canceled_date.present? %>
                <%= link_to "Cancel", kaui_engine.subscription_path(:id => sub.subscription_id), :method => :delete, :class => "btn btn-mini" %>
                <%= link_to "Add Addons", kaui_engine.new_subscription_path(:params => { :bundle_id => sub.bundle_id, :base_subscription => sub.product_name }), :class => "btn btn-mini" %>
              <% else %>
              <% end %>
              <% if sub.product_category == 'ADD_ON' %>
                <%= link_to "Cancel", kaui_engine.subscription_path(:id => sub.subscription_id), :method => :delete, :class => "btn btn-mini" unless sub.canceled_date.present? %>
                <% if sub.canceled_date.present? && Time.parse(sub.canceled_date) > Time.now %>
                  <%= link_to "Reinstate", kaui_engine.reinstate_subscription_path(:id => sub.subscription_id), :method => :put, :class => "btn btn-mini" %>
                <% end %>
              <% end %>
            </nobr>
          </td>
        </tr>
      <% end %>
    <% end %>
  </tbody>
</table>