module Admin
  class EmployeesController < Admin::ApplicationController
    before_filter :set_relation_options, only: [:new, :edit, :create, :update]
    skip_before_filter :setup_administrator!, only: :test_login
    skip_authorize_resource only: :test_login
    skip_before_filter :set_locale, only: :test_login
    # GET /employees
    # GET /employees.json
    def index
      @employees = Employee.all

      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @employees }
      end
    end

    def test_login
      if Rails.env.test? and employee = Employee.find_by_email(params[:email])
        sign_in employee
      end
      render nothing: true
    end

    # GET /employees/1
    # GET /employees/1.json
    def show
      @employee = Employee.find(params[:id])

      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @employee }
      end
    end

    # GET /employees/new
    # GET /employees/new.json
    def new
      @employee = Employee.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @employee }
      end
    end

    # GET /employees/1/edit
    def edit
      @employee = Employee.find(params[:id])
    end

    # POST /employees
    # POST /employees.json
    def create
      @employee = Employee.new(employee_params)

      respond_to do |format|
        if @employee.save
          format.html { redirect_to [:admin, @employee], notice: t('action.create.successfull', model: Employee.model_name.human) }
          format.json { render json: @employee, status: :created, location: @employee }
        else
          format.html { render action: "new" }
          format.json { render json: @employee.errors, status: :unprocessable_entity }
        end
      end
    end

    # PUT /employees/1
    # PUT /employees/1.json
    def update
      @employee = Employee.find(params[:id])

      respond_to do |format|
        if @employee.update_attributes(employee_params)
          format.html { redirect_to [:admin, @employee], notice: t('action.update.successfull', model: Employee.model_name.human) }
          format.json { head :no_content }
        else
          format.html { render action: "edit" }
          format.json { render json: @employee.errors, status: :unprocessable_entity }
        end
      end
    end

    # DELETE /employees/1
    # DELETE /employees/1.json
    def destroy
      @employee = Employee.find(params[:id])
      @employee.destroy

      respond_to do |format|
        format.html { redirect_to admin_employees_path, notice: t('action.destroy.successfull', model: Employee.model_name.human) }
        format.json { head :no_content }
      end
    end

    private

    def set_relation_options
      @employees = Employee.all
      @lists = List.all
    end

    def employee_params
      params.require(:employee).permit!
    end
  end
end
