<?php

namespace App\Http\Controllers;
use App\Http\Requests\StoreUser as StoreRequest;
use App\Models\Admin;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use RealRashid\SweetAlert\Facades\Alert;
class Utilisateur_PolicierController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $utilisateur_policier = DB::table('users')
            ->where('role', '=', 0)
            ->orderBy('created_at', 'asc')
            ->paginate(150);
        return view('super_admin.liste_compte_policier',compact('utilisateur_policier'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */

    public function store(StoreRequest $request)
    {
        $police = new User();
        $police->email = $request->input('email');
        $police->name = $request->input('name');
        $police->password = Hash::make($request->input('password'));
        $police->save();
        Alert::success('Message',' update successfully');
        return redirect()->route('compte.index')
            ->with('modif','Utilisateur enregistrée avec succès.');

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(User $compte)
    {
        return view('super_admin.password',compact('compte'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $compte)
    {

        $police= User::find($compte);
        $police->email = $request->input('email');
        $police->name = $request->input('name');
        $police->password = Hash::make($request->input('password'));
        $police->save();
        Alert::success('Message',' update successfully');
        return redirect()->route('compte.index')
            ->with('modif','Utilisateur enregistrée avec succès.');
    }

    public function search()
    {
    $search= request()->input('search');
    $admins=  User::where('email','like' , "%$search%")
      ->orwhere('name','like' , "%$search%")
      ->paginate(6);
      return view('super_admin.SearchPolicier')->with('admins',$admins);
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

     public function destroy($id)
     {

         $admin = Admin::find($id);

         if ($admin) {
             $admin->delete();

             // Supprimer également l'utilisateur correspondant
             $user = User::find($id);

             if ($user) {
                 $user->delete();
             }
         }

         Alert::success('Message', 'Suppression effectuée avec succès');
         return back()->with('info', 'Suppression effectuée avec succès.');
     }
}
