<?php

namespace app\api\controller\User;

use app\BaseController;
use app\common\model\User\User as UserUser;
use app\Request;
use app\common\arw\adjfut\src\UploadFile;
use app\common\arw\adjfut\src\Validate;

class User extends BaseController
{
    /**
     * 编辑用户信息
     */
    public function updateUserInfo(Request $request): array
    {
        $param = Validate::param([
            'user_name' => 'require',
            'user_phone' => 'require|mobile',
            'user_id_card' => 'require'
        ]);
        $user = $request->getCurrentUser();
        $user->allowField([
            'user_id_card',
            'user_phone',
            'user_name'
        ]);
        $user->save($param);
        return [
            'code' => 0,
            'msg' => '更新成功'
        ];
    }
    /**
     * 获取用户列表
     */
    public function getUserList(Request $request): array
    {
        $user = $request->param('user');
        $con = [
            'user_status' => UserUser::STATUS_ENABLE
        ];
        $search = [];
        if ($user) {
            $search['user_name'] = $user;
        }
        $query = UserUser::withSearch(
            array_keys($search),
            $search
        )->where($con);
        $select =  self::pageWrapper($query)->field([
            'user.user_guid',
            'user.user_name',
            'user.user_phone',
            'user.user_position',
            'user.user_department',
        ])->group('user.user_guid')->order([
            'user_id' => 'desc'
        ])->select();
        $count =  $query->count();
        return [
            'code' => 0,
            'data' => $select,
            'count' => $count,
            'msg' => 'ok'
        ];
    }
    /**
     * 获取用户信息
     */
    public function getUserInfo(Request $request): array
    {
        $user = $request->getCurrentUser();
        $token = $request->getCurrentToken();
        return [
            'code' => 0,
            'data' => [
                'user_img' => $user->user_img,
                'user_name' => $user->user_name,
                'user_phone' => $user->user_phone,
                'user_position' => $user->user_position,
                'user_department' => $user->user_department,
                'user_create_name' => $user->user_create_name,
                'user_department' => $user->user_department,
                'user_sex' => $user->user_sex,
                'token_update_time' => $token->token_update_time,
            ],
            'msg' => 'ok'
        ];
    }

    /**
     * 更新用户头像
     */
    public function updateUserImg(Request $request): array
    {
        $user = $request->getCurrentUser();
        $upload = new UploadFile('uploads', 'image');
        $path = $upload->putFile('UserImg');
        $user->user_img = $path;
        $user->save();
        return [
            'code' => 0,
            'msg' => '更新成功'
        ];
    }

    /**
     * 更新用户密码
     */
    public function updateUserPassword(Request $request): array
    {
        $param = Validate::param([
            'origin' => 'require',
            'new' => 'require',
        ]);
        $user = $request->getCurrentUser();
        $origin = UserUser::encryptPassword($param['origin']);
        if ($origin != $user->user_password) {
            throwErrorMsg('原密码错误');
        }
        $user->user_password = $param['new'];
        $user->save();
        return [
            'code' => 0,
            'msg' => '修改成功'
        ];
    }
}
