<?php

namespace App\Http\Controllers\Api\mini;

use App\Models\Project;
use App\Models\ProjectUser;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;

class ProjectController extends BaseController
{
    protected $model;

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

    public function addUser(Request $request)
    {
        $item = ProjectUser::where([
            'project_id' => $request->input('project_id'),
            'user_id' => $request->input('user_id'),
//            'project_role_id' => $request->input('project_role_id')
        ])->first();
        if($item) return $this->error('该成员已添加');
        $res = ProjectUser::create([
            'project_id' => $request->input('project_id'),
            'user_id' => $request->input('user_id'),
            'project_role_id' => $request->input('project_role_id')
        ]);
        if(!$res) return $this->error('添加失败');
        return $this->success('操作成功');
    }

    public function updateUser(Request $request)
    {
        $res = ProjectUser::where('id', '=', $request->input('id'))->update([
            'project_id' => $request->input('project_id'),
            'user_id' => $request->input('user_id'),
            'project_role_id' => $request->input('project_role_id')
        ]);
        if(!$res) return $this->error('操作失败');
        return $this->success('操作成功');
    }

    public function create(Request $request)
    {
        if(empty($request->input('name'))) return $this->error(['msg' => '项目名称不能为空']);
        if($item = $this->model->where('name', $request->input('name'))->first()) return $this->error(['msg' => '项目名称已存在']);
        $user = Auth::guard('mini')->user();
        $res = $this->model->create([
            'name' => $request->input('name'),
            'active' => 2,
            'user_id' => $user->id
        ]);
        if(!$res) return $this->error(['msg' => '创建失败']);
        $res->createDefaultUser();
        return $this->success(['msg' => '创建成功']);
    }

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

        $items = $user->projects()->where('active', 1);
        $tmp_items = collect(['name']);
        foreach($tmp_items as $tmp_item) {
            if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
                $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
            }
        }

        $items = $items->paginate();

        foreach($items as $item) {
            $item->date = substr($item->created_at, 0, 10);
            $item->manager = $item->getManager();
        }

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

    public function getAll(Request $request)
    {
        if($request->input('type') == 'drop_menu') {
            $items = $this->model->select('name as text', 'id as value')->get();
            $items = $items->prepend(collect(['text' => '请选择项目', 'value' => '']));
            return $this->success(['data' => $items]);
        }
        $items = $this->model;
        if($request->input('user_id')) {
            $project_ids = ProjectUser::where('user_id', $request->input('user_id'))->pluck('project_id');
            if(count($project_ids) > 0) $items = $items->whereNotIn('id', $project_ids);
        }
        $items = $items->get();
        return $this->success(['msg' => '创建成功', 'data' => $items]);
    }

    public function detail(Request $request)
    {
        $project = $this->model->find($request->input('id'));
        $project->role = $project->getUserProjectRole();
        return $this->success(['msg' => '操作成功', 'data' => $project]);
    }

    public function getUsers(Request $request)
    {
//        $list = ProjectUser::where('project_id', $request->input('id'))->join('users', 'project_users.user_id', '=', 'users.id')->join('project_roles', 'project_users.project_role_id', '=', 'project_roles.id')->select('project_users.*', 'users.avatar', 'users.name as user', 'project_roles.name as role_name');
        $list = ProjectUser::where('project_id', $request->input('id'))->with(['projectRole', 'user']);
        if($request->has('keyword')) {
            $keyword = '%' . $request->input('keyword') . '%';
            $list = $list->whereHas('user', function (Builder $query) use($keyword) {
                $query->where('phone', 'like', $keyword)->orWhere('name', 'like', $keyword);
            });
        }
        $list = $list->get();
        return $this->success(['msg' => '操作成功', 'data' => $list]);
    }

    public function deleteUser(Request $request)
    {
        $res = ProjectUser::where('id', '=', $request->input('id'))->delete();
        if($res) return $this->success(['msg' => '操作成功']);
        return $this->error();
    }
}
