<div class="container-fluid m-top-100">
  <div id="loading_wrapper" style="display:none;">
    <div id="loading"></div>
  </div>
  <div class="row">
   <!-- Column One -->
   <div class="<%= @webview==true ? 'col-lg-12 col-md-12 col-sm-12' : 'col-lg-10 col-md-10 col-sm-10' %>">

     <!-- Nav tabs -->
     <ul class="nav nav-tabs tab-col-teal" role="tablist">
       <li class="nav-item">
         <a class="nav-link" data-toggle="tab" href="#complete" role="tab"><%= t :competed %></a>
       </li>
       <li class="nav-item">
         <a class="nav-link active" data-toggle="tab" href="#tables" role="tab"><%= t :tables %></a>
       </li>
       <li class="nav-item">
         <a class="nav-link" data-toggle="tab" href="#rooms" role="tab"><%= t :rooms %></a>
       </li>
       <li class="nav-item">
         <a class="nav-link" data-toggle="tab" href="#orders" role="tab"><%= t :orders %></a>
       </li>
       <li class="nav-item credit_items">
         <a class="nav-link" data-toggle="tab" href="#credits" role="tab"><%= t :credits %></a>
       </li>
     </ul>
     <!-- Nav tabs - End -->

     <div class="tab-content m-t-10"  id="custom-slimscroll">
       <!--- Panel 0 - Completed Orders -->
       <div class="tab-pane dining" id="complete" 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"><%= sale.sale_status %></span>
            </div>
          </div>
          <% end %>
        </div>
      </div>

      <!--- Panel 1 - Table Orders -->
      <div class="tab-pane dining active" 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" data-id="<%= table.id %>">
                  <% else %>
                  <div class="card tables orange text-white" data-id="<%= table.id %>">
                <% end %>
                <div class="card-block">
                  Table <%= table.name %>  ( <%= table.seater %> Seat ) <br>
                  <%= table.zone.name %>
                </div>
              </div>
          <% else %>
          <% if table.get_checkout_booking.nil? %>
          <div class="card tables blue text-white" data-id="<%= table.id %>">
            <% else %>
            <div class="card tables orange text-white" data-id="<%= table.id %>">
              <% end %>
              <div class="card-block">
                Table <%= table.name %>  ( <%= table.seater %> Seat ) <br>
                <%= table.zone.name %>
              </div>
            </div>
            <% end %>
            <% else %>
            <div class="card tables green text-white table_<%= table.id %>" data-id="<%= table.id %>">
             <div class="card-block">
               Table <%= table.name %>  ( <%= table.seater %> Seat ) <br>
               <%= table.zone.name %>
             </div>
           </div>
           <% end %>
           <% end %>
         </div>
       </div>

       <!--- Panel 2 - Room Orders -->
       <div class="tab-pane dining" 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" data-id="<%= room.id %>">
            <% else %>
            <div class="card rooms blue text-white" data-id="<%= room.id %>">
              <% end %>
              <div class="card-block">
                <%= room.name %> <br>
                <%= room.zone.name %>
              </div>
            </div>
            <% 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 %>
            </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 <%=(order.status=="new") ? 'blue' : 'red'%> text-white" data-id="<%= order.order_id %>">
          <div class="card-block">
            <%
            order_status = ""
            sale_order = order.sale_orders.first
            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 Items -->
    <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" onchange="getCreditData('cashier');" style="height: 32px;" placeholder="Receipt No." class="form-control">
        </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 %></th>
            <th> <%= t :customer %></th>
            <th> <%= t("views.right_panel.detail.credit_amount") %> </th>
          </thead>
          <tbody class="tbd_credit_lists"></tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<!-- tabs - End -->
</div>
<div class="<%= @webview==true ? 'hidden' : 'col-lg-2 col-md-2 col-sm-2' %>">
 <!-- <button id="refreshbutton" type="button" class="btn btn-block radius-btn"> Refresh off </button> -->
 <button type="button" class="btn btn-block btn-default waves-effect" id='back'>
  <i class="material-icons">reply</i>
  <%= t("views.btn.back") %>
