module PsAdmin
  module Articles
    # articles
    class AllPageController < ApplicationController
      before_action :authenticate_admin!
      layout 'admin'
      def index
        @all_pages = AllPage.where('type_of = 3 AND article_id = ?', params[:aid])
        @published_pages = PublishedPage.where('type_of = 1 OR type_of = 2').order('priority ASC')
      end

      def show; end

      def new
        @article = Article.find(params[:aid])
        @all_page = AllPage.new(article_id: params[:aid], type_of: 3)
      end

      def create

        @all_page = AllPage.new(all_page_params)
        @article = Article.find(@all_page.article_id)
        @all_page.updated_by = current_admin.id
        @all_page.updated_at = Time.now
        @all_page.published = false
        if @all_page.save
          if params[:publish] == '1'
            params[:id] = @all_page.id
            publish
          end
          redirect_to controller: '/ps_admin/article', action: 'show', id: @article.id
        else
          render :new
        end
      end

      def edit
        @article = Article.find(params[:aid])
        @all_page = AllPage.find(params[:id])
      end

      def update
        @all_page = AllPage.find(params[:id])
        @article = Article.find(@all_page.article_id)
        @all_page.updated_by = current_admin.id
        @all_page.updated_at = Time.now
        @all_page.published = false
        if @all_page.update_attributes(all_page_params)
          if params[:publish] == '1'
            params[:id] = @all_page.id
            publish
          end
        else
          @all_page.errors.full_messages.each do |msg|
            puts "<li>#{msg}</li>"
          end
          render :edit
        end
      end

      def destroy
        if @all_page = AllPage.find(params[:id])
          @all_page.destroy
        end
        redirect_to controller: '/ps_admin/article', action: 'show', id: params[:aid]
      end

      def publish
        if @all_page = AllPage.find(params[:id])
          if @published_page = @all_page.published_page
            @published_page.name = @all_page.name
            @published_page.title = @all_page.title
            @published_page.meta_description = @all_page.meta_description
            @published_page.all_page_id = @all_page.id
            @published_page.nofollow = @all_page.nofollow
            @published_page.type_of = @all_page.type_of
            @published_page.small_text = @all_page.small_text
            @published_page.full_text = @all_page.full_text
            @published_page.article_id = @all_page.article_id
            @published_page.updated_by = current_admin.id
            @published_page.updated_at = Time.now
          else
            @published_page = PublishedPage.new
            @published_page.name = @all_page.name
            @published_page.title = @all_page.title
            @published_page.meta_description = @all_page.meta_description
            @published_page.all_page_id = @all_page.id
            @published_page.nofollow = @all_page.nofollow
            @published_page.type_of = @all_page.type_of
            @published_page.small_text = @all_page.small_text
            @published_page.full_text = @all_page.full_text
            @published_page.article_id = @all_page.article_id
            @published_page.priority = PublishedPage.all.size + 1;
            @published_page.updated_by = current_admin.id
            @published_page.updated_at = Time.now
            @published_page.created_by = current_admin.id
            @published_page.created_at = Time.now
          end
          if @published_page.save
            @all_page.published = true
            @all_page.save
          else
             if @published_page.errors
              @published_page.errors.full_messages.each do |msg|
                puts msg
              end
            end

          end
        end
        @all_page.reload
      end

      def unpublish
        if @all_page = AllPage.find(params[:id])
          if @all_page.published_page
            @all_page.published_page.destroy
          end
          @all_page.published = false
          @all_page.save
        end
        @all_page.reload
      end

      private
      def all_page_params
        params.require(:all_page).permit(:name, :article_id, :title, :type_of, :small_text, :full_text)
      end
    end
  end
end
