<?php

namespace App\Http\Controllers;

use App\Models\Book;
use App\Models\Book_Discipline;
use App\Models\Discipline;
use Illuminate\Http\Request;

class SearchController extends Controller
{
    public function search(Request $request)
    {
        $key = $request->recherche;
        $book_disc = Book_Discipline::all();
        $disciplines = Discipline::all();
        if ($request->hidden == "1") {
            $result = Book::where('titre', 'like', '%' . $key . '%')->orwhere('auteur', 'like', '%' . $key . '%')->orwhere('ISBN', $key)->orderBy('titre', 'asc')->get();
            return view('admin.result', compact('result', 'key', 'book_disc', 'disciplines'));
        } else {
            $result = Book::where([
                ['titre', 'like', '%' . $key . '%'],
                ['etat', 1]
            ])
            ->orwhere([
                ['auteur', 'like', '%' . $key . '%'],
                ['etat', 1]
            ])
            ->orwhere([
                ['ISBN',  $key],
                ['etat', 1]
            ])
            ->orderBy('titre', 'asc')
            ->get();
            return view('users.result', compact('result', 'key', 'book_disc', 'disciplines'));
        }
    }
}