</button>
<%if current_login_employee.role == "administrator" || current_login_employee.role == "manager" %>
      <!-- <button id="back" type="button" class="btn btn-block btn-lg bg-default"> <i class="material-icons">reply</i> <%= t("views.btn.back") %>
    </button> -->
    <%end%>
    <%if current_login_employee.role != "waiter" %>
    <button id="cash_in" type="button" class="btn btn-block btn-lg bg-blue waves-effect"> <%= t("views.btn.cash_in") %> </button>
    <button id="cash_out" type="button" class="btn btn-block btn-lg bg-blue waves-effect"> <%= t("views.btn.cash_out") %> </button>
    <!-- Temporary Disabled -->

    <button id="close_cashier" type="button" data-count="<%= @occupied_table %>" class="btn btn-block btn-lg bg-blue waves-effect"> <%= t("views.btn.close_cashier") %></button>
    <%end%>
  </div>
</div>
</div>
<!-- Column One -->
<!--  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(){
    localStorage.removeItem("tax_type");

    var webview = <%= @webview %>;

    showHideNavbar(webview,"index");
    if(webview){
      $("#loading_wrapper").show();
      getAllMenu();
    }

    getCreditSales("","","cashier"); //credit sales script data binding

    $(".tables").on('click', function(){
      localStorage.setItem('dinning_type','table');
      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(){
      localStorage.setItem('dinning_type','room');
      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;
    });

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

// $(function() {
//     var timer;

//     function start() {
//         timer = setTimeout(function() { console.log("refresh");window.location.reload()}, 10000); //10000
//     };

//     function stop() {
//         clearTimeout(timer);
//     };

// 	if(localStorage.getItem("refreshflag") != null){
// 		if (localStorage.getItem("refreshflag") == 0) {
// 			$('#refreshbutton').html("Refresh off");
// 			$('#refreshbutton').css("background-color","red");
//             stop();
// 		}
// 		else{
// 			$('#refreshbutton').html("Refresh on");
//       $('#refreshbutton').css("background-color","green");
// 			start();
// 		}
// 	}
// 	else{
// 		localStorage.setItem("refreshflag", 0);
// 	}
//     var url = location.href;
//     if (location.href.slice(-1) == "/"){
//       url = url.substring(0, url.length - 1);
//     }
//     var arr = url.split("/");

//     if(arr[arr.length-1] != "origami"){
//         stop();
//     }
//     $('#refreshbutton').bind("click", function(){
//     	if(localStorage.getItem("refreshflag") == "0"){
//     		localStorage.setItem("refreshflag", 1);
//     		$(this).html("Refresh on");
//         $(this).css("background-color","green");
//     		// $(this).html("Auto Refresh on<span class='glyphicon glyphicon-ok'></span>");
//     		start();
//     	}
//     	else{
//     		stop();
//             localStorage.setItem("refreshflag", 0);
//             $(this).html("Refresh off");
//             $(this).css("background-color","red");
//     	}
//     });
//     function refreshing(){
//     	if(localStorage.getItem("refreshflag") == "0"){
//     		localStorage.setItem("refreshflag", 1);
//     		$(this).html("Refresh on");
//         $(this).css("background-color","green");
//     		// $(this).html("Auto Refresh on<span class='glyphicon glyphicon-ok'></span>");
//     		start();
//     	}
//     	else{
//     		stop();
//             localStorage.setItem("refreshflag", 0);
//             $(this).html("Refresh off");
//             $(this).css("background-color","red");
//     	}
//     }
// });

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

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

$('#close_cashier').on('click',function(e){
    e.preventDefault(); // Prevent the href from redirecting directly
    var linkURL = '/origami/shift/cashier/close';
    occupied_count = $(this).data("count");
    warnBeforeRedirect(linkURL,occupied_count);
  })

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

function warnBeforeRedirect(linkURL,occupied_count) {
  if (occupied_count>0) {
    text = "<h5><span style=\"color: #CC0000\"> We have "+occupied_count+" occupied table </span> </h5>.<br>Are you sure you want to close cashier?  ";
  }else{
    text = "Are you sure you want to close cashier?";
  }
  swal({
    title: "Alert!",
    text: text,
    type: "warning",
    showCancelButton: true,
    html: true
  }, function() {
      // Redirect the user
      window.location.href = linkURL;
    });
}
</script>
