<div class="container-fluid">
<div id="loading_wrapper" style="display:none;">
    <div id="loading"></div>
  </div>
<div class="row">
 <!-- Column One -->
 <div class="col-lg-6 col-md-6 col-sm-6">

   <!-- Nav tabs -->
   <ul class="nav nav-tabs tab-col-teal" role="tablist" id="mytab">
   <li class="nav-item">
       <a class="nav-link" data-toggle="tab" href="#completed" role="tab">Completed</a>
     </li>
     <li class="nav-item">
       <a class="nav-link " data-toggle="tab" href="#tables" role="tab">Tables</a>
     </li>
     <li class="nav-item">
       <a class="nav-link active" data-toggle="tab" href="#rooms" role="tab">Rooms</a>
     </li>
     <li class="nav-item">
       <a class="nav-link" data-toggle="tab" href="#orders" role="tab">Orders</a>
     </li>
   </ul>
   <!-- Nav tabs - End -->
   <div id="custom-slimscroll">
     <div class="tab-content m-t-10" >
       <!--- Panel 0 - Completed Orders -->
       <div class="tab-pane dining" id="completed" role="tabpanel">
         <div class="card-columns">
           <% @complete.each do |sale| %>
           <div class="card sales red text-white" data-id = "<%= sale.sale_id %>">
             <div class="card-block">
                <%= sale.receipt_no %><span class="pull-right font-12"><%= sale.sale_status %></span>
             </div>
           </div>
           <% end %>
         </div>
       </div>

       <!--- Panel 1 - Table Orders -->
       <div class="tab-pane dining" id="tables" role="tabpanel">
         <div class="card-columns">
           <% @tables.each do |table| %>
           <% if table.status == 'occupied' %>
              <% if table.get_booking.nil? %>  
                <% if table.get_checkout_booking.nil? %>            
                  <div class="card tables red text-white table_<%= table.id %>" data-id="<%= table.id %>"> 
                <% else %>               
                  <div class="card tables orange text-white table_<%= table.id %>" data-id="<%= table.id %>">  
                <% end %>              
                    <div class="card-block">
                       <%= table.name %>
                         <span class="pull-right font-12 new_text_<%= table.id %>"> billed</span>
                    </div>
                  </div>
              <% else %>                
                  <% if table.get_checkout_booking.nil? %>
                    <div class="card tables blue text-white table_<%= table.id %>" data-id="<%= table.id %>"> 
                  <% else %>                
                    <div class="card tables orange text-white table_<%= table.id %>" data-id="<%= table.id %>">      
                  <% end %>           
                    <div class="card-block">
                       <%= table.name %>
                         <span class="pull-right font-12 new_text_<%= table.id %>"> new</span>
                    </div>
                  </div>
              <% end %>
           <% else %>
           <div class="card tables green text-white table_<%= table.id %>" data-id="<%= table.id %>">
             <div class="card-block">
                <%= table.name %>
                <span class="pull-right font-12 new_text_<%= table.id %> hide">
             </div>
           </div>
           <% end %>
           <% end %>
         </div>
       </div>

       <!--- Panel 2 - Room Orders -->
       <div class="tab-pane dining active" id="rooms" role="tabpanel">
         <div class="card-columns">
           <% @rooms.each do |room| %>
           <% if room.status == 'occupied' %>

             <% if room.get_booking.nil? %>
                 <div class="card rooms red text-white table_<%= room.id %>" data-id="<%= room.id %>">
                   <div class="card-block">
                      <%= room.name %>
                      <span class="pull-right font-12 new_text_<%= room.id %>"> billed</span>
                      <div  style="font-size:12px;"></div>
                   </div>
                 </div>
             <% else %>
                 <div class="card rooms blue text-white table_<%= room.id %>" data-id="<%= room.id %>">
                   <div class="card-block">
                      <%= room.name %>
                      <span class="pull-right font-12 new_text_<%= room.id %>"> new</span>
                   </div>
                 </div>
             <% end %>
           <% else %>
           <div class="card rooms green text-white table_<%= room.id %>" data-id="<%= room.id %>">
             <div class="card-block">
                <%= room.name %>
                <span class="pull-right font-12 new_text_<%= room.id %> hide">
             </div>
           </div>
           <% end %>
           <% end %>
         </div>
       </div>

       <!--- Panel 3 - Orders -->
       <div class="tab-pane dining" id="orders" role="tabpanel">
         <div class="card-columns">
           <% @orders.each do |order| %>
           <div class="card orders red text-white" data-id = "<%= order.order_id %>">
             <div class="card-block">
                <%= order.order_id %>
             </div>
           </div>
           <% end %>
         </div>
       </div>
     </div>
   </div>
   <!-- tabs - End -->
 </div>
 <!-- Column One -->

 <!-- Column Two -->
  <div class="col-lg-5 col-md-5 col-sm-5">
     <div class="card"  >
       <div class="card-header">
         <% if @status_order == 'order' %>
            <div id="save_order_id" data-order="<%= @obj_order.order_id %>"><strong id="order-title">ORDER DETAILS </strong> |  Table <%= @room.name rescue "" %>
            <% if @booking.checkout_by.nil? && !@booking.reserved_by.nil? && %>
              <span class="float-right"><%= @booking.checkin_at.utc.getlocal.strftime("%I:%M %p") %> - <%= @booking.checkout_at.utc.getlocal.strftime("%I:%M %p") %></span>
            <% else %>
              <span class="float-right">Checkin Time : <%= @booking.checkin_at.utc.getlocal.strftime("%I:%M %p") %></span>
            <% end %>
            </div>

         <% elsif @status_sale == 'sale' %>
            <div><strong id="order-title">INVOICE DETAILS </strong> |  Table <%= @room.name rescue "" %>
            <% if @booking.checkout_by.nil? && !@booking.reserved_by.nil? && %>
              <span class="float-right"><%= @booking.checkin_at.utc.getlocal.strftime("%I:%M %p") %> - <%= @booking.checkout_at.utc.getlocal.strftime("%I:%M %p") %></span>
            <% else %>
              <span class="float-right">Checkin Time : <%= @booking.checkin_at.utc.getlocal.strftime("%I:%M %p") %></span>
            <% end %>
            </div>

         <% end %>

       </div>
       <div class="card-block">
         <div class="card-title row p-l-5 p-r-5">
           
            <% if @status_sale == 'sale' %>
            <div class="col-lg-6 col-md-6 col-sm-6">
            &nbsp;  Receipt No: 
              <span id="receipt_no">
                 <%= @obj_sale.receipt_no rescue '' %>
              </span>
              </div>
             <% else %>
             <div class="col-lg-7 col-md-7 col-sm-7">
             &nbsp; Order No: 
              <span id="receipt_no">
                 <%= @obj_order.order_id rescue '' %>
              </span>
              </div>
            <% end %>
           <% if @status_sale == 'sale' %>
            <div class="col-lg-6 col-md-6 col-sm-6 text-right">
            Date:  <span id="receipt_date"><%= @date.utc.getlocal.strftime("%d/%m/%Y - %I:%M %p") rescue '-'%></span>
           </div>
             <% else %>
               <div class="col-lg-5 col-md-5 col-sm-5 text-right">
                Date:  <span id="receipt_date"><%= @date.utc.getlocal.strftime("%d/%m/%Y") rescue '-'%></span>
               </div>
            <% end %>
           
         </div>
         <div class="card-title row customer_detail p-l-5 p-r-5" >
           <div class="col-lg-6 col-md-6 col-sm-6">
             <% if @status_sale == 'sale' %>
              <p class="hidden customer-id"><%= @obj_sale.customer_id rescue '' %></p>
              &nbsp; Customer : <%= @obj_sale.customer.name rescue '-' %>
             <%else%>
               <p class="hidden customer-id"><%= @obj_order.customer_id rescue "-" %></p>
              &nbsp; Customer : <%= @customer.name rescue "-" %>
             <%end%>
           </div>

         </div>
         <div id="order-detail-slimscroll" data-height="<%= @status_sale=="sale" ? 240 : 160%>">
           <div class="card-text">
             <table class="table table-striped" id="order-items-table">
               <thead>
                 <tr>
                   <th></th>
                   <th class="item-name">Items</th>
                   <th class="item-attr">QTY</th>
                   <th class="item-attr">Price</th>
                 </tr>
               </thead>
               <tbody>
                 <%
                   count = 0
                   sub_total = 0
                   if @status_sale == "sale"
                    @sale_array[0].sale_items.each do |sale_item|

                      sub_total = sub_total + sale_item.price
                 %>
                 <input type="hidden" id="sale_id" value="<%= @sale_array[0].sale_id %>">
                 <%
                 # Can't check for discount
                  # unless sale_item.price == 0
                      count += 1
                  %>
                 <tr>
                   <td><%= count %></td>
                   <td class='item-name'><%= sale_item.product_name %></td>
                   <td class='item-attr'><%= sale_item.qty %></td>
                   <td class='item-attr'><%= sale_item.price %></td>
                 </tr>

                 <%

                       # end
                     end
                   end

                   if @status_order == 'order' && @status_sale != 'sale'
                      unless @order_items.nil? || @order_items.empty?
                         count = 0
                      @order_items.each do |order_item |
                          count += 1
                      sub_total = sub_total + (order_item.price * order_item.qty)

                  # unless order_item.price == 0 %>
                 <tr>
                   <td><%= count %></td>
                   <td class='item-name'><%= order_item.item_name %></td>
                   <td class='item-attr'><%= order_item.qty %></td>
                   <td class='item-attr'><%= order_item.qty*order_item.price %></td>
                 </tr>
                 <%

                       # end
                      end
                     end
                   end
                 %>
               </tbody>
             </table>
           </div>
         </div>
         <div class="card-footer">
           <table class="table" id="order-charges-table" border="0">
             <tr>
               <td class="charges-name"><strong>Sub Total:</strong></td>
               <td class="item-attr"><strong id="sub-total"><%= sub_total %></strong></td>
             </tr>
             <tr>
               <%if @obj_sale != nil && @obj_sale.discount_type == 'member_discount'%>
                <td class="charges-name"><strong>Member Discount:</strong></td>
               <%else%>
                <td class="charges-name"><strong>Discount:</strong></td>
               <%end%>

               <td class="item-attr"><strong id="order-discount">(<%= @obj_sale.total_discount rescue 0%>)</strong></td>
             </tr>
            <% if @status_sale == "sale" %>
             <tr>
               <td class="charges-name"><strong>Tax:</strong></td>
               <td class="item-attr"><strong id="order-Tax"><%= @obj_sale.total_tax rescue 0%></strong></td>
             </tr>
              <tr>
               <td class="charges-name"><strong>Rounding Adj:</strong></td>
               <td class="item-attr"><strong id="order-round-adj"><%= @obj_sale.rounding_adjustment rescue 0%></strong></td>
             </tr>
             <tr>
               <td class="charges-name"><strong>Grand Total:</strong></td>
               <td class="item-attr"><strong id="order-grand-total"><%= @obj_sale.grand_total rescue 0%></strong></td>
             </tr>
              <% end %>
              <tr class="rebate_amount"></tr>
           </table>
         </div>
         <br>
          <%
            if @status_sale == 'sale'
                unless @order_items.nil? || @order_items.empty?
                  %>
                  Pending New Order
                   <table class="table table-striped">
                  <%
                  count = 0
                  @order_items.each do |order_item |
                    count += 1
                  %>

                  <tr>
                    <td><%= count %>
                    <td class='item-name'><%= order_item.item_name %></td>
                    <td class='item-attr'><%= order_item.qty %></td>
                    <td class='item-attr'><%= order_item.qty*order_item.price %></td>
                  </tr>

                  <%
                   end
                   %>
                     </table>
                    <button class='btn bg-blue' id='add_invoice'> Add to existing invoice </button>
                   <% end %>
                   <% if @sale_array.size > 1 %>
                   <br><br>
                    Pending Payment
                    <% end %>
                    <% @sale_array.each do |sale|
                      if @sale_array.size > 1
                      unless sale.receipt_no == @sale_array[0].receipt_no
                      %>

                        <table class="table table-striped">
                          <tr>
                            <td>Receipt No - <%= sale.receipt_no %></td>
                            <td><button class='btn btn-sm bg-blue invoicedetails' id="<%= sale.sale_id %>">Show Detail </button></td>
                          </tr>
                        </table>
                      <%
                       end
                     end
                  end
                end
          %>

       </div>
     </div>
  </div>

 <!-- Column Three -->
 <div class="col-lg-1 col-md-1 col-sm-1">
   <!-- Waiter Buttons -->
   <button type="button" class="btn btn-default btn-block" id='back'><i class="material-icons">reply</i> Back</button>
   <button type="button" id="add_order" class="btn btn-block btn- bg-blue waves-effect">Add Order</button>
   <button type="button" id="survey" class="btn btn-block bg-blue waves-effect">Survey</button>
   <% if @room.bookings.length >= 1 %>
     <% if @status_order == 'order' && @status_sale != 'sale' %>
       <!-- <button type="button" class="btn bg-blue btn-block" >Add Order</button> -->
       <!-- <button type="button" id="customer" class="btn bg-blue btn-block" disabled>Customer</button> -->
       <!-- <button type="button" class="btn bg-blue btn-block" disabled >Edit</button> -->
       <!-- <button type="button" id="discount" class="btn bg-blue btn-block" disabled >Discount</button> -->
        <!-- <button type="button" id="other-charges" class="btn bg-blue btn-block" disabled>Charges</button> -->
        <button type="button" id="customer" class="btn bg-blue btn-block" >Customer</button>
       <button type="button" class="btn bg-blue btn-block" id='move'>Move</button>
       <button type="button" id="request_bills" class="btn bg-blue btn-block">Req.Bill</button>
       <!-- <button type="button" id="first_bill" class="btn bg-blue btn-block" disabled>First Bill</button> -->
       <!-- <button type="button" id="pay" class="btn bg-blue btn-block" disabled>Pay</button> -->
       <!-- <button type="button" class="btn bg-blue btn-block" disabled=""> Void </button> -->
    <% end %>
    <% if @status_sale == 'sale' %>
        <!-- <button type="button" class="btn bg-blue btn-block" disabled>Add Order</button> -->
        <button type="button" id="customer" class="btn bg-blue btn-block" >Customer</button>
        <button type="button" class="btn bg-blue btn-block" id='edit'>Edit</button>
        <button type="button" id="discount" class="btn bg-blue btn-block" >Discount</button>
        <button type="button" id="other-charges" class="btn bg-blue btn-block" >Charges</button>
        
        <button type="button" id="commissions" class="btn btn-block bg-blue waves-effect">Commissions</button>
        <button type="button" id="in_duties" class="btn btn-block bg-blue waves-effect">In Duties</button>
        <!-- <button type="button" class="btn bg-blue btn-block" id='move' disabled="">Move</button> -->
        <!-- <button type="button" id="request_bills" class="btn bg-blue btn-block" disabled> Req.Bill</button> -->
        <%if @membership.discount && @obj_sale.customer.membership_id %>
          <button type="button" class="btn btn-block bg-blue waves-effect" data-toggle="modal" data-target="#paymentModal">First Bill</button>
        <%else%>
          <button type="button" id="first_bill" class="btn btn-block bg-blue waves-effect">First Bill</button>
        <%end%> 
        <button type="button" id="pay" class="btn bg-blue btn-block">Pay</button>
        <button type="button" id="void" class="btn bg-blue btn-block" > Void </button>
     <% end %>


           <span class="hidden" id="member_discount"><%= @membership.discount%></span>
           <span class="hidden" id="membership_id"><%= @obj_sale.customer.membership_id rescue 0%></span>
          <div class="modal fade" id="paymentModal" tabindex="-1" role="dialog">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title" id="paymentModalLabel">Choose Payment</h4>
                        </div>
                        <div class="modal-body">
                           <select class="form-control show-tick payment_method"  id="payment_method" >
                             <option value="Cash">Cash</option>
                            <% @payment_methods.each do |pay| %>
                              <option value="<%= pay.payment_method %>">
                                <%= pay.payment_method %>
                              </option>
                            <%end %>
                            
                            </select>   
                        </div>
                        <div class="modal-footer  p-r-30">
                            <button type="button" class="btn btn-link btn-danger waves-effect" data-dismiss="modal">CLOSE</button>
                            &nbsp;  &nbsp;
                           <button type="button" class="btn btn-link bg-blue waves-effect choose_payment">SAVE</button>
                        </div>
                    </div>
                </div>
            </div>
     <!-- Cashier Buttons -->

   <!-- <button type="button" id="re-print" class="btn bg-blue btn-block" >Re.Print</button> -->
   <% end %>
 </div>
</div>
</div>
<script>
$(document).ready(function(){
  // $(".tables").on('click', function(){
  //     var customer_id=$(".customer-id").text();
  //     show_customer_details(customer_id);

  //     var dining_id = $(this).attr("data-id");
  //     window.location.href = '/origami/table/' + dining_id;
  // })
  // $(".sales").on('click',function(){
  //     var customer_id=$(".customer-id").text();
  //     show_customer_details(customer_id);

  //   var sale_id = $(this).attr("data-id");
  //   window.location.href = '/origami/sale/' + sale_id;
  // })
  // $(".rooms").on('click', function(){
  //     var customer_id=$(".customer-id").text();
  //     show_customer_details(customer_id);

  //     var dining_id = $(this).attr("data-id");
  //     window.location.href = '/origami/room/' + dining_id;
  // })
  // $(".orders").on('click',function(){
  //     var customer_id=$(".customer-id").text();
  //     show_customer_details(customer_id);

  //   var order_id = $(this).attr("data-id");
  //   window.location.href = '/origami/order/' + order_id;
  // })

  /* start check first bill or not*/
    var receipt_no = "";
    if(($("#receipt_no").html()!=undefined) && ($("#receipt_no").html()!="")){
      receipt_no = ($("#receipt_no").html()).trim();
    }
    // console.log(checkReceiptNoInFirstBillData(receipt_no));
    if(checkReceiptNoInFirstBillData(receipt_no,"")){
      $("#pay").show();
    }else{
      $("#pay").hide();
    }
  /* end check first bill or not*/

  $('.invoicedetails').on('click',function(){
    var dining_id = "<%= @room.id %>";
    var sale_id = this.id;
    window.location.href = '/origami/table/'+ dining_id + "/table_invoice/"+sale_id;
  })
  $(".tables").on('click', function(){

      var customer_id=$(".customer-id").text();
      show_customer_details(customer_id);

      var dining_id = $(this).attr("data-id");
      window.location.href = '/origami/table/' + dining_id;
  });

  $(".sales").on('click',function(){

      var customer_id=$(".customer-id").text();
      show_customer_details(customer_id);

      var sale_id = $(this).attr("data-id");
      window.location.href = '/origami/sale/' + sale_id;
  });

  $(".rooms").on('click', function(){

      var customer_id=$(".customer-id").text();
      show_customer_details(customer_id);

      var dining_id = $(this).attr("data-id");
      window.location.href = '/origami/room/' + dining_id;
  });

  $(".orders").on('click',function(){

      var customer_id=$(".customer-id").text();
      show_customer_details(customer_id);

      var order_id = $(this).attr("data-id");
      window.location.href = '/origami/order/' + order_id;
  });

   // bind survey to order or sale
        $("#survey").on('click', function () {
            var sale = $('#sale_id').val();
            if (sale) {
                var sale_id = sale
            } else {
                var sale_id = "<%= @room.id %>";
            }
            //var table_id = $('.tables').attr("data-id");            
            window.location.href = '/origami/' + sale_id + "/surveys"
        });
  // bind customer to order or sale
  $("#customer").on('click', function(){
    var sale = $('#sale_id').val();
    if (sale) {
      var sale_id = sale
    }else{
      var sale_id = $('#save_order_id').attr('data-order');
    }
    var table_id = $('.tables').attr("data-id");

    window.location.href = '/origami/'+ sale_id  + "/customers"

  });

  $('#re-print').click(function() {
    var sale_id = $('#sale_id').val();
    window.location.href = '/origami/'+ sale_id  + "/reprint"

    return false;
  });

  
  // Discount for Payment
  $('#discount').click(function() {
    var sale = $('#sale_id').val();
    if (sale!="") {
      var sale_id = sale
    }else{
      var sale_id = $('#save_order_id').attr('data-order');
    }

    if(sale_id!=""){
      window.location.href = '/origami/' + sale_id + '/discount'
    }
    else {
      swal ( "Oops" ,  "Please select an table!" ,  "warning" );
    }

    return false;
  });

  // Add Other Charges
  $('#other-charges').click(function() {
    var sale = $('#sale_id').val();
    if (sale!="") {
      var sale_id = sale
    }else{
      var sale_id = $('#save_order_id').attr('data-order');
    }

    if(sale_id!=""){
      window.location.href = '/origami/' + sale_id + '/other_charges'
    }
    else {
      swal ( "Oops" ,  "Please select an table!" ,  "warning" );
    }

    return false;
  });

});

