<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\LiquidRenderer;
use App\Models\DistPages;
class PageController extends Controller
{

    protected $liquidRenderer;

    public function __construct(LiquidRenderer $liquidRenderer)
    {
        $this->liquidRenderer = $liquidRenderer;
    }

    /**
     * Display a listing of the pages.
     *
     * @return \Illuminate\Http\Response
     */
    public function list()
    {
        $pages = DistPages::paginate(10); // 每页显示10个页面
        return $this->liquidRenderer->render('pages.list', ['pages' => $pages]);
    }

    /**
     * Display the specified page.
     *
     * @param  string  $slug
     * @return \Illuminate\Http\Response
     */
    public function detail($slug)
    {

        $page = DistPages::where('slug', $slug)->first();

        // 如果没有找到且是数字，通过 id 获取页面
        if (!$page) {
            if (is_numeric($slug))
            {
                $page = DistPages::where('id', $slug)->firstOrFail();
            }
            else{
                abort(404);
            }
        }

        return $this->liquidRenderer->render('pages_detail.liquid', ['page' => $page]);
    }
}
