<?php

namespace App\Http\Livewire\Pages\News;

use App\Models\Shippingnetnews;
use App\Models\Shippingnetnewstype;
use App\Models\Shippingnetservergroup;
use Livewire\Component;
use Livewire\WithFileUploads;

class NewsEdit extends Component
{
    use WithFileUploads;

    public $newsId, $topic, $topicColor, $category, $type, $content, $issueDate, $expireDate, $authorDate, $description;
    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($editNewsId)
    {
        $news = Shippingnetnews::findOrFail($editNewsId);
        $this->newsId = $editNewsId;
        $this->topic = $news->topic;
        $this->topicColor = $news->topiccolor;
        $this->category = $news->groupID;
        $this->type = $news->newstype;
        $this->description = $news->description;
        $this->content = $news->content;
        $this->issueDate = $news->issueDate;
        $this->expireDate = $news->expireDate;
        $this->types = Shippingnetnewstype::all()->pluck('newstype', 'id');
        $this->categories = Shippingnetservergroup::all()->pluck('groupname', 'groupID');
    }

    public function uploadImage($file)
    {


        $path = $file->store('images', 'public');
        
        return asset('storage/' . $path);
    }

    public function submitForm()
    {
        $this->validate();

        $news = Shippingnetnews::findOrFail($this->newsId);

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

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

        // Update the existing record
        $news->update([
            'topic' => $this->topic,
            'groupID' => $this->category,
            'newstype' => $this->type,
            'description' => $this->description,
            'content' => $this->content,
            'issueDate' => $this->issueDate,
            'expireDate' => $this->expireDate,
            'authorDate' => $this->authorDate ?? date("Y-m-d"),
            'topiccolor' => $this->topicColor,
            'uploadFilePathName1' => $filePath1 ?? $news->uploadFilePathName1,
            'uploadFilePathName2' => $filePath2 ?? $news->uploadFilePathName2,
            'uploadFileName1' => $filePath1 ?? $news->uploadFileName1,
            'uploadFileName2' => $filePath2 ?? $news->uploadFileName2,
        ]);

        $this->resetForm();
        $this->emit('showNewsList', 'News successfully updated.');
    }

    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()
    {
        $this->emit('showNewsList');
    }

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