class Admins::Topic::TechnicalDiscussionsController < Admins::Topic::BaseController
  before_action :require_business
  before_action :find_discussion, only: [:edit, :update, :destroy]

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

  def new 
    @discussion = ::Topic::TechnicalDiscussion.new
  end

  def create 
    @discussion = ::Topic::TechnicalDiscussion.new(discussion_params)
    if @discussion.save 
      redirect_to admins_topic_technical_discussions_path
      flash[:success] = "新增技术探讨成功"
    else
      redirect_to admins_topic_technical_discussions_path
      flash[:danger] = "新增技术探讨失败"
    end
  end

  def edit 
  end

  def update 
    @discussion.attributes = discussion_params
    if @discussion.save 
      redirect_to admins_topic_technical_discussions_path  
      flash[:success] = "更新技术探讨成功"
    else  
      redirect_to admins_topic_technical_discussions_path
      flash[:danger] = "更新技术探讨失败"
    end
  end

  def destroy 
    if @discussion.destroy 
      redirect_to admins_topic_technical_discussions_path
      flash[:success] = "删除技术探讨成功"
    else  
      redirect_to admins_topic_technical_discussions_path
      flash[:danger] = "删除技术探讨失败"
    end
  end

  private 
  def find_discussion
    @discussion = ::Topic::TechnicalDiscussion.find_by_id(params[:id])
  end

  def discussion_params 
    params.require(:topic_technical_discussion).permit(:title, :description, :identifier, :date, :order_index)
  end
end