class Admin::RolesController < OrbitMemberController
  
  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.asc("_id").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]
    render layout: false
  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
    render layout: false
  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 get_variables_for_new
  #   @designs = Design.all.entries
  #   @themes = Design.first.themes
  #   @module_apps = ModuleApp.for_frontend_select
  #   @app_frontend_urls = nil
  #   @categories = nil
  #   @tags = nil
  #   @page_frontend_data_counts = nil
  #   @frontend_styles = nil
  #   @selected={
  #     :design => @designs.first,
  #     :theme=> @themes.first,
  #     :module_app=>nil,#@module_apps.first
  #     :app_frontend_url=> nil, #@module_apps.first
  #     :category=>nil,
  #     :tag=>nil,
  #     :page_frontend_data_count=>nil,
  #     :frontend_style => nil
  #   }
  # end

  # def new
  #   @item = Page.new(params[:page])
  #   @item.parent = Item.find(params[:parent_id]) rescue nil
  #   get_variables_for_new
  #   render layout: false
  # end

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

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


      if @sub_role.save
        redirect_to admin_role_sub_role_url(params[:sub_role][:role_id])
      else
        @sub_role = SubRole.new(params[:sub_role])
        flash.now[:error] = t('create.error.category')
        render action: :new
      end
	  
	  else

      @attribute = Role.new(params[:role])
     #  if @attribute.save
     #    redirect_to :action => :index
	    # end
      if @attribute.save
        redirect_to admin_roles_url
      else
        @attribute = Role.new(params[:role])
        flash.now[:error] = t('create.error.category')
        render action: :new
      end
    end
	
  end
  
  def update
  
    if params[:sub_role]

      @sub_role = SubRole.find(params[:sub_role][:id])
      if @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

        redirect_to admin_role_sub_role_url(@sub_role.role_id)
      else
        flash.now[:error] = t('update.error.category')
        render action: :edit
      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

      if @attribute.update_attributes(params[:role])
        @attribute.attribute_fields.each{|t| t.destroy if t["to_delete"] == true}
        redirect_to admin_roles_url
      else
        flash.now[:error] = t('update.error.category')
        render action: :edit
      end
	  
    end
	
  end
  
  def destroy
    
	  if params[:sub_role]
	
	    @attribute = SubRole.find(params[:sub_role][:id])
	  
      respond_to do |format|
        if @attribute.destroy
		      format.html { redirect_to(admin_role_sub_role_url(@attribute.role_id)) }
          format.js 
        end
      end
	  
	  else
	
      @attribute = Role.find(params[:id])
      @attribute.destroy
	    respond_to do |format|
        format.html { redirect_to admin_roles_url }
        format.js  { render 'admin/attributes/destroy' }
      end
    end
	
  end

  def toggle
    @attribute = Role.find(params[:role_id])
    @attribute.disabled = @attribute.disabled ? false : true
    @attribute.save!
    redirect_to action: :index 
  end

  def sub_role_toggle
  
    @sub_attribute = SubRole.find(params[:role_id])

    @sub_attribute.disabled = @sub_attribute.disabled ? false : true
    @sub_attribute.save!

    redirect_to admin_role_sub_role_url(@sub_attribute.role_id)

  end

  
  protected
  
  def set_attribute
    @attribute_type = 'role'
    @class = 'roles'
  end
  
end