<?php

namespace App\Livewire\Admin\Cms;

use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\WithFileUploads;

class MediaUploader extends Component
{
    use WithFileUploads;

    public string $field = '';

    public string $accept = 'image/*';

    public string $disk = 'public';

    public string $directory = 'cms/uploads';

    #[Validate('file|max:10240')]
    public $file;

    public function updatedFile(): void
    {
        $this->validate();

        $path = $this->file->store($this->directory, $this->disk);

        $this->dispatch('media-uploaded', field: $this->field, path: '/storage/'.$path);

        $this->file = null;
    }

    public function render()
    {
        return view('livewire.admin.cms.media-uploader');
    }
}
