<div class="container-fluid">
  <div class="page-header">
   <ul class="breadcrumb">
    <li class="breadcrumb-item"><a href="<%= root_path %>">Home</a></li>
    <li class="breadcrumb-item active">CRM</li>
     <!-- <li class="breadcrumb-item"><a href="<%= root_path %>">Home</a></li> -->
    <span class="float-right">
      <%= link_to 'Back', crm_customers_path %>
    </span>
  </ul>
</div>

<div class="row">
  <div class="col-lg-12 col-md-12 col-sm-12">
    <!-- Column One -->
    <!-- Nav tabs -->
    <ul class="nav nav-tabs tab-nav-right" role="tablist">

      <li role="presentation" class="active nav-item">
        <a class="nav-link" data-toggle="tab" href="#customer" ><%= t :customer_details %></a>
      </li>
      <li role="presentation" class="nav-item" >
        <a class="nav-link" data-toggle="tab" href="#order" ><%= t :order_details %></a>
      </li>
      <li role="presentation" class="nav-item" >
        <a class="nav-link" data-toggle="tab" href="#sale" ><%= t :sale_details %></a>
      </li>
      <li role="presentation" class="nav-item" >
        <a class="nav-link" data-toggle="tab" href="#credit" ><%= t :credit %> <%= t :details %></a>
      </li>
    </ul>
    <!-- Nav tabs - End -->

    <div class="tab-content" style="min-height:670px; max-height:670px; ">

      <div class="tab-pane active" id="customer" role="tabpanel" style="min-height:670px; max-height:670px; ">
        <h5><%= t :customer_details %></h5>
        <div class="table-responsive">
        <br><br>
          <table class="table table-striped">
            <thead>
              <tr>
                <th><%= t("views.right_panel.detail.card_no") %></th>
                <th><%= t("views.right_panel.detail.name") %></th>
                <th><%= t("views.right_panel.detail.company") %></th>
                <th><%= t("views.right_panel.detail.contact_no") %></th>
                <th><%= t("views.right_panel.detail.email") %></th>
                <th><%= t("views.right_panel.detail.nrc_passport_no") %></th>
                <th><%= t("views.right_panel.detail.address") %></th>
                <th><%= t("views.right_panel.detail.dob") %></th>
                <th><%= t("views.right_panel.detail.membership_type") %></th>
              </tr>
            </thead>


            <tbody>
              <tr>
                <td><%= @customer.card_no rescue '-'%></td>
                <td><%= @customer.name %></td>
                <td><%= @customer.company rescue '-' %></td>
                <td><%= @customer.contact_no %></td>
                <td><%= @customer.email %></td>
                <td><%= @customer.nrc_no rescue '-' %></td>
                <td><%= @customer.address rescue '-'%></td>
                <td><%= @customer.date_of_birth  rescue '-'%></td>
                <% if @customer.membership_type.to_f > 0%>
                  <td><%lookup= Lookup.find_by_value(@customer.membership_type) %>
                     <%= lookup.try(:name) %>
                  </td>
                <%else%>
                 <td>-</td>
                <%end%>

              </tr>
              <tr><th colspan="9"></th></tr>

            </tbody>
          </table>
        </div>

        <div class="table-responsive">
          <h6><%= t("views.right_panel.detail.paymal_transactions ") %></h6>
          <table class="table table-striped">
            <thead>
              <tr>
                <th><%= t("views.right_panel.detail.date") %></th>
                <!-- <th>Redeem</th> -->
                <th><%= t("views.right_panel.detail.amount") %></th>
                <!-- <th>Balance</th> -->
                <th><%= t("views.right_panel.detail.status") %></th>
                <th><%= t("views.right_panel.detail.from_account") %></th>

                <th><%= t("views.right_panel.detail.receipt_no") %></th>
              </tr>
            </thead>
            <tbody>
              <% if @response["status"] == true && @response["data"].count > 0 %>
                <% @response["data"].each do |transaction| %>
                  <tr>
                    <td><%= transaction["date"]%></td>
                    <% if transaction["status"] == 'Rebate' || transaction["status"] == 'Discount' || transaction["status"] == 'Reversal'%>
                      <td><%= transaction["deposit"] %></td>
                    <% else %>
                      <td>(<%= transaction["withdraw"] %>)</td>
                    <% end %>
                    <td><%= transaction["status"] %></td>
                    <td><%= transaction["account_status"] %></td>
                    <td><%= transaction["receipt_no"] %></td>
                  </tr>
                <% end %>
                <% if @response["data"].first["status"] != 'Discount'%>
                <tr  style="border-top:2px solid #666;">
                  <th>Balance</th>
                  <th colspan="4">
                  <% if @response["data"].first["status"] == 'Reversal'%>
                    <%= @response["data"].first["balance"] + @response["data"][1]["balance"]%>
                  <%else%>
                  <%= @response["data"].first["balance"] %>
                    <%end%>
                  </th>
                </tr>
                <%end%>
              <%else%>
                <tr style="border-top:2px solid #666;">
                  <td colspan="5"><p class="align-center">There is no transaction coz of <%=@response["message"]%> ...</p>
                  </td>
                </tr>
              <% end %>
            </tbody>
          </table>
        </div>

      </div>

      <div class="tab-pane" id="order" role="tabpanel" style="min-height:670px; max-height:670px; ">

        <h5><%= t :order_details %></h5>
        <div class="table-responsive">
          <table class="table table-striped">
            <thead>
              <tr>
                <th><%= t("views.right_panel.detail.order_id") %></th>
                <th><%= t("views.right_panel.detail.type") %></th>
                <th><%= t("views.right_panel.detail.order_status") %></th>
                <th><%= t("views.right_panel.detail.order_date") %></th>
                <th><%= t("views.right_panel.detail.items_count") %></th>
              </tr>
            </thead>

            <tbody>
              <% @orders.each do |order| %>
              <tr>
                  <td><%= link_to order.order_id, transactions_order_path(order) %></td>
                  <td><%= order.order_type %></td>
                  <td><%= order.status %></td>
                  <td><%= order.date.strftime("%d-%m-%Y %I:%m %p") %> </td>
                  <td><%= order.item_count %> </td>
              </tr>
              <% end %>
            </tbody>
          </table>
        </div>

      </div>

      <div class="tab-pane" id="sale" role="tabpanel" style="min-height:670px; max-height:670px; ">
          <h5><%= t :sale_details %></h5>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                    <th><%= t("views.right_panel.detail.sale_id") %></th>
                    <th><%= t("views.right_panel.detail.receipt_no") %></th>
                    <th><%= t("views.right_panel.detail.total_discount") %></th>
                    <th><%= t("views.right_panel.detail.tax_amount") %></th>
                    <th><%= t("views.right_panel.detail.sub_total") %></th>
                    <th><%= t("views.right_panel.detail.grand_total") %></th>
                    <th><%= t :cashier %></th>
                    <th><%= t("views.right_panel.detail.sales_status") %></th>
                    <th><%= t("views.right_panel.detail.receipt_date") %></th>
                </tr>

              </thead>

              <tbody>
                <% add_grand_total =0 %>
                  <% @sales.each do |sale| %>
                    <tr>

                      <td><%= link_to sale.sale_id, transactions_sale_path(sale) %></td>
                <td><%= sale.receipt_no %></td>
                <td><%= sale.total_discount %></td>
                <td><%= sale.total_tax %></td>
                <td><%= sale.total_amount %></td>
                <td><%= sale.grand_total rescue '-' %></td>
                <td><%= sale.cashier_name rescue '-' %></td>
                <td> <%= sale.sale_status %> </td>
                <td> <%= sale.receipt_date.strftime("%d-%m-%Y %I:%M %p") %> </td>
                    </tr>
                    <% add_grand_total += sale.grand_total%>
                  <% end %>
                  <% if !@sales.empty?%>
                    <tr  style="border-top:2px solid #666;">
                      <th colspan="5">Balance</th>
                      <th colspan="4"><%= add_grand_total %></th>
                    </tr>
                  <% end %>

              </tbody>
            </table>
         </div>
      </div>

      <div class="tab-pane" id="credit" role="tabpanel" style="min-height:670px; max-height:670px; ">
        <h5><%= t :credit %> <%= t :details %></h5>
        <table class="table table-responsive tbl_credit_lists">
          <thead>
            <th><%= t :credit %> <%= t :sale %> <%= t("views.right_panel.detail.date") %></th>
            <th><%= t("views.right_panel.detail.receipt_no") %></th>
            <th> <%= t :cashier %></th>
            <th> <%= t("views.right_panel.detail.credit_amount") %> </th>
          </thead>
          <tbody>
            <% if !@credit_sales.nil? %>
              <% @credit_sales.each do |credit| %>
                <tr>
                  <td><%= credit.sale_date.strftime("%e %b %Y %I:%M%p") %></td>
                  <td><%= credit.receipt_no %></td>
                  <td><%= credit.cashier_name %></td>
                  <td><%= credit.payment_amount %></td>
                </tr>
              <% end %>
            <% end %>
          </tbody>
        </table>
      </div>

      </div>
    </div>

  </div>
</div>
