<?php

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

use Illuminate\Support\Facades\Notification;

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

  }
  

    public function Index(){

        $articles = Promotion::paginate(5);


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

  

     public function create(){
        $produits = Produit::select('nom', 'id')->oldest('nom')->get();
        $produit = Promotion::with('produits')->first();

        return view('backend.promotion.add',compact('produits','produit'));
     }
     public function store(Request $request) {

         
      request()->validate([
            
         'titre'=> 'required',
         'produits'=> 'required',
         
         'description'=> 'required',
         
     
    ]);
        $article = new Promotion();
        $article->titre = $request->input('titre');
        $article->produit_id = $request->input('produits');
        $article->debut = $request->input('debut');
        $article->fin = $request->input('fin');


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

        $article->save();

        
      
           return redirect()->route('promotions');

    }
      
     public function createPack(){
        
        return view('backend.promotion.Pack');

        
     }
     public function storePack(Request $request) {
      request()->validate([
            
         'titre'=> 'required',
         
         'description'=> 'required',
         
         'photo'=> '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);
             
          Promotion::create( 
             [
               'titre' => $request->input('titre'),
     
             'debut' => $request->input('debut'),
               'fin' => $request->input('fin'),
         
         
             'description' => $request->input('description'),
               'remise' => $request->input('remise'),
               'photo' => $tmp_file->folder . '/' . $tmp_file->file,
               
                 ],
             );
             Storage::deleteDirectory('image/tmp/'.$tmp_file->folder);
             $tmp_file->delete();
      
         return redirect()->route('promotions');
 
  }

  }

            public function show($id){
               $article=Promotion::find($id);
               $articles = Promotion::with('produits')->get();

               return view('backend.promotion.show',compact('article','articles'));
            }
            public function edit($id) {
               $article = Promotion::find($id);
               $produits = Produit::select('nom', 'id')->oldest('nom')->get();

               return view('backend.promotion.edit',['article'=>$article],compact('produits'));

            }

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

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

               $article->titre = $request->input('titre');
               $article->produit_id = $request->input('produits');
               $article->debut = $request->input('debut');
               $article->fin = $request->input('fin');


               $article->description = $request->input('description');
               $article->remise = $request->input('remise');
               
               $article->created_at = \Carbon\Carbon::now();

               $article->save();
                  return redirect()->route('promotions')->with('success','Modifié');

            }
            public function editPack($id) {
               $article = Promotion::find($id);
             
               return view('backend.promotion.editPack',['article'=>$article]);

            }
            public function updatePack(Request $request,$id) {

               $tmp_file = temporaryFile::where('folder',$request->image)->first();
               $article = Promotion::find($id);
               $article->titre = $request->input('titre');
           
               $article->debut = $request->input('debut');
               $article->fin = $request->input('fin');


               $article->description = $request->input('description');
               $article->remise = $request->input('remise');
                 
               if($tmp_file){
                   Storage::copy('image/tmp/'.$tmp_file->folder.'/'.$tmp_file->file,'image/'.$tmp_file->folder.'/'.$tmp_file->file);
                      
                   $article->update( 
                      [
                        'photo' => $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('promotions')->with('success','modifié');
         
           }

     public function DeleteAuto(){
        
        
          $stale_posts = Promotion::where('fin', '<',\Carbon\Carbon::now())->get();

          foreach ($stale_posts as $post) {
            $post->delete();
        }
     
     }


     public function fileUploadPromotion(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 deleteUploadPromotion(){

$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 changeStatus(Request $request)
    {
        $article = Promotion::find($request->Id);
        $article->etat = $request->etat;
        $article->save();
  
        return response()->json(['success'=>'Status change successfully.']);
    }


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


      $article = Promotion::find($id);
      $article->delete();
  
      return back();
  }

}
