class Admin::ArticlesController < Admin::DashboardController
	require 'rest_client'
	require 'json'

	before_action :set_article, only: [:edit, :show, :update, :destroy, :publish, :unpublish]

	def index
		@articles = Article.paginate(:page => params[:page], :per_page => 100).order(:id => "DESC")
	end

	def reorder
		@articles = Article.where(:status => "Active").order(:priority => "ASC").limit(300)
	end

	def new
		@article = Article.new
	end

	def show
		
	end

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

	def update
		respond_to do |format|
			if @article.update(article_params)
				format.html { redirect_to admin_article_path(@article.id), :notice => "Article was saved successfully." }
			else
				format.html { render :edit }
			end
		end
	end

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

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

	private

		def set_article
			@article = Article.find(params[:id]) if params[:id]
		end

		def article_params
			params.require(:article).permit(:title, :category_id, :author_id, :anchor_tag_id, :summary, :post_url, :h1_tag, :meta_title, :meta_description, :thumbnail, :published_on, :content, :key_words, :buyers_journey_stages, :og_title, :og_description, :og_image, :read_time)
		end

end