<?php

namespace App\Http\Livewire\Pages\News;

use App\Models\Shippingnetnews;
use App\Models\Shippingnetnewstype;
use App\Models\Shippingnetservergroup;
use Livewire\Component;
use App\Models\User;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Hash;
use Str;

class NewsCreate extends Component
{

    use WithFileUploads;

    public $topic, $topicColor, $category, $type, $content, $issueDate, $expireDate,  $authorDate, $description;
    public $image ,$imageTmp;
    public $uploadFilePathName1;
    public $uploadFilePathName2;
    public $categories = [], $types = [];
    protected $rules = [
        'topic' => 'required|string|max:255',
        'description' => 'nullable|string',
        'content' => 'nullable|string',
        'issueDate' => 'nullable|date',
        'expireDate' => 'nullable|date',
        'topicColor' => 'nullable|string',
        'uploadFilePathName1' => 'nullable|file|max:10240', // 10MB Max
        'uploadFilePathName2' => 'nullable|file|max:10240', // 10MB Max
    ];
    public function mount()
    {
        $this->types = Shippingnetnewstype::all()->pluck('newstype', 'id');
        $this->categories = Shippingnetservergroup::all()->pluck('groupname', 'groupID');
    }
    // public function updatedContent($value)
    // {
    //     $this->content = $value;
    // }

    public function uploadImage()
    {
        $this->validate([
            'imageTmp' => 'image|max:2048', // ขนาดไฟล์สูงสุด 2MB
        ]);

        $imagePath = $this->imageTmp->store('livewire-tmp', 'public');
        $uploadedImageUrl = asset('storage/' . $imagePath); // สร้าง URL

        // ส่ง URL กลับไปยัง JavaScript
        $this->emit('imageUploaded', $uploadedImageUrl);
    }

    public function submitForm()
    {

        $this->validate();
        if ($this->uploadFilePathName1) {
            $filePath1 = $this->uploadFilePathName1->store('runtime/news', 'public');
        }

        if ($this->uploadFilePathName2) {
            $filePath2 = $this->uploadFilePathName2->store('runtime/news', 'public');
        }

        Shippingnetnews::create([
            'topic' => $this->topic,
            'groupID' => $this->category,
            'newstype' => $this->type,
            'description' => $this->description,
            'content' => $this->content, // Quill Editor HTML content
            'issueDate' => $this->issueDate,
            'expireDate' => $this->expireDate,
            'authorDate' => date("Y-m-d"),
            'topiccolor' => $this->topicColor,
            'uploadFilePathName1' => $filePath1 ?? '',
            'uploadFilePathName2' => $filePath2 ?? '',
            'uploadFileName1' => $filePath1 ?? '',
            'uploadFileName2' => $filePath2 ?? '',
        ]);

        $this->resetForm();

        if (auth()->guest()) {
            return redirect()->route('login');
        }
        $this->emit('showNewsList', 'News successfully created.');
    }
    public function removeFile($fileName)
    {
        $this->$fileName = null;
    }
    public function resetForm()
    {
        $this->reset([
            'topic',
            'category',
            'type',
            'description',
            'content',
            'issueDate',
            'expireDate',
            'authorDate',
            'topicColor',
            'uploadFilePathName1',
            'uploadFilePathName2'
        ]);
    }
    public function goBack()
    {
        if (auth()->guest()) {
            return redirect()->route('login');
        }
        $this->emit('showNewsList');
    }

    public function render()
    {
        return view('livewire.pages.news.news-create');
    }
}
