<?php

namespace AutoAdmin\Bundle\Admin\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\NotBlank;

class ImportRedirectType extends AbstractType
{
    /**
     * @inheritDoc
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('file', FileType::class, [
                'label' => 'Fichier à importer',
                'attr' => [
                    "placeholder" => "Sélectionner un fichier à importer"
                ],
                'required' => true,
                'constraints' => [
                    new NotBlank(),
                    new File([
                        'mimeTypes' => [
                            'text/csv',
                            'text/plain',
                            'text/tsv',
                            'text/comma-separated-values',
                            'application/vnd.ms-excel',
                            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        ],
                        'mimeTypesMessage' => 'Le fichier doit être au format CSV',
                    ])
                ],
                'help_html' => true,
                'help' => 'Le fichier doit être au format CSV, <a href="'.$options['help'].'" target="_blank">fichier d\'exemple</a>',
            ])
            ->add('skip_first_line', CheckboxType::class, [
                'label' => 'Ignorer la première ligne',
                'required' => false,
            ])
            ->add('submit', SubmitType::class, [
                'label' => 'Importer',
            ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => null
        ]);
    }
}