<?php
namespace app\widget;


use app\model\Category;
use think\facade\{Db,View, Cache};

class Menu
{
    public function index($categoryId)
    {
        $menus = Cache::get('rules');
        if(empty($menus)){
            $menus = Category::getListForFrontMenu();
        }
        $currentFirstId = 0;
        if (is_numeric($categoryId) && $categoryId > 0) {
            $currentFirstId = Category::firstGradeById($categoryId);
        }

        //产品菜单

        $product_children=Category::alias('c')
            ->leftJoin('model m', 'c.model_id=m.id')
            ->field('c.*, m.manager, m.template, m.name as modelName')
            ->where('c.parent_id', 5)
            ->order('c.sort','asc')
            ->select()
            ->toArray();

        $data = [
            'categoryId' => $categoryId,
            'menus' => $menus,
            'product_children' => $product_children?$product_children:[],
        ];


        $this_menu=  \app\model\Category::alias('c')
            ->leftJoin('model m', 'c.model_id=m.id')
            ->field('c.*, m.manager, m.name as modelName')
            ->when(3, function($query)  {
                $query->whereIn('c.id', []);
            })
            ->order('sort','asc')
            ->select()
            ->toArray();
        return View::assign($data)->fetch('public/menu');
    }
}