class Origami::HomeController < BaseOrigamiController
  def index
    if params[:booking_id] != nil
      type=params[:booking_id].split('-')[0];
      # Sale
      if type == "SAL"
        @selected_item = Sale.find(params[:booking_id])
        @selected_item_type="Sale"
      # Booking
      else
        @selected_item = Order.find(params[:booking_id])
        @selected_item_type="Order"
      end
    end

    @completed_orders = Order.get_completed_order()
  	@booking_orders = Order.get_booking_order_table()
  	@booking_rooms = Order.get_booking_order_rooms()
  	@orders = Order.get_orders()
  end

  # def item_show
  # 	selection(params[:booking_id],1)
  # end

  def selection(selected_id, is_ajax)
    str = []
    type=selected_id.split('-')[0];
    # Sale
    if type == "SAL"
      @order_details = SaleItem.get_order_items_details(params[:booking_id])
      @order_details.each do |ord_detail|
        str.push(ord_detail)
      end
    # Booking
    else
      @order_details = OrderItem.get_order_items_details(params[:booking_id])
      @order_details.each do |ord_detail|
        str.push(ord_detail)
      end
    end

    if is_ajax == 1
      render :json => str.to_json
    else
      str
    end
  end

  def update_sale_by_customer

    id = params[:sale_id][0,3]
    if(id == "SAL")
       sale   = Sale.find(params[:sale_id])
    else
       sale   = Order.find(params[:sale_id])
    end

    status = sale.update_attributes(customer_id: params[:customer_id])

    if status == true
        render json: JSON.generate({:status => true})
    else
        render json: JSON.generate({:status => false, :error_message => "Record not found"})

    end
  end

  def get_customer

    @customer       = Customer.find(params[:customer_id])

    response        = Customer.get_member_account(@customer)

    respond_to do |format|
    format.js do
      render :json => {
        :response_data => response.as_json,
        :customer => @customer}
     end
     end
  end

end
