class Panel::CalendarNew::BackEnd::EventsController < OrbitBackendController
  open_for_visitor :only => [:index, :show]
  # GET /events
  # GET /events.json

  def index
    
    if params[:start].present? && params[:end].present?
      sdt = Time.at(params[:start].to_i)
      edt = Time.at(params[:end].to_i)
      @monthly_events = Event.monthly_event(sdt,edt)
      @re = Event.recurring_event(sdt,edt)
      @events = @monthly_events.inject(@re, :<<)    
    end
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @events }
    end
  end

  # GET /events/1
  # GET /events/1.json
  def show
    @event = Event.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @event }
    end
  end

  def agenda
    agenda_start = Date.parse(params[:agenda_start])
    agenda_end = Date.parse(params[:agenda_end])
    @events = Event.agenda_events(agenda_start,agenda_end)
    render :json=>@events.to_json
  end

  # GET /events/new
  # GET /events/new.json
  def new
    @event = Event.new
    @categories = get_categories_for_index
    @end_d_t = ""
    @start_d_t = ""
    @all_day = false;
    @recurring = false;
    if params
      case params[:allDay]
       when "true"
        @all_day = true
       when "false"
        @all_day = false
      case params[:recurring]
        when "true"
          @recurring = true
        when "false"
          @recurring = false
        end
      end
      @end_d_t = params[:endDate]
      @start_d_t = params[:startDate]
    end
    render :layout => false
   
  end

  # GET /events/1/edit
  def edit
    @event = Event.find(params[:id])
    @categories = get_categories_for_index
    @end_d_t =  @event.end.strftime("%Y/%m/%d %H:%M").to_s
    @start_d_t = @event.start.strftime("%Y/%m/%d %H:%M").to_s
    @all_day = @event.all_day
    @recurring = @event.recurring
    render :layout => false
  end

  # POST /events
  # POST /events.json
  def create
    @event = Event.new(params[:event])
       
    if @event.present? && @event.save
         render json: @event.to_json
    else
      respond_to do |format|
        format.html { render action: "new" }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /events/1
  # PUT /events/1.json
  def update
    @event = Event.find(params[:id])

    
      if @event.update_attributes(params[:event])
          render json: @event.to_json
      else
        respond_to do |format|
          format.html { render action: "edit" }
          format.json { render json: @event.errors, status: :unprocessable_entity }
        #format.js
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.json
  def destroy
    @event = Event.find(params[:id])
    @event.destroy
    render :json => {"success"=>true}.to_json
    # respond_to do |format|
    #   format.html { redirect_to events }
    #   format.json { head :no_content }
    # end
  end

end


