class Panel::Calendar::BackEnd::EventsController < OrbitBackendController
	include Panel::Calendar::BackEnd::CalsHelper
	def new
		@calendars = Cal.all
		@event = Event.new
		if params[:ref] == "add-btn"
			@all_day_disabled = true
		else
			@all_day_disabled = false
		end
		@hours = getHoursForForm
		if params[:time]

			@hours.each_with_index do |h,i|
				if h['val'].to_s == params[:time].to_s
					@sindex = i
					@eindex = i + 2
					break
				end
			end
		else
			@sindex = 16
			@eindex = 18
		end
		@ampm = Array.new
		@ampm << "AM"
		@ampm << "PM"
		if params[:ses]
			@sampm = params[:ses]
			@eampm = params[:ses]
			if @eindex >= 22
				@eampm = "PM"
			end
		end
		@placeholder = params[:month]+"/"+params[:date]+"/"+params[:year]
		render :layout => false
	end
	def edit
		@calendars = Cal.all
		@event = Event.find(params[:id])
		if @event.all_day
			@all_day_disabled = true
		else
			@all_day_disabled = false
		end
		@hours = getHoursForForm
		@ampm = Array.new
		@ampm << "AM"
		@ampm << "PM"
		render :layout => false
	end 
	def create
		title = params[:event][:title]
		note = params[:event][:note]
		$start = params[:start_date]
		$end = params[:end_date]
		$starttime = params[:start_time]
		$endtime = params[:end_time]
		
		temp = $start.split("/")
		start_month = temp[0]
		start_date = temp[1]
		start_year = temp[2]
		@m = start_month
		@y = start_year
		temp = $end.split("/")
		end_month = temp[0]
		end_date = temp[1]
		end_year = temp[2]

		start_am_pm = params[:start_am_pm]
		end_am_pm = params[:end_am_pm]

		cal_id = params[:event][:cal_id]

		# if start_am_pm == "PM"
		# 	temp_start_time = $starttime + 12
		# end
		# if end_am_pm == "PM"
		# 	temp_end_time = $endtime + 12
		# end
		if params[:all_day]
			all_day = true
			$starttime = 0
			$endtime = 0
		else
			all_day = false
		end

		final_start_time = Date.new(start_year.to_i,start_month.to_i,start_date.to_i)
		final_end_time = Date.new(end_year.to_i,end_month.to_i,end_date.to_i)
		start_week = final_start_time.strftime("%U")
		end_week = final_end_time.strftime("%U")		
		total_days = final_end_time - final_start_time
		@event = Event.new
		@event.title = title
		@event.note = note
		@event.start_year = start_year
		@event.end_year = end_year
		@event.start_month = start_month
		@event.end_month = end_month
		@event.start_date = start_date
		@event.end_date = end_date
		@event.start_time = $starttime
		@event.start_am_pm = start_am_pm
		@event.end_time = $endtime
		@event.end_am_pm = end_am_pm
		@event.cal_id = cal_id
		@event.final_start_time = final_start_time
		@event.final_end_time = final_end_time
		@event.total_days = total_days
		@event.start_week = start_week
		@event.end_week = end_week
		@event.all_day = all_day
		@event.save!

		respond_to do |h|
			h.js
		end
	end

	def update

		title = params[:event][:title]
		note = params[:event][:note]
		$start = params[:start_date]
		$end = params[:end_date]
		$starttime = params[:start_time]
		$endtime = params[:end_time]
		
		temp = $start.split("/")
		start_month = temp[0]
		start_date = temp[1]
		start_year = temp[2]
		@m = start_month
		@y = start_year
		temp = $end.split("/")
		end_month = temp[0]
		end_date = temp[1]
		end_year = temp[2]

		start_am_pm = params[:start_am_pm]
		end_am_pm = params[:end_am_pm]

		cal_id = params[:event][:cal_id]

		# if start_am_pm == "PM"
		# 	temp_start_time = $starttime + 12
		# end
		# if end_am_pm == "PM"
		# 	temp_end_time = $endtime + 12
		# end
		if params[:all_day]
			all_day = true
			$starttime = 0
			$endtime = 0
		else
			all_day = false
		end

		final_start_time = Date.new(start_year.to_i,start_month.to_i,start_date.to_i)
		final_end_time = Date.new(end_year.to_i,end_month.to_i,end_date.to_i)
		start_week = final_start_time.strftime("%U")
		end_week = final_end_time.strftime("%U")		
		total_days = final_end_time - final_start_time
		@event = Event.find(params[:id])
		@event.title = title
		@event.note = note
		@event.start_year = start_year
		@event.end_year = end_year
		@event.start_month = start_month
		@event.end_month = end_month
		@event.start_date = start_date
		@event.end_date = end_date
		@event.start_time = $starttime
		@event.start_am_pm = start_am_pm
		@event.end_time = $endtime
		@event.end_am_pm = end_am_pm
		@event.cal_id = cal_id
		@event.final_start_time = final_start_time
		@event.final_end_time = final_end_time
		@event.total_days = total_days
		@event.start_week = start_week
		@event.end_week = end_week
		@event.all_day = all_day
		@event.save!

		respond_to do |h|
			h.js
		end
	end

	def show
		@event = Event.find(params[:id])
		@start_month_name = Date::ABBR_MONTHNAMES[@event.start_month]
		@end_month_name = Date::ABBR_MONTHNAMES[@event.end_month]
		@start_day_name = getDayName(@event.start_date,@event.start_month,@event.start_year)
		@end_day_name = getDayName(@event.end_date,@event.end_month,@event.end_year)
		render :layout=>false
	end

	def destroy
		event = Event.find(params[:id])
		event.delete
		render :json => {"success"=>"true"}.to_json
	end
end


