class Oqs::HomeController < BaseOqsController
  def index
  	queue_stations=OrderQueueStation.all

    # Query for OQS with delivery status false
  	@queue_items_details = queue_items_query(false)

    # Query for OQS with delivery status true
    @queue_completed_item = completed_order

    @queue_stations_items=Array.new

    # Calculate Count for each station tab
    queue_stations.each do |que|
      i = 0
      zone_id = 0
      @queue_items_details.each do |qid|
        dining = DiningFacility.find_by_name(qid.zone)
        que.order_queue_process_by_zones.each do |qz|
          if qid.station_id == qz.order_queue_station_id && qid.zone_id == dining.zone_id
            zone_id = qid.zone_id
            i=i+1
          end
        end        
      end
      @queue_stations_items.push({:zone_id => zone_id  , :station_name => que.station_name, :is_active => que.is_active , :is_ap => que.auto_print, :item_count => i })
    end

    @queue_stations_items
  end

  # Get Order items
  def get_order_items
    items = []
    table_name = params[:table_id]
    status = params[:status]
    dining = DiningFacility.find_by_name(table_name);
    # oqpz = OrderQueueProcessByZone.find_by_zone_id(dining.zone_id)
    # if status == ""
    #   AssignedOrderItem.where("order_queue_station_id=#{ oqpz.order_queue_station_id } AND delivery_status=0").find_each do |aoi|
    #     oi = OrderItem.find_by_item_code(aoi.item_code)
    #     items.push(oi)
    #   end
    # else
    #   AssignedOrderItem.where("order_queue_station_id=#{ oqpz.order_queue_station_id } AND delivery_status=1").find_each do |aoi|
    #     oi = OrderItem.find_by_item_code(aoi.item_code)
    #     items.push(oi)
    #   end
    # end

    booking = Booking.find_by_dining_facility_id(dining.id)
    BookingOrder.where("booking_id='#{ booking.booking_id }'").find_each do |bo|
      order=Order.find(bo.order_id)
      order.order_items.each do |oi|
        items.push(oi)
      end
    end

    # booking_id = dining.get_new_booking
    # BookingOrder.where("booking_id='#{ booking_id }'").find_each do |bo|
    #     order=Order.find(bo.order_id);
    #     order.order_items.each do |oi|
    #       items.push(oi)
    #     end
    # end

    render :json => items.to_json
  end

  def show
  end

  # update delivery status when complete click
  def update_delivery_status
    removed_item = []
    assigned_item_id = params[:id]
    assigned_item=AssignedOrderItem.find(assigned_item_id)
    assigned_items=AssignedOrderItem.where("item_code='" + assigned_item.item_code + "' AND " + "order_id='" + assigned_item.order_id + "'");

    # update delivery status for completed same order items
    assigned_items.each do |ai|
      ai.delivery_status=true
      ai.save
      removed_item.push(ai.assigned_order_item_id)
    end
    render :json => removed_item.to_json
  end

  # Query for OQS with delivery status
  def queue_items_query(status) 
    AssignedOrderItem.select("assigned_order_items.assigned_order_item_id, oqs.id as station_id, oqs.station_name, oqs.is_active, oqpz.zone_id, df.name as zone, df.type, odt.order_id, odt.item_code, odt.item_name, odt.price, odt.qty, odt.item_order_by, odt.options, cus.name as customer_name, odt.created_at")
    .joins(" left join order_queue_stations as oqs on oqs.id = assigned_order_items.order_queue_station_id
              left join order_queue_process_by_zones as oqpz on oqpz.order_queue_station_id = oqs.id
              left join orders as od ON od.order_id = assigned_order_items.order_id
              left join order_items as odt ON odt.item_code = assigned_order_items.item_code AND odt.order_id = assigned_order_items.order_id
              left join customers as cus ON cus.customer_id = od.customer_id
              left join booking_orders as bo on bo.order_id = assigned_order_items.order_id
              left join bookings as bk on bk.booking_id = bo.booking_id
              left join dining_facilities as df on df.id = bk.dining_facility_id")
    .where("assigned_order_items.delivery_status = #{status}  AND odt.price <> 0")
    .group("assigned_order_items.assigned_order_item_id")
  end

  # Completed Order
  def completed_order
    AssignedOrderItem.select("assigned_order_items.assigned_order_item_id, oqs.id as station_id, oqs.station_name, oqs.is_active, oqpz.zone_id, df.name as zone, df.type, odt.order_id, odt.item_code, odt.item_name, odt.price, odt.qty, odt.item_order_by, odt.options, cus.name as customer_name, odt.created_at")
    .joins(" left join order_queue_stations as oqs on oqs.id = assigned_order_items.order_queue_station_id
              left join order_queue_process_by_zones as oqpz on oqpz.order_queue_station_id = oqs.id
              left join orders as od ON od.order_id = assigned_order_items.order_id
              left join order_items as odt ON odt.item_code = assigned_order_items.item_code AND odt.order_id = assigned_order_items.order_id
              left join customers as cus ON cus.customer_id = od.customer_id
              left join booking_orders as bo on bo.order_id = assigned_order_items.order_id
              left join bookings as bk on bk.booking_id = bo.booking_id
              left join dining_facilities as df on df.id = bk.dining_facility_id")
    .where("assigned_order_items.delivery_status = true  AND odt.price <> 0")
    .group("assigned_order_items.order_id")


     # completed_order = AssignedOrderItem.group(:order_id).where('delivery_status=true');
  end

end
