<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Services\LiquidRenderer;
use App\Models\DistProduct;
use App\Models\DistProductCategory; // 引入分类模型


class ProductController extends Controller
{
    protected $liquidRenderer;

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

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

    public function category($slug)
    {
       // $products = DistProduct::paginate(10); // 每页显示10个产品
//        return $this->liquidRenderer->render('products_categories.liquid', ['products' => $products]);

        // 获取分类信息
        $category = DistProductCategory::findOrFail($slug);

        // 获取分类下的所有产品，并按 is_pinned 排序，然后进行分页
        $products = DistProduct::where('category_id', $category->id)
            ->orderBy('is_pinned', 'desc') // 按 is_pinned 降序排序
            ->paginate(12);

        // 渲染模板并传递数据
        return $this->liquidRenderer->render('products_categories.liquid', [
            'category' => $category, // 分类名称
            'products' => $products,           // 分类下的产品
        ]);

    }

    /**
     * Display the specified product.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function detail($id)
    {
        $product = DistProduct::getProduct($id);



        return $this->liquidRenderer->render('products_detail.liquid',
            [
                'product' => $product,
                'csrf_token' => csrf_token()
            ]);
    }
}
