<?php
// src/AppBundle/Form/TaskType.php
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
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 Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;

class AdSoftType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $publicTags = $options['data']->getPublicTag();
        if ($publicTags != null) {
            if (is_array($publicTags)) {
                $publicTags = implode(',', $publicTags);
            }
        }
        $contract_type = "";
        $extra = $options['data']->getExtra();
        if ($extra != null) {
            if(is_array($extra)){
                $contract_type = $extra['contract_type'];
            }
        }

        $location_gmap = $options['data']->getLocation();

        $location = json_encode($location_gmap);

        if ($location_gmap != null && is_array($location_gmap) && array_key_exists('gmap_address', $location_gmap)) {
            $location_gmap = $location_gmap['gmap_address']['address_components'][0]['long_name'];
        } else {
            $location_gmap = "";
        }

        $builder
            ->add('location_gmap', TextType::class, array(
                'mapped' => false,
                'label' => 'Localisation de l\'annonce',
                'attr' => array(
                    'value' => $location_gmap
                )
            ))
            ->add('location', HiddenType::class, array(
                'data' => $location,
            ))
            ->add('description', TextareaType::class, array(
                'label' => 'Description de l\'annonce',
                'attr' => array(
                    'class' => 'materialize-textarea',
                    'placeholder' => 'Décrire votre annonce'
                )
            ))
            ->add('contract_type', ChoiceType::class, array(
                'mapped' => false,
                'label' => 'Type de contrat',
                'data' => $contract_type,
                'choices'  => array(
                    'CDI' => 'CDI',
                    'CDD' => 'CDD',
                    'Apprentissage' => 'Apprentissage',
                    'Professionalisation' => 'Professionalisation',
                    'Stage' => 'Stage',
                    'Indépendant' => 'Indépendant'
                )
            ))
            ->add('publicTag', TextType::class, array(
                'label' => 'profile.company.tags',
                'data' => $publicTags,
            ));
    }
}