<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\admin\api\UserApi;
use think\exception\ValidateException;
use app\admin\validate\User as UserValidate;

/**
 * Description of Role
 *
 * @author sgq
 */
class User extends AdminController {

    /**
     * @auth {{/mgr}}
     * @return type
     */
    public function index() {
        return view();
    }

    /**
     * @auth {{/mgr/list}}
     * @return type
     */
    public function list() {
        $list = UserApi::getList($this->request->param());
        return json($list);
    }

    /**
     * @auth {{/mgr/add}}
     * @return type
     */
    public function add() {
        if ($this->request->isPost()) {
            try {
                $params = $this->request->param();
                validate(UserValidate::class)->scene("add")->check($params);
                $res = UserApi::create($params);
                switch ($res) {
                    case 10001:
                        return json(["msg" => "添加成功"]);
                    default:
                        throw new ValidateException("未知原因,请联系管理员");
                }
            } catch (ValidateException $e) {
                return json(["msg" => $e->getMessage()], 500);
            }
        }
        return view();
    }

    /**
     * @auth {{/mgr/edit}}
     * @return type
     */
    public function edit() {
        $params = $this->request->param();
        if ($this->request->isPost()) {
            try {
                validate(UserValidate::class)->scene("edit")->check($params);
                $res = UserApi::update($params);
                switch ($res) {
                    case 10001:
                        return json(["msg" => "编辑成功"]);
                    case 10002:
                        throw new ValidateException("账户已经被删除,不能编辑");
                    case 10003:
                        throw new ValidateException("不能编辑管理员账户");
                    default:
                        throw new ValidateException("未知原因,请联系管理员");
                }
            } catch (ValidateException $e) {
                return json(["msg" => $e->getMessage()], 500);
            }
        }
        $id = $params["userId"];
        return view("", ["user" => UserApi::getOne($id)]);
    }

    public function info() {
        $userId = $this->user["uid"];
        if ($this->request->isPost()) {
            try {
                $params = $this->request->param();
                $params["id"] = $userId;
                validate(UserValidate::class)->scene("info")->check($params);
                $res = UserApi::update($params);
                switch ($res) {
                    case 10001:
                        return json(["msg" => "编辑成功"]);
                    case 10002:
                        throw new ValidateException("账户已经被删除,不能编辑");
                    case 10003:
                        throw new ValidateException("不能编辑管理员账户");
                    default:
                        throw new ValidateException("未知原因,请联系管理员");
                }
            } catch (ValidateException $e) {
                return json(["msg" => $e->getMessage()], 500);
            }
        }
        $user = UserApi::getOne($userId);
        return view("", ["user" => $user]);
    }

    public function change_pwd() {
        $params = $this->request->param();
        $id = $this->user["uid"];
        if ($this->request->isPost()) {
            try {
                validate(UserValidate::class)->scene("change_pwd")->check($params);
                $oldPwd = $params["old_password"];
                $pwd = $params["password"];
                $res = UserApi::setPwd($id, $oldPwd, $pwd);
                switch ($res) {
                    case 10001:
                        return json(["msg" => "密码修改成功"]);
                    case 10002:
                        throw new ValidateException("账户已经被删除,密码修改失败");
                    case 10003:
                        throw new ValidateException("不能修改管理员账户的密码");
                    case 10004:
                        throw new ValidateException("原密码错误,修改密码失败");
                    default:
                        throw new ValidateException("未知原因,请联系管理员");
                }
            } catch (ValidateException $e) {
                return json(["msg" => $e->getMessage()], 500);
            }
        }
        return view("", ["user" => UserApi::getOne($id)]);
    }

    /**
     * @auth {{/mgr/delete}}
     */
    public function delete() {
        if ($this->request->isPost()) {
            $res = UserApi::delete($this->request->param("userId"));
            switch ($res) {
                case 10001:
                    return json(["msg" => "删除成功"]);
                case 10002:
                    return json(["msg" => "不能重复删除"], 500);
                case 10003:
                    return json(["msg" => "不能操作管理员账户"], 500);
                default:
                    return json(["msg" => "未知原因,请联系管理员"], 500);
            }
        }
    }

    /**
     * @auth {{/mgr/reset}}
     */
    public function reset() {
        if ($this->request->isPost()) {
            $id = $this->request->param("userId");
            $info = UserApi::getOne($id);
            if (!$info)
                return json(["msg" => "没有对应的管理员账户"]);
            $salt = $info["salt"];
            $def_pwd = "JJrc@123";
            $password = UserApi::getPwd($def_pwd, $salt);
            $res = UserApi::reset($id, $password);
            switch ($res) {
                case 10001:
                    return json(["msg" => "密码重置成功"]);
                case 10002:
                    return json(["msg" => "账户已经被删除,密码重置失败"], 500);
                case 10003:
                    return json(["msg" => "不能重置管理员账户"], 500);
                default:
                    return json(["msg" => "未知原因,请联系管理员"], 500);
            }
        }
        return view();
    }

    /**
     * @auth {{/mgr/freeze}}
     */
    public function freeze() {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $res = UserApi::setFreeze($params["userId"], 2);
            switch ($res) {
                case 10001:
                    return json(["msg" => "冻结成功"]);
                case 10002:
                    return json(["msg" => "账户已经被删除,冻结失败"], 500);
                case 10003:
                    return json(["msg" => "不能重置管理员账户"], 500);
                default:
                    return json(["msg" => "未知原因,请联系管理员"], 500);
            }
        }
    }

    /**
     * @auth {{/mgr/unfreeze}}
     */
    public function unfreeze() {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $res = UserApi::setFreeze($params["userId"], 1);
            switch ($res) {
                case 10001:
                    return json(["msg" => "解除冻结成功"]);
                case 10002:
                    return json(["msg" => "账户已经被删除,解除冻结失败"], 500);
                case 10003:
                    return json(["msg" => "不能重置管理员账户"], 500);
                default:
                    return json(["msg" => "未知原因,请联系管理员"], 500);
            }
        }
    }

    /**
     * @auth {{/mgr/setRole}}
     */
    public function setRole() {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            $res = UserApi::setRole($params["userId"], $params["roleIds"]);
            switch ($res) {
                case 10001:
                    return json(["msg" => "角色分配成功"]);
                case 10002:
                    return json(["msg" => "账户已经被删除,角色分配失败"], 500);
                case 10003:
                    return json(["msg" => "不能分配管理员账户角色"], 500);
                default:
                    return json(["msg" => "未知原因,请联系管理员"], 500);
            }
        }
    }

    /**
     * @auth {{/mgr/role_assign}}
     */
    public function role_assign() {
        $user = UserApi::getOne($this->request->param("userId"));
        return view("", ["user" => $user]);
    }

    /**
     * 上传头像
     */
    public function upload() {
        if ($this->request->file()) {
            $avatar = $this->request->file("file");
            $upload = new \app\common\api\UploadApi();
            $result = $upload->uploadOne($avatar, "image", "user/avatar");
            if ($result->code == 200) {
                $url = $result->filepath;
                return json([$url]);
            }
            return json(["msg" => $result->msg], 500);
        }
    }

}