<?php

namespace Lc\ShopBundle\Form\Backend\ProductFamily;

use Doctrine\ORM\EntityManagerInterface;
use Lc\ShopBundle\Context\ProductInterface;
use Lc\ShopBundle\Context\UnitInterface;
use Lc\ShopBundle\Services\Utils;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ProductType extends AbstractType
{
        protected $em;
        protected $utils;


        public function __construct(EntityManagerInterface $entityManager, Utils $utils)
        {
                $this->em = $entityManager;
                $this->utils = $utils;
        }
        public function buildForm(FormBuilderInterface $builder, array $options)
        {


                $builder->add('title', TextType::class, array(
                        "required" => false
                )) ;


                $builder->add('quantity', NumberType::class, array(
                        'label' => 'Quantité',
                        'required'=>false,
                        'attr' => [
                                'append_html' => 'g'
                        ]
                ));

                $unitClass = $this->em->getClassMetadata(UnitInterface::class)->getName();

                $builder->add('unit', EntityType::class, array(
                        'class'=> $unitClass,
                        'data' => 0,
                        'required'=>false,
                        'choice_attr' => function ($choice) {
                                return [
                                        'data-unit-reference' => $choice->getUnitReference(),
                                        'data-coefficient' => $choice->getCoefficient()
                                ];
                        },
                ));


                $builder->add('price', NumberType::class, array(
                        'label' => 'Prix',
                        "required" => false
                ));

                $builder->add('priceWithTax', NumberType::class, array(
                        'label' => 'Prix TTC',
                        'required' => false,
                        'mapped' => false
                ));

                $builder->add('buyingPrice', NumberType::class, array(
                        'label' => 'Prix d\'achat',
                        'required'=>false
                ));

                $builder->add('buyingPriceWithTax', NumberType::class, array(
                        'label' => 'Prix d\'achat TTC',
                        'required' => false,
                        'mapped' => false
                ));

                $builder->add('buyingPriceByRefUnit', NumberType::class, array(
                        'label' => 'Prix d\'achat',
                        'required'=>false
                ));

                $builder->add('buyingPriceByRefUnitWithTax', NumberType::class, array(
                        'label' => 'Prix d\'achat TTC',
                        'required' => false,
                        'mapped' => false
                ));
                $builder->add('multiplyingFactor', NumberType::class, array(
                        'label' => 'Coefficiant de multiplication',
                        'mapped'=>false,
                        'required'=>false
                ));

                $builder->add('priceByRefUnit', NumberType::class, array(
                        'required'=>false
                ));

                $builder->add('priceByRefUnitWithTax', NumberType::class, array(
                        'required' => false,
                        'mapped' => false
                ));

                $builder->add('availableQuantity', NumberType::class, array(
                        'label' => 'Quantité disponible',
                        'required' => false,
                ));

                $builder->add('availableQuantityDefault', NumberType::class, array(
                        'label' => 'Quantité disponible',
                        'required' => false,
                ));

                $builder->add('propertyExpirationDate', TextType::class, array(
                        'required' => false
                ));

                $builder->add('position', HiddenType::class);
                $builder->add('status', HiddenType::class);
                $builder->add('exportTitle', TextType::class, array(
                        'required' =>false
                ));
                $builder->add('exportNote', TextType::class, array(
                        'required' =>false
                ));

        }

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