class Admins::SitePagesController <  Admins::BaseController
    before_action :require_admin
    before_action :finder_site_page, except: [:index]

    def index
        params[:sort_by] = params[:sort_by].presence || 'created_at'
        params[:sort_direction] = params[:sort_direction].presence || 'desc'
    
        pages = Admins::SitePagesQuery.call(params)
    
        @site_pages = paginate pages.preload(:user)
    end

    def show
        render 'edit'
    end

    def edit
    end


    def destroy
        if @site_page.destroy 
            redirect_to admins_site_pages_path
            flash[:success] = "删除站点成功"
        else  
            redirect_to admins_site_pages_path
            flash[:danger] = "删除站点失败"
        end
    end

    def update
        if update_params[:state] == "false" && update_params[:state_description].blank?
            flash[:danger] = '关闭站点理由不能为空'
        else
            @site_page.update(update_params)
            flash[:success] = '保存成功'
        end
        render 'edit'
    end

    private
    def finder_site_page
        @site_page = Page.find(params[:id])
        @user = @site_page.user
    end

    def update_params
        params.require(:page).permit(:state, :state_description)
    end
end
