<?php

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Form\Admin;

use CpCreation\VitiCore\Product\Model\ProductCollection;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ProductCollectionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, [
                'label' => 'Nom',
            ])
            ->add('code', TextType::class, [
                'label' => 'Code',
                'required' => false,
                'help' => 'Code saisi à la main et utilisé par les composants Twig. Exemple : collection="homepage-cuvees".',
            ])
            ->add('description', TextareaType::class, [
                'label' => 'Description',
                'required' => false,
            ])
            ->add('publishedAt', DateTimeType::class, [
                'required' => false,
                'label' => 'Date de début de publication',
                'help' => 'Laisser vide pour rendre la collection visible immédiatement.',
                'input' => 'datetime',
                'format' => 'yyyy-MM-dd HH:mm',
                'html5' => false,
                'widget' => 'single_text',
                'attr' => ['class' => 'datetimepicker'],
            ])
            ->add('unPublishedAt', DateTimeType::class, [
                'required' => false,
                'label' => 'Date de fin de publication',
                'help' => 'Laisser vide pour ne pas programmer de fin de publication.',
                'input' => 'datetime',
                'format' => 'yyyy-MM-dd HH:mm',
                'html5' => false,
                'widget' => 'single_text',
                'attr' => ['class' => 'datetimepicker'],
            ])
            ->add('mode', ChoiceType::class, [
                'label' => 'Mode',
                'choices' => [
                    'Manuel' => ProductCollection::MODE_MANUAL,
                ],
                'help' => 'Pour le moment, la collection est composée manuellement avec les produits sélectionnés ci-dessous.',
            ])
            ->add('items', CollectionType::class, [
                'label' => false,
                'required' => false,
                'entry_type' => ProductCollectionItemType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'prototype' => true,
                'by_reference' => false,
                'delete_empty' => true,
                'help' => 'Ajoutez les produits à afficher dans cette collection.',
                'attr' => [
                    'data-name-prefix' => 'Produit',
                    'class' => 'product-collection-item-collection',
                ],
                'entry_options' => [
                    'label' => false,
                ],
            ]);
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => ProductCollection::class,
        ]);
    }
}
