<?php

namespace Lc\ShopBundle\Form\Backend\Order;

use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Lc\ShopBundle\Context\AddressInterface;
use Lc\ShopBundle\Context\MerchantUtilsInterface;
use Lc\ShopBundle\Context\OrderShopInterface;
use Lc\ShopBundle\Context\UserInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class OrderInvoiceAddressType extends AbstractType
{
        protected $em;
        protected $merchantUtils;

        public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils)
        {
                $this->em = $em;
                $this->merchantUtils = $merchantUtils;
        }

        public function buildForm(FormBuilderInterface $builder, array $options)
        {
                $entity = $options['data'];
                $addressClass = $this->em->getClassMetadata(AddressInterface::class);

                $builder->add('invoiceAddress', EntityType::class, array(
                        'class' => $addressClass->name,
                        'placeholder' => '',
                        'query_builder' => function (EntityRepository $er) use ($entity) {
                                return $er->createQueryBuilder('a')
                                        ->where('a.user = :user')
                                        ->setParameter('user', $entity->getUser());
                        },
                ))
                ->add('save', ButtonType::class, array(
                        'label' => 'action.save'
                ));

        }

        public
        function configureOptions(OptionsResolver $resolver)
        {
                $resolver->setDefaults([
                        'data_class' => $this->em->getClassMetadata(OrderShopInterface::class)->getName(),

                ]);
        }
}
