<?php

namespace RxConnexionBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

use RxConnexionBundle\Entity\AuditTrailEntity;

/**
 * @Route("/main/audit_trail")
 */
class AuditTrailController extends Controller
{
    /**
     * @Route("", name="audit_trail")
     */
    public function indexAction() {

        $authService = $this->get('rx_connexion.authService');
        if(!$authService->isLoggedIn()) return $authService->redirectToLogin();
        if(!$authService->isUserHasAccesses(array('Admin AuditTrail'))) return $authService->redirectToHome();

        return $this->render('@RxConnexion/AuditTrail/index.html.twig', array(
            'title' => 'Audit Trail',
            'page_title' => 'Audit Trail',
            'javascripts' => array('rxconnexion/admin/js/auditTrail/auditTrail.js?v=1.0.1')
        ));
    }

    /**
     * @Route("/ajax_list", name="audit_trail_ajax_list")
     */
    public function ajax_list(Request $request) {

        $get = $request->query->all();

        $result = array(
            "draw" => intval($get['draw']),
            "recordsTotal" => 0,
            "recordsFiltered" => 0,
            "data" => array()
        );

        $authService = $this->get('rx_connexion.authService');
        if($authService->isLoggedIn()) {

            $data = $this->getDoctrine()->getManager()->getRepository('RxConnexionBundle:AuditTrailEntity')->ajax_list($get, $this->get('session')->get('userData'));

            foreach($data['results'] as $row) {

                $url = $this->generateUrl('audit_trail_details', array(
                    'id' => base64_encode($row['id'])
                ));
                $action = "<a class='action-button-style' href='$url'>Details</a>";

                $values = array(
                    $row['createdAt'],
                    $row['userFullName'],
                    $row['tranLabel'],
                    $row['axn'],
                    $row['tableNameId'],
                    $action
                );

                $result['data'][] = $values;
            }

            $result['recordsTotal'] = $data['total'];
            $result['recordsFiltered'] = $data['total'];
        }

        return new JsonResponse($result);
    }

    /**
     * @Route("/details/{id}", name="audit_trail_details")
     */
    public function detailsAction($id) {

        $authService = $this->get('rx_connexion.authService');
        if(!$authService->isLoggedIn()) return $authService->redirectToLogin();
        if(!$authService->isUserHasAccesses(array('Admin AuditTrail'))) return $authService->redirectToHome();

        $auditTrail = $this->getDoctrine()->getManager()->getRepository(AuditTrailEntity::class)->detailsById(base64_decode($id));

        return $this->render('@RxConnexion/AuditTrail/details.html.twig', array(
            'title' => 'Audit Trail Details',
            'page_title' => 'Audit Trail Details',
            'auditTrail' => $auditTrail
        ));
    }
}
