<?php

namespace ViartasThemes\FlorialLanding\Forms;

use ViartasBuilders\Forms\Field;
use ViartasBuilders\Forms\Form;
use ViartasBuilders\Forms\Group;
use ViartasShop\Cart\Models\Accounting\Country;

class ContactForm extends Form
{
    /**
     * @return array
     */
    protected function formOptions(): array
    {
        return [
            '_method' => 'POST',
            'id' => 'contact-form',
            'class' => 'backend-form-validator',
            'action' => 'create',
            'routes' => [
                'create' => [
                    'route' => 'web.main-page.index',
                    'routeAttributes' => [],
                ],
                'update' => [
                    '_method' => 'POST',
                    'route' => 'web.main-page.index',
                    'routeAttributes' => [
                        'id' => 'id',
                    ],
                ],
            ],
        ];
    }

    /**
     * @return array[]
     */
    protected function fields(): array
    {
        $options = cache()->remember('countries-form', now()->addDay(), function () {
            return Country::orderBy('title->'.app()->getLocale(), 'asc')
                ->get(['id','title','iso2','emoji_flag', 'calling_codes'])
                ->mapWithKeys(function ($c) {
                    $label = trim(sprintf('%s %s [%s]', $c->emoji_flag, $c->title, $c->calling_codes[0] ?? ''));
                    return [$c->id => $label];
                })
                ->toArray();
        });

        return [
            'group1' => [
                'full_name' => [
                    'title' => __('globals.first_name'),
                    'type' => Field::TEXT,
                    'size' => 6,
                    'required' => true,
                    //'placeholder' => true,
                    //'label_position' => 'bottom',
                    'parent_class' => 'form-group fv-row mb-1',
                ],
                'phone' => [
                    'title' => __('globals.phone'),
                    'type' => Field::TEXT,
                    'size' => 6,
                    'required' => true,
                    'rules' => ['integer', 'min:5'],
                    //'input_group' => true,
                    'placeholder' => true,
                    'parent_class' => 'form-group fv-row mb-1',
                    //'label_position' => 'bottom',
                    //'parent_class' => 'form-floating mb-4 theme-form-floating',
                    //'prepend' => '<span class="input-group-text phone-code" style="padding-top: 15px;"></span>',
                ],
                'email' => [
                    'title' => __('globals.email'),
                    'type' => Field::TEXT,
                    'size' => 12,
                    'required' => true,
                    'parent_class' => 'form-group fv-row mb-3',
                    //'placeholder' => true,
                    //'label_position' => 'bottom',
                    //'parent_class' => 'form-floating mb-4 theme-form-floating',
                ],
                'message' => [
                    'title' => 'Trumpai parašykite, kokios puokštės ieškote',
                    'type' => Field::TEXTAREA,
                    'size' => 12,
                    'required' => true,
                    //'placeholder' => true,
                    //'label_position' => 'bottom',
                    'parent_class' => 'form-group fv-row mb-3',
                ],
                'save' => [
                    'title' => 'Siųsti užklausą',
                    'size' => '12',
                    'spinner' => true,
                    'parent_class' => 'form-group fv-row mb-1',
                    //'position' => 'text-end',
                    'class' => 'btn btn-primary',
                    'type' => Field::BUTTON_SUBMIT,
                ],
            ],
        ];
    }

    /**
     * @return array[]
     */
    protected function groupAttributes(): array
    {
        return [
            'group1' => [
                'title' => 'Užsakymo forma',
                'layout' => Group::CLEAN,
                'size' => 12,
            ],
        ];
    }

    /**
     * @return array[]
     */
    protected function validationMessages(): array
    {
        return [
            'first_name' => [],
        ];
    }
}
