
<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"><a href="<%= transactions_bookings_path %>"><%= t :booking %></a></li>
    <li class="breadcrumb-item active"><%= t :details %></li>
    <span class="float-right">
      <%= link_to 'Back', transactions_bookings_path %>
    </span>
  </ol>
</div>
<div class="row">
  <div class="col-lg-12 col-md-12 col-sm-12">
    <!-- Nav tabs -->
        <ul class="nav nav-tabs" role="tablist">
            <li class="nav-item">
                <a class="nav-link active" data-toggle="tab" href="#booking" role="tab"><%= t :booking_details %></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" data-toggle="tab" href="#order" role="tab"><%= t :order_details %></a>
            </li>
             <li class="nav-item">
                <a class="nav-link" data-toggle="tab" href="#sale" role="tab"><%= t :sale_details %></a>
            </li>
        </ul>

        <!-- Nav tabs - End -->
    <div class="tab-content" style="min-height:670px; max-height:670px;">
        <div class="tab-pane active" id="booking" role="tabpanel">
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th><%= t("views.right_panel.detail.booking_id") %></th>
                  <th><%= t("views.right_panel.detail.sale_id") %></th>
                  <th><%= t("views.right_panel.detail.table") %></th>
                  <th><%= t("views.right_panel.detail.checkin_time") %></th>
                  <th><%= t("views.right_panel.detail.checkout_time") %></th>
                  <th><%= t("views.right_panel.detail.checkin_by") %></th>
                  <th> <%= t("views.right_panel.detail.checkout_by") %></th>
                  <th><%= t("views.right_panel.detail.booking_status") %></th>
                  <th><%= t("views.right_panel.detail.booking_date") %></th>
                  <%if current_login_employee.role == "administrator" %>
                    <th>
                      <%if (!@order.nil?) && (@booking.sale_id.nil?) %>
                      <button id="request_bill" class="btn bg-blue btn-md waves-effect">Req. Bill</button>
                      <% end %>
                      <%if @order.nil? && @booking.booking_status == 'assign' && (@booking.sale_id.nil?) %>
                        <button id="change_booking_status" class="btn bg-blue btn-md waves-effect">Update Status</button>
                      <% end %>
                    </th>
                  <% end %>
                </tr>
              </thead>

              <tbody>
                <tr>
                  <td><%= @booking.booking_id%></td>
                  <td><%= link_to @booking.sale_id, transactions_sale_path(@booking.sale_id) rescue '-' %></td>
                  <td><%= @booking.dining_facility.name rescue '' %></td>
                  <td><%= @booking.checkin_at.utc.getlocal.strftime("%I:%M %p") %></td>
                  <td><%= @booking.checkout_at.utc.getlocal.strftime("%I:%M %p") rescue '-'  %></td>
                  <td><%= @booking.checkin_by rescue '-' %></td>
                  <td><%= @booking.checkout_by rescue '-' %></td>
                  <td><%= @booking.booking_status %> </td>
                  <td><%= @booking.created_at.utc.getlocal.strftime("%d-%m-%Y %I:%M %p") %> </td>
                  <%if current_login_employee.role == "administrator" %>
                      <%if (!@order.nil?) && (@booking.sale_id.nil?) %>
                        <td></td>
                      <% end %>
                      <%if @order.nil? %>
                        <td></td>
                      <% end %>
                    </th>
                  <% end %>
                </tr>
              </tbody>
            </table>
          </div>
        </div>

        <div class="tab-pane" id="order" role="tabpanel">
          <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.header.menu_item") %></th>
                          <th><%= t("views.right_panel.detail.qty") %></th>
                          <th><%= t("views.right_panel.detail.unit_price") %></th>
                          <th><%= t("views.right_panel.detail.option") %></th>
                          <th><%= t("views.right_panel.detail.status") %></th>
                          <th><%= t("views.right_panel.detail.waiter") %></th>
                          <th><%= t("views.right_panel.detail.created_at") %></th>
                        </tr>
                      </thead>

                    <tbody>
                    <% @order_items.each do |order_item| %>
                        <tr>
                          <td><%= link_to order_item.order_id, transactions_order_path(order_item.order_id) %><%=  %></td>
                          <td><%= order_item.item_name %></td>
                          <td><%= order_item.qty %></td>
                          <td><%= order_item.price %></td>
                          <td><%= order_item.options %></td>
                          <td><%= order_item.order_item_status %></td>
                          <td><%= order_item.item_order_by %> </td>
                          <td><%= order_item.created_at.strftime("%d-%m-%y %I:%M %p") %></td>
                        </tr>
                      <% end %>
                    </tbody>
                    </table>
                </div>
        </div>
         <div class="tab-pane" id="sale" role="tabpanel">
          <div class="table-responsive">
            <table class="table table- table-striped table-hover js-basic- ">
              <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>
                <% if @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.utc.getlocal.strftime("%d-%m-%Y %I:%M %p") %> </td>
                    </tr>
              <% else %>
                    <tr><td colspan="9"><strong><p style="text-align: center">There is no data ....</p></strong></td></tr>
              <% end %>
              </tbody>
            </table>
          </div>
        </div>
    </div>
  </div>
</div>



<script type="text/javascript">
  $(document).ready(function(){
    $("#request_bill").on('click', function(){
      var order_id = '';
      <%if !@order.nil? %>
        order_id ='<%= @order.order_id %>';
      <% end %>
      var ajax_url = "/origami/" + order_id + "/request_bills";
      localStorage.setItem("tax_type", "all");
      if(order_id!=undefined && order_id!=null && order_id!=""){
        if($('#request_bill').is(":visible")) {
          $('#request_bill').prop("disabled",true);
        }
        $.ajax({
            type: "GET",
            url: ajax_url,
            success: function (result) {
              if (!result.status) {
                $('#request_bill').prop("disabled",false);
                swal("Information!", result.error_message);
              }
              else {
                location.reload();
              }
            }
        });
      }else{
        swal("Opps","There is no orders!","warning");
      }
    });
  });

  $("#change_booking_status").on("click", function(){
    $('#change_booking_status').prop("disabled",true);
    var booking_id = '<%= @booking.booking_id %>';
    $.ajax({
        type: "GET",
        url: "/transactions/bookings/update_status/"+booking_id,
        success: function (result) {
          if (!result.status) {
            $('#change_booking_status').prop("disabled",false);
            swal("Opps",result.error_message,"warning");
          }
          else {
            swal({
              title: "Information",
              text: result.message,
              type: "success",
              html: true,
              closeOnConfirm: false,
              closeOnCancel: false,
              allowOutsideClick: false
            }, function() {
              // Redirect the user
              window.location.reload();
            });
          }
        }
    });
  });
</script>
