<div class="row">
  <div class="col s12">
    <div class="card">
      <div class="card-content">
        <div class="card-title">Your payment method</div>
        <div class="row">
          <div class="col s12 m6">
            <p>
              <strong>For your security, we don't store your payment information on file.</strong><br /><br />
            </p>
            <p>
              Instead, we rely on industry-leading payment processor Stripe to safely and securely process payments and handle your
              subscription info.
            </p>
          </div>
          <div class="col s12 m6">
            <% if @stripe_customer.sources.total_count == 0 %>
              <p>
                We don't currently have a payment method on file for you. You'll be asked to add one whenever you
                upgrade, but you can add one at any time here.
                <br /><br />
              </p>
              <p class="center">
                <%= link_to "Add payment method", payment_info_path, class: 'btn white blue-text' %>
              </p>
            <% else %>
              <p>
                We have a payment method on file for you through Stripe
                (<%= @stripe_customer.sources.data[0].try(:brand) || 'a card' %> ending in <%= @stripe_customer.sources.data[0].last4 %>),
                but since we don't store it, you cannot edit it. You can choose to add a new one (replacing the old),
                or delete the existing one.
              </p>
            <% end %>
          </div>
        </div>
      </div>
      <% if @stripe_customer.sources.total_count > 0 %>
      <div class="card-action">
        <%= link_to "Add new payment method", payment_info_path %>
        <%= link_to "Delete existing payment method", delete_payment_method_path %>
      </div>
      <% end %>
    </div>
  </div>

  <% if current_user.paypal_invoices.any? %>
    <div class="col s12">
      <div class="card-panel yellow darken-3">
        Please note that this list only reflects recurring subscription payments and doesn't include any standalone Premium Code purchases yet.
      </div>
    </div>
  <% end %>

  <div class="col s12">
    <div class="card">
      <div class="card-content">
        <div class="card-title">
          <div class="right">
            <% account_balance = @stripe_customer.account_balance %>
            <%= 'Outstanding balance:' if account_balance > 0 %> <%= number_to_currency(account_balance.abs / 100.0) if account_balance != 0 %> <%= 'credit' if account_balance < 0 %>
          </div>
          Billing history
        </div>
        <table class="bordered">
          <thead>
            <tr>
              <th>Date</th>
              <th>Description</th>
              <th>Amount Billed</th>
            </tr>
          </thead>
          <tbody>
            <% @stripe_invoices.first(10).each do |invoice| %>
              <tr>
                <td>
                  <%= Time.at(invoice.date).strftime("%B %d, %Y") %><br />
                  at <%= Time.at(invoice.date).strftime("%I:%M %p") %><br />
                  <%= '(Pending)' if Time.at(invoice.date) > Time.now %>
                </td>
                <td>
                  <ul>
                    <% invoice.lines.data.reverse.each do |line_item| %>
                      <li>
                        <%
                          action = line_item.amount < 0 ? 'prorated' : 'charged'
                          if line_item.description.nil?
                            description = [
                              "a #{line_item.plan.name} subscription ",
                              "from #{Time.at(line_item.period.start).strftime('%B %d, %Y')} ",
                              "to #{Time.at(line_item.period.end).strftime('%B %d, %Y')}"
                            ].join
                          else
                            description = line_item.description
                          end
                        %>

                        You were <%= action %> <%= number_to_currency(line_item.amount / 100) %> for <%= description %>.
                      </li>
                    <% end %>
                  </ul>
                </td>
                <td>
                  <%= number_to_currency(invoice.amount_due / 100) %>
                </td>
              </tr>
            <% end %>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>