<?php

namespace App\Http\Controllers\backend;

use App\Http\Controllers\Controller;
use App\Models\Logiciel;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Redirect;

use App\Models\temporaryFile;

use Illuminate\Http\Request;

class LogicielController extends Controller
{
    public function index()
    {
        $articles = Logiciel::paginate(15);

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

    public function create()
    {

        return view("backend.logiciel.create");
    }

    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);

            if ($request->filled('titre_seo')) {
                Logiciel::create(
                    [
                        'nom' => $request->nom,
                        'description' => $request->description,
                        'prix' => $request->prix,
                        'prix_etat' => $request->input('prix_etat', '0'),
                        'image' => $tmp_file->folder . '/' . $tmp_file->file,
                        'mot_cle' =>  $request->mot_cle,
                        'resume_seo' =>  $request->resume_seo,
                        'titre_seo' => $request->titre_seo,


                    ],
                );
            } else {

                Logiciel::create(
                    [
                        'nom' => $request->nom,
                        'description' => $request->description,
                        'prix' => $request->prix,
                        'prix_etat' => $request->input('prix_etat', '0'),
                        'image' => $tmp_file->folder . '/' . $tmp_file->file,
                        'mot_cle' =>  $request->mot_cle,
                        'titre_seo' => $request->nom,
                        'resume_seo' =>  $request->description,
                    ],
                );
            }
            Storage::deleteDirectory('image/tmp/' . $tmp_file->folder);
            $tmp_file->delete();

            return redirect()->route('logiciel.index');
        }
    }

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

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

    public function changeStatus(Request $request)
    {
        $article = Logiciel::find($request->Id);
        $article->etat = $request->etat;
        $article->save();

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

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


        $contact = Logiciel::find($id);
        $contact->delete();

        return redirect()->route('logiciel.index');;
    }

    public function edit($id)
    {

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

        return view('backend.logiciel.edit', ['article' => $article]);
    }
    public function update(Request $request, $id)
    {
        request()->validate([
            'nom' => 'required',
            'description' => 'required',
            
        ]);
        $article = Logiciel::find($id);
        $article->nom = $request->input('nom');
        $article->prix = $request->input('prix');

        $article->prix_etat = $request->input('prix_etat', '0');
        // dd(  $article->prix_etat);
        $article->description = $request->input('description');


        if ($request->filled('titre_seo')) {
            $article->titre_seo = $request->input('titre_seo');
        } else {
            $article->titre_seo = $request->input('nom');
        }
        if ($request->filled('resume_seo')) {
            $article->resume_seo = $request->input('resume_seo');
        } else {
            $article->resume_seo = strip_tags($request->input('description'));
        }
        $article->mot_cle = $request->input('mot_cle');



        $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->save();
        return redirect()->route('logiciel.index')->with('success', 'Modifié');
    }


    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, 'public');


            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('');
        }
    }
}
