<?php

namespace Modules\Mall\Http\Controllers\Api;

use App\Api\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Mall\Http\Resources\Api\Goods\GoodsCollection;
use Modules\Mall\Http\Resources\Api\Goods\GoodsResource;
use Modules\Mall\Models\Goods;

class GoodsController extends Controller
{

    /**
     * Notes: 商品列表
     * @Author: Mr.wang
     * @Date  : 2021/5/11 16:20
     * @param  Request  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        #todo 商品分类搜索，可展示下级分类下的所有商品，分类数据缓存

        $name       = $request->get('name');
        $shopId     = $request->get('shop_id');
        $categoryId = $request->get('category_id');
        $brandId    = $request->get('brand_id');
        $payType    = $request->get('pay_type');
        $tagId      = $request->get('tag_id');
        $isSelf     = $request->get('is_self');
        $perPage    = $request->get('per_page');
        $orderBy    = $request->get('order_by');

        $goods = Goods::shown()
                      ->select(Goods::LIST_SELECT_FIELDS)
                      ->with(['tags', 'shop'])
                      ->when($name, function (Builder $query) use ($name) {
                          $query->where('name', 'like', "%$name%");
                      })
                      ->when($isSelf, function (Builder $query) {
                          $query->whereHas('shop', function (Builder $query) {
                              $query->where('is_self', 1);
                          });
                      })
                      ->when($shopId, function (Builder $query) use ($shopId) {
                          $query->where('shop_id', $shopId);
                      })
                      ->when($categoryId, function (Builder $query) use ($categoryId) {
                          $query->where('category_id', $categoryId);
                      })
                      ->when($brandId, function (Builder $query) use ($brandId) {
                          $query->where('brand_id', $brandId);
                      })
                      ->when($payType, function (Builder $query) use ($payType) {
                          $query->where('pay_type', $payType);
                      })
                      ->when($tagId, function (Builder $query) use ($tagId) {
                          $query->whereHas('tags', function (Builder $query) use ($tagId) {
                              $query->whereIn('id', explode(',', $tagId));
                          });
                      })
                      ->when($orderBy, function (Builder $query) use ($orderBy) {
                          switch ($orderBy) {
                              case 'sales':
                                  $query->orderByDesc('sales');
                                  break;
                              case 'price':
                                  $query->orderByPrice();
                                  break;
                          }
                      })
                      ->paginate($perPage);

        return $this->success(new GoodsCollection($goods));
    }

    public function show(Goods $goods): JsonResponse
    {
        $goods->incrementClicks();

        return $this->success(new GoodsResource($goods));
    }

}
