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

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


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

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

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

        $dateTimeFrom = new \DateTime();

        $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 : $dateTimeFrom->format('m/d/Y'),
            'company' => $company,
            'branch' => $branch,
            'provider' => $clinician
        );

        $em = $this->getDoctrine()->getManager();
        $managementDashboardCbtDaily = $em->getRepository('RxConnexionBundle:ScheduleEntity')->management_dashboard_booked_count_daily($params);

        return new JsonResponse($managementDashboardCbtDaily);
    }

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

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

        $dateTimeFrom = new \DateTime();
        $dateTimeTo = new \DateTime();

        $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 : $dateTimeFrom->format('m/01/Y'),
            'date_to' => $dateTo ? $dateTo : $dateTimeTo->format('m/d/Y'),
            'company' => $company,
            'branch' => $branch,
            'provider' => $clinician
        );

        $em = $this->getDoctrine()->getManager();
        $managementDashboardCbtMonthly = $em->getRepository('RxConnexionBundle:ScheduleEntity')->management_dashboard_booked_count_monthly($params);

        return new JsonResponse($managementDashboardCbtMonthly);
    }
}