<?php

namespace app\controller\admin;

use app\extra\basic\Base;
use app\extra\service\system\UserService;
use app\extra\tools\CodeExtend;
use app\middleware\AuthMiddleware;
use app\model\system\SystemUser;
use app\validate\saas\UserValidate;
use DI\Attribute\Inject;
use LinFly\Annotation\Route\Controller;
use LinFly\Annotation\Route\Route;
use support\Request;
use support\Response;
use LinFly\Annotation\Route\Middleware;


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

    #[Inject]
    protected UserService $service;

    #[Inject]
    protected SystemUser $model;

    #[Inject]
    protected UserValidate $validate;

    protected string $scene = "edit";

    #[Route(path: "list",methods: "get")]
    public function getStoreList(Request $request): Response
    {
        try {
            $param = $request->get();
            $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: "passwd",methods: "post")]
    public function setUserPasswd(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"        => trans(20010),
                "password.require"  => trans(20010)
            ],"post");
            if (!is_array($param)) return error($param);
            $user = $this->model->where("id",$param['id'])->findOrEmpty();
            if ($user->isEmpty()) return errorTrans("empty.error");
            $user->password = md5($param['password'].$user['salt']);
            $state = $user->save();
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            return error($throwable->getMessage());
        }
    }
    /**
     * 新增/编辑代理
     * @param Request $request
     * @return Response
     */
    #[Route(path: "save",methods: "post")]
    public function save(Request $request): Response
    {
        try {
            $param = $request->post();
            $param['salt'] = strtoupper(CodeExtend::random(10,3));
            $param['password'] = md5($param['password'].$param['salt']);
            if (!$this->validate->check($param)) return error($this->validate->getError());
            $user = $this->model->where("username",$param['username'])->findOrEmpty();
            if (!$user->isEmpty()) return errorTrans("error.user-exist");
            $state = $this->model->setAutoData($param);
            if (!$state) return errorTrans("error.data");
            return successTrans("success.data");
        } catch (\Throwable $throwable) {
            echo $throwable->getMessage()."\n";
            echo $throwable->getFile()."\n";
            echo $throwable->getLine()."\n";
            return error($throwable->getMessage());
        }
    }

    /**
     * 修改密码
     * @param Request $request
     * @return Response
     */
    #[Route(path: "state",methods: "post")]
    public function setUserState(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"        => trans(20010)
            ],"post");
            if (!is_array($param)) return error($param);
            $user = $this->model->where("id",$param['id'])->findOrEmpty();
            if ($user->isEmpty()) return errorTrans("empty.error");
            if ($user['status'] == 1) {
                $user->status = 0;
            } else {
                $user->status = 1;
            }
            $user->updated_at = getDateFull();
            $state = $user->save();
            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 setBatchData(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"        => trans("empty.require"),
                "value.require"     => trans("empty.require"),
                "field.require"     => trans("empty.require"),
                "type.require"      => trans("empty.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 delUser(Request $request): Response
    {
        try {
            $param = $this->_valid([
                "id.require"    => trans("empty.require"),
                "type.default"  => "one",
            ],"post");
            if (!is_array($param)) return error($param);
            if ($param['type'] == "batch") {
                $state = $this->model->where("id","in",$param['id'])->delete();
            } else {
                $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());
        }
    }


}