<div class="container-fluid">
<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 active" 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" 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>
     <li class="nav-item credit_items">
        <a class="nav-link" data-toggle="tab" href="#credits" role="tab"><%= t :credit %></a>
      </li>
   </ul>
   <input type="hidden" id="server_mode" value="<%= ENV["SERVER_MODE"] %>">
   <!-- Nav tabs - End -->
   <div id="custom-slimscroll">
      <div class="tab-content">
     <!--- Panel 0 - Completed Orders -->
     <div class="tab-pane active" id="completed" role="tabpanel" style="max-height:600px; overflow:auto">
       <div class="card-columns" style="padding-top:10px; column-gap: 1.2rem;">
         <% @complete.each do |sale| %>
         <div class="card sales bg-red text-white" data-id = "<%= sale.sale_id %>">
           <div class="card-block">
              <%= sale.receipt_no %><span style="font-size:12px;float:right;line-height:inherit;"><%= sale.sale_status %></span>
           </div>
         </div>
         <% end %>
       </div>
     </div>

     <!--- Panel 1 - Table Orders -->
     <div class="tab-pane " id="tables" role="tabpanel" style="">
       <div class="card-columns" style="padding-top:10px; column-gap: 1.2rem;">
         <% @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 %> <br>
                  <%= table.zone.name %>
                    <!-- <% if !@order_items_count.nil? %>
                      <% if @order_items_count.key?(table.id) %>
                        (Orders : <%= @order_items_count[table.id] %>)
                      <% end %>
                    <% end %> -->
                    <span class="float-right font-12 new_text_<%= table.id %>"> billed</span>
                    <div style="font-size:12px;"></div>
                  </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 %> <br>
                  <%= table.zone.name %>
                    <!-- <% if !@order_items_count.nil? %>
                      <% if @order_items_count.key?(table.id) %>
                        (Orders : <%= @order_items_count[table.id] %>)
                      <% end %>
                    <% end %> -->
                    <span class="float-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 %> <br>
                  <%= table.zone.name %>
                  <span class="float-right font-12 new_text_<%= table.id %> hide"> new</span>
                </div>
              </div>
            <% end %>
         <% end %>
       </div>
     </div>

     <!--- Panel 2 - Room Orders -->
     <div class="tab-pane" id="rooms" role="tabpanel" style="">
       <div class="card-columns" style="padding-top:10px; column-gap: 1.2rem;">
         <% @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 %> <br>
                  <%= room.zone.name %>
                  <!-- <% if !@order_items_count.nil? %>
                    <% if @order_items_count.key?(room.id) %>
                      (Orders : <%= @order_items_count[room.id] %>)
                    <% end %>
                  <% end %> -->
                  <span class="float-right font-12 new_text_<%= room.id %>"> billed</span>
                </div>
              </div>
            <% else %>
              <div class="card rooms blue text-white table_<%= room.id %>" data-id="<%= room.id %>">
                <div class="card-block">
                  <%= room.name %><br>
                  <%= room.zone.name %>
                    <!-- <% if !@order_items_count.nil? %>
                      <% if @order_items_count.key?(room.id) %>
                        (Orders : <%= @order_items_count[room.id] %>)
                      <% end %>
                    <% end %> -->
                    <span class="float-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 %><br>
                  <%= room.zone.name %>
                  <span class="float-right font-12 new_text_<%= room.id %> hide"> 
                </div>
              </div>
            <% end %>
         <% end %>
       </div>
     </div>

     <!--- Panel 3 - Orders -->
     <div class="tab-pane" id="orders" role="tabpanel" style="">
       <div class="card-columns" style="padding-top:10px; column-gap: 1.2rem;">
         <% @orders.each do |order| %>
          <div class="card orders <%=(order.status=="new") ? 'blue' : 'red'%> text-white" data-id="<%= order.order_id %>">
           <div class="card-block">
              <%
                order_status = ""
                sale_order = SaleOrder.find_by_order_id(order)
                if sale_order
                  unless sale_order.sale_id.nil?
                    sale = Sale.find(sale_order.sale_id)
                    order_status = sale.sale_status
                    if order_status == 'new'
                      order_status  = order.status
                    end
                  end
                else
                  order_status = order.status
                end
              %>
              <%= order.order_id %> <% if !order_status.empty? %>| <%= order_status %> <% end %>
           </div>
         </div>
         <% end %>
       </div>
     </div>

     <!-- Credit Item Lists-->
      <div class="tab-pane dining" id="credits" role="tabpanel">
        <div class="card-block">
          <div class="row m-t-10 m-l-10 clearfix">
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
              <input type="text" name="filter" id="filter" style="height: 32px;" placeholder="Receipt No." class="form-control" onchange="getCreditData('cashier');">
            </div>
            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
              <select class="form-control" id="sel_customer" name="sel_customer" placeholder="Select Customer" onchange="getCreditData('cashier');" >
                <option value="">-- Select Customer --</option>
                <% if !@customers.empty? %>
                <% @customers.each do |cus| %>
                <option value="<%= cus[0] %>"><%= cus[1] %></option>
                <% end %>
                <% end %>
              </select>
            </div>
          </div>
          <div class="row clearfix m-t-20 m-l-10">
            <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 %> <%= t("views.right_panel.detail.name") %></th>
                <th> <%= t :customer %> <%= t("views.right_panel.detail.name") %></th>       
                <th> <%= t("views.right_panel.detail.credit_amount") %> </th>
              </thead>
              <tbody class="tbd_credit_lists"></tbody>
            </table>
          </div>
        </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">
          <div><strong id="order-title">INVOICE DETAILS </strong>| Table <%= @dining.name rescue "" %></div>
       </div>
       <div class="card-block">
         <div class="card-title row p-l-5 p-r-5">
           <div class="col-lg-6 col-md-6 col-sm-6">
              Receipt No: <span id="receipt_no">
                 <%= @sale.receipt_no rescue '' %>
             </span>
           </div>
           <div class="col-lg-6 col-md-6 col-sm-6 text-right">
             Date:  <span id="receipt_date"><%= @sale.created_at.strftime("%d/%m/%Y - %I:%M %p") rescue '-'%></span>
           </div>
         </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">
             Customer : <%= @sale.customer.name rescue "-" %>

           </div>

         </div>
         <div id="order-detail-slimscroll" data-height="160">
         <div class="card-text" style="">
           <table class="table table-striped" id="order-items-table">
             <thead>
               <tr>
                 <th class="item-name">Items</th>
                 <th class="item-attr">QTY</th>
                 <th class="item-attr">Price</th>
               </tr>
             </thead>
             <tbody>
               <%
                 sub_total = 0
                  @sale.sale_items.each do |sale_item|
                  sub_total = sub_total + sale_item.price
               %>
               <input type="hidden" id="sale_id" value="<%= @sale.sale_id %>">
               <% #unless sale_item.price <= 0 %>
               <tr>
                 <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

                %>
             </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="order-sub-total"><%= sub_total %></strong></td>
             </tr>
             <tr>
                <%if @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">(<%= @sale.total_discount rescue 0%>)</strong></td>
             </tr>
             <tr>
               <td class="charges-name"><strong>Tax:</strong></td>
               <td class="item-attr"><strong id="order-Tax"><%= @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"><%= @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"><%= @sale.grand_total rescue 0%></strong></td>
             </tr>
              <tr class="rebate_amount"></tr>
           </table>
         </div>
       </div>
     </div>
  </div>

 <!-- Column Three -->
 <div class="col-lg-1 col-md-1 col-sm-1">
   <button type="button" class="btn btn-default btn-block" id='back'><i class="material-icons">reply</i>Back</button>
  <% if @sale.sale_status != 'void' && @sale.sale_status != 'waste' && @sale.sale_status != 'spoile' %>
     <% if current_login_employee.role == "cashier" %>
      <% if @sale.payment_status != 'foc' %>
        <a class="btn btn-block bg-danger waves-effect access_modal" data-toggle="modal"  data-type="void"> Void</a>
      <% end %>
        <a class="btn btn-block bg-blue waves-effect access_modal" data-toggle="modal"  data-type="reprint"> Re.Print</a>
     <% else %>      
        <% if @sale.payment_status != 'foc' %>
          <button type="button" id="void" class="btn bg-danger btn-block">VOID</button>   
        <% end %>
           <button type="button" id="reprint" class="btn bg-blue btn-block">Re.Print</button>   
     <% end %>
   <% end %> 
   
 </div>
