<?php

namespace App\Livewire;

use App\Models\AdRequest;
use Filament\Actions\Concerns\HasForm;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\ViewField;
use Filament\Forms\Components\Wizard;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form as FilamentForm;
use Filament\Forms\Get;
use Illuminate\Support\HtmlString;
use Livewire\Component;
use Livewire\Livewire;

class Form extends Component implements HasForms
{
    use InteractsWithForms;

    public ?array $data = [];
    public bool $canContinue = true;
    public bool $finishedForm = false;

    public function mount(): void
    {
        $this->form->fill();
    }

    public function form(FilamentForm $form): FilamentForm
    {
        return $form
            ->schema([
                Wizard::make([
                    Wizard\Step::make('what')
                        ->icon('heroicon-o-globe-alt')
                        ->description('Waar wilt u voor adverteren?')
                        ->schema([
                            Radio::make('what')->options([
                                'personel' => 'Personeel werven',
                                'jubileum' => 'Jubileum',
                                'easter' => 'Pasen',
                                'bbq' => 'Barbecue seizoen',
                                'christmas' => 'Kerst',
                            ])->label('Waar wilt u voor adverteren?')
                            ->required()
                        ])->label('Waarvoor?'),
                    Wizard\Step::make('accounts')
                        ->icon('heroicon-o-link')
                        ->description('Zijn uw Social media kanalen al gekoppeld met Uw Slager Social?')
                        ->schema([
                            Radio::make('accounts')
                                ->options([
                                    'yes' => 'Ja',
                                    'no' => 'Nee',
                                    'unknown' => 'Weet ik niet',
                                ])
                                ->reactive()
                                ->required()
                                ->label('Zijn uw Social media kanalen al gekoppeld met Uw Slager Social?'),
                            TextInput::make('accounts->facebook')
                                ->label('Naam van uw facebook pagina')
                                ->required()
                                ->visible(fn(Get $get) => $get('accounts') === 'no'),
                            TextInput::make('accounts->instagram')
                                ->label('Naam van uw Instagram pagina (optioneel)')
                                ->visible(fn(Get $get) => $get('accounts') === 'no'),
                            Placeholder::make('instagram')
                                ->label('')
                                ->content('Neem contact op Uw Slager, wij gaan kijken of u gekoppeld bent om te adverteren.')
                                ->visible(fn(Get $get) => $get('accounts') === 'unknown'),
                        ])->label('Accounts'),
                    Wizard\Step::make('info')
                        ->icon('heroicon-o-information-circle')
                        ->description('Informatie voor advertentie')
                        ->schema([
                            TextInput::make('name')
                                ->label('Naam slagerij')
                                ->required(),
                            DatePicker::make('start_date')
                                ->label('Startdatum')
                                ->required(),
                            DatePicker::make('end_date')
                                ->label('Einddatum')
                                ->required(),
                            Select::make('budget')
                                ->options([
                                    '100' => '€100',
                                    '150' => '€150',
                                    '200' => '€200',
                                ])
                                ->reactive()
                                ->label('Budget')
                                ->required(),
                            FileUpload::make('image')
                                ->label('Afbeelding')
                                ->image()
                                ->previewable()
                                ->required(),
                            Select::make('area')
                                ->label('Gewenst advertentiegebied')
                                ->options([
                                    'winkel+5km' => 'Winkel + 5km',
                                    'winkel+10km' => 'Winkel + 10km',
                                    'winkel+15km' => 'Winkel + 15km',
                                ])
                                ->required(),
                            TextInput::make('url')
                                ->label('Link landingspagina')
                                ->helperText('Bijvoorbeeld uwslagerpietje.nl/barbeque')
                                ->required(),
                            CheckboxList::make('channels')
                                ->options([
                                    'facebook' => 'Facebook',
                                    'instagram' => 'Instagram',
                                ])->label('Mijn slagerij heeft een account op')
                                ->required(),
                        ])->label('Informatie'),
                    Wizard\Step::make('summary')
                        ->label('Samenvatting')
                        ->icon('heroicon-o-clipboard-document-check')
                        ->description('Controleer uw gegevens')
                        ->schema([
                            ViewField::make('summary')
                                ->view('livewire.form-summary')
                                ->reactive()
                        ])
                ])->submitAction(new HtmlString(<<<HTML
        <button class="px-4 py-3 bg-green-600 text-white font-bold rounded-md" type="submit">
            Accepteren
        </button>
HTML))
            ])
            ->statePath('data');
    }

    public function create(): void
    {
        AdRequest::query()->create($this->form->getState());

        $this->finishedForm = true;
    }

    public function resetForm() : void
    {
        $this->finishedForm = false;
        $this->form->fill([]);
    }
    public function render()
    {
        return view('livewire.form');
    }
}
