<?php

namespace app\controller\admin;

use app\extra\basic\Base;
use app\extra\service\system\DicService;
use app\middleware\AuthMiddleware;
use app\model\system\SystemData;
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/dic"),Middleware(AuthMiddleware::class)]
class Dic extends Base
{


    #[Inject]
    protected DicService $service;

    #[Inject]
    protected SystemData $model;


    /**
     * @param Request $request
     * @return Response
     */
    #[Route(path: "list",methods: "get")]
    public function getDicList(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "dicId.default"  => 0,
            ]);
            $data = $this->service->getMenuList($param);
            return success("ok",$data);
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     */
    #[Route(path: "save",methods: "post")]
    public function saveMenuData(Request $request): Response
    {
        try {
            $param = $request->post();
            $param['content'] = json_encode([]);
            $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: "sub",methods: "post")]
    public function saveSubData(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"    => trans("empty.require"),
                "name.require"  => trans("empty.require"),
                "code.require"  => trans("empty.require"),
            ],'post');
            if (!is_array($param)) return error($param);
            $data = $this->model->where("id",$param['id'])->findOrEmpty();
            if ($data->isEmpty()) return errorTrans("empty.data");
            $content = !empty($data['content']) ? $data['content'] : [];
            $dataId = $param['id'];
            foreach ($content as $val) {
                if ($val['code'] == $param['code']) {
                    return error("数据字段重复");
                }
            }
            unset($param['id']);
            $content[] = $param;
            $state = $this->model->setAutoData(['id' => $dataId,'content' => json_encode($content)]);
            if(!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }


    #[Route(path: "sub_del",methods: "post")]
    public function subDataDel(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"    => trans("empty.require"),
                "key.require"   => trans("empty.require"),
            ],'post');
            if (!is_array($param)) return error($param);
            $data = $this->model->where("id",$param['id'])->findOrEmpty();
            if ($data->isEmpty()) return errorTrans("empty.data");
            $content = !empty($data['content']) ? $data['content'] : [];
            if (empty($content)) return errorTrans("empty.data");
            unset($content[$param['key']]);
            $state = $this->model->setAutoData(['id' => $param['id'],'content' => json_encode($content)]);
            if(!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

    #[Route(path: "del",methods: "post")]
    public function DataDel(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"    => trans("empty.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 = $this->model->where("id",$param['id'])->delete();
            if(!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }

}