<?php

namespace App\Http\Controllers\backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Slide;
use Illuminate\Support\Facades\Storage;
use App\Models\temporaryFile;



class SlideController extends Controller
{

    public function __construct(){
        return $this->middleware(['auth','verified']);
  
    }
    public function index()

    {
        $articles = Slide::paginate(5);
        return view('backend.slide.index',compact('articles'));
    }

    public function create(){
        return view('backend.slide.add');
    } 

    public function store( Request $request){
        request()->validate([
            'titre' => 'required',
            'image' => 'required',
       ]);
       
        
        $tmp_file = temporaryFile::where('folder',$request->image)->first();

        if($tmp_file){
         Storage::copy('image/tmp/'.$tmp_file->folder.'/'.$tmp_file->file,'image/'.$tmp_file->folder.'/'.$tmp_file->file);
            
        Slide::create( 
            [
                'titre' => $request->titre,
                'image' => $tmp_file->folder . '/' . $tmp_file->file,
                ],
            );
            Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
            $tmp_file->delete();
        }
           
        
           return redirect()->route('slides');
   
    }
    public function show($id){
        $article=Slide::find($id);

        return view('backend.slide.show',['article'=>$article]);
    }

    
    public function edit($id) {
        $article = Slide::find($id);
        return view('backend.slide.edit',['article'=>$article]);

    }
    public function update(Request $request, $id) {
       
        $article = Slide::find($id);
        $article->titre = $request->input('titre');
        $tmp_file = temporaryFile::where('folder',$request->image)->first();

        if($tmp_file){
            Storage::copy('image/tmp/'.$tmp_file->folder.'/'.$tmp_file->file,'image/'.$tmp_file->folder.'/'.$tmp_file->file);
               
            $article->update([
           
                "image" => $tmp_file->folder . '/' . $tmp_file->file
            ]);
            Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
            $tmp_file->delete();
           }
          

          $article->created_at = \Carbon\Carbon::now();
        $article->save();
        return redirect ()->route('slides')->with('success','Modifié');
    }

    public function destroy(Request $request, $id){

        $article = Slide::find($id);
        $article->delete();

        return back();
    }
    public function changeStatus(Request $request)
    {
        $article = Slide::find($request->Id);
        $article->etat = $request->etat;
        $article->save();
  
        return response()->json(['success'=>'Status change successfully.']);
    }

    
    public function fileUpload(Request $request){
   
        if($request->hasfile('image')){
         
         $image = $request->file('image');
         $file = $image->getClientOriginalName();
         $folder = uniqid('post', 'true');
         $image->storeAs('image/tmp/' . $folder,$file);
           

           TemporaryFile::create([
             'folder' => $folder,
             'file'=> $file           
             ]);
       return  $folder;   
            
           
        }
     return '';

         
}

public function deleteUpload(){

 $tmp_file = TemporaryFile::where('folder',request()->getContent())->first();
 if($tmp_file){
     Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
     $tmp_file->delete();
     return response('');
 }
}
}