</div>

<div class="modal fade" id="voidModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title" id="voidModalLabel">Please Enter Reason for Void</h4>
      </div>
      <div class="modal-body">
         <input type="textarea" name="remark" class="form-control col-md-12 remark" id="remark">
      </div>
      <div class="modal-footer ">
        <div class="row p-r-20">
          <div class="col-md-5">
            <button type="button" class="btn btn-link bg-red waves-effect " id="void" active="true">VOID</button>
          </div>
          <div class="col-md-5">
            <button type="button" class="btn btn-link bg-blue waves-effect" data-dismiss="modal">CLOSE</button>
          </div>
        </div>
        
      </div>
    </div>
  </div>
</div>
</div>

<div class="modal fade" id="AccessCodeModal" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-sm" role="document">
    <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title" id="AccessCodeModalLabel">Enter Access Code</h4>
      <button type="button" class="close" id="close" data-dismiss="modal" aria-hidden="true" style="font-size: 20px;color:#111;">&times;</button>
      </div>
      <div class="modal-body" style="padding: 0px 25px 15px 25px !important"> 
         <input type="password" id="access_code" class="access_code form-control col-md-12 ">
        <div class="row bottom p-l-15 p-r-15 m-t-10">
             <div class="col-md-3 access_number border-top border-left" data-value="1" data-type="num">1</div>
             <div class="col-md-3 access_number border-top border-left" data-value="2" data-type="num">2</div>
             <div class="col-md-3 access_number border-top border-left" data-value="3" data-type="num">3</div>
             <div class="col-md-3 access_number border-top border-left" data-value="4" data-type="num">4</div>
           </div>
           <div class="row bottom p-l-15 p-r-15">
             <div class="col-md-3 access_number border-top border-left" data-value="5" data-type="num">5</div>
             <div class="col-md-3 access_number border-top border-left" data-value="6" data-type="num">6</div>
             <div class="col-md-3 access_number border-top border-left" data-value="7" data-type="num">7</div>
             <div class="col-md-3 access_number border-top border-left" data-value="8" data-type="num">8</div>
           </div>
           <div class="row bottom p-l-15 p-r-15">
             <div class="col-md-3 access_number border-top border-left" data-value="9" data-type="num">9</div>
             <div class="col-md-3 access_number border-top border-left" data-value="0" data-type="num">0</div>
             <div class="col-md-3 access_number border-top border-left orange" data-type="clr">Clr</div>
             <div class="col-md-3 access_number ok border-top border-left blue" data-type="ok" data-action="">OK</div>
              
           </div>
      </div>
    </div>
  </div>
