class Origami::OrdersController < BaseOrigamiController
  def show
    @webview = false
    if check_mobile
      @webview = true
    end

    @tables   = Table.unscope(:order).all.active.order('status desc')
    @rooms    = Room.unscope(:order).all.active.order('status desc')
    @complete = Sale.where("DATE_FORMAT(CONVERT_TZ(created_at, '+00:00', '<%= Time.zone.formatted_offset %>'), '%Y-%m-%d') = ? and sale_status != 'new'",Time.current.strftime('%Y-%m-%d'))
    @orders   = Order.includes("sale_orders").where("DATE_FORMAT(CONVERT_TZ(created_at, '+00:00', '<%= Time.zone.formatted_offset %>'), '%Y-%m-%d') = ? and status != 'billed' and source != 'quick_service'",Time.current.strftime('%Y-%m-%d')).order('date desc')
    @order = Order.find(params[:order_id])
    booking = Booking.select('bookings.booking_id, bookings.dining_facility_id')
              .joins(" JOIN booking_orders as bo on bo.booking_id = bookings.booking_id")
              .where("bo.order_id='#{params[:order_id]}'").first()

    @booking = Array.new
    if !booking.nil?
      if booking.dining_facility_id.to_i > 0
        dining_facilities = DiningFacility.find_by_id(booking.dining_facility_id)
        @booking.push({'booking_id' => booking.booking_id, 'dining_facility_id' => booking.dining_facility_id, 'type' => dining_facilities.type})
      else
        @booking.push({'booking_id' => booking.booking_id, 'dining_facility_id' => booking.dining_facility_id, 'type' => nil})
      end

    end
    @customers = Customer.pluck("customer_id, name")
    #for split bill
    lookup_spit_bill = Lookup.collection_of('split_bill')
    @split_bill = 0
    if !lookup_spit_bill[0].nil?
      @split_bill = lookup_spit_bill[0][1]
    end

    sale_order = SaleOrder.find_by_order_id(@order.order_id)
    if sale_order
      unless sale_order.sale_id.nil?
        sale = Sale.find(sale_order.sale_id)
        @sale_status = sale.sale_status
      end
    end

    @order.order_items.each_with_index do |item, order_item_index|
      if !item.set_menu_items.nil?
        instance_item_sets = JSON.parse(item.set_menu_items)
        arr_instance_item_sets = Array.new
        instance_item_sets.each do |instance_item|
          item_instance_name = MenuItemInstance.find_by_item_instance_code(instance_item["item_instance_code"]).item_instance_name
          arr_instance_item_sets.push(item_instance_name)
          item.price = item.price.to_f + instance_item["price"].to_f
        end
        @order.order_items[order_item_index].set_menu_items = arr_instance_item_sets
      end
    end

    # bookings = Booking.all
    # if !bookings.today.nil?
    #   @order_items_count = Hash.new
    #   bookings.each do |booking|
    #     if booking.sale_id.nil? && booking.booking_status != 'moved'
    #       if !booking.booking_orders.empty?
    #         booking.booking_orders.each do |booking_order|
    #           order = Order.find(booking_order.order_id)
    #           if !order.order_items.empty?
    #             if !@order_items_count.key?(booking.dining_facility_id)
    #               @order_items_count.store(booking.dining_facility_id, order.order_items.count)
    #             else
    #               @order_items_count[booking.dining_facility_id] += order.order_items.count
    #             end
    #           end
    #         end
    #       end
    #     else
    #       if !booking.sale_id.nil?
    #         sale = Sale.find(booking.sale_id)
    #         if sale.sale_status !='completed'
    #           if !@order_items_count.key?(booking.dining_facility_id)
    #             @order_items_count.store(booking.dining_facility_id, sale.sale_items.count)
    #           else
    #             @order_items_count[booking.dining_facility_id] = sale.sale_items.count
    #           end
    #         end
    #       end
    #     end
    #   end
    # end
  end

end
