class Oqs::EditController < BaseOqsController
  authorize_resource :class => false
  def index
  	assigned_item_id = params[:id]
    @link_type = params[:type]
    @dining_type = nil
    @booking = nil
    if params[:type] == 'oqs'
      assigned_item = AssignedOrderItem.find(assigned_item_id)
      @order_item = OrderItem.where("order_id='#{ assigned_item.order_id }' AND item_instance_code='#{ assigned_item.instance_code }'")
    elsif params[:type] == "pending"
      assigned_item = OrderItem.find(assigned_item_id)
      @booking = Booking.joins(" JOIN booking_orders as bko on bko.booking_id = bookings.booking_id")
                .where("bko.order_id = '#{assigned_item.order_id}'").first()
      @order_item = OrderItem.where("order_id='#{ assigned_item.order_id }' AND item_instance_code='#{ assigned_item.item_instance_code }'")
    else
      assigned_item = OrderItem.find(assigned_item_id)
      dining = DiningFacility.find_by_id(params[:type])
      @dining_type = dining.type
      @order_item = OrderItem.where("order_id='#{ assigned_item.order_id }' AND item_instance_code='#{ assigned_item.item_instance_code }'")
    end
  end

  def update
    order_items_id = params[:order_items_id]
    qty_weight = params[:qty_weight]
    remarks = params[:remarks]

    order_item = OrderItem.find(order_items_id)
    order = Order.find(order_item.order_id)
    if qty_weight.to_i <= order_item.qty.to_i
      before_updated_qty = order_item.qty

      order_item.item_order_by = current_user.name
      order_item.qty = qty_weight
      order_item.remark = remarks
      order_item.save

      if ENV["SERVER_MODE"] != "cloud" && order.source == 'cashier' #no print in cloud server
        # print
        assigned_item = AssignedOrderItem.find_by_instance_code(order_item.item_instance_code)    
        assigned_items = nil
        if !assigned_item.nil?
           assigned_items = AssignedOrderItem.where("item_code='" + assigned_item.item_code + "' AND " + "order_id='" + assigned_item.order_id + "'")
        end  
        if !assigned_items.nil?
          assigned_items.each do |assign_item|
            # order queue stations
            oqs = assign_item.order_queue_station

            printer = PrintSetting.all 

            unique_code="OrderItemPdf"
            if !printer.empty?
              printer.each do |printer_setting|
                if printer_setting.unique_code == 'OrderItemPdf' 
                  unique_code="OrderItemPdf"
                elsif printer_setting.unique_code == 'OrderItemSlimPdf'
                  unique_code="OrderItemSlimPdf"
                elsif printer_setting.unique_code == 'OrderSetItemPdf'
                  unique_code="OrderSetItemPdf"
                elsif printer_setting.unique_code == 'OrderItemCustomisePdf'
                  unique_code="OrderItemCustomisePdf"
                elsif printer_setting.unique_code == 'OrderSetItemCustomisePdf'
                  unique_code="OrderSetItemCustomisePdf"  
                elsif printer_setting.unique_code == 'OrderItemSlimCustomisePdf'
                  unique_code="OrderItemSlimCustomisePdf"
                end  
              end
            end   

            print_settings=PrintSetting.find_by_unique_code(unique_code)
            order_queue_printer= Printer::OrderQueuePrinter.new(print_settings)
            order_queue_printer.print_order_item(print_settings, oqs, order_item.order_id, order_items_id, print_status=" (Cancelled)", before_updated_qty )
          end
        end
      end
      render :json => {:status=> true }
    else
      render :json => {:status=> false, :message => "Not allowed over quantity!" }
    end
  end

  #Shop Name in Navbor
  helper_method :shop_detail
  def shop_detail
    @shop = Shop.first
  end
end
