<div class="page-header">
 <ol class="breadcrumb">
  <li class="breadcrumb-item"><a href="<%= root_path %>"><%= t("views.right_panel.button.home") %></a></li>
  <li class="breadcrumb-item active"><%= t :inventory %></li>
  <span class="float-right">
    <%= link_to t('.back', :default => t("views.btn.back")), inventory_path %>
  </span>
</ol>
</div>


<div class="row">
  <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <table class="table table-responsive table-striped table-hover">
      <thead>
        <tr>
          <th><%= t("views.right_panel.detail.date") %></th>
          <th><%= t("views.right_panel.detail.type") %></th>
          <th><%= t("views.right_panel.detail.item") %> <%= t("views.right_panel.detail.name") %></th>
          <th><%= t :credit %></th>
          <th><%= t :debit %></th>
          <th><%= t("views.right_panel.detail.balance") %></th>
          <th><%= t("views.right_panel.detail.remark") %></th>
        </tr>
      </thead>
      <tbody>
        <% total_credit = 0 %>
        <% total_debit = 0 %>
        <% total_balance = 0 %>

        <% if !@stock_journals.nil? && !@stock_journals.empty? %>
          <% @stock_journals.each do |result| %>
          <tr>
            <td><%= result.created_at.strftime('%e %b %Y %I:%M %p') rescue '-' %></td>
            <td><%= result.trans_type rescue '-' %></td>
            <td>
              <% menu_item = MenuItemInstance.find_by_item_instance_code(result.item_code)%>
              <% if menu_item.nil? %>
              <%= Product.find_by_item_code(result.item_code).name rescue "-" %>
              <% else %>
              <%= menu_item.menu_item.name rescue "-" %>
              - <%= menu_item.item_instance_name rescue "-" %>
              <% end %>
            </td>
            <td><%= result.credit rescue '-' %></td>
            <td><%= result.debit rescue '-' %></td>
            <td><%= result.balance rescue '-' %></td>
            <td><%= result.remark rescue '-' %></td>
          </tr>
          <% !result.credit.nil? ? total_credit += result.credit : total_credit += 0 %>
          <% !result.debit.nil? ? total_debit += result.debit : total_debit += 0 %>
          <% !result.balance.nil? ? total_balance += result.balance : total_balance += 0 %>
          <% end %>

          <!-- <tr style="border-top: 3px solid grey;">
            <td colspan="3"></td>
            <td><b><%= total_credit rescue '-' %></b></td>
            <td><b><%= total_debit rescue '-' %></b></td>
            <td><b><%= total_balance rescue '-' %></b></td>
            <td colspan="2"></td>
          </tr> -->
        <% else %>
          <tr>
            <td colspan="8" class="text-center">There is no record...</td>
          </tr>
        <% end %>
      </tbody>
    </table>
    <br>
    <% if !@stock_journals.nil? && !@stock_journals.empty? %>
       <%= paginate @stock_journals %>
   <% end %>
  </div>
</div>