// Print for first bill
$("#first_bill").on('click', function(){ 
  var sale_id = $('#sale_id').val();
  var ajax_url = "/origami/sale/"+ sale_id + "/first_bill";
  $.ajax({
      type: "GET",
        url: ajax_url,
        success:function(result){
          receipt_no = ($("#receipt_no").html()).trim();
          if((receipt_no!=undefined) && (receipt_no!=""))
            createReceiptNoInFirstBillData(receipt_no,"");
          
          location.reload();
        }
    });
});

$(".choose_payment").on('click', function () {
  $( "#loading_wrapper").show();
  var sale_id = $('#sale_id').val();
  type = $('.payment_method').val();

  calculate_member_discount(sale_id,type);

  var ajax_url = "/origami/sale/" + sale_id + "/first_bill";
  $.ajax({
    type: "GET",
    url: ajax_url,
    success: function (result) {
      $( "#loading_wrapper" ).hide();
      receipt_no = ($("#receipt_no").html()).trim();
      if((receipt_no!=undefined) && (receipt_no!=""))
        createReceiptNoInFirstBillData(receipt_no,type);
      
      location.reload();
    }
  });  
});
function calculate_member_discount(sale_id,type) {
 var sub_total = $('#sub-total').text();
 if (type == "Cash") {
   is_card = false
 }else{
  is_card = true
}
$.ajax({
  type: "POST",
  url: "/origami/" + sale_id + "/member_discount",
  data: {'sale_id':sale_id, 'sub_total':sub_total,'is_card':is_card },
  async: false,
  success:function(result){ 
  }
});

}

