<?php

namespace App\Http\Controllers;

use App\Models\Infrastructure;
use App\Models\ManifestationInteret;
use App\Models\TypeUnite;
use Illuminate\Http\Request;
use RealRashid\SweetAlert\Facades\Alert;

class ManifestationInteretController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Infra = Infrastructure::all();
        $TypeUnite = TypeUnite::all();
        return view('Admin.pages.File_ManifInteret.ManifInteret',compact('Infra','TypeUnite'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $donnees = ManifestationInteret::select(
            'manifestation_interets.id',
            'manifestation_interets.nom',
            'manifestation_interets.prenoms',
            'manifestation_interets.email',
            'manifestation_interets.telephone',
            'manifestation_interets.nbChambre',
            'manifestation_interets.nbDouche',
            'manifestation_interets.numDossier',
            'type_unites.nomType',
            'infrastructures.nomInfra'
        )
        ->leftJoin('type_unites', 'manifestation_interets.typeUnite_id', '=', 'type_unites.id')
        ->leftJoin('infrastructures', 'manifestation_interets.infra_id', '=', 'infrastructures.id')
        ->get();



        return response()->json($donnees);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {



    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\GestionInfrast$ManifestationInterets  $gestionInfrast$ManifestationInterets
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\GestionInfrast$ManifestationInterets  $gestionInfrast$ManifestationInterets
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $ManifestationInteret = ManifestationInteret::find($id);
        return response()->json([
            'status' => 200,
            'ManifestationInteret' => $ManifestationInteret,
        ]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\GestionInfrast$ManifestationInterets  $gestionInfrast$ManifestationInterets
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        //la route update a été détacher de la route ressource  (faite un php artisan route:list)
        $ManifestationInterets_id = $request->input('id');
        $ManifestationInterets = ManifestationInteret::find($ManifestationInterets_id);
        try {
            $ManifestationInterets->nom = $request->input('nom');
            $ManifestationInterets->prenoms = $request->input('prenoms');
            $ManifestationInterets->email = $request->input('email');
            $ManifestationInterets->telephone = $request->input('telephone');
            $ManifestationInterets->infra_id = $request->input('infra_id');
            $ManifestationInterets->client_id = $request->input('client_id');
            $ManifestationInterets->typeUnite_id = $request->input('typeUnite_id');
            $ManifestationInterets->nbChambre = $request->input('nbChambre');
            $ManifestationInterets->nbDouche = $request->input('nbDouche');
            $ManifestationInterets->numDossier = $request->input('numDossier');
            $ManifestationInterets->update();
            Alert::toast('Update success', 'success');
            return redirect()->route('ManifestationInteret.index');
        } catch (\Illuminate\Database\QueryException $exception) {
            Alert::toast('Veuillez rempli tous champs', 'error');
            return redirect()->back()->withInput();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\GestionInfrast$ManifestationInterets  $gestionInfrast$ManifestationInterets
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {

        $data_id = $request->input('deleteManifestationInteret');
        $data = ManifestationInteret::find($data_id);
        try {
            $data->delete();
            Alert::toast('Delete success', 'success');
        } catch (\Illuminate\Database\QueryException $exception) {
            $errorInfo = $exception->errorInfo;

            if ($errorInfo[0] === '23000' && $errorInfo[1] === 1451) {
                $affectedTables = $this->getAffectedTables($errorInfo[2]);
                $errorMessage = "Impossible de supprimer l'événement. Il est référencé dans les tableaux suivants : " . implode(", ", $affectedTables);
                Alert::toast($errorMessage, 'error');
            } else {
                Alert::toast($exception->getMessage(), 'error');
            }
        }

        return  redirect()->back()->withInput();
    }

    private function getAffectedTables($errorMessage)
    {
        preg_match_all("/`(.+?)`/", $errorMessage, $matches);
        return $matches[1];
    }
}
