<?php

namespace Lc\ShopBundle\Controller\Backend;

use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use Lc\ShopBundle\Form\Backend\Common\ReminderType;
use Symfony\Component\HttpFoundation\Response;

class ReminderController extends AdminController
{

        public function renderTemplate($actionName, $templatePath, array $parameters = [])
        {
                if($this->request->isXmlHttpRequest() && ($actionName == 'new' || $actionName == 'edit')) {
                        $response['flashMessages'] = $this->utils->getFlashMessages();

                        $response['data'] = $this->render('@LcShop/backend/default/modal/edit_reminder.twig', $parameters)->getContent();
                        return new Response(json_encode($response));
                }else{
                        return parent::renderTemplate($actionName, $templatePath, $parameters);
                }

        }


        protected function redirectToReferrer(){
                $action = $this->request->query->get('action');

                if($action == 'new'){
                        $this->utils->addFlash('success', 'success.reminder.add');
                }elseif($action == 'edit'){
                        $this->utils->addFlash('success', 'success.reminder.edit');
                }elseif($action == ' setReminderDone'){
                        $this->utils->addFlash('success', 'success.reminder.done');
                }

                if($this->request->isXmlHttpRequest()) {
                        $response['flashMessages'] = $this->utils->getFlashMessages();
                        return new Response(json_encode($response));
                }
        }

        public function setReminderDoneAction(){
                $id = $this->request->query->get('id');
                $done = $this->request->query->get('done');

                $easyadmin = $this->request->attributes->get('easyadmin');
                $reminder = $easyadmin['item'];

                $reminder->setDone($done);
                $this->em->persist($reminder);
                $this->em->flush();
                return $this->redirectToReferrer();
        }

}
