class Admin::RolesController < OrbitBackendController
  
  layout "new_admin"
  before_filter :authenticate_user!
  before_filter :is_admin?
  before_filter :set_attribute, :only => [:index, :show, :new, :edit, :role_field, :sub_role, :add_sub_role, :edit_sub_role, :sub_role_field]
  helper Admin::AttributeValuesViewHelper

  def index
    @attributes = Role.all.entries
    render :template => 'admin/attributes/index'
  end
  
  def show
    
  end
  
  def role_field
  
	@field_name = 'role'
	
	@attribute = Role.find(params[:role_id])
	
  end
  
  def sub_role
	
	@attribute = Role.find(params[:role_id])
	@attribute_fields_upper_object = @attribute.sub_roles
	
	
  end
  
  def add_sub_role
	
	@attribute = Role.find(params[:role_id])
	# @sub_attribute = Role.find(params[:role_id])
	
	@sub_attribute = SubRole.new
    @sub_attribute.role_id = params[:role_id]
  
  end
  
  def edit_sub_role
	
	@sub_attribute = SubRole.find(params[:role_id])
	
	@attribute = Role.find(@sub_attribute.role_id)
	
    @sub_attribute.role_id = @sub_attribute.role_id
  
  end
  
  def sub_role_field
  
    @field_name = 'sub_role'
	
	@sub_attribute = SubRole.find(params[:role_id])
	
	@attribute = Role.find(@sub_attribute.role_id)
	
  end
  
  def add_attribute_field
	
	if params[:sub_role]
	
	@field_name = 'sub_role'
	
	attribute = SubRole.find(params[:sub_role][:id]) rescue nil
    @attribute_field_counter = attribute.attribute_fields.count
    @attribute_field = attribute.attribute_fields.build
    @attribute_field.save
	
    @attribute_field[:af_count] = @attribute_field_counter
	
    @attribute = SubRole.find(params[:sub_role][:id])
	
	else
	
	@field_name = 'role'
	
    attribute = Role.find(params[:role_id]) rescue nil
    @attribute_field_counter = attribute.attribute_fields.count
    @attribute_field = attribute.attribute_fields.build
    @attribute_field.save
	
    @attribute_field[:af_count] = @attribute_field_counter
	
    @attribute = Role.find(params[:role_id])
	
	end
	
	respond_to do |format|
      format.js  { render 'add_attribute_field' }
    end
	

  end
  
  def new
    @attribute = Role.new
	
	if !params[:parent_id]
      render :template => 'admin/attributes/new'
    end
	
  end
  
  def edit
    @attribute = Role.find(params[:id])
    @attribute_fields_upper_object = @attribute.sub_roles
    render :template => 'admin/attributes/edit'
  end
  
  def create
  
    if params[:sub_role]

      @sub_role = SubRole.new(params[:sub_role])
	  
      # @sub_role.role_id = params[:id]

      respond_to do |format|
        if @sub_role.save
          format.html { redirect_to(admin_role_sub_role_url(params[:sub_role][:role_id])) }
        end
      end
	  
	else

      @attribute = Role.new(params[:role])
      @attribute.save
      redirect_to :action => :index
	  
    end
	
  end
  
  def update
  
    if params[:sub_role]

      @sub_role = SubRole.find(params[:sub_role][:id])
      @sub_role.update_attributes(params[:sub_role])
      @sub_role.attribute_fields.each{|t| t.destroy if t["to_delete"] == true}
      respond_to do |format|
        format.html { redirect_to(admin_role_sub_role_url(@sub_role.role_id)) }
        format.js  { render 'toggle_enable' }
      end
	  
	else

      @attribute = Role.find(params[:id])
      @attribute.update_attributes(params[:role])
      @attribute.attribute_fields.each{|t| t.destroy if t["to_delete"] == true}
      respond_to do |format|
        format.html { redirect_to :action => :index }
        format.js  { render 'admin/attributes/toggle_enable' }
      end
	  
    end
	
  end
  
  def destroy
    
	if params[:sub_role]
	
	  @sub_role = SubRole.find(params[:sub_role][:id])
	  
      respond_to do |format|
        if @sub_role.destroy
		  format.html { redirect_to(admin_role_sub_role_url(@sub_role.role_id)) }
        end
      end
	  
	else
	
      @attribute = Role.find(params[:id])
      @attribute.destroy
      redirect_to :action => :index
	
    end
	
  end
  
  protected
  
  def set_attribute
    @attribute_type = 'role'
    @class = 'roles'
  end
  
end