</div> 

<div class="modal fade" id="voidModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
      <h4 class="modal-title" id="voidModalLabel">Please Enter Reason for Void</h4>
      </div>
      <div class="modal-body">
         <input type="textarea" name="remark" class="form-control col-md-12 remark" id="remark">
      </div>
      <div class="modal-footer ">
        <div class="row p-r-20">
          <div class="col-md-5">
            <button type="button" class="btn btn-link  p-t-5 p-b-5 bg-red waves-effect " id="void" active="true">VOID</button>
          </div>
          <div class="col-md-5">
            <button type="button" class="btn btn-link  p-t-5 p-b-5 bg-blue waves-effect" data-dismiss="modal">CLOSE</button>
          </div>
        </div>
        
      </div>
    </div>
  </div>
</div>

<!--  script data for credit lists -->
<script type="text/html" id="html_credit_items">
  <tr class="row_{key} credit_detail" data-id="{sale_id}" data-type="{cashier_type}">
    <td>{receipt_date}</td>
    <td>{receipt_no}</td>
    <td>{cashier_name}</td>
    <td>{customer_name}</td>
    <td>{credit_amount}</td>
  </tr>
</script>
<!-- script data for credit lists -->
<script>
$(document).ready(function(){
  /* check webview loaded*/
  var webview = <%= @webview %>;
  showHideNavbar(webview);
  getCreditSales("","","cashier"); //credit sales script data binding

  $(".tables").on('click', function(){
      var dining_id = $(this).attr("data-id");
      window.location.href = '/origami/table/' + dining_id;
  })
  $(".sales").on('click',function(){
    var sale_id = $(this).attr("data-id");
    window.location.href = '/origami/sale/' + sale_id;
  })
  $(".rooms").on('click', function(){
      var dining_id = $(this).attr("data-id");
      window.location.href = '/origami/room/' + dining_id;
  })
  $(".orders").on('click',function(){
    var order_id = $(this).attr("data-id");
    window.location.href = '/origami/order/' + order_id;
  })
});

