diff --git a/app/Http/Controllers/Bo/SingleOptinController.php b/app/Http/Controllers/Bo/SingleOptinController.php index abd1b70..f34b86c 100644 --- a/app/Http/Controllers/Bo/SingleOptinController.php +++ b/app/Http/Controllers/Bo/SingleOptinController.php @@ -3,8 +3,9 @@ namespace App\Http\Controllers\Bo; use App\Http\Controllers\Controller; +use App\Models\Answer; +use App\Models\Question; use App\Models\SingleOptin; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; use Illuminate\Support\Str; @@ -19,14 +20,17 @@ class SingleOptinController extends Controller public function index(Request $request): \Illuminate\Contracts\View\View { /** @var \Illuminate\Database\Eloquent\Builder $query */ - $query = SingleOptin::query(); + $query = SingleOptin::query() + ->when( + $request->filled('brand'), + fn($q) => $q->whereHas('brand', fn($q) => $q->where('id', $request->brand)) + ) + ->when( + $request->filled('answer'), + fn($q) => $q->whereHas('answers', fn($q) => $q->where('id', $request->answer)) + ); - if ($request->brand) { - $query = $query->whereHas('brand', function (Builder $q) use ($request) { - $q->where('id', $request->brand); - }); - } - if ($request->search) { + if ($request->filled('search')) { $this->searchQuery( $query, $request->search, @@ -36,16 +40,28 @@ class SingleOptinController extends Controller } $this->sortQuery($query); - $searchFields = trans('E-mail'); + $query->with('brand'); - $query->with(['brand']); - - /** @var \Illuminate\Pagination\AbstractPaginator */ $singleoptinModels = $this->paginate($query); + $searchFields = trans('E-mail'); - return view('bo.pages.singleoptins.index', compact('singleoptinModels', 'searchFields')); + if ($request->filled('brand')) { + $questionModels = Question::where('brand_id', $request->brand)->get(); + + if ($request->filled('question')) { + $answerModels = Answer::where('question_id', $request->question)->get(); + } + } + + return view('bo.pages.singleoptins.index', [ + 'singleoptinModels' => $singleoptinModels, + 'searchFields' => $searchFields, + 'questionModels' => $questionModels ?? null, + 'answerModels' => $answerModels ?? null, + ]); } + /** * Remove the specified resource from storage. * diff --git a/resources/ts/bo/bo.ts b/resources/ts/bo/bo.ts index 3b1c053..23b50f3 100644 --- a/resources/ts/bo/bo.ts +++ b/resources/ts/bo/bo.ts @@ -16,7 +16,7 @@ import "./base/common"; // CUSTOM import "./pages/customer.delete"; import "./pages/data.delete"; -import "./pages/search-bar-brand"; +import "./pages/search-bar"; import "./pages/service.index"; import "./pages/stats"; import "./pages/statsExport"; diff --git a/resources/ts/bo/pages/search-bar-brand.ts b/resources/ts/bo/pages/search-bar-brand.ts deleted file mode 100644 index 8a9ac51..0000000 --- a/resources/ts/bo/pages/search-bar-brand.ts +++ /dev/null @@ -1,11 +0,0 @@ -const brandSelect = document.getElementById("brandSelect") as HTMLSelectElement; -if (brandSelect) { - brandSelect.addEventListener("change", function () { - if (brandSelect.value !== "0") { - const url = new URL(location.href); - url.searchParams.set("brand", brandSelect.value); - url.searchParams.delete("rst"); - location.assign(url.search); - } - }); -} diff --git a/resources/ts/bo/pages/search-bar.ts b/resources/ts/bo/pages/search-bar.ts new file mode 100644 index 0000000..bffb549 --- /dev/null +++ b/resources/ts/bo/pages/search-bar.ts @@ -0,0 +1,30 @@ +enum FilterKey { + Brand = "brand", + Question = "question", + Answer = "answer", +} + +function setupFilterListener(selectId: string, paramKey: FilterKey): void { + const select = document.getElementById(selectId) as HTMLSelectElement | null; + if (!select) return; + + select.addEventListener("change", () => { + const value = select.value; + if (value !== "0") { + const url = new URL(location.href); + url.searchParams.set(paramKey, value); + url.searchParams.delete("rst"); + location.assign(url.search); + } + }); +} + +const filterMap: Record = { + brandSelect: FilterKey.Brand, + questionSelect: FilterKey.Question, + answerSelect: FilterKey.Answer, +}; + +Object.entries(filterMap).forEach(([selectId, paramKey]) => { + setupFilterListener(selectId, paramKey); +}); diff --git a/resources/views/bo/modules/search-bar-brand.blade.php b/resources/views/bo/modules/search-bar-brand.blade.php index 6fd8f31..e22d980 100644 --- a/resources/views/bo/modules/search-bar-brand.blade.php +++ b/resources/views/bo/modules/search-bar-brand.blade.php @@ -1,26 +1,72 @@ -
-
- - + + @foreach (\App\Models\Brand::all() as $brand) + + @endforeach + + + {{-- QUESTION SELECT --}} + @if (request()->routeIs('bo.single_optins.index') && $selectedBrand && $questionModels) + + @endif + + {{-- ANSWER SELECT --}} + @if ($selectedQuestion && $answerModels) + + @endif +
+
+
+ {{-- SEARCH FIELD --}} + + + + {{-- HIDDEN INPUTS --}} + + + @foreach (['brand' => $selectedBrand, 'question' => $selectedQuestion, 'answer' => $selectedAnswer] as $key => $val) + @if ($val) + + @endif @endforeach - -
🔍🠚
- - - - - @if (request()->brand) - - @endif - - + + +
+ diff --git a/resources/views/bo/modules/search-bar.blade.php b/resources/views/bo/modules/search-bar.blade.php index b70a953..3916e17 100644 --- a/resources/views/bo/modules/search-bar.blade.php +++ b/resources/views/bo/modules/search-bar.blade.php @@ -11,7 +11,7 @@
-
+
@if(request()->sort_col)