<?php

namespace app\controller\admin;

use app\extra\basic\Base;
use app\extra\service\blue\ExpertLevelService;
use app\extra\tools\DataExtend;
use app\middleware\AuthMiddleware;
use app\model\blue\BlueExpertLevel;
use DI\Attribute\Inject;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Middleware;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;


#[Controller(prefix: "/api/level"),Middleware(AuthMiddleware::class)]
class Level extends Base
{

    #[Inject]
    protected BlueExpertLevel $model;

    #[Inject]
    protected ExpertLevelService $service;


    #[Route(path: "list",methods: "get")]
    public function getCategoryData(Request $request): Response
    {
        try {
            $param = $request->all();
            $list = $this->service->getList($param);
            return successTrans("success.data",pageFormat($list),200);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 编辑信息
     * @param Request $request
     * @return Response
     */
    #[Route(path: "save",methods: "post")]
    public function setListData(Request $request): Response
    {
        try {
            $param = $request->post();
            $state = $this->model->setAutoData($param);
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 批量操作
     * @param Request $request
     * @return Response
     */
    #[Route(path: "batch",methods: "post")]
    public function setGoodsBatch(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"        => "参数错误",
                "value.require"     => "参数错误",
                "field.require"     => "参数错误",
                "type.require"      => "参数错误"
            ],"post");
            if (!is_array($param)) return error($param);
            if ($param['type'] == "batch") {
                $state = $this->model->where("id","in",$param['id'])->save([$param['field'] => $param['value']]);
            } else {
                $state = $this->model->where("id",$param['id'])->save([$param['field'] => $param['value']]);
            }
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    /**
     * 删除
     * @param Request $request
     * @return Response
     */
    #[Route(path: "del",methods: "post")]
    public function delData(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"        => "参数错误",
            ],"post");
            if (!is_array($param)) return error($param);
            $data = $this->model->where("id",$param['id'])->findOrEmpty();
            if ($data->isEmpty()) return errorTrans("empty.data");
            $state = $data->delete();
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}