class Origami::OrderReservationController < BaseOrigamiController

  def index
  	@order = OrderReservation.latest_order #.active
    @count_on_order = OrderReservation.get_count_on_order
    @count_on_completed = OrderReservation.get_count_on_completed
    @shop = Shop.find_by_id(1)
  end

  def update
  	@id     = params[:order_id]
    @status = params[:status]
    min_type = params[:min_type]
    if min_type != ""
      expected_waiting_time = params[:expected_time]
    else
      expected_waiting_time = min_type + " " +params[:expected_time]
    end
    remark = params[:remark]
  	access_code = params[:access_code]
  	@order_reservation  = OrderReservation.find(@id)

		if @status == "processed"
			result = OrderReservation.create_doemal_order(@order_reservation,current_user)
		elsif @status == "delivery"
      OrderReservation.update_order_reservation(@id, nil, "ready_to_delivery")
      response = OrderReservation.send_status_to_ordering(@order_reservation.callback_url,@order_reservation.transaction_ref,"ready_to_delivery")
      result = {:status=> true, :message => "ready for delivery" }
    elsif @status == "completed"
      result = OrderReservation.update_doemal_payment(@order_reservation,current_user)
    else
			if @status == "rejected"
        OrderReservation.update_order_reservation(@id, nil, "rejected",nil,remark,access_code,current_user)
			  result = {:status=> true, :message => "rejected" }
  		else
        OrderReservation.update_order_reservation(@id, nil, "accepted", expected_waiting_time)
  			result = {:status=> true, :message => "accepted" }
  		end
		end
		render :json => result.to_json
  end

  def get_order
    if (params[:id])
      @order = OrderReservation.find(params[:id])
      return @order
    end
  end

  def send_status
    order_reservation = OrderReservation.find_by_transaction_ref(params[:ref_no])
    if !order_reservation.nil?
      if !ShiftSale.current_shift.nil? || params[:status] == 'accepted' || (order_reservation.status == 'new' && params[:status] == 'rejected')
        response = OrderReservation.send_status_to_ordering(params[:url],params[:ref_no],params[:status],params[:waiting_time],params[:min_type],params[:reason])
      else
        response = { status: false, message: 'No current shift open for this employee!'}
      end
    else
      response = { status: false, message: 'There is no order for '+params[:ref_no]+'!'}
    end
    render :json => response
  end

end