$('#reprint').on('click', function () {
    var sale_id = $('#sale_id').val();
    var ajax_url = '/origami/'+ sale_id  + "/reprint";

    $.ajax({
      type: "GET",
      url: ajax_url,
      success: function (result) {
        if ($("#server_mode").val() == "cloud") {
          if(typeof code2lab != 'undefined'){
            code2lab.printFile(result.filepath.substr(6), result.printer_url);
          }
        }
        location.reload();
      }
    });
  });

$('#pay').on('click',function() {
   var sale_id = $('#sale_id').val();
   window.location.href = '/origami/sale/'+ sale_id + "/payment";
});
$('#back').on('click',function(){
  window.location.href = '/origami/';
})

// $('#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/';
//           //     }
//           // });
//       }
//   });

$(document).on('click', '.access_modal', function(event){
      type = $(this).data("type");
      $(".ok").attr("data-action",type);
      $('#AccessCodeModal').modal('show');
  });
 $('#void').on('click', function () {
  var access_code  = localStorage.getItem("access_code");
        swal({
            title: "Alert",
            text: "Are you sure want to Void?",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, void it!",
            closeOnConfirm: false
        }, function (isConfirm) {
          if (isConfirm) {
            var sale_id = $('#sale_id').val();
            var ajax_url = "/origami/sale/" + sale_id +'/'+'cashier'+ '/void';
            $.ajax({
                type: 'POST',
                url: ajax_url,
                data: {'access_code':access_code},
                success: function () {
                    window.location.href = '/origami/sale/'+sale_id;
                }
            });
          }
        });
    });

 function check_emp_access_code(access_code,type) {
      var url = "/origami/check_emp_access_code/" + access_code ;
      $.ajax({
          type: 'POST',
          url: url,
          data: {},
          success: function (result) {
            console.log(result)
             if (result.status == true) {
              createAccessCode(code);
               if (type == "edit") {
                  var dining_id = $('#dining').text();
                  var sale_id = $('#sale_id').text();
                  if (dining_id) {
                    window.location.href = '/origami/table/' + dining_id + "/sale/" + sale_id + "/"+cashier_type+"/edit";
                  }else{
                    window.location.href = "/origami/table/sale/" + sale_id + "/"+cashier_type+"/edit";
                  }
               }else if(type == "void"){
                 $('#AccessCodeModal').modal('hide');
                 $('#voidModal').modal('show');
                 // overall_void();
               }else if(type == "waste") {
                 // $('#AccessCodeModal').modal('hide');
                 // $('#focModal').modal('show');
                  waste_and_spoilage("waste")
               }else if(type == "waste") {
                 // $('#AccessCodeModal').modal('hide');
                 // $('#focModal').modal('show');
                  waste_and_spoilage("waste")
               }else if(type == "reprint") {
                  $('#AccessCodeModal').modal('hide');
                  var sale_id = $('#sale_id').val();
                  var ajax_url = "/origami/"+ sale_id  + "/reprint";
                  $.ajax({
                    type: "GET",
                    url: ajax_url,
                    success: function (result) {
                      // For Server Print - from jade
                      if ($("#server_mode").val() == "cloud") {
                        if(typeof code2lab != 'undefined'){
                          code2lab.printFile(result.filepath.substr(6), result.printer_url);
                        }
                      }
                      location.reload();
                    }
                  }); 
               }else if(type == "foc"){
                 $('#AccessCodeModal').modal('hide');
                 $('#focModal').modal('show');
                 // overall_foc();
               }
             }else{
               swal("Opps",result.message,"warning")
             }
          }
      });
  }
</script>
