require_dependency "phcmemberspro/application_controller"

module Phcmemberspro
	class Directory::CatlistsController < ApplicationController

		# Security & Action Filters
		before_action :require_user
		before_action :set_paper_trail_whodunnit
		before_action :directorycategory
		before_action :set_directory_catlist, only: [:show, :edit, :update, :destroy]
		layout 'layouts/phcmemberspro/directory/directory_all.html.erb'

		# Directory Listing Index
		def index
			category = Directory::Category.find(params[:category_id])
			@directory_catlists = category.catlists.where(oganization_id: membership_info.org_id)
		end

		# Show Directory Listing
		def show
			category = Directory::Category.find(params[:category_id])
			@directory_catlist = category.catlists.find(params[:id])
		end

		# New Directory Listing
		def new
			category = Directory::Category.find(params[:category_id])
			@directory_catlist = category.catlists.build
		end

		# Edit Directory Listing Action
		def edit
			category = Directory::Category.find(params[:category_id])
			@directory_catlist = category.find(params[:id])
			respond_to do |format|
				format.html # new.html.erb
				format.xml  { render :xml => @directory_catlist }
			end
		end

		# Create Directory Listing Action
		def create
			@category = Directory::Category.find(params[:category_id])
			@directory_catlist = @category.catlists.create(directory_catlist_params)
			@directory_catlist.user_id = current_user.id
			@directory_catlist.membership_id = membership_info.id
			@directory_catlist.oganization_id = membership_info.org_id
			@directory_catlist.mains_id = Members::Business.find(params[:category_id])
			respond_to do |format|
				if @directory_catlist.save
					format.html { redirect_to directory_category_catlists_path, notice: 'Comment for Directorycategory was Successfully Created.' }
					format.json { render action: 'show', status: :created, location: @directory_catlist }
					else
						format.html { render action: 'new' }
						format.json { render json: @directory_catlist.errors, status: :unprocessable_entity }
				end
			end
		end

		# Update Directory Listing Action
		def update
			@directory_catlist.user_id = current_user.id
			@directory_catlist.membership_id = membership_info.id
			@directory_catlist.oganization_id = membership_info.org_id
			respond_to do |format|
				if @directory_catlist.update(directory_catlist_params)
					format.html { redirect_to directory_category_catlists_path, notice: 'Comment for Directorycategory was Successfully Updated.' }
					format.json { head :no_content }
					else
						format.html { render action: 'edit' }
						format.json { render json: @directory_catlist.errors, status: :unprocessable_entity }
				end
			end
		end

		# Delete Directory Listing
		def destroy
			@category = Directory::Category.find(params[:category_id])
			@directory_catlist = @category.catlists.find(params[:id])
			@directory_catlist.destroy
			respond_to do |format|
				format.html { redirect_to directory_category_catlists_path, notice: 'Comment for Directorycategory was Successfully Deleted.'  }
				format.json { head :no_content }
			end
		end

		private

		# Get Directory Category
		def directorycategory  
			@directory_category = Directory::Category.find(params[:category_id])
		end

		# Grab User Session Key (For ID)
		def current_user
			@_current_user ||= AuthRocket::Session.from_token(session[:ar_token]).try(:user)
		end

		# Get Current User from Above and Get Membership Info
		def membership_info
		    AuthRocket::Membership.all(user_id: current_user.id).first
		end

		# Common Callbacks
		def set_directory_catlist
			@directory_catlist = Directory::Catlist.find(params[:id])
		end

		# Whitelists
		def directory_catlist_params
			params.require(:directory_catlist).permit(:business_id, :category_id, :user_id, :membership_id, :oganization_id)
		end

	end
end