#encoding
module RailsAdmin
  module Config
    module Actions
        class OrderAward < RailsAdmin::Config::Actions::Base
            register_instance_option :visible?  do
                unless bindings[:object].blank?
                    authorized? && bindings[:object].status == "processing" && bindings[:object].has_commission ==false && bindings[:object].source =="sys"
                else
                    authorized?
                end
            end

            register_instance_option :member do
                true
            end

            register_instance_option :link_icon do
                'icon-plane'
            end

            register_instance_option :pjax? do
                false
            end

            register_instance_option :controller do
                Proc.new do
                    require 'rest-client'
                    order = Order.where("id = ? and status = ? and has_commission = ? and source = ? ",@object.id,"processing",false,"sys").first
                    n = "发放成功"
                    unless order.blank?
                        n = "已成功发放"
                        url = "#{CONFIG_FILE["api_host"]}/railsadmin/order/award/#{@object.id}"
                        begin
                            ret = RestClient.get url, {} #per_page: 5
                        rescue Exception => e
                            n = "发放失败！"
                        end
                    end
                    redirect_to :back, notice: "#{n}"
                end
            end
        end
    end
  end
end