<?php

namespace App\Livewire\Admin;

use Livewire\Component;
use Livewire\WithFileUploads;
use App\Models\Slide;
use Illuminate\Support\Facades\File;

class Slides extends Component
{
    use WithFileUploads;


    public $isUpdateSlideMode = false;
    public $slide_id, $slide_heading, $slide_link, $slide_image, $slide_status = true;
    public $selected_slide_image = null;

    protected $listeners = [
        'updateSlidesOrdering',
        'deleteSlideAction'
    ];

    public function updatedSlideImage() {
        if($this->slide_image) {
            $this->selected_slide_image = $this->slide_image->temporaryUrl();
        }
    }

    public function addSlide() {
        $this->slide_id = null;
        $this->slide_heading = null;
        $this->slide_link = null;
        $this->selected_slide_image = null;
        $this->slide_status = true;
        $this->isUpdateSlideMode = false;
        $this->showSlidemodalForm();
    }

    public function showSlideModalForm() {
        $this->resetErrorBag();
        $this->dispatch('showSlideModalForm');
    }

    public function hideSlideModalForm() {
        $this->dispatch('hideSlideModalForm');
        $this->isUpdateSlideMode = false;
        $this->slide_id = $this->slide_heading = $this->slide_link = $this->slide_image = null;
        $this->slide_status = true;
    }

    public function createSlide() {
        $this->validate([
            'slide_heading' => 'required',
            'slide_link' => 'nullable|url',
            'slide_image' => 'required'
        ], [
            'slide_heading.required' => 'Der Text wird benötigt',
            'slide_link.url' => 'Gebe einen gültigen Link ein',
            'slide_image.required' => 'Bild wird benötigt',
        ]);

        $path = "slides/";
        $file = $this->slide_image;
        $filename = "SLD_".date("YmdHis", time()).'.'.$file->getClientOriginalExtension();

        $upload = $file->storeAs($path, $filename, 'slides_uploads');

        if(!$upload) {
            $this->dispatch('showToastr', ['type' => 'error', 'message' => 'Fehler beim Hochladen von Image']);
        } else {
            $slide = new Slide();
            $slide->image = $filename;
            $slide->heading = $this->slide_heading;
            $slide->link = $this->slide_link;
            $slide->status = $this->slide_status == true ? 1 : 0;
            $saved = $slide->save();

            if($saved) {
                $this->hideSlideModalForm();
                $this->dispatch('showToastr', ['type' => 'success', 'message' => 'Slider wurde erstellt']);
            } else {
                $this->dispatch('showToastr', ['type' => 'error', 'message' => 'Fehler beim erstellen von Slider']);
            }
        }
  
    }

    public function editSlide($id) {
        $slide = Slide::findOrFail($id);
        $this->slide_id = $slide->id;
        $this->slide_heading = $slide->heading;
        $this->slide_link = $slide->link;
        $this->slide_status = $slide->status == 1 ? true : false;
        $this->slide_image = null;
        $this->selected_slide_image = "/images/slides/".$slide->image;
        $this->isUpdateSlideMode = true;
        $this->showSlidemodalForm();
    }


    public function updateSlide() {
        $slide = Slide::findOrFail($this->slide_id);

        $this->validate([
            'slide_heading' => 'required',
            'slide_link' => 'nullable|url',
            'slide_image' => 'nullable'
        ], [
            'slide_heading.required' => 'Der Text wird benötigt',
            'slide_link.url' => 'Gebe einen gültigen Link ein',
        ]);

        $slide_image_name = $slide->image;

        if($this->slide_image) {
            $path = 'slides/';
            $file = $this->slide_image;
            $filename = 'SLD_'.date('YmdHis', time()).'.'.
            $file->getClientOriginalExtension();

            $upload = $file->storeAs($path, $filename, 'slides_uploads');

            if(!$upload) {
                $this->dispatch('showToastr', ['type' => 'error', 'message' => 'Fehler beim Hochladen von Image']);
            } else {
                $slide_path = 'images/'.$path;
                $old_slide_image = $slide->image;

                if($old_slide_image != ''  && File::exists(public_path($slide_path.$old_slide_image))) {
                    File::delete(public_path($slide_path.$old_slide_image));
                }

                $slide_image_name = $filename;
            }
        }

        $slide->image = $slide_image_name;
        $slide->heading = $this->slide_heading;
        $slide->link = $this->slide_link;
        $slide->status = $this->slide_status == true ? 1 : 0;
        $saved = $slide->save(); 

        if($saved) {
            $this->hideSlideModalForm();
            $this->dispatch('showToastr', ['type' => 'success', 'message' => 'Slider wurde bearbeitet']);
        } else {
            $this->dispatch('showToastr', ['type' => 'error', 'message' => 'Fehler beim bearbeiten von Slider']);
        }
    }

    public function updateSlidesOrdering($positions) {
        foreach($positions as $position) {
            $index = $position[0];
            $newPosition = $position[1];
            Slide::where('id', $index)->update([
                'ordering' => $newPosition
            ]);
            $this->dispatch("showToastr", ['type' => "success", "message" => "Slides wurden neu angeordnet"]);
        }
    }

    public function deleteSlideAction($id) {
        $slide = Slide::findOrFail($id);
        $path = "slides/";
        $slide_path = "images/".$path;
        $slide_image = $slide->image;

        if($slide_image != '' && File::exists(public_path($slide_path.$slide_image))) {
            File::delete(public_path($slide_path.$slide_image));
        }

        $delete = $slide->delete();

        if($delete) {
            $this->dispatch("showToastr", ['type' => "success", "message" => "Slide wurden gelöscht"]);
        } else {
            $this->dispatch("showToastr", ['type' => "error", "message" => "Slide konnte nicht gelöscht werden"]);
        }


    }

    public function render()
    {
        return view('livewire.admin.slides', [
            'slides' => Slide::orderBy('ordering', 'asc')->get()
        ]);
    }
}
