<?php

namespace App\Http\Controllers\V1;


use App\Models\User;
use App\Models\UserFeedback;
use App\Models\UserInfo;
use App\Services\Api\SortService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    public function __construct()
    {
        $this->user = auth('api')->user();
        $this->userId = $this->user ? $this->user->id : 0;

        //如果用户被删除，会自动退出登录
        if (!empty($this->user->deleted_at)) {
            $this->user->online = 0;
            $this->user->save();
            auth('api')->logout();
        }
    }

    //问题反馈
    public function feedback(Request $request)
    {
        $content  = $request->input('content');
        $file_url = $request->input('file_url', '');

        $validator = Validator::make($request->all(), [
            'content' => 'required|min:2|max:200',
        ]);
        if($validator->fails()){
            return $this->error($validator->errors()->first());
        }

        $feedback = new UserFeedback();
        $feedback->user_id = $this->userId;
        $feedback->content = $content;
        $feedback->file = !empty($file_url) ? json_encode($file_url) : '';
        $feedback->save();

        return $this->success();
    }

    //按照首字母进行分组排序，类似手机通讯录(注意：get()后面直接 ->toArray()在数据为空的时候导致报错)
    public function groupByInitial(Request $request)
    {
        $keyword = $request->input('keyword', '');

        $list = User::query()
            ->select('id', 'name', 'avatar')
            ->when($keyword, function ($query, $keyword){
                $query->where('name', 'like', '%'.$keyword.'%');
            })
            ->whereNotNull('name')
            ->whereNull('deleted_at')
            ->get();

        if($list->isNotEmpty()){
            $list = $list->toArray();
            $list = SortService::getResultList($list);
        }else{
            $list = [];
        }

        return $this->success($list);
    }

    public function info()
    {
        $user = auth('api')->user();
        $user = User::with(['account'])->where('id', $user->id)->first();

        return $this->success($user);
    }

    public function update(Request $request)
    {
        try
        {
            $req = $request->all();
            $app = $this->getUniFactory(\user()->info->platform);
            /* @var User $user*/
            $user = User::with(['info'])->find(\user()->id);
            $res = $app->login($req['code']);

            $user->remember_token = $res['session_key'];
            $decryptedData = $app->decryptData($user->remember_token, $req['iv'], $req['encryptedData']);


            if (isset($decryptedData['nickName'])) {
                $user->nickname = $decryptedData['nickName'];
                $user->avatar = $decryptedData['avatarUrl'];
            }else{
                $user->mobile = $decryptedData['phoneNumber'];
            }
            $user->save();
            return $this->success($user);
        }catch (\Exception $e){
            return $this->error($e->getMessage());
        }
    }

    // h5 绑定
    public function h5Bind()
    {

    }

    // h5 解绑
    public function h5ReBind()
    {

    }

    // 微信小程序 绑定
    public function miniProgramBind()
    {

    }

    // 微信小程序 解绑
    public function miniProgramReBind()
    {

    }

    // 手机号 绑定
    public function smsBind()
    {

    }

    // 手机号 解绑
    public function smsReBind()
    {

    }





}
