<?php

namespace App\Http\Controllers\Api\mini;

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

class WorkPointController extends BaseController
{
    protected $model;

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

    //工作点位列表
    public function get(Request $request)
    {
        $items = $this->model;
        if($request->input('project_id')) {
            $items = $items->where('project_id', $request->input('project_id'))->where('status',1);
        }

        if($request->input('type') == 'drop_menu') {
            $user = Auth::guard('mini')->user();

            $project_ids = ProjectUser::where('user_id', $user->id)->pluck('project_id')->toArray();

            $items = $items->select('name as text', 'id as value')->whereIn('project_id',$project_ids)->get();
            $items = $items->prepend(collect(['text' => '所需工点', 'value' => '']));
            return $this->success(['data' => $items]);
        }
        $items = $items->get();
        return $this->success(['data' => $items]);
    }
}
