class Admins::Topic::CkBannersController < Admins::Topic::BaseController
  before_action :require_business
  before_action :find_banner, only: [:edit, :update, :destroy]

  def index
    @banners = paginate(::Topic::CkBanner)
    @banners = paginate(::Topic::CkBanner.where("title like ?", "%#{params[:search]}%")) if params[:search].present?
  end

  def new
    @banner = ::Topic::CkBanner.new
  end

  def create
    @banner = ::Topic::CkBanner.new(banner_params)
    if @banner.save
      save_image_file(params[:image], @banner)
      redirect_to admins_topic_ck_banners_path
      flash[:success] = "新增banner成功"
    else
      redirect_to admins_topic_ck_banners_path
      flash[:danger] = "新增banner失败"
    end
  end

  def edit
  end

  def update
    @banner.attributes = banner_params
    if @banner.save
      save_image_file(params[:image], @banner)
      redirect_to admins_topic_ck_banners_path
      flash[:success] = "更新banner成功"
    else
      redirect_to admins_topic_ck_banners_path
      flash[:danger] = "更新banner失败"
    end
  end

  def destroy
    if @banner.destroy
      redirect_to admins_topic_ck_banners_path
      flash[:success] = "删除banner成功"
    else
      redirect_to admins_topic_ck_banners_path
      flash[:danger] = "删除banner失败"
    end
  end

  private
  def find_banner
    @banner = ::Topic::CkBanner.find_by_id(params[:id])
  end

  def banner_params
    params.require(:topic_ck_banner).permit(:title, :order_index, :url)
  end
end