$('#pay').on('click',function() {
   var sale_id = $('#sale_id').val();
   var url = '/origami/sale/'+ sale_id + "/rounding_adj" ;

   $.ajax({
      type: "POST",
        url: '/origami/sale/'+ sale_id + "/rounding_adj",
        success:function(result){          
          window.location.href = '/origami/sale/'+ sale_id + "/payment";
        }
    });
   
});
// Bill Request
$('#request_bills').click(function() {
    var order_id = $('#save_order_id').attr('data-order');
    var ajax_url = "/origami/" + order_id + "/request_bills";
    $.ajax({
      type: "GET",
        url: ajax_url,
        // data: 'order_id='+ order_id,
        success:function(result){
          if(!result.status){
            $.confirm({
              title: 'Information!',
              content: result.error_message,
              buttons: {
                confirm: {
                text: 'Ok',
                btnClass: 'btn-green',
                  action: function(){
                    window.location.href = '/origami';
                  }
                }
              }
            });
          }
          else {
            location.reload();
          }
        }
    });
});

$('#move').on('click',function(){
  var dining_id = "<%= @room.id %>"
  window.location.href = '/origami/table/'+ dining_id + "/moveroom";
})

$('#back').on('click',function(){
  window.location.href = '/origami/';
})

$('#add_invoice').on('click',function(){
  var dining_id = "<%= @room.id %>"
  var sale_id = "<%= @obj_sale.sale_id rescue "" %>"
  var ajax_url = "/origami/sale/append_order";
  $.ajax({
    type: "POST",
      url: ajax_url,
      data: 'dining_id='+ dining_id + "&sale_id=" + sale_id,
      success:function(result){
        swal({
          title: "Information!",
          text: "Invoice updated",
          html: true,
          closeOnConfirm: false,
          closeOnCancel: false,
          allowOutsideClick: false
        }, function () {
          window.location.reload();
        });
      }
  });
});

