<?php

namespace App\Admin;

use App\Entity\Siege;
use App\Enum\CurrencyEnum;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Security\Core\Security;

/**
 * Administration des operation du siege.
 *
 * KOHINOS : Outil de gestion de Monnaie Locale Complémentaire
 *
 * @author Julien Jorry <julien.jorry@gmail.com>
 */
class OperationSiegeAdmin extends OperationAdmin
{
    protected $security;
    protected $session;
    protected $datagridValues = [
        '_sort_order' => 'DESC',
        '_sort_by' => 'createdAt',
    ];

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

    protected function configureRoutes(RouteCollection $collection)
    {
        $collection->clearExcept(['list', 'export']);
    }

    /**
     * {@inheritdoc}
     */
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        parent::configureDatagridFilters($datagridMapper);
        $datagridMapper
            ->add('currency', 'doctrine_orm_callback', [
                'callback' => function ($queryBuilder, $alias, $field, $value) {
                    if (!$value['value']) {
                        return;
                    }
                    $queryBuilder
                        ->where($alias . '.currency = :type')
                        ->setParameter('type', $value['value']);

                    return true;
                },
                'advanced_filter' => false,
                'show_filter' => true,
                'field_type' => ChoiceType::class,
                'field_options' => [
                    'choices' => CurrencyEnum::getAvailableTypes(),
                    'choice_label' => function ($choice) {
                        return CurrencyEnum::getTypeName($choice);
                    },
                    'empty_data' => CurrencyEnum::CURRENCY_EMLC,
                    'data' => CurrencyEnum::CURRENCY_EMLC,
                    'placeholder' => false,
                    'expanded' => true,
                    'multiple' => false,
                ],
            ])
        ;
    }
}
