<?php
/**
 * Form Type for Form
 * 
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\FormBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Form Type for Form
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class FormType extends AbstractType
{

    /**
     * {@inhertDoc@}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('titel', TextType::class, array('label' => 'form.titel', 'trim' => true));
        $builder->add('referentie', TextType::class, array('label' => 'form.referentie', 'trim' => true));
        $builder->add('emailOnderwerp', TextType::class, array('label' => 'form.email_onderwerp', 'trim' => true));

            $builder->add('fields', CollectionType::class, array(
            'label' => false,
            'allow_add' => true,
            'allow_delete' => true,
            'entry_type' => FormFieldType::class,
            'by_reference' => true,
            'options' => [
                'form_id' => $options['data']->getId()
            ]
        ));

        $builder->add('ontvangerEmail', TextType::class, array('label' => 'form.ontvanger_email', 'trim' => true));
        $builder->add('ontvangerNaam', TextType::class, array('label' => 'form.ontvanger_naam', 'trim' => true));

        $builder->add('bccEmail', TextType::class, array('label' => 'form.bcc_email', 'trim' => true, 'required' => false));

        $builder->add('afzenderEmail', TextType::class, array('label' => 'form.afzender_email', 'trim' => true));
        $builder->add('afzenderNaam', TextType::class, array('label' => 'form.afzender_naam', 'trim' => true));

        $builder->add('kopieNaarAfzender', 'choice', array(
            'label' => 'form.kopie_naar_afzender',
            'choices' => array(
                'boolean.no' => false,
                'boolean.yes' => true
            ),
            'choices_as_values' => true,
            'placeholder' => false,
            'expanded' => false,
            'required' => true
        ));

        $builder->add('redirectPath', TextType::class, array('label' => 'form.redirect_path', 'trim' => true));
        $builder->add('formHtml', TextareaType::class, array('label' => 'form.form_html'));

        $builder->add('emailHtml', TextareaType::class, array('label' => 'form.email_html'));
        $builder->add('kopieEmailOnderwerp', TextType::class, array('label' => 'form.kopie_email_onderwerp', 'trim' => true, 'required' => false));
        $builder->add('kopieEmailHtml', TextareaType::class, array('label' => 'form.kopie_email_html', 'required' => false));

        $builder->add('save', SubmitType::class, array('label' => 'form.save'));
        $builder->add('saveAndClose', SubmitType::class, array('label' => 'form.save_and_close'));

    }


    /**
     * {@inhertDoc@}
     */
    public function getBlockPrefix()
    {
        return 'visualm_form';
    }
    
    /**
     * {@inhertDoc@}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'VisualM\FormBundle\Entity\Form',
            'translation_domain' => 'forms',
            'validation_groups' => function(FormInterface $form) {
                $data = $form->getData();
                if ($data->getId() === null) {
                    return array('form', 'Default', 'add', 'form_add');
                } else {
                    return array('form', 'Default', 'edit', 'form_edit');
                }
            }
        ));
    }

}