<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\LiquidRenderer;
use App\Models\SitePage;
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 = SitePage::paginate(10)->where('dist_id', getDistId())->where('status', '1'); // 每页显示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 = SitePage::where('status', '1')->where('dist_id', getDistId())->where('slug', $slug)->orWhere('id', $slug)->first();


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

        if($page->page_type==0) {
            $previousPage = SitePage::where('dist_id', getDistId())
                ->where('status', '1')
                ->where('id', '<', $page->id)
                ->orderBy('id', 'desc')
                ->first();

            $nextPage = SitePage::where('dist_id', getDistId())
                ->where('status', '1')
                ->where('id', '>', $page->id)
                ->orderBy('id', 'asc')
                ->first();

            // 创建分页数据结构
            $paginator = [
                'previous_page' => $previousPage ? true : false, // 是否有上一页
                'previous_page_url' => $previousPage ? "/pages/" . ($previousPage->slug ?: $previousPage->id) : null, // 上一页的 URL
                'next_page' => $nextPage ? true : false, // 是否有下一页
                'next_page_url' => $nextPage ? "/pages/" . ($nextPage->slug ?: $nextPage->id) : null // 下一页的 URL
            ];
        }
        else{
            $paginator = [];
        }



        // 构建导航数据 开始
        $breadcrumbs = [
            [
                'url' => '/',
                'name' => 'Home',
            ]
        ];

        if ($page->tags->isNotEmpty()) {
            $tag = $page->tags->first();
            $breadcrumbs[] = [
                'url' => $tag->slug ? "/collections/{$tag->slug}" : "/collections/{$tag->id}",
                'name' => $tag->name,
            ];
        }

        $breadcrumbs[] = [
            'url' => '#',
            'name' => $page->title,
        ];
        // 构建导航数据 结束

        $template_file ='pages_detail.liquid';

        if(!empty($page->template_file ))
        {
            $template_file=$page->template_file;
        }
        return $this->liquidRenderer->render($template_file,
            [
                'page' => $page,
                'breadcrumbs' => $breadcrumbs,
                'paginator' => $paginator,
                'csrf_token' => csrf_token(),

            ]);
    }
}
