<?php

namespace App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Translation\TranslatorInterface;

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

    public function setTranslator(TranslatorInterface $translator)
    {
        $this->translator = $translator;
    }

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

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

    protected function configureExportFields(): array
    {
        return [
            $this->trans('Date') => 'createdAt',
            $this->trans('Montant') => 'montant',
            $this->trans('Compte') => 'currency',
            $this->trans('Type') => 'flux.type',
            $this->trans('Moyen') => 'flux.moyen',
            $this->trans('Operateur') => 'flux.operateur',
        ];
    }

    public function getDataSourceIterator()
    {
        $iterator = parent::getDataSourceIterator();
        $iterator->setDateTimeFormat('d/m/Y H:i:s'); //change this to suit your needs

        return $iterator;
    }

    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        unset($this->listModes['mosaic']);
        $listMapper
            ->add('createdAt', 'datetime', ['label' => 'Date'])
            ->add('flux.type', 'text', ['label' => 'Type', 'template' => '@kohinos/bundles/SonataAdminBundle/Block/translated_value.html.twig'])
            ->add('montant', null, ['label' => 'Montant'])
            ->add('currency', null, ['label' => 'Compte'])
            ->add('flux.expediteur', null, ['label' => 'Expediteur'])
            ->add('flux.destinataire', null, ['label' => 'Destinataire'])
            ->add('account.prestataire', null, ['label' => 'Presta'])
            ->add('flux.operateur', null, ['label' => 'Operateur'])
            ->addIdentifier('flux.reference', null, ['label' => 'Reference'])
            // @TODO : ajouter le verify uniquement si l'on souhaite (param url)=> sinon c'est beaucoup trop long...
            // ->addIdentifier('verify', null, array('label' => 'Vérifié'))
        ;
    }

    public function getTemplate($name)
    {
        if ('list' == $name) {
            return '@kohinos/block/base_list_with_total.html.twig';
        }

        return parent::getTemplate($name);
    }

    public function getTotalLabel()
    {
        return $this->translator->trans('Total des opérations :');
    }

    // public function getTotal()
    // {
    //     $datagrid = $this->getDatagrid();
    //     $datagrid->buildPager();

    //     $query = clone $datagrid->getQuery();
    //     $query
    //         ->select('SUM( ' . $query->getRootAlias() . '.montant) as total')
    //         ->setFirstResult(null)
    //         ->setMaxResults(null);

    //     $result = $query->execute([], \Doctrine\ORM\Query::HYDRATE_SINGLE_SCALAR);

    //     return $result;
    // }
}
