# encoding:utf-8
module RailsAdmin
  module Config
    module Actions
    
      class GenerateMenu < RailsAdmin::Config::Actions::Base
        register_instance_option :visible? do
          if not bindings[:object].blank?
            authorized? && bindings[:object].class == WxMenu && bindings[:object].action == 'root'
          else
            authorized?
          end
        end
        # We want the action on members, not the Users collection
        register_instance_option :member do
          true
        end
        register_instance_option :link_icon do
          'icon-refresh'
        end
        # You may or may not want pjax for your action
        register_instance_option :pjax? do
          false
        end

        register_instance_option :controller do
          Proc.new do
            require 'rest-client'            
            root_menu = WxMenu.where("id=?", params[:id]).first

            n = "生成菜单成功！"
            if not root_menu.blank? 
              gzh_id = root_menu.wx_gongzhonghao_id
              
              url = "#{CONFIG_FILE["api_host"]}/railsadmin/wx_menu"
              begin
                ret = RestClient.post url, {gzhId: gzh_id}
              rescue Exception => e
                p "************ generate menu fail . #{e.to_s}"
                n = "生成菜单失败！"
              end
            end                        
            redirect_to :back, notice: "#{n}"
          end
        end
      end
    end
  end
end
