class Admin::RoleFiltersController < OrbitMemberController
     
  include OrbitControllerLib::DivisionForDisable

  # layout "new_admin"
  
  before_filter :authenticate_user!
  before_filter :is_admin?
  # before_filter :set_attribute, :only => [:index, :show, :new, :edit]
  helper Admin::AttributeValuesViewHelper
  
  def index
	  get_types
	  @role = Role.find(params[:role_id])
    @role_filters =  @types.where(:role_id=>params[:role_id])
    @role_filter = @types.new(:display => 'List')
	
	# @url = admin_role_filter_role_filters_path

    if @app_type == 'role_statuses'
      @url = eval("admin_#{@app_type}_path")
    else
	    @url = eval("admin_#{@app_type}s_path")
	  end

    respond_to do |format|
      format.html # index.html.erb
      format.js
    end

  end


  def show
  
	  get_types
	
    @role_filter = @types.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.js
    end
  end


  def new
  
	  get_types
	
    @role_filter = @types.new(:display => 'List')
  
    # @url = polymorphic_path([:admin, @role_filter])

    if @app_type == 'role_statuses'
      @url = eval("admin_#{@app_type}_path(:role_id=>params[:role_id])")
    else
      @url = eval("admin_#{@app_type}s_path(:role_id=>params[:role_id])")
    end
	
	  @verb = :post

    render layout: false
    # respond_to do |format|
    #   format.html # new.html.erb
    #   format.js
    # end
  end
  

  def edit
  
	  get_types
	
    @role_filter = @types.find(params[:id])
	
	  @url = polymorphic_path([:admin, @role_filter])
	
	  @verb = :put

    render layout: false

    # respond_to do |format|
    #   format.html
    #   format.js
    # end
  end

  def create
    
	  get_types
	
    @role_filter = @types.new(params[:role_filter])

    if @app_type == 'role_statuses'
      @url = eval("admin_#{@app_type}_path(:role_id=>params[:role_filter][:role_id])")
    else
      @url = eval("admin_#{@app_type}s_path(:role_id=>params[:role_filter][:role_id])")
    end

    if @role_filter.save
      redirect_to @url
    else
      @role_filter = @types.new(params[:role_filter])
      flash.now[:error] = t('create.error.category')
      render action: :new
    end

  end


  def update
  
	  get_types
	
    @role_filter = @types.find(params[:id])
	# debugger
	# @url = admin_role_filter_role_filter_path(@role_filter)
	# @url = eval("admin_role_filter_#{@app_type}_path(#{@role_filter})")
	  @url = polymorphic_path([:admin, @role_filter])

    if @app_type == 'role_statuses'
      @url = eval("admin_#{@app_type}_path(:role_id=>@role_filter.role_id)")
    else
      @url = eval("admin_#{@app_type}s_path(:role_id=>@role_filter.role_id)")
    end


    @role_filter = @types.find(params[:id])
    if @role_filter.update_attributes(params[:role_filter])
      redirect_to @url
    else
      flash.now[:error] = t('update.error.category')
      render action: :edit
    end

  end


  def destroy
  
	  get_types
	
    @role_filter = @types.find(params[:id])
    @role_filter.disable = @role_filter.disable ? false : true

    if @role_filter.save!
      respond_to do |format|
        format.html { redirect_to(admin_role_filter_role_filters_url) }
        # format.xml  { head :ok }
        format.js
      end      
    else
      flash[:error] = t("role_filter.update_failed")
      format.html { render :action => "index" }
    end

  end

  def toggle
  
    get_types
    
    if @app_type == 'role_statuses'
      @role_filter = @types.find(params[:role_status_id])
      @url = eval("admin_#{@app_type}_path(:role_id=>@role_filter.role_id)")
    else
      @role_filter = @types.find(params[:role_category_id])
      @url = eval("admin_#{@app_type}s_path(:role_id=>@role_filter.role_id)")
    end

    @role_filter.disable = @role_filter.disable ? false : true

    @role_filter.save!

    redirect_to @url

  end
  
  protected
  
  def get_types
    @types = @app_type.classify.constantize
  end
  
end
