<?php

namespace App\Admin;

use App\Entity\Adherent;
use App\Entity\Prestataire;
use App\Enum\MoyenEnum;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Security\Core\Security;

class CotisationAdmin extends AbstractAdmin
{
    protected $baseRouteName = 'cotisation';
    protected $baseRoutePattern = 'cotisation';
    protected $security;

    protected $translator;
    protected $datagridValues = [
        '_sort_order' => 'DESC',
        '_sort_by' => 'createdAt',
    ];

    public function setSecurity(Security $security)
    {
        $this->security = $security;
    }

    /**
    * {@inheritdoc}
    */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
            ->add('cotisationInfos.annee', null, array('label' => 'Année'))
            ->add('montant', null, array('label' => 'Montant'))
            ->add('cotisationInfos.recu', null, array('label' => 'Recu ?'))
            ->add('operateur.username', null, array('label' => 'Login'))
            ->add('operateur.email', null, array('label' => 'Email'))
        ;
    }

    /**
    * {@inheritdoc}
    */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $cotisation = $this->getSubject();
        $now = new \DateTime();
        $formMapper
            ->with('Cotisation', ['class' => 'col-md-8'])
                ->add('cotisationInfos.annee', null, array('label' => 'Année', 'data' => $now->format('Y')))
                ->add('montant', null, array('label' => 'Montant'))
                ->add('moyen', ChoiceType::class, array(
                    'required' => true,
                    'choices' => MoyenEnum::getAvailableTypes(),
                    'choice_label' => function ($choice) {
                        return MoyenEnum::getTypeName($choice);
                    },
                ))
        ;
        if ($this->security->getUser() != null && $this->security->getUser()->isGranted('ROLE_TRESORIER')) {
            $formMapper
                ->add('cotisationInfos.recu', CheckboxType::class, array('label' => 'Reçu'));
        }
        $formMapper->end()
            ->with('Date', ['class' => 'col-md-4'])
                ->add('cotisationInfos.debut', DateType::class, array(
                    'label' => 'Date de début',
                    'data' => new \DateTime(),
                    'widget' => 'single_text',
                    'html5' => false,
                    'attr' => ['class' => 'js-datepicker'],
                ))
                ->add('cotisationInfos.fin', DateType::class, array(
                    'label' => 'Date de fin',
                    'data' => new \DateTime('+ 1 year'),
                    'widget' => 'single_text',
                    'html5' => false,
                    'attr' => ['class' => 'js-datepicker'],
                ))
            ->end()
        ;
    }

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->remove('delete');
        if ($this->security->getUser() != null && !$this->security->getUser()->isGranted('ROLE_TRESORIER')) {
            $collection->clearExcept('list');
        }
    }

    /**
    * {@inheritdoc}
    */
    protected function configureListFields(ListMapper $listMapper)
    {
        unset($this->listModes['mosaic']);
        $listMapper
            ->addIdentifier('operateur', null, array(
                'label' => 'Opérateur',
                'disabled' => true
            ))
            ->addIdentifier('cotisationInfos.annee')
            ->addIdentifier('montant')
            ->addIdentifier('moyen')
            ->addIdentifier('createdAt')
        ;
    }
}
