<?php

namespace App\Livewire\Admin\Settings\Seo;

use Livewire\Component;
use App\Models\Admin\Language;
use App\Models\Admin\SeoSettings;

class Edit extends Component
{
    public $selectedLanguage;

    public $title;
    public $description;
    public $keywords;

    public function mount()
    {
        $this->selectedLanguage = Language::where('default', 1)->first()->short;
        $this->title = SeoSettings::where('id', 1)->first()->getTranslation('title', $this->selectedLanguage);
        $this->description = SeoSettings::where('id', 1)->first()->getTranslation('description', $this->selectedLanguage);
        $this->keywords = SeoSettings::where('id', 1)->first()->getTranslation('keywords', $this->selectedLanguage);
    }

    public function updatedSelectedLanguage($value)
    {
        $this->title = SeoSettings::where('id', 1)->first()->getTranslation('title', $value);
        $this->description = SeoSettings::where('id', 1)->first()->getTranslation('description', $value);
        $this->keywords = SeoSettings::where('id', 1)->first()->getTranslation('keywords', $value);
    }

    public function save()
{
    $seoSettings = SeoSettings::firstOrNew(['id' => 1]);

    $seoSettings->setTranslation('title', $this->selectedLanguage, $this->title);
    $seoSettings->setTranslation('description', $this->selectedLanguage, $this->description);
    $seoSettings->setTranslation('keywords', $this->selectedLanguage, $this->keywords);

    $seoSettings->save();

    $this->dispatch('showAlert', [
        'title' => 'Başarılı!', 'text' => 'Ayarlar güncellendi.', 'icon' => 'success'
    ]);
}

    public function render()
    {
        return view('livewire.admin.settings.seo.edit');
    }
}
