#encoding
module RailsAdmin
  module Config
    module Actions
      class AddWxkuCommodity < RailsAdmin::Config::Actions::Base
        register_instance_option :visible?  do
          if not bindings[:object].blank?
               if  bindings[:object].class.base_class.name == 'WxkuCommodity'
                authorized? && bindings[:object].status_ticket == ''
               else
                authorized?
              end             
          else
            authorized?
          end
        end

        register_instance_option :member do
            true
        end

        register_instance_option :link_icon do
            'icon-thumbs-up'
        end

        register_instance_option :pjax? do
            false
        end

        register_instance_option :controller do
          Proc.new do
            require 'rest-client'
            n = "导入商品成功！"
            url = "#{CONFIG_FILE["api_host"]}/railsadmin/wxku_commodity/#{@object.id}/addOrUpdate"
            p url
            begin
              ret = RestClient.post url, {}
            rescue Exception => e
              p "************ AddWxkuCommodity fail . #{e.to_s}"
              n = "导入商品失败！"
            end
            redirect_to :back, notice: "#{n}"
          end
        end
      end
    end
  end
end