class Admins::Topic::SchoolsController < Admins::Topic::BaseController
  before_action :find_school, only: [:edit, :update, :destroy]

  def index 
    q = ::Topic::School.ransack(title_cont: params[:search])
    schools = q.result(distinct: true)
    @schools = paginate(schools)
  end

  def new 
    @school = ::Topic::School.new
  end

  def create 
    @school = ::Topic::School.new(school_params)
    if @school.save 
      save_image_file(params[:image], @school)
      redirect_to admins_topic_schools_path
      flash[:success] = "新增合作高校logo成功"
    else
      redirect_to admins_topic_schools_path
      flash[:danger] = "新增合作高校logo失败"
    end
  end

  def edit 
  end

  def update 
    @school.attributes = school_params
    if @school.save 
      save_image_file(params[:image], @school)
      redirect_to admins_topic_schools_path
      flash[:success] = "更新合作高校logo成功"
    else  
      redirect_to admins_topic_schools_path
      flash[:danger] = "更新合作高校logo失败"
    end
  end

  def destroy 
    if @school.destroy 
      redirect_to admins_topic_schools_path
      flash[:success] = "删除合作高校logo成功"
    else  
      redirect_to admins_topic_schools_path
      flash[:danger] = "删除合作高校logo失败"
    end
  end

  private 
  def find_school
    @school= ::Topic::School.find_by_id(params[:id])
  end

  def school_params 
    params.require(:topic_school).permit(:title, :url, :order_index)
  end
end