<?php

namespace App\Livewire\Admin\Settings\General;

use Livewire\Component;
use App\Models\Admin\Settings;
use Livewire\Attributes\Validate;

class Edit extends Component
{
    public Settings $settings;

    public $logo;
    public $logo_path;
    public $favicon;
    public $favicon_path;
    public $recaptcha_site_key;
    public $recaptcha_secret_key;
    public $gtag_js_code;
    public $facebook_url;
    public $twitter_url;
    public $instagram_url;
    public $linkedin_url;
    public $youtube_url;

    public function mount()
    {
        $this->settings = Settings::first() ?? new Settings();

        $this->logo = basename($this->settings->logo);
        $this->logo_path = $this->settings->logo;
        $this->favicon = basename($this->settings->favicon);
        $this->favicon_path = $this->settings->favicon;
        $this->recaptcha_site_key = $this->settings->recaptcha_site_key;
        $this->recaptcha_secret_key = $this->settings->recaptcha_secret_key;
        $this->gtag_js_code = $this->settings->gtag_js_code;
        $this->facebook_url = $this->settings->facebook_url;
        $this->twitter_url = $this->settings->twitter_url;
        $this->instagram_url = $this->settings->instagram_url;
        $this->linkedin_url = $this->settings->linkedin_url;
        $this->youtube_url = $this->settings->youtube_url;
    }

    public function save()
    {
        Settings::updateOrCreate(
            ['id' => 1,]
,           [
                'logo' => $this->logo_path,
                'favicon' => $this->favicon_path,
                'recaptcha_site_key' => $this->recaptcha_site_key,
                'recaptcha_secret_key' => $this->recaptcha_secret_key,
                'gtag_js_code' => $this->gtag_js_code,
                'facebook_url' => $this->facebook_url,
                'twitter_url' => $this->twitter_url,
                'instagram_url' => $this->instagram_url,
                'linkedin_url' => $this->linkedin_url,
                'youtube_url' => $this->youtube_url,
            ],
        );

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

    public function render()
    {
        return view('livewire.admin.settings.general.edit', [
            'logo_path' => $this->logo_path,
            'favicon_path' => $this->favicon_path,
        ]);
    }
}
