class Student::StudentUsersController < ApplicationController
  layout "student"
  
  before_filter :auth_suser, :except => [:new, :create]
  
  def index
    
    @users = StudentUser.all
  end


  def show
    @user = StudentUser.find(params[:id])

  end


  def new
    @user = StudentUser.new()
  
  end

  def edit
    @user = StudentUser.find(params[:id])
  
  end

  def create
  	@user = StudentUser.new(params.require(:student_user).permit!)
    @user.lock = true
    @user.locked_at = Time.now
    if params[:conditions]
      if @user.save
      
        @user.authenticate(params[:password])
        session[:student_user_id] = @user.id
        
        StudentMails.new_account_admin(@user).deliver
        
        redirect_to student_root_path, notice: "Vous êtes désormais inscrit. Vous pourrez vous connecter une fois votre compte vérifié."
      

          
    	else
    		render :action => "new" 
    	end
    else
      @condition_error = true
      render :action => "new" 
    
    end
  end

  def update
    @user = StudentUser.find(params[:id])
    if @user == current_suser or moderator?
    	
    
      if @user.update_attributes(params.require(:student_user).permit!)
        redirect_to student_student_user_path(@user)
          
    	else
    		render :action => "edit" 
    	end
    end
  end
  		


  def destroy
    if moderator?
    	@user = StudentUser.find(params[:id])
  	  @user.destroy
      redirect_to student_student_users_path
    end

  end



end
