<?php

namespace app\api\controller\store;

use app\models\store\StoreCategory;
use app\Request;
use think\facade\Config;

class CategoryController
{
    public function category(Request $request)
    {
        $category = StoreCategory::with('children')->where('is_show', 1)->order('sort desc,id desc')->where('pid', 0)->select();
        $category = $category->hidden(['add_time', 'is_show', 'sort', 'children.sort', 'children.add_time', 'children.pid', 'children.is_show'])->toArray();
        $mining_enabled = Config::get('activity.mining_enabled', false);
        if ($mining_enabled && count($category) > 0) {
            $mining_id = Config::get('activity.mining_cate_id');
            $mining_name = Config::get('activity.mining_display_name');
            $mining_pic = Config::get('activity.mining_display_pic');
            $category[0]['children'] = $category[0]['children']??[];
            $category[0]['children'][] = [
                'id'=>$mining_id,
                'cate_name' => $mining_name,
                'pic' => $mining_pic,
            ];
        }
        return app('json')->success($category);
    }
}