<?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/management_dashboard")
 */
class ManagementDashboardController extends Controller
{
    /**
     * @Route("", name="management_dashboard")
     */
    public function indexAction() {

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

        return $this->render('@RxConnexion/ManagementDashboard/index.html.twig', array(
            'title' => 'Management Dashboard',
            'page_title' => 'Management Dashboard',
            'javascripts' => array('rxconnexion/admin/js/managementDashboard/managementDashboard.js?v=1.1.11'),
            'stylesheets' => array('rxconnexion/admin/css/admin/index.css?v=1.0.5'),
        ));
    }

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

        ini_set('memory_limit', '2048M');
        set_time_limit(0);

        $dateFrom = $request->request->get('dateFrom');
        $company = $request->request->get('company');
        $branch = $request->request->get('branch');
        $clinician = $request->request->get('clinician');

        $params = array(
            'date_from' => $dateFrom ? $dateFrom : date('m/d/Y'),
            'company' => $company ? $company : null,
            'branch' => $branch ? $branch : null,
            'provider' => $clinician ? $clinician : null,
        );

        $em = $this->getDoctrine()->getManager();
        $managementDashboardDaily = $em->getRepository('RxConnexionBundle:PatientChartEntity')->managementDashboard_daily($params);

        return new JsonResponse($managementDashboardDaily);
    }

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

        ini_set('memory_limit', '2048M');
        set_time_limit(0);

        $dateFrom = $request->request->get('dateFrom');
        $dateTo = $request->request->get('dateTo');
        $company = $request->request->get('company');
        $branch = $request->request->get('branch');
        $clinician = $request->request->get('clinician');

        $params = array(
            'date_from' => $dateFrom ? $dateFrom : date('m/01/Y'),
            'date_to' => $dateTo ? $dateTo : date('m/d/Y'),
            'company' => $company ? $company : null,
            'branch' => $branch ? $branch : null,
            'provider' => $clinician ? $clinician : null,
        );

        $em = $this->getDoctrine()->getManager();
        $managementDashboardMonthly = $em->getRepository('RxConnexionBundle:PatientChartEntity')->managementDashboard_monthly($params);

        return new JsonResponse($managementDashboardMonthly);
    }

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

        ini_set('memory_limit', '2048M');
        set_time_limit(0);

        $dateFrom = $request->request->get('dateFrom');
        $dateTo = $request->request->get('dateTo');
        $company = $request->request->get('company');
        $branch = $request->request->get('branch');
        $clinician = $request->request->get('clinician');

        $params = array(
            'date_from' => $dateFrom ? $dateFrom : date('m/d/Y', strtotime(date('m/d/Y'). ' -73 day')),
            'date_to' => $dateTo ? $dateTo : date('m/d/Y', strtotime(date('m/d/Y'). ' -43 day')),
            'company' => $company ? $company : null,
            'branch' => $branch ? $branch : null,
            'provider' => $clinician ? $clinician : null,
        );

        $em = $this->getDoctrine()->getManager();
        $managementDashboardCycle = $em->getRepository('RxConnexionBundle:PatientChartEntity')->managementDashboard_cycle($params);

        return new JsonResponse($managementDashboardCycle);
    }

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

        ini_set('memory_limit', '2048M');
        set_time_limit(0);

        $dateFrom = $request->request->get('dateFrom');
        $dateTo = $request->request->get('dateTo');
        $company = $request->request->get('company');
        $branch = $request->request->get('branch');
        $clinician = $request->request->get('clinician');

        $params = array(
            'date_from' => $dateFrom ? $dateFrom : date('m/01/Y'),
            'date_to' => $dateTo ? $dateTo : date('m/d/Y'),
            'company' => $company ? $company : null,
            'branch' => $branch ? $branch : null,
            'provider' => $clinician ? $clinician : null,
        );

        $em = $this->getDoctrine()->getManager();
        $managementDashboardEncounter = $em->getRepository('RxConnexionBundle:PatientChartEntity')->managementDashboard_encounters($params);

        return new JsonResponse($managementDashboardEncounter);
    }

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

        ini_set('memory_limit', '2048M');
        set_time_limit(0);

        $dateFrom = $request->request->get('dateFrom');
        $dateTo = $request->request->get('dateTo');
        $company = $request->request->get('company');
        $branch = $request->request->get('branch');
        $clinician = $request->request->get('clinician');

        $params = array(
            'date_from' => $dateFrom ? $dateFrom : date('m/d/Y', strtotime(date('m/d/Y'). ' -73 day')),
            'date_to' => $dateTo ? $dateTo : date('m/d/Y', strtotime(date('m/d/Y'). ' -43 day')),
            'company' => $company ? $company : null,
            'branch' => $branch ? $branch : null,
            'provider' => $clinician ? $clinician : null,
        );

        $em = $this->getDoctrine()->getManager();
        $managementDashboardConversion = $em->getRepository('RxConnexionBundle:PatientChartEntity')->managementDashboard_conversion($params);

        return new JsonResponse($managementDashboardConversion);
    }
}