<div class="page-header">
  <h3>Account <%= @account.name %></h3>
  <%= link_to @account.email, Kaui.account_home_path.call(@account.external_key) %>
</div>
<form class="form-horizontal">
  <div class="control-group">
    <label for="bundles" class="control-label">Filter by bundle name</label>
    <div class="controls">
      <%= select_tag "bundles", options_for_select([ "" ] + @bundle_names.values.sort, @selected_bundle || 'all') %>
    </div>
  </div>
</form>
<hr/>
<table id="timeline-table" class="table table-condensed data-table">
  <thead>
    <tr>
      <th class="data-table-sort-desc sort-title-string">Requested Date</th>
      <th class="sort-title-string">Effective Date</th>
      <th>Bundle Name</th>
      <th>Event Type</th>
      <th>Details</th>
      <th>Reason Code / Comments</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    <% if @account.present? && @timeline.present? %>
      <% @timeline.invoices.each do |invoice_stub| %>
        <% if invoice_stub.bundle_keys.present? %>
          <% bundles = invoice_stub.bundle_keys.split(",").map {|bundle_key| @bundle_names[bundle_key] }.join(",") %>
        <% end %>
        <% if invoice_stub.invoice_id.present? && @invoices_by_id.has_key?(invoice_stub.invoice_id) %>
          <% invoice = @invoices_by_id[invoice_stub.invoice_id] %>
        <% else %>
          <% invoice = invoice_stub %>
        <% end %>
        <tr title="<%= bundles %>">
          <td>
            <% if invoice.invoice_dt.present? %>
              <span class="hide" title="<%= invoice.invoice_dt %>"></span>
              <%= format_date(invoice.invoice_dt).html_safe %>
            <% else %>
              [unknown]
            <% end %>
          <td>
            <% if invoice.target_dt.present? %>
              <span class="hide" title="<%= invoice.target_dt %>"></span>
              <%= format_date(invoice.target_dt).html_safe %>
            <% else %>
              [unknown]
            <% end %>
          </td>
          <td>
            <% if invoice_stub.bundle_keys.present? %>
              <% invoice_stub.bundle_keys.split(",").each do |bundle_key| %>
                <%= link_to @bundle_names[bundle_key], Kaui.bundle_home_path.call(bundle_key) %><br/>
              <% end %>
            <% end %>
          </td>
          <td><%= "INVOICE" %></td>
          <td><%= "Amount:" %> <%= invoice.amount %>  <%= @account.currency %><br/>
              <%= "Balance:" %> <%= invoice.balance %> <%= @account.currency %><br/>
              <% if invoice.credit_adjustment.present? &&  invoice.credit_adjustment > 0  %>
                <%= "Credit adjustment:" %> <%= invoice.credit_adjustment %> <%= @account.currency %> <br/>
              <% end %>
              <% if invoice.credit_balance_adjustment.present? &&  invoice.credit_balance_adjustment > 0  %>
                <%= "Credit balance adjustment:" %> <%= invoice.credit_balance_adjustment %>  <%= @account.currency %> <br/>
              <% end %>
              <% if invoice.refund_adjustment.present? &&  invoice.refund_adjustment < 0  %>
                <%= "Refund adjustment:" %> <%= invoice.refund_adjustment %>  <%= @account.currency %> <br/>
              <% end %>
              <%= "Invoice #" %>
              <%= link_to invoice.invoice_number, invoice_path(:id => invoice.invoice_id) %>
          </td>
          <td></td>
          <td>
            <% if invoice.balance > 0 %>
              <nobr>
                <%= link_to "External Payment",
                            kaui_engine.new_credit_path(:params => { :account_id => @account.account_id,
                                                                     :invoice_id => invoice.invoice_id }),
                            :class => "btn btn-mini" %>
              </nobr>
              <% end %>
          </td>
        </tr>
      <% end %>
      <% @timeline.payments.each do |payment| %>
        <% if payment.bundle_keys.present? %>
          <% bundles = payment.bundle_keys.split(",").map {|bundle_key| @bundle_names[bundle_key] }.join(",") %>
        <% end %>
        <% if payment.invoice_id.present? %>
          <% invoice = @invoices_by_id[payment.invoice_id] %>
        <% else %>
          <% invoice = nil %>
        <% end %>
        <tr title="<%= bundles %>">
          <td>
            <% if payment.requested_dt.present? %>
              <span class="hide" title="<%= payment.requested_dt %>"></span>
              <%= format_date(payment.requested_dt).html_safe %>
            <% else %>
              [unknown]
            <% end %>
          </td>
          <td>
            <% if payment.effective_dt.present? %>
              <span class="hide" title="<%= payment.effective_dt %>"></span>
              <%= format_date(payment.effective_dt).html_safe %>
            <% else %>
              [unknown]
            <% end %>
          </td>
          <td>
            <% payment.bundle_keys.split(",").each do |bundle_key| %>
              <%= link_to @bundle_names[bundle_key], Kaui.bundle_home_path.call(bundle_key) %><br/>
            <% end %>
          </td>
          <td><%= "PAYMENT" %></td>
          <td>
            <%= "Total amount:" %> <%= payment.amount %> <%= @account.currency %><br/>
            <%= "Paid amount:" %> <%= payment.amount %> <%= @account.currency %><br/>
            <span <% if payment.status == 'Failed' %>class="alert-error" <% elsif payment.status == 'Success' %>class="alert-success" <% end %>>
              <%= payment.status %>
            </span>
            <br/>
            <%= "Invoice #" %> <%= link_to invoice.invoice_number, invoice_path(:id => invoice.invoice_id) %>
          <td></td>
          <td>
            <% if payment.payment_id.present? %>
              <nobr>
                <%= link_to 'Refund', kaui_engine.new_refund_path(:params => { :payment_id => payment.payment_id,
                                                                               :account_id => @account.account_id,
                                                                               :invoice_id => payment.invoice_id }),
                                                                  :class => "btn btn-mini" %>
                <%= link_to 'Chargeback', kaui_engine.new_chargeback_path(:params => { :payment_id => payment.payment_id,
                                                                                       :account_id => @account.account_id,
                                                                                       :invoice_id => payment.invoice_id }),
                                                                          :class => "btn btn-mini" %>
              </nobr>
              <% elsif invoice.balance > 0 %>
                <%= link_to 'Pay', kaui_engine.new_chargeback_path(:params => { :payment_id => payment.payment_id,
                                                                                :account_id => @account.account_id,
                                                                                :invoice_id => payment.invoice_id }),
                                                                   :class => "btn btn-mini" %>
            <% end %>
          </td>
        </tr>
      <% end %>
      <% @timeline.bundles.each do |bundle| %>
        <% bundle.subscriptions.each do |sub| %>
          <% sub.events.each do |event| %>
            <tr title="<%= @bundle_names[bundle.external_key] %>">
              <td>
                <% if event.requested_dt.present? %>
                  <span class="hide" title="<%= event.requested_dt %>"></span>
                  <%= format_date(event.requested_dt).html_safe %>
                <% else %>
                  [unknown]
                <% end %>
              </td>
              <td>
                <% if event.effective_dt.present? %>
                  <span class="hide" title="<%= event.effective_dt %>"></span>
                  <%= format_date(event.effective_dt).html_safe %>
                <% else %>
                  [unknown]
                <% end %>
              </td>
              <td>
                <%= link_to @bundle_names[bundle.external_key], Kaui.bundle_home_path.call(bundle.external_key) %><br/>
              </td>
              <td><%= event.event_type %></td>
              <td><%= event.product %> <%= event.billing_period == 'NO_BILLING_PERIOD' || event.billing_period.nil? ? "" : event.billing_period.downcase.capitalize %> <%= event.phase.downcase.capitalize if event.phase.present? %></td>
              <td> </td>
              <td> </td>
            </tr>
          <% end %>
        <% end %>
      <% end %>
    <% end %>
  </tbody>
</table>
<div class="form-actions">
  <%= link_to 'Back', :back, :class => 'btn' %>
</div>
<%= javascript_tag do %>
  function filterBundles() {
    var bundle = $("#bundles").val();
    var dataTable = $("#timeline-table").dataTable();

    $(dataTable.fnGetNodes()).each(function() {
      var tr = $(this);
      if (bundle == "" || tr.attr("title").split(",").indexOf(bundle) >= 0) {
        tr.removeClass("hide");
      }
      else {
        tr.addClass("hide");
      }
    });
    dataTable.fnDraw();
  }
  $(document).ready(function() {
    $("#bundles").change(filterBundles);
    filterBundles();
  });
<% end %>