<?php

namespace VGM\WOBundle\Controller;

use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Symfony\Component\Validator\Constraints\DateTime;
use VGM\WOBundle\Entity\WorkCategory;

/**
 * Worker controller.
 *
 */
class WorkCategoryController extends Controller
{
    public function indexAction()
    {
        return $this->render('VGMWOBundle:WorkCategories:index.html.twig');
    }

    public function getWorkCategoriesByLevelAjaxAction(Request $request){
        $level = $request->get('level');

        $em = $this->getDoctrine()->getManager();

        $areas = $em->getRepository('VGMWOBundle:WorkCategory')->findBy(array('level'=>$level), array('caption' => 'ASC'));

        $areas2send = [];
        if(count($areas) > 0){
            foreach($areas as $area){
                //$area = new WorkCategory();
                $areas2send[] = $this->getFullCategory($area);
                /*array("id" => $area->getId(),
                    "code" => $area->getCode(),
                    "name" => $area->getCaption(),
                    "description" => $area->getDescription(),
                    "level" => $area->getLevel(),
                    "active" => $area->getActive());*/
            }
        }

        $response = array("code" => 100, "success" => true, 'message' => "Ok",
            "areas" => $areas2send);

        return new Response(json_encode($response));
    }

    public function getFullCategory($cat){
        //$cat = new WorkCategory();
        $children = [];
        if(count($cat->getChildren()) > 0){
            foreach($cat->getChildren() as $child){
                $children[] = $this->getFullCategory($child);
            }
        }
        $cat2send = array("id" => $cat->getId(),
            "code" => $cat->getCode(),
            "name" => $cat->getCaption(),
            "description" => $cat->getDescription(),
            "level" => $cat->getLevel(),
            "active" => $cat->getActive(),
            "children" => $children);

        return $cat2send;
    }

    public function getAreasAjaxAction(Request $request){
        $em = $this->getDoctrine()->getEntityManager();
        $sql = "SELECT id,code,caption as name,description"
              ." FROM workcategory WHERE level = 0";

        $connection = $em->getConnection();
        $statement = $connection->prepare($sql);
        $statement->execute();
        $results = $statement->fetchAll();

        $output = array(
            "aaSorting" => [[3,'desc']],
            "iTotalRecords" => count($results),
            "iTotalDisplayRecords" => count($results),
            "aaData" => array()
        );
        $columns = array( 'id','code','name','description');

        foreach($results as $arow){
            $row = array();
            foreach($columns as $column){
                $row[] = $arow[$column] != null ? $arow[$column] : "";
            }
            $output['aaData'][] = $row;
        }

        return new Response(json_encode($output));
/*


        $em = $this->getDoctrine()->getManager();

        $areas = $em->getRepository('VGMWOBundle:WorkCategory')->findBy(array(), array('caption' => 'ASC'));

        $areas2send = [];
        if(count($areas) > 0){
            foreach($areas as $area){
                $area = new WorkCategory();
                $areas2send[] = array("id" => $area->getId(),
                    "code" => $area->getCode(),
                    "name" => $area->getCaption(),
                    "description" => $area->getDescription(),
                    "active" => $area->getActive());
            }
        }

        $response = array("code" => 100, "success" => true, 'message' => "Ok",
            "areas" => $areas2send);

        return new Response(json_encode($response));*/
    }
}