<!-- <div class="row">
  <div class="col-lg-12">
    <ol class="breadcrumb">
       <li><a href="<%= crm_root_path %>">Home</a></li>
       <li class="active">
           <a href="<%= transactions_credit_notes_path %>">Credit Note</a>
       </li>

    </ol>
  </div>
</div> -->
<div class="page-header">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="<%= dashboard_path %>"><%= t("views.right_panel.button.home") %></a></li>
    <li class="breadcrumb-item active"><%= t("views.right_panel.detail.credit_note") %></li>
    <span class="float-right">
      <%= link_to 'Back', dashboard_path %>
    </span>
  </ol>
</div>
<style>
  @media screen and (max-width: 785px) {
    section.content{
      margin: 80px 5px 0 5px !important;
    }
  }
</style>
<div class="row">
  <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
      <div class="main-box-body clearfix p-l-5 p-r-5">
        <div class="table-responsive">
          <table class="table table-">
            <tbody>
               <tr>
                 <td colspan="">
                    <%= form_tag transactions_credit_notes_path, :method => :get do %>
                       <div class="row clearfix">
                         <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 mbl-style">
                           <label class="font-14"><%= t("views.right_panel.detail.enter_keyboards") %></label>
                           <input type="text" name="receipt_no" class="form-control m-t-3" placeholder="Receipt No/Cashier Name/Status" style="margin-right: 10px">
                         </div>
                         <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 col-mbl-view mbl-style">
                          <label class="font-14"><%= t("views.right_panel.detail.select_customer") %></label>
                           <select class="selectpicker form-control col-md-12" name="customer">
                            <option value=""><%= t("views.right_panel.detail.select_customer") %></option>
                              <%  @customers.each do |customer| %>
                                 <option value="<%= customer.customer_id %>">
                                <%= customer.name %></option>
                              <%end %>
                            </select>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 col-mbl-view mbl-style">
                          <label class="font-14"><%= t("views.right_panel.detail.order_source") %></label>
                           <%= select_tag "order_source", options_for_select(@sources, :selected => params[:order_source]), :class => "form-control" %>
                        </div>
                         <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 col-mbl-view mbl-style">
                           <label class="font-14 mbl_lbl"><%= t("views.right_panel.detail.from") %></label>
                           <input class="form-control datepicker m-t-3" name="from" id="date" type="text" placeholder="From date">
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 col-mbl-view mbl-style">
                          <label class="font-14 mbl_lbl"><%= t("views.right_panel.detail.to") %></label>
                          <input class="form-control datepicker m-t-3" name="to" id="date" type="text" placeholder="To date">
                        </div>

                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 mbl-right-btn mbl-style">
                          <label class="font-14"> &nbsp;</label><br>
                          <input type="submit" value="Search" class='btn btn-primary btn-md'>
                        </div>
                       </div>
                    <% end %>
                  </td>
               </tr>

            </tbody>


          </table>
        </div>
        <div class="card">
          <div class="table-responsive">
            <% if (!@credit_notes.empty?) && (params[:order_source] == "doemal_order") %>
              <div class="row pt-3 pb-3 pr-3 float-right">
                <div class="col-md-12 col-sm-12 col-lg-12">
                  <button class="btn btn-lg bg-blue waves-effect credit_payment"><%= t :credit %> <%= t :payment %></button>
                </div>
              </div>
            <% end %>
            <table class="table table-striped">
              <thead>
                <tr>
                  <% if params[:order_source] == "doemal_order" %>
                    <th></th>
                  <% end %>
                  <th><%= t("views.right_panel.detail.sale_id") %></th>
                  <th><%= t("views.right_panel.detail.receipt_no") %></th>
                  <th><%= t("views.right_panel.detail.credit_amount") %></th>
                  <th><%= t :cashier %></th>
                  <th><%= t :customer %> <%= t("views.right_panel.detail.name") %></th>
                  <th><%= t("views.right_panel.detail.order_source") %></th>
                  <th><%= t("views.right_panel.detail.receipt_date") %></th>
                </tr>
              </thead>

              <tbody>
              <% if @credit_notes != 0 %>

                    <% @credit_notes.each do |sale| %>
                      <tr>
                        <% if params[:order_source] == "doemal_order" %>
                          <td><input type="checkbox" class="" name="chk_order" value="<%= sale.sale_id %>"></td>
                        <% end %>
                        <td><%= link_to sale.sale_id, transactions_sale_path(sale) %></td>
                        <td><%= sale.receipt_no %></td>
                        <td class="credit_<%= sale.sale_id %>"><%credit = SalePayment.where('sale_id = ? AND payment_method=?', sale.sale_id,"creditnote").first %>
                          <%= credit.payment_amount rescue '-' %>
                        </td>
                        <td><%= sale.cashier_name rescue '-' %></td>
                        <td><%= link_to sale.customer.name, crm_customer_path(sale.customer_id) %></td>
                        <td>
                          <%if sale.source == "cashier" || sale.source == "emenu" %>
                            Cashier
                          <% elsif sale.source == "quick_service" %>
                            Quick Service
                          <% else %>
                            Online Order
                          <% end %>
                        </td>
                        <td> <%= sale.receipt_date.strftime("%d-%m-%Y %I:%M %p") %> </td>
                      </tr>
                    <% end %>
                <% else %>
                      <tr><td colspan="8"><strong><p style="text-align: center"><%= t("views.right_panel.detail.no_data_txt") %>....</p></strong></td></tr>
                <% end %>
              </tbody>
            </table>
           <br>
           <% if @credit_notes != 0 %>
               <%= paginate @credit_notes %>
           <% end %>
        </div>
        </div>
      </div>

  </div>
</div>

<script type="text/javascript">
  var arr_sale = [];
  $(function () {
     $("input[name='chk_order']").on('click',function(){
      var arr = {};
      if($(this).is(':checked')){
        arr[$(this).val()] = parseFloat($(".credit_"+$(this).val()).html());
        // console.log(arr);
        arr_sale.push(arr);
      }else{
        arr[$(this).val()] = parseFloat($(".credit_"+$(this).val()).html());
        arr_sale = removeItem(arr_sale, arr, $(this).val());
      }
      // console.log(arr_sale);
     });

     $(".credit_payment").on('click', function(){
        $.ajax({
          type: 'POST',
          url: '/origami/payment/credit_payment',
          data: {data: JSON.stringify(arr_sale)},
          success: function (result) {
            // console.log(result);
            if (result.status == true) {
              swal({
                title: "Information!",
                text: "Credit Payment Successfully",
                html: true,
                closeOnConfirm: false,
                closeOnCancel: false,
                allowOutsideClick: false
              }, function () {
                window.location.reload();
              });
            }else{
              swal("Opps",result.message,"warning");
            }
          }
        });
     });
  });

  function removeItem(arr_item, remove, sale_id){
    if((arr_item!=undefined) && (arr_item!=null)){
      if(arr_item.length > 0){
        $.each(arr_item, function(i_key, i_value){
          $.each(i_value, function(key,val){
            if(key == sale_id){
              arr_item.splice(arr_item.indexOf(key),1);
            }
          });
        });
      }
    }
    return arr_item;
  }
</script>
