class Transactions::OrderReservationsController < ApplicationController
	load_and_authorize_resource except: [:create]
  before_action :set_transactions_order_reservation, only: [:show, :edit, :update, :destroy]
  before_action :check_user

  # GET /transactions/order_reservations
  # GET /transactions/order_reservations.json
  def index
		 
    from, to  	= get_date_range_from_params
    filter 		= params[:filter]

    if from.nil? && to.nil?
        @order_reservations = OrderReservation.select("order_reservations.*, deliveries.provider, deliveries.delivery_fee")
        						.joins(" JOIN deliveries on deliveries.order_reservation_id = order_reservations.order_reservation_id")
        						.where("NOT order_reservation_status='new'")
        						.order("order_reservation_id desc")
        @order_reservations = Kaminari.paginate_array(@order_reservations).page(params[:page]).per(20)
    else
        order_reservation = OrderReservation.search(filter,from,to)
        if order_reservation.length > 0
           @order_reservations = order_reservation
           @order_reservations = Kaminari.paginate_array(@order_reservations).page(params[:page]).per(20)
         else
           @order_reservations = 0
         end
    end
    @from       = from
    @to         = to
    # get printer info
    @print_settings = PrintSetting.get_precision_delimiter()
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @order_reservations }
    end

  end

  # GET /transactions/order_reservations/1
  # GET /transactions/order_reservations/1.json
  def show

    @order_reservation = OrderReservation.select("order_reservations.*, deliveries.provider, deliveries.delivery_fee")
        						.joins(" JOIN deliveries on deliveries.order_reservation_id = order_reservations.order_reservation_id")
        						.where("order_reservations.order_reservation_id = ?", params[:id])
    							.first()
    #get customer amount
    @customer       = Customer.find(@order_reservation.customer_id)
    #get delivery details
    @delivery       = Delivery.find_by_order_reservation_id(@order_reservation.order_reservation_id)
    # get printer info
    @print_settings = PrintSetting.get_precision_delimiter()
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @order_reservation }
    end
  end

  # date range
  PERIOD = {
    "today" => 0,
    "yesterday" => 1,
    "this_week" => 2,
    "last_week" => 3,
    "last_7" => 4,
    "this_month" => 5,
    "last_month" => 6,
    "last_30" => 7,
    "this_year" => 8,
    "last_year" => 9
  }

  def get_date_range_from_params
      period_type = params[:period_type]
      period = params[:period]

        if params[:from].present? && params[:to].present?
          from = Time.zone.parse(params[:from])
          to = Time.zone.parse(params[:to])
        else
            case period.to_i
              when PERIOD["today"]
                from = Time.current
                to = Time.current
              when PERIOD["yesterday"]
                from = 1.day.ago
                to = 1.day.ago
              when PERIOD["this_week"]
                from = Time.current.beginning_of_week
                to = Time.current
              when PERIOD["last_week"]
                from = 1.week.ago.beginning_of_week
                to = 1.week.ago.end_of_week
              when PERIOD["last_7"]
                from = 7.day.ago
                to = Time.current
              when PERIOD["this_month"]
                from = Time.current.beginning_of_month
                to = Time.current
              when PERIOD["last_month"]
                from = 1.month.ago.beginning_of_month
                to = 1.month.ago.end_of_month
              when PERIOD["last_30"]
                from = 30.day.ago
                to = Time.current
              when PERIOD["this_year"]
                from = Time.current.beginning_of_year
                to = Time.current
              when PERIOD["last_year"]
                from = 1.year.ago.beginning_of_year
                to = 1.year.ago.end_of_year
            end
        end

      from = from.beginning_of_day
      to = to.end_of_day

      return from, to
  end

  def check_user
    if current_user.nil?
      redirect_to root_path
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_transactions_order_reservation
      @transactions_order_reservation = OrderReservation.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def transactions_order_reservation_params
      params.require(:transactions_order_reservation).permit(:cashier_id, :cashier_name, :requested_by, :requested_at, :receipt_no, :receipt_date, :customer_id, :payment_status, :order_reservation_status, :total_amount, :total_discount, :total_tax, :tax_type, :grand_total, :rounding_adjustment, :amount_received, :amount_changed)
    end
end
