<?php
namespace App\Controllers;

use App\Models\Page;

class PageController extends Controller
{
    public function show($slug)
    {
        $page = new Page();
        $p = $page->bySlug($slug);
        if (!$p) { \Core\App::notFound(); return ''; }

        $pTitle = e($p['title'] ?? '页面');
        $pSummary = mb_substr(strip_tags($p['summary'] ?? $p['body'] ?? ''), 0, 160);

        $seo = page_seo($slug, [
            'title'       => $pTitle,
            'description' => $pSummary,
            'keywords'    => '',
            'og_type'     => 'article',
        ]);
        return $this->view('page/show', [
            'pageSeo' => [
                'title'       => $seo['title'],
                'description' => $seo['description'],
                'keywords'    => $seo['keywords'],
                'og_type'     => $seo['og_type'] ?: 'article',
                'og_image'    => $seo['og_image'],
                'canonical'   => $seo['canonical'],
                'noindex'     => $seo['noindex'],
                'breadcrumb'  => [
                    ['name' => '首页', 'url' => site_url()],
                    ['name' => $p['title'] ?? '页面', 'url' => absolute_url()],
                ],
            ],
            'p' => $p,
        ]);
    }
}
