<% if @invoice.present? %>
  <h3>Invoice number: INV<%= @invoice.invoice_number %></h3>
  <dl class="dl-horizontal">
    <dt>Account Name:</dt>
    <dd><%= @account.name %>&nbsp;</dd>
    <dd><%= link_to @account.email, Kaui.account_home_path.call(@account.external_key) %>&nbsp;</dd>
    <dt>Invoice date:</dt>
    <dd><%= format_date(@invoice.invoice_date).html_safe %>&nbsp;</dd>
    <dt>Target date:</dt>
    <dd><%= format_date(@invoice.target_date).html_safe %>&nbsp;</dd>
    <dt>Amount:</dt>
    <dd><%= humanized_money_with_symbol @invoice.amount_to_money(@account.currency) %>&nbsp;</dd>
    <dt>Balance:</dt>
    <dd><%= humanized_money_with_symbol @invoice.balance_to_money(@account.currency) %>&nbsp;</dd>
  </dl>
  <div class="page-header">
    <h3>Invoice items</h3>
  </div>
  <script language="javascript">
    function hightlightLinkedItems(invoice_item_id, linked_invoice_item_id) {
      $("#invoice_table tr:gt(0)").each(function(index) {
        var ids = $(this).attr("id").split("_");
        var item_id = ids[2];
        var linked_item_id = ids[3];
        if ((invoice_item_id.length > 0 && item_id == invoice_item_id) ||
            (linked_invoice_item_id.length > 0 && item_id == linked_invoice_item_id) ||
            (invoice_item_id.length > 0 && invoice_item_id == linked_item_id)) {
          $(this).css("background", "yellow");
        } else {
          $(this).css("background", "none");
        }
      });
    }
  </script>
  <table id="invoice_table" class="table table-condensed data-table">
    <thead>
      <tr>
        <th style="width: 10%">External Key</th>
        <th style="width: 30%">Description</th>
        <th style="width: 10%">Start date</th>
        <th style="width: 10%">End date</th>
        <th style="width: 10%">Charged through date</th>
        <th style="width: 10%">Price list</th>
        <th style="width: 10%">Amount</th>
        <th style="width: 10%"></th>
      </tr>
    </thead>
    <tbody>
      <% @invoice.items.each do |item| %>
        <% sub = @subscriptions[item.subscription_id] %>
        <% bundle = @bundles[item.bundle_id] %>
        <tr id=<%= "invoice_item_#{item.invoice_item_id}_#{item.linked_invoice_item_id}" %>>
          <td>
            <%= link_to Kaui.bundle_key_display_string.call(bundle.external_key), Kaui.bundle_home_path.call(bundle.bundle_id) if bundle.present? and bundle.external_key.present? %>
          </td>
          <td onClick="hightlightLinkedItems('<%= item.invoice_item_id %>', '<%= item.linked_invoice_item_id %>'); return false;"><%= item.description %></td>
          <td onClick="hightlightLinkedItems('<%= item.invoice_item_id %>', '<%= item.linked_invoice_item_id %>'); return false;"><%= format_date(item.start_date).html_safe if item.start_date %></td>
          <td onClick="hightlightLinkedItems('<%= item.invoice_item_id %>', '<%= item.linked_invoice_item_id %>'); return false;"><%= format_date(item.end_date).html_safe if item.end_date %></td>
          <td onClick="hightlightLinkedItems('<%= item.invoice_item_id %>', '<%= item.linked_invoice_item_id %>'); return false;"><%= format_date(sub.charged_through_date).html_safe if sub.present? %></td>
          <td onClick="hightlightLinkedItems('<%= item.invoice_item_id %>', '<%= item.linked_invoice_item_id %>'); return false;"><%= sub.price_list.downcase.capitalize if sub.present? and sub.price_list.present? %></td>
          <td onClick="hightlightLinkedItems('<%= item.invoice_item_id %>', '<%= item.linked_invoice_item_id %>'); return false;"><%= humanized_money_with_symbol item.amount_to_money %></td>
          <td><%= link_to "Adjust",
                          kaui_engine.edit_invoice_item_path(item.invoice_item_id, :invoice_id => @invoice.invoice_id),
                          :class => "btn btn-mini" if item.amount > 0 and item.subscription_id.present? %></td>
        </tr>
      <% end %>
    </tbody>
  </table>
  <div class="page-header">
    <% if @payments.present? %>
      <%= render :partial => "kaui/payments/payments_table" %>
    <% end %>
  </div>
<% else %>
  <p>Invoice not found</p>
<% end %>
<%= link_to 'Back', :back, :class => 'btn' %>
<%= link_to "View customer invoice html", kaui_engine.show_html_invoice_path(@invoice.invoice_id), :class => 'btn', :target => "_blank" %>