<?php

namespace App\Http\Controllers\Goods;

use App\Http\Controllers\Controller;
use App\Http\Resources\GoodsTypeResource;
use App\Models\GoodsType;
use App\Models\Log as LogModel;
use App\Utils\FormatUtils;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class GoodsTypesController extends Controller
{
    public function __construct(Request $request)
    {
        $this->log = new LogModel([
            'module' => 'goods',
            'action' => $request->getMethod(),
            'target_type' => 'goods_type',
        ]);
    }

    public function index(Request $request)
    {
        $build = GoodsType::query();
        if(!empty($request->level)){
            $build->where("level","=",$request->level);
        }
        $goodsTypes = $build->paginate($request->get('per_page'));

        return GoodsTypeResource::collection($goodsTypes);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:191',
            'parent_id' => 'sometimes',
            'show' => 'sometimes|integer',
        ]);
        if ($validator->fails()) {
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());

            return response($this->res, $this->res['httpCode']);
        }
        $level = 1;
        if ($request->parent_id) {
            $parentGoodsType = GoodsType::query()->where("id", "=", $request->parent_id)->get()->toArray();
            $level = $parentGoodsType['level'] ?? 1 + 1;
            $existTypeName = GoodsType::query()->where("parent_id", "=", $request->parent_id)
                ->where("name",$request->name)->first();
            if($existTypeName){
                throw new \Exception("当前品类下该名称已存在");
            }
        }

        $goodsType = new GoodsType();
        $goodsType->parent_id = $request->parent_id ?? 0;
        $goodsType->show = $request->show ?? 1;
        $goodsType->name = $request->name;
        $goodsType->level = $level;
        $goodsType->save();

        $this->setAfterUpdateForLog($request->all());
        $this->addLog(0, 'add');

        return response($this->res, $this->res['httpCode']);
    }

    public function show($id)
    {
        return new GoodsTypeResource(GoodsType::query()->find($id));
    }

    public function update($id, Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'sometimes|string|max:191',
            'parent_id' => 'sometimes',
            'show' => 'sometimes|integer',
        ]);
        if ($validator->fails()) {
            $this->setValidatorFailResponse($validator->getMessageBag()->getMessages());

            return response($this->res, $this->res['httpCode']);
        }
        $goodsType = GoodsType::query()->find($id);
        $this->setBeforeUpdateForLog($goodsType->name);
        $level= 1;
        if ($request->parent_id) {
            $parentGoodsType = GoodsType::query()->where("id", "=", $request->parent_id)->get()->toArray();
            $level = $parentGoodsType['level'] ?? 1 + 1;
        }

        $goodsType->parent_id = $request->parent_id ?? 0;
        $goodsType->show = $request->show ?? 1;
        if($request->name){
            $goodsType->name = $request->name;
        }
        $goodsType->level = $level;
        $goodsType->save();
        $this->setAfterUpdateForLog($goodsType->name);
        $this->addLog($id, 'name');

        return new GoodsTypeResource($goodsType);
    }

    public function destroy($id)
    {
        $goodsType = GoodsType::query()->find($id);
        $goodsType->delete();
        $this->addLog($id, 'status');

        return response($this->res, $this->res['httpCode']);
    }

    public function tree(Request $request)
    {
        $goodsTypes = GoodsType::query()->get()->toArray();
        $menus = FormatUtils::formatTreeData($goodsTypes,  $request->parent_id??0);
        return $this->success($menus);
    }
}