// //show cusotmer rebate amount
// function show_customer_details(customer_id){
//   var url = '<%= @show_customer_details_path %>'
//       //Start Ajax
//       $.ajax({
//         type: "GET",
//         url: url,
//         data: {},
//         dataType: "json",
//         success: function(data) {
//           $("#customer_name").text(data["customer"].name);
//           if (data["response_data"]["data"].length) {
//             $.each(data["response_data"]["data"], function (i) {
//               if(data["response_data"]["data"][i]["accountable_type"] == "RebateAccount"){
//                 var balance = data["response_data"]["data"][i]["balance"];
//                 if (data["response_data"]["status"]==true) {
//                   $('.rebate_amount').removeClass('hide');
//                   row =
//                          '<td class="charges-name">' + "Rebate Balance" +'</td>'
//                          +'<td class="item-attr">' + balance + '</td>';

//                          $(".rebate_amount").html(row);
//                 }

//               }
//             });
//             }else{
//               $('.rebate_amount').addClass('hide');
//             }
//         }
//       });
//         //End Ajax
//   }

  //show cusotmer rebate amount
  function show_customer_details(customer_id){
  var url = '<%= @show_customer_details_path %>'
    //Start Ajax
    $.ajax({
      type: "GET",
      url: url,
      data: {},
      dataType: "json",
      success: function(data) {
        $("#customer_name").text(data["customer"].name);
        if (data["response_data"]["data"].length) {
          $.each(data["response_data"]["data"], function (i) {
            if(data["response_data"]["data"][i]["accountable_type"] == "RebateAccount"){
              var balance = data["response_data"]["data"][i]["balance"];
              if (data["response_data"]["status"]==true) {
                $('.rebate_amount').removeClass('hide');
                row =
                       '<td class="charges-name">' + "Rebate Balance" +'</td>'
                       +'<td class="item-attr">' + balance + '</td>';

                       $(".rebate_amount").html(row);
              }

            }
          });
          }else{
            $('.rebate_amount').addClass('hide');
          }
      }
    });
    //End Ajax
  }

  $('#edit').on('click',function(){
    var dining_id = "<%= @room.id %>"
    var sale_id = "<%= @obj_sale.sale_id rescue "" %>"
    window.location.href = '/origami/table/'+ dining_id + "/sale/"+ sale_id + "/edit";
  });

  $('#void').on('click',function () {
      var sure = confirm("Are you sure want to Void");
      if (sure == true) {
          var sale_id = $('#sale_id').val();
          var ajax_url = "/origami/sale/" + sale_id + '/void';
          $.ajax({
              type: 'POST',
              url: ajax_url,
              success: function () {
                  window.location.href = '/origami/';
              }
          })
      } else {

      }
  });

  $('#commissions').on('click', function () {
        var dining_id = "<%= @room.id %>"
        var sale_id = "<%= @obj_sale.sale_id rescue "" %>"
        window.location.href = '/origami/table/' + dining_id + "/sale/" + sale_id + "/load_commissioners";
    });

    $('#in_duties').on('click', function () {
        var dining_id = "<%= @room.id %>"
        window.location.href = '/origami/assign_in_duty/'+ dining_id;
    });

  // Ordering
  $('#add_order').on('click', function () {
        var dining_id = "<%= @room.id %>"
        window.location.href = '/origami/addorders/' + dining_id;
    });
</script>
