<?php

use App\Models\WebsiteSetting;
use App\Livewire\BaseVolt;

return new class extends Basevolt {

    public $hero_alihaksara_text;
    public $hero_alihaksara_placeholderinput;
    public $alihaksara_address;

    public $alihaksara_sosmed = [
        'facebook' => '',
        'instagram' => '',
        'x' => '',
        'tiktok' => '',
        'youtube' => '',
    ];

    public function mount()
    {
        $this->hero_alihaksara_text = WebsiteSetting::where('key','hero_alihaksara_text')->value('value');

        $this->hero_alihaksara_placeholderinput = WebsiteSetting::where('key','hero_alihaksara_placeholderinput')->value('value');

        $this->alihaksara_address = WebsiteSetting::where('key','alihaksara_address')->value('value');

        // Sosmed (JSON)
        $sosmed = WebsiteSetting::where('key','alihaksara_sosmed')->value('value');

        if ($sosmed) {
            $this->alihaksara_sosmed = json_decode($sosmed, true);
        }
    }

    public function save()
    {
        WebsiteSetting::updateOrCreate(
            ['key'=>'hero_alihaksara_text'],
            ['value'=>$this->hero_alihaksara_text]
        );

        WebsiteSetting::updateOrCreate(
            ['key'=>'hero_alihaksara_placeholderinput'],
            ['value'=>$this->hero_alihaksara_placeholderinput]
        );

        WebsiteSetting::updateOrCreate(
            ['key'=>'alihaksara_address'],
            ['value'=>$this->alihaksara_address]
        );

        WebsiteSetting::updateOrCreate(
            ['key'=>'alihaksara_sosmed'],
            ['value'=>json_encode($this->alihaksara_sosmed)]
        );

        session()->flash('notify',[
            'type'=>'success',
            'title'=>'Success',
            'message'=>'Homepage updated'
        ]);

        $this->success('Data updated successfully.');
    }


    protected function view($data = [])
    {
        return app('view')->file('C:\laragon\www\alihaksara-kemendikbud\storage\framework/views/livewire/views/bcde9089.blade.php', $data);
    }
};
