class Admins::Topic::ServiceTermsController < Admins::Topic::BaseController
  before_action :require_business
  before_action :find_service_term, only: [:edit, :update, :destroy]

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

  def new 
    @service_term = ::Topic::ServiceTerm.new
  end

  def create 
    @service_term = ::Topic::ServiceTerm.new(service_term_params)
    if @service_term.save 
      redirect_to admins_topic_service_terms_path
      flash[:success] = "新增服务条款成功"
    else
      redirect_to admins_topic_service_terms_path
      flash[:danger] = "新增服务条款失败"
    end
  end

  def edit 
  end

  def update 
    @service_term.attributes = service_term_params
    if @service_term.save 
      redirect_to admins_topic_service_terms_path
      flash[:success] = "更新服务条款成功"
    else  
      redirect_to admins_topic_service_terms_path
      flash[:danger] = "更新服务条款失败"
    end
  end

  def destroy 
    if @service_term.destroy 
      redirect_to admins_topic_service_terms_path
      flash[:success] = "删除服务条款成功"
    else  
      redirect_to admins_topic_service_terms_path
      flash[:danger] = "删除服务条款失败"
    end
  end

  private 
  def find_service_term
    @service_term = ::Topic::ServiceTerm.find_by_id(params[:id])
  end

  def service_term_params 
    params.require(:topic_service_term).permit(:title, :description, :order_index)
  end
end