class Www::TaDiscussionsController < WwwController
  before_filter :prepare_breadcrumb, only: [:show]
  layout 'discussion'

  def index
    @near_events = TaDiscussion.where("discussion_date >= ?", DateTime.current).order("discussion_date DESC").first
    @events = TaDiscussion.where("discussion_date < ?", DateTime.current).order("discussion_date ASC")
    @available_discussions = TaDiscussion.count rescue 0
    @suggestion = TaSuggestion.new
  end

  def show
    
  end

  def new_ta_discussion_form
    @subscription = TaDiscussionForm.new
    render layout: false
  end

  def create_ta_discussion_form
    @event = TaDiscussion.find(params[:id])
    unless @event.slots.to_i <= 0
      @subscription = TaDiscussionForm.new(params[:ta_discussion_form])
      @subscription.ta_discussion_id = @event.id
      @success = @subscription.save
      @event.slots = @event.slots.to_i - 1
      @event.save if @success
      @errors = @subscription.errors.messages.to_json.html_safe unless @success
      flash.notice = "Te has inscrito al evento exitosamente"
    else
      flash.notice = "El limite de cupos ha llegado a su maximo."
    end
  end
  private

  def event
    @event = TaDiscussion.find(params[:id])
  end
  helper_method :event

  def prepare_breadcrumb
    add_breadcrumb 'Listado de eventos', www_ta_discussions_url
    add_breadcrumb event.title
  end

end