<?php

namespace Modules\Module\Components;

use App\Module\Module;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Modules\Module\ModuleSettings;

class SettingView extends Component
{

    public array $modules;
    public array $all;
    public $settingClass = ModuleSettings::class;

    public function rules(): array
    {
        return [
            'modules' => 'present|array',
            'modules.*' => ['string', Rule::in(Module::values())],
        ];
    }

    public function mount(): void
    {
        $this->modules = app(ModuleSettings::class)->modules;
        $this->all = Module::forSelect();
    }

    public function save(): void
    {
        app(ModuleSettings::class)->fill($this->validate())->save();
        $this->dispatch('success', 'Einstellungen gespeichert.');
    }

    public function render()
    {
        return <<<'HTML'
            <x-page::setting-layout :active="$settingClass">
                <x-slot:right>
                    <x-form::save-button form="modulesettingform"></x-form::save-button>
                </x-slot:right>
                <form id="modulesettingform" class="grow p-6 grid grid-cols-2 gap-3 items-start content-start"
                    wire:submit.prevent="save">
                    <div class="col-span-full text-gray-100 mb-3">
                        <p class="text-sm">Hier kannst du Funktionen innerhalb von Adrema (Module) aktivieren oder deaktivieren
                            und so den Funktionsumfang auf deine Bedürfnisse anpassen.</p>
                    </div>
                    @foreach ($all as $module)
                    <x-form::lever wire:model="modules" hint="lala" :value="$module['id']" name="modules" size="sm" :label="$module['name']"></x-form::lever>
                    @endforeach
                </form>
            </x-page::setting-layout>
        HTML;
    }
}
