class Admin::PillarsController < Admin::DashboardController
	
	before_action :set_pillar, only: [:edit, :show, :update, :destroy, :publish, :unpublish]

	def index
		@pillars = Pillar.paginate(:page => params[:page], :per_page => 20).order(:id => "DESC")
	end

	def new
		@pillar = Pillar.new
	end

	def show
		
	end

	def create
		@pillar = Pillar.new(pillar_params)
		@pillar.status = "Inactive"
		respond_to do |format|
			if @pillar.save
				format.html { redirect_to admin_pillar_path(@pillar.id), :notice => "Pillar was saved successfully." }
			else
				format.html { render :new }
			end
		end
	end

	def update
		respond_to do |format|
			if @pillar.update(pillar_params)
				format.html { redirect_to admin_pillar_path(@pillar), :notice => "Pillar was saved successfully." }
			else
				format.html { render :edit }
			end
		end
	end

	def publish
		if @pillar.update_attributes(:status => "Active")
			flash[:notice] = "Pillar has been published successfully"
		else
			flash[:notice] = "There was an error in publishing pillar. Please try again"
		end
		redirect_to admin_pillar_path(@pillar)
	end

	def unpublish
		if @pillar.update_attributes(:status => "Inactive")
			flash[:notice] = "Pillar has been unpublished successfully"
		else
			flash[:notice] = "There was an error in unpublishing pillar. Please try again"
		end
		redirect_to admin_pillar_path(@pillar)
	end

	private

		
		def set_pillar
			@pillar = Pillar.find(params[:id]) if params[:id]
		end

		def pillar_params
			params.require(:pillar).permit(:title, :post_url, :content, :published_on, :status, :h1_tag, :meta_title,:meta_description, :og_title, :og_image, :og_description, :name, :banner, :topic)
		end

end