<?php

namespace App\Http\Controllers;

use App\Models\Artist;
use Illuminate\Http\Request;

class ArtistController extends Controller
{

    public function search(Request $request)
    {
        $query = Artist::query();

        if ($request->filled('selected')) {
            // 取得已選擇的項目（初始化時用）
            return $query->whereIn('id', (array) $request->input('selected'))
                        ->get(['id', 'name']);
        }

        if ($request->filled('search')) {
            // 關鍵字搜尋（選單輸入文字時用）
            $search = $request->input('search');
            $query->where('name', 'like', "%{$search}%");
        }

        return $query->limit(10)->get(['id', 'name']);
    }

}