class Admins::Topic::JoinUsController < Admins::Topic::BaseController
  before_action :require_business
  before_action :find_join_us, only: [:edit, :update, :destroy]

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

  def new 
    @join_us = ::Topic::JoinUs.new
  end

  def create 
    @join_us = ::Topic::JoinUs.new(join_us_params)
    if @join_us.save 
      redirect_to admins_topic_join_us_path
      flash[:success] = "新增加入我们成功"
    else
      redirect_to admins_topic_join_us_path
      flash[:danger] = "新增加入我们失败"
    end
  end

  def edit 
  end

  def update 
    @join_us.attributes = join_us_params
    if @join_us.save 
      redirect_to admins_topic_join_us_path
      flash[:success] = "更新加入我们成功"
    else  
      redirect_to admins_topic_join_us_path
      flash[:danger] = "更新加入我们失败"
    end
  end

  def destroy 
    if @join_us.destroy 
      redirect_to admins_topic_join_us_path
      flash[:success] = "删除加入我们成功"
    else  
      redirect_to admins_topic_join_us_path
      flash[:danger] = "删除加入我们失败"
    end
  end

  private 
  def find_join_us
    @join_us = ::Topic::JoinUs.find_by_id(params[:id])
  end

  def join_us_params 
    params.require(:topic_join_us).permit(:title, :uuid, :order_index)
  end
end