<?php

namespace Lc\ShopBundle\Form\Backend\Common;

use Lc\ShopBundle\Model\Address;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Lc\ShopBundle\Context\AddressInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraints\NotBlank;

class AddressType extends AbstractType
{
        protected $em;

        public function __construct(EntityManagerInterface $entityManager)
        {
                $this->em = $entityManager;
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {

                $builder
                        ->add('title', TextType::class, ['label' => 'Titre'])
                        ->add('type', ChoiceType::class, [
                                'label' => 'Type',
                                'choices' => [
                                        'field.Address.typeOptions.' . Address::TYPE_INDIVIDUAL => Address::TYPE_INDIVIDUAL,
                                        'field.Address.typeOptions.' . Address::TYPE_LEGAL_PERSON => Address::TYPE_LEGAL_PERSON
                                ]
                        ])
                        ->add('civility', ChoiceType::class, [
                                'label' => 'Civilité',
                                'required' => false,
                                'choices' => [
                                        '--' => '',
                                        'Femme' => 1,
                                        'Homme' => 0,
                                ],
                        ])
                        ->add('lastname', TextType::class, ['required' => false])
                        ->add('firstname', TextType::class, ['required' => false])
                        ->add('address', TextareaType::class)
                        ->add('zip', TextType::class)
                        ->add('city', TextType::class)
                        ->add('phone', CollectionType::class, [
                                'allow_add'=>true,
                                'allow_delete'=>true,
                                'entry_options'  => [
                                        'label'=>false,
                                ],
                                'required' => false
                        ])
                        ->add('company', TextType::class, ['required' => false])
                        ->add('siret', TextType::class, ['required' => false])
                        ->add('tva', TextType::class, ['required' => false])
                        ->add('country', HiddenType::class, ['data' => 'France'])
                ;

        }

        public function configureOptions(OptionsResolver $resolver)
        {
                $resolver->setDefaults([
                        'label' => false,
                        'data_class' => $this->em->getClassMetadata(AddressInterface::class)->getName(),
                        'translation_domain' => 'lcshop'
                ]);
        }
}
