# -*- encoding : utf-8 -*-

class Admin::MailTypesController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
  
  def admin_space
    @admin_space = "preferences"
  end
  
  def index
    @mail_types = MailType.all

    @mail_types = sort_by_sorting(@mail_types, "slug ASC")
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 100
        per_page = params[:search][:per_page]
        page = (params[:page] and params[:page] != "") ? params[:page] : 1 
        @mail_types = @mail_types.page(page).per(per_page)
      
      }
    end
  end

  def show
    @mail_type = MailType.find(params[:id])

  end

  def new
    @mail_type = MailType.new
  
  end

  def edit
    @mail_type = MailType.find(params[:id])
    
  end

  def create
    @mail_type = MailType.new(params.require(:mail_type).permit!)

    if @mail_type.save
      
    else
      render action: "new"
 
    end

  end


  def update
    @mail_type = MailType.find(params[:id])


    if @mail_type.update_attributes(params.require(:mail_type).permit!)
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @mail_type = MailType.find(params[:id])
    @mail_type.destroy
    
  end
end
