<?php

namespace App\Http\Controllers\Api\mini;

use App\Models\Project;
use App\Models\ProjectRole;
use App\Models\ProjectUser;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

class UserController extends BaseController
{
    protected $model;

    public function __construct()
    {
        $this->model = new User();
    }

    public function search(Request $request)
    {
        $items = $this->model->with('project_users');
        $tmp_items = collect(['keyword']);
        foreach($tmp_items as $tmp_item) {
            if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
//                $items = $items->where('name', 'like', '%' . $request->input($tmp_item) . '%')->orWhere('phone', 'like', '%' . $request->input($tmp_item) . '%');
                $items = $items->where('name', '=', $request->input($tmp_item))->orWhere('phone', '=', $request->input($tmp_item));
            }
        }

        $select_items = collect(['id']);
        foreach($select_items as $tmp_item) {
            if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
                $items = $items->where($tmp_item, '=', $request->input($tmp_item));
            }
        }

        $items = $items->limit(1)->get();

        foreach($items as $item) {
            $item->project = $item->projects->implode('name', ',');
            $item->project = $item->project ? $item->project : '暂未添加项目';
            foreach ($item->project_users as $project_user) {
                $project = Project::find($project_user->project_id);
                $project_user->project_name = $project ? $project->name : '';
                $role = ProjectRole::find($project_user->project_role_id);
                $project_user->role_name = $role ? $role->name : '';
            }
        }

        return $this->success(['msg' => '操作成功', 'data' => $items]);
    }

    public function update(Request $request)
    {
        $user = Auth::guard('mini')->user();
        $user->update($request->only('avatar', 'name', 'phone'));
        return $this->success(['data' => $user]);
    }

    public function changePassword(Request $request)
    {
        $user = Auth::guard('mini')->user();
        if(!$user) return $this->error(['msg' => '找不到用户']);
        if(Hash::check($request->input('new'), $user->password)) {
            $res = $user->update(['password' => bcrypt($user->password)]);
            if($res) return $this->success();
        }
        return $this->error(['msg' => '原密码错误']);
    }

    public function getTopRole()
    {
        $user = Auth::guard('mini')->user();

        $project_role = $user->getTopRole();
        if($project_role) {
            $project_role->rights = $project_role->getRights();
        }

        return $this->success(['data' => $project_role]);
    }

    public function getRight(Request $request)
    {
        $user = Auth::guard('mini')->user();
        $project_user = ProjectUser::where([
            'user_id' => $user->id,
            'project_id' => $request->input('id')
        ])->first();
        $right = ['managerMember' => false, 'createInner' => false, 'createOut' => false];
        if($project_user) {
            $project_role = ProjectRole::find($project_user->project_role_id);
            if($project_role) {
                $right['managerMember'] = !in_array($project_role->key, ['work', 'machine', 'assist']);
                $right['createInner'] = $project_role->key == 'machine';
                $right['createOut'] = $project_role->key == 'work';
            }
        }
        return $this->success(['data' => $right]);
    }
}
