<?php

namespace App\Http\Controllers\backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Categorie;
use App\Models\CategorieProduit;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Redirect;

use App\Models\temporaryFile;


class CategorieController extends Controller
{
    public function __construct(){
        return $this->middleware(['auth','verified']);
  
    }
    
    public function Index(){
        $articles = Categorie::paginate(5);

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


     public function create(){
        return view('backend.category.add');
       } 
     public function store( Request $request){
   
        request()->validate([
            'nom' => '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);
               
           Categorie::create( 
               [
                   'nom' => $request->nom,
                   'image' => $tmp_file->folder . '/' . $tmp_file->file,
                   ],
               );
               Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
               $tmp_file->delete();
        
           return redirect()->route('categories');
   
    }
}
    
    public function show($id){
        $article=Categorie::find($id);

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

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

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

        $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('categories')->with('success','Modifié');
    }

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

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

        // Produit::whereCategoryId($id)->update(['category_id' => null]);
        $products = Categorie::find($id)->produits;
        
        if($products->isNotEmpty()){
            return redirect ()->route('categories')
                    ->with('message', 'Produit existe dans cette categorie');
        }
        else{
            $category= Categorie::find($id);
            $category->delete();
            return Redirect::to('/idara/categories')
                        ->with('message', 'Categorie supprimée');
        }
        
        return back();
    }


    public function changeStatus(Request $request)
    {
        $article = Categorie::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('');
 }
}

}
