<?php

namespace App\Http\Controllers;

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

class ImageController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($bienId)
    {
        // Récupérer les images du bien
        $images_bien = Image::where('bien_id',$bienId)->orderBy('num', 'ASC')->get();

        return view('images.create', compact('bienId','images_bien'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'nom' => 'mimes:jpeg,png,jpg',
        ]);

        $bienId = $request->input('bien_id');
        
        // Récupérer les images du bien
        $images_bien = Image::where('bien_id',$bienId)->orderBy('num', 'ASC')->get();

        // Récupérer le numéro de la dernière image du bien
        $last_image_bien = Image::where('bien_id',$bienId)->orderBy('num', 'DESC')->first();

        // Si le bien a moins de 10 images
        if( !is_null($images_bien) && !is_null($last_image_bien) && ($last_image_bien->num <= 9) ){
            
            $filename = $request->file('nom')->storeAs('images', $request->file('nom')->hashName(),'public');
        
            // Enrigistrer l'image dans la BDD
            $image = new Image;
            $image->num = $last_image_bien->num + 1;
            $image->commentaire = $request->input('commentaire');
            $image->bien_id = $request->input('bien_id');
            $image->nom = $filename;
            $image->save();
        
        // Si le bien n'a pas d'images
        } elseif(is_null($last_image_bien)) {

            $filename = $request->file('nom')->storeAs('images', $request->file('nom')->hashName(),'public');
        
            // Enrigistrer l'image dans la BDD
            $image = new Image;
            $image->num = 1;
            $image->commentaire = $request->input('commentaire');
            $image->bien_id = $request->input('bien_id');
            $image->nom = $filename;
            $image->save();

        // Si le bien à plus de 10 images
        } else {

        }

        return redirect()->route('images.create',$bienId);
        return view('images.create',compact('bienId','images_bien'));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Image  $image
     * @return \Illuminate\Http\Response
     */
    public function show(Image $image)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Image  $image
     * @return \Illuminate\Http\Response
     */
    public function edit(Image $image)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Image  $image
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Image $image)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Image  $image
     * @return \Illuminate\Http\Response
     */
    public function destroy(Image $image)
    {
        //
    }
}
