<?php

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

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.');
    }

};
?>

<div class="space-y-6 max-w-xl">

    <flux:heading size="xl">
        Homepage Settings
    </flux:heading>

    <flux:card>

        <form wire:submit.prevent="save" class="space-y-4">

            <flux:textarea
                label="Hero Alih Aksara Text"
                wire:model.live="hero_alihaksara_text"
            />

            <flux:input
                label="Placeholder Input Alih Aksara"
                wire:model.live="hero_alihaksara_placeholderinput"
            />

            <flux:textarea
                label="Alamat"
                wire:model.live="alihaksara_address"
            />

            <flux:heading size="md">Sosial Media</flux:heading>

            <flux:input
                label="Facebook"
                wire:model.live="alihaksara_sosmed.facebook"
            />

            <flux:input
                label="Instagram"
                wire:model.live="alihaksara_sosmed.instagram"
            />

            <flux:input
                label="X (Twitter)"
                wire:model.live="alihaksara_sosmed.x"
            />

            <flux:input
                label="TikTok"
                wire:model.live="alihaksara_sosmed.tiktok"
            />

            <flux:input
                label="YouTube"
                wire:model.live="alihaksara_sosmed.youtube"
            />

            <div class="flex justify-end">
                <flux:button type="submit" variant="primary">
                    Save
                </flux:button>
            </div>

        </form>

    </flux:card>

</div>
