<?php

namespace App\Http\Controllers;

use App\Models\Cart;
use App\Models\Category;
use App\Models\Product;
use App\Models\Series;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\View;

class CategoriesController extends Controller
{
    public function index()
    {
        $categories = Category::all();
        return view('categories.index', compact('categories'));
    }

    public function category(Request $request)
    {
        $id = $request->id;
        $category = Category::query()->findOrFail($id);
        $brands = Product::query()->withoutGlobalScopes()->select('cs', DB::raw('any_value(category1)as category1'))
            ->where('category1', $id)->groupBy('cs')->where('stock','ok')->orderBy('cs', 'asc')->get();
        $name = preg_replace('/(battery|adapter)/i', '', $category->category);
        $name = trim($name);
        $sameCategory = Category::query()->where('category', 'like', "%$name%")->get();
        $new = Product::query()->where('category1', $id)->limit(10)->get();

        return view('categories.category', compact('category', 'brands', 'sameCategory', 'new'));
    }

    public function brand(Request $request)
    {
        $brand = rawurldecode($request->brand);
        $id = $request->id;
        $category = Category::query()->findOrFail($id);
        if ($category->type==='battery'){
            $categoryName = 'batería';
        }else{
            $categoryName = 'cargador';
        }
        //分类相同名字
        $series = Series::query()->where('cate_id', $id)->where('cs', $brand)->get();
        $products = Product::query()->withoutGlobalScopes()->where('category1', $id)->where('cs', $brand)
            ->when($request->series_id,function($query) use($request){
                return $query->where('series', $request->series_id);
            })
            ->orderBy('pid', 'desc')->paginate(21);
        $products->appends($request->all());
        $brands = Product::query()->withoutGlobalScopes()->select('cs', DB::raw('any_value(category1)as category1'))->where('category1', $id)
            ->groupBy('cs')->limit(40)->get();
        $newId = Cart::query()->selectRaw('product_id')->where('brand',$brand)->groupBy('product_id')->limit(15)->get()->pluck('product_id')->toArray();
        $new = Product::query()->whereIn('pid',$newId)->limit(10)->get();
        return view('categories.brand', compact('category', 'products', 'brand', 'brands', 'new', 'series','categoryName'));
    }

    public function new(Request $request)
    {
        $page = $request->page ?? 1;
        $perPage = 20;
        $items = Product::query()->where('stock', 'ok')->orderBy('pid', 'desc')->limit(100)->get();
        $products = new LengthAwarePaginator(
            $items->slice(($page - 1) * $perPage, $perPage),
            $items->count(),
            $perPage,
            $page,
            ['path' => $request->url(), 'query' => $request->query()]
        );
        return view('categories.new', compact('products', 'page'));
    }

    public function series(Request $request)
    {
        $id = $request->id;
        $seriesCurrent = Series::query()->findOrFail($id);
        $products = Product::query()->where('series', $id)->paginate(18);
        $series = Series::query()->where('cate_id', $seriesCurrent->cate_id)->where('cs', $seriesCurrent->cs)->get();

        $brands = Product::query()->withoutGlobalScopes()->select('cs', DB::raw('any_value(category1)as category1'))->where('category1', $seriesCurrent->cate_id)
            ->groupBy('cs')->get();
        $new = Product::query()->where('category1', $seriesCurrent->cate_id)->where('cs',$seriesCurrent->cs)->limit(5)->get();
        return view('categories.series', compact('series', 'products','seriesCurrent','brands','new'));
    }

    public function hot(Request $request)
    {
        $page = $request->page ?? 1;
        $perPage = 20;
        $carts = Cart::query()->with('product')->selectRaw('product_id,max(created_at)as created_at')->groupBy('product_id')->orderBy('created_at','desc')->limit(50)->get();
        $items = [];
        foreach ($carts as $cart) {
            if (isset($cart->product)) {
                $items[] = $cart->product;
            }
        }
        $products = new LengthAwarePaginator(
            array_slice($items, ($page - 1) * $perPage, $perPage),
            count($items),
            $perPage,
            $page,
            ['path' => $request->url(), 'query' => $request->query()]
        );
        return view('categories.hot', compact('products'));
    }
}
