<?php


namespace App\Http\Controllers\backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Presentation;
use App\Models\Media;

use Illuminate\Support\Facades\Storage;
use App\Http\Traits\EditorTrait;

use App\Models\temporaryFile;

class PresentationController extends Controller
{

    public function __construct(){
        return $this->middleware(['auth','verified']);
  
    }
          public function changeStatus(Request $request)
        {
            $article = Presentation::find($request->Id);
            $article->etat = $request->etat;
            $article->save();
      
            return response()->json(['success'=>'Status change successfully.']);
        }


     public function Index(){
        $articles = Presentation::get();

        return view("backend.presentation.index",compact('articles'));
     }

       public function Create(){

       $article = Presentation::all();
        if($article->isEmpty()){
            return view('backend.presentation.add');
        }else{
            abort(404);
        }
     }
     public function store(Request $request, Presentation $article) {

        
        request()->validate([
            'titre' => 'required',
            'texte' => 'required',
       ]);
       
        $article = new Presentation();
        $article->titre = $request->input('titre');
        $article->texte = $request->input('texte');
        

        $article->save();
           return redirect()->route('presentations');

    }
    public function show($id){
        $article=Presentation::find($id);

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

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

    }
    public function update(Request $request, $id) {
        request()->validate([
            'titre' => 'required',
            'texte' => 'required',
       ]);
        $article = Presentation::find($id);
        $article->titre = $request->input('titre');

        $article->texte = $request->input('texte');
        $article->details = $request->input('details');

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

     public function editImage($id) {
        $article = Presentation::find($id);
        return view('backend.presentation.extensions.image',['article'=>$article]);
    } 

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

        

       $article = Presentation::find($id);

       $tmp_file = temporaryFile::where('folder',$request->image)->first();


       request()->validate([
        'image' => 'required',
       
     ]);

       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->save();
      
        return back()->with('success', 'Image Ajouter');

    }
    
        public function deleteImage(Request $request) {

            $article = Presentation::find($request->id);
            Storage::disk('public')->delete($article->image);
            Presentation::where('image', $article->image)->update(['image' => (new \App\Models\Presentation)->defaultImage()]);
            // unlink("storage/".$presentation->image);
    
            // Presentation::select("image", $presentation->image)->delete();
    
            return back()->with("success", "Image deleted successfully.");
    
        }

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

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

            return back();
        }
        
        public function editDetails($id) {
            $article = Presentation::find($id);
            return view('backend.presentation.extensions.details',['article'=>$article]);
        } 

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

        request()->validate([
            'details' => 'required',
       ]);
       
        $article = Presentation::find($id);
        $article->details = $request->input('details');
        
        
        $article->save();
        return redirect ()->route('presentations')->with('success','Ajouté');


    }
    public function editVideo($id) {
        $article = Presentation::find($id);
        return view('backend.presentation.extensions.videos',['article'=>$article]);
    } 
      public function uploadVideo(Request $request, $id)
            {
            $this->validate($request, [
               
                'videos' => 'required|file|mimetypes:video/mp4',
            ]);
                     $article = Presentation::find($id);

            if ($request->hasFile('videos'))
             {
                $path = $request->file('videos')->store('videos');
                $article->videos = $path;
            }
            $article->save();
            
        }

        public function deleteVideo(Request $request) {

            $article = Presentation::find($request->id);
            Storage::disk('public')->delete($article->videos);
            Presentation::where('videos', $article->videos)->update(['videos' => (new \App\Models\Presentation)->defaultVideo()]);
            // unlink("storage/".$presentation->image);
    
            // Presentation::select("image", $presentation->image)->delete();
    
            return back()->with("success", "Video deleted successfully.");
    
        }
        public function test(Request $request, $id){

            $presentation = Presentation::find($request->presentationId);
            $presentation->etat = $request->etat;
            $presentation->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('');
     }
    }
}
 
   
        
         
 