class EnterprisesController < ApplicationController
  include AjaxScaffold::Controller
  
  before_filter :login_required
  before_filter :update_params_filter
  after_filter :clear_flashes
  
  def update_params_filter
    update_params :default_scaffold_id => "enterprise", :default_sort => nil, :default_sort_direction => "asc"
  end
  def index
    redirect_to :action => 'list'
  end
  def return_to_main
    # If you have multiple scaffolds on the same view then you will want to change this to
    # to whatever controller/action shows all the views 
    # (ex: redirect_to :controller => 'AdminConsole', :action => 'index')
    redirect_to :action => 'list'
  end

  def list
  end
  
  # All posts to change scaffold level variables like sort values or page changes go through this action
  def component_update
    @show_wrapper = false # don't show the outer wrapper elements if we are just updating an existing scaffold 
    if request.xhr?
      # If this is an AJAX request then we just want to delegate to the component to rerender itself
      component
    else
      # If this is from a client without javascript we want to update the session parameters and then delegate
      # back to whatever page is displaying the scaffold, which will then rerender all scaffolds with these update parameters
      return_to_main
    end
  end

  def component  
    @show_wrapper = true if @show_wrapper.nil?
    @sort_sql = Enterprise.scaffold_columns_hash[current_sort(params)].sort_sql rescue nil
    @sort_by = @sort_sql.nil? ? "#{Enterprise.table_name}.#{Enterprise.primary_key} asc" : @sort_sql  + " " + current_sort_direction(params)

    @paginator, @enterprises = paginate(:enterprises, :order => @sort_by, :per_page => BscgemUtils::PAGE_SIZE, :conditions => @enterprise_conditions)
    
    render :action => "component", :layout => false
  end

  def new
    @enterprise = Enterprise.new
    @successful = true

    return render(:action => 'new.rjs') if request.xhr?

    # Javascript disabled fallback
    if @successful
      @options = { :action => "create" }
      render :partial => "new_edit", :layout => true
    else 
      return_to_main
    end
  end
  
  def create
    begin
      @enterprise = Enterprise.new(params[:enterprise])
      @successful = @enterprise.save
    rescue
      flash[:error], @successful  = $!.to_s, false
    end
    
    return render(:action => 'create.rjs') if request.xhr?
    responds_to_parent do
      render :action => 'create.rjs'
    end   
  end

  def edit
    begin
      @enterprise = Enterprise.find(params[:id])
      @successful = !@enterprise.nil?
    rescue
      flash[:error], @successful  = $!.to_s, false
    end
    
    return render(:action => 'edit.rjs') if request.xhr?

    if @successful
      @options = { :scaffold_id => params[:scaffold_id], :action => "update", :id => params[:id] }
      render :partial => 'new_edit', :layout => true
    else
      return_to_main
    end    
  end

  def update
    begin
      @enterprise = Enterprise.find(params[:id])
      @successful = @enterprise.update_attributes(params[:enterprise])
    rescue
      flash[:error], @successful  = $!.to_s, false
    end
    
    return render(:action => 'update.rjs') if request.xhr?
    responds_to_parent do
      render :action => 'update.rjs'
    end   
  end

  def destroy
    begin
      @successful = Enterprise.find(params[:id]).destroy
    rescue
      flash[:error], @successful  = $!.to_s, false
    end
    
    return render(:action => 'destroy.rjs') if request.xhr?
    
    # Javascript disabled fallback
    return_to_main
  end
  
  def cancel
    @successful = true
    
    return render(:action => 'cancel.rjs') if request.xhr?
    
    return_to_main
  end

  def logo
    @enterprise = Enterprise.find(params[:id])
    send_file(@enterprise.full_filename)
  end

  def my_enterprise
    begin
      @enterprise = current_user.enterprise
    rescue
      flash[:error], @successful  = $!.to_s, false
    end
    
    @options = { :scaffold_id => params[:scaffold_id], :action => "update", :id => @enterprise[:id], :mine => true }
    render :partial => 'new_edit', :layout => true
  end
end
