module PsAdmin
  # Order logic for admins
  class OrderController < ApplicationController
    before_action :authenticate_admin!
    before_action :set_order, except: [:index]
    layout 'admin'
    def index
      set_orders
    end

    def show
      otp = OrderToPdf.new(@order).call
      puts "#{otp.inspect}"
    end

    def destroy
      return if @order.blank?
      @order.destroy
      set_orders
    end

    def anonimize
      return if @order.blank?
      @order.anonimize_data
      set_orders
    end

    def status_histories
      render layout: false
    end

    def change_status
      return if params[:status].blank?
      @order.status = params[:status]
      @order.save
      OrderMailer.status_change(@order).deliver_later
    end

    private

    def set_order
      @order = Order.where(id: params[:id]).first
    end

    def set_orders
      @orders = Order.complete.page(params[:page]).per(ENV['order_per_page'])
                     .order('created_at DESC')
    end
  end
end
