<?php

namespace App\Http\Controllers\backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Produit;
use App\Models\Categorie;
use App\Models\Media;
use App\Models\Promotion;

use App\Models\CategorieProduit;
use Illuminate\Support\Facades\Redirect;

use App\Models\temporaryFile;


use Illuminate\Support\Facades\Storage;

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

    public function Index(){

        $articles = Produit::paginate(5);

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

     public function create(){
        
        $categories = Categorie::select('nom', 'id')->oldest('nom')->get();
        

        return view('backend.produit.add',compact('categories'));

       } 
     public function store( Request $request){
        request()->validate([
            'nom' => 'required',
            'description' => 'required',
           
            'description' => '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);
               
         $article =  Produit::create( 
               [
                   'nom' => $request->nom,
                //    'category_id' => $request->input('categorie'),
                   'description' => $request->description,
                   'image' => $tmp_file->folder . '/' . $tmp_file->file,
                   ],
               );

               $article->categories()->sync((array)$request->input('categories'));

               Storage::deleteDirectory('diapo/tmp/'.$tmp_file->folder);
               $tmp_file->delete();
        
           }
           
    
           return redirect()->route('produits');
   
    }

    
    public function show($id){
       

        $article=Produit::find($id);
        $diapos=Media::where('produit_id','=',$article->id)->get();
       
        if ($article->document != null) {
            

            $size = Storage::size('document/'.$article->document); 
            $file_size = $size * 0.0009765625; 
            $sizeFile = number_format($file_size,0);
            
            $infoPath = pathinfo(public_path('document/'.$article->document));
            $extension = $infoPath['extension'];
            
            return view('backend.produit.show',compact('article','diapos','extension','sizeFile'));
        }
        
  

        return view('backend.produit.show',compact('article','diapos'));

    }

    

    
    public function edit($id) {
      
        $article = Produit::find($id);
        $categories = Categorie::select('nom', 'id')->oldest('nom')->get();
        
     
        return view('backend.produit.edit',['article'=>$article],compact('categories'));

    }
    public function update(Request $request, $id) {
        request()->validate([
            'nom' => 'required',
             'description' => 'required',
            'categories'=> 'required',
            

        
       ]);
        $article = Produit::find($id);
        $article->nom = $request->input('nom');
     

        $article->description = $request->input('description');

        $article->categories()->sync((array)$request->input('categories'));


        $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('diapo/tmp/'.$tmp_file->folder);
                $tmp_file->delete();
           }
          $article->created_at = \Carbon\Carbon::now();
        $article->save();
        return redirect ()->route('produits')->with('success','Modifié');
    }

    
    public function diapo($id) {
        $article = Produit::find($id);
        return view('backend.produit.extension.diapo',['article'=>$article]);
    } 

    public function updateDiapo(Request $request, $id) {
        
        request()->validate([
            
            'diapo'=> 'required',

        
       ]);
     
        $tmp_file = temporaryFile::where('folder',$request->diapo)->first();
    
        
        $article = Produit::find($id);
         
       
  
          if($tmp_file){
            
            Storage::copy('diapo/tmp/'.$tmp_file->folder.'/'.$tmp_file->file,'diapo/'.$tmp_file->folder.'/'.$tmp_file->file);
           
                Media::create( 
                [
                    'produit_id' => $article->id,
                    'diapo' => $tmp_file->folder . '/' . $tmp_file->file,
                    ],
                );
                Storage::deleteDirectory('diapo/tmp/'.$tmp_file->folder);
                $tmp_file->delete();

            
        return back()->with('success', 'Image ajouter');
          
    }
    $article->save();

}

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

    $diapo = Media::find($id);
    $diapo->delete();

    return back();
}

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


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

    // return back();

    $promo = Promotion::where('produit_id', $id)->count();
        if($promo > 0){
            return redirect ('idara/produits')
                    ->with('message', 'Produit existe dans une promotion');
        }
        else{
            $article = Produit::find($id);

            $article->delete();
            return Redirect::to('idara/produits')
                        ->with('message', 'produit supprimée');
        }
        
        return back();
}
    

public function fileUploadProduit(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 fileDeleteProduit(){

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

public function fileUpload(Request $request){
   
           if($request->hasfile('diapo')){
            
            $diapo = $request->file('diapo');
            $file = $diapo->getClientOriginalName();
            $folder = uniqid('post', 'true');
            $diapo->storeAs('diapo/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('diapo/tmp/'.$tmp_file->folder);
        $tmp_file->delete();
        return response('');
    }
}
public function changeStatus(Request $request)
{
    $article = Produit::find($request->Id);
    $article->etat = $request->etat;
    $article->save();

    return response()->json(['success'=>'Status change successfully.']);
}

public function homeStatus(Request $request)
{
    $article = Produit::find($request->Id);
    $article->home = $request->home;
    $article->save();

    return response()->json(['success'=>'Status change successfully.']);
}


//doc

public function editDoc($id) {
    $article = Produit::find($id);
    return view('backend.produit.extension.document',['article'=>$article]);
} 

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

    request()->validate([
            
        'document'=> 'required',

    
   ]);

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


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

    if($tmp_file){
     Storage::copy('document/tmp/'.$tmp_file->folder.'/'.$tmp_file->file,'document/'.$tmp_file->folder.'/'.$tmp_file->file);
   
         $article->update([
        
            "document" => $tmp_file->folder . '/' . $tmp_file->file
          ]);
          Storage::deleteDirectory('document/tmp/'.$tmp_file->folder);
              $tmp_file->delete();
              
    }
  
    $article->save();
   
     return back()->with('success', 'Document Ajouter');

 }

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

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

     
}

public function docUploadDelete(){

$tmp_file = TemporaryFile::where('folder',request()->getContent())->first();
if($tmp_file){
 Storage::deleteDirectory('document/tmp/'.$tmp_file->folder);
 $tmp_file->delete();
 return response('');
}
}
public function downloadDoc(Produit $produit)
{   
    
   
    return  Storage::download('document/'.$produit->document);
     
     
    
}
  


}