
require 'rest_client'

class AjaxController < ApplicationController
	
	skip_before_filter :verify_authenticity_token

	def subscribe
		api = URI::encode("https://api.hubapi.com/contacts/v1/contact/?hapikey=8dc5557f-0970-44be-9c22-a214022082d9")
		data = {value: params[:email_address], property: "email"}
		puts data.inspect
		payload = { properties: [data] }
		puts payload.to_json
		response = RestClient.post api, payload.to_json, {content_type: :json, accept: :json}
		if response
			render json: { :status => "Success", :message => "Thank you. You will now receive notifications for new posts via email." }
		else
			render json: { :status => "Error", :message => "There was an error. Please try again later." }
		end
	end

	def landing_pages_form_data
		data = {name: params[:name], email: params[:email], phone: params[:phone], service_name: params[:form_name], message: params[:message], country_code: params[:country_code]}

		if data[:name].nil? || data[:name] == ""
			return render json: {:status => "error", :message => "Please enter your name"}
		end

		if data[:email].nil? || data[:email] == ""
			return render json: {:status => "error", :message => "Please enter your email address"}
		end

		CampaignLead.create(data)
		@leadCount = CampaignLead.where(:country_code => params[:country_code]).count
		CampaignMailer.send_email(data[:name], data[:email], data[:phone], data[:message], data[:service_name], data[:country_code], @leadCount).deliver
		return render json: {:status => "success"}
	end

end