<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use App\Entity\Group;
use App\Entity\Subgroup;
use App\Entity\Assetname;
use App\Entity\Vendor;
use App\Entity\Businessunit;
use App\Repository\GroupRepository;
use App\Repository\SubgroupRepository;
use App\Repository\AssetnameRepository;
use App\Repository\VendorRepository;
use App\Repository\BusinessunitRepository;
use Symfony\Component\HttpFoundation\Request;
use Dompdf\Dompdf;
use Dompdf\Options;

class ReportsController extends AbstractController
{
    /**
     * @Route("/reports/depreciationschedule", name="reports_depreciationschedule")
     */
    public function index(Request $request): Response
    {
        $form = $this->createFormBuilder(null)           
            ->add('fromDate', DateType::class, [
                'attr' => ['class' => 'form-control'],
                'widget' => 'single_text',
                'empty_data' => null,
                'label' => 'From Date*',            
                ])

            ->add('toDate', DateType::class, [
                'attr' => ['class' => 'form-control'],
                'widget' => 'single_text',
                'empty_data' => null,
                'label' => 'To Date*',                
                ])
            
            ->add('groups', EntityType::class, [
                // looks for choices from this entity
                'class' => Group::class,
                'query_builder' => function (GroupRepository $er) {
                    return $er->createQueryBuilder('v')                       
                        ->orderBy('v.name', 'ASC');
                },
                // uses the User.username property as the visible option string
                'choice_label' => 'name',
                'placeholder' => 'Select a Group...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Group',
                'required' => false,
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ])

            ->add('subgroup', EntityType::class, [
                // looks for choices from this entity
                'class' => Subgroup::class,
                'query_builder' => function (SubgroupRepository $er) {
                    return $er->createQueryBuilder('v')                       
                        ->orderBy('v.name', 'ASC');
                },
                // uses the User.username property as the visible option string
                'choice_label' => 'name',
                'placeholder' => 'Select a Subgroup...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Sub Group',
                'required' => false,
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ]) 

            ->add('assetname', EntityType::class, [
                // looks for choices from this entity
                'class' => Assetname::class,
                'query_builder' => function (AssetnameRepository $er) {
                    return $er->createQueryBuilder('v')                       
                        ->orderBy('v.name', 'ASC');
                },
                // uses the User.username property as the visible option string
                'choice_label' => 'name',
                'placeholder' => 'Select a Asset Name...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Asset Name',
                'required' => false,
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ]) 

            ->add('vendor', EntityType::class, [
                // looks for choices from this entity
                'class' => Vendor::class,
                'query_builder' => function (VendorRepository $er) {
                    return $er->createQueryBuilder('v')                       
                        ->orderBy('v.name', 'ASC');
                },
                // uses the User.username property as the visible option string
                'choice_label' => 'name',
                'placeholder' => 'Select a Vendor...',
                'attr' => [
                    'class' => 'form-control select-tags',
                    ],
                'label' => 'Vendor',
                'required' => false,
                // used to render a select box, check boxes or radios
                // 'multiple' => true,
                // 'expanded' => true,
                ]) 

            ->add('businessunit', EntityType::class, [
                    // looks for choices from this entity
                    'class' => Businessunit::class,
                    'query_builder' => function (BusinessunitRepository $er) {
                        return $er->createQueryBuilder('v')                       
                            ->orderBy('v.name', 'ASC');
                    },
                    // uses the User.username property as the visible option string
                    'choice_label' => 'name',
                    'placeholder' => 'Select a Businessunit...',
                    'attr' => [
                        'class' => 'form-control select-tags',
                        ],
                    'label' => 'Businessunit',
                    'required' => false,
                    // used to render a select box, check boxes or radios
                    // 'multiple' => true,
                    // 'expanded' => true,
                    ])     
            
       
        ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
           
             $data = $form->getData();                     
             $formDate =  $form->get('fromDate')->getData()->format('Y-m-d');
             $toDate =  $form->get('toDate')->getData()->format('Y-m-d');
             $groupId = $form->get('groups')->getData() ? $form->get('groups')->getData()->getId() : 0;
             $subGroupId = $form->get('subgroup')->getData() ? $form->get('subgroup')->getData()->getId() : 0;
             $assetNameId = $form->get('assetname')->getData() ? $form->get('assetname')->getData()->getId() : 0;
             $vendorId = $form->get('vendor')->getData() ? $form->get('vendor')->getData()->getId() : 0;
             $businessUnitId = $form->get('businessunit')->getData() ? $form->get('businessunit')->getData()->getId() : 0;
           
            // Configure Dompdf according to your needs
            $pdfOptions = new Options();
            $pdfOptions->set('defaultFont', 'Arial');
            
            // Instantiate Dompdf with our options
            $dompdf = new Dompdf($pdfOptions);

            $entityManager = $this->getDoctrine()->getManager();
                      
            $query = $entityManager->getConnection()->prepare("CALL usp_depreciation_calculation('$formDate', '$toDate', $groupId, $subGroupId, $assetNameId, $vendorId, $businessUnitId)");
            $query->execute();
            $depSchedules = $query->fetchAll();
            // dump($formDate, $toDate);
            // dd($depSchedules);
                                  
            // Retrieve the HTML generated in our twig file
            $html = $this->renderView('reports/depreciationSchedule.html.twig', [
                'depSchedules' => $depSchedules,
                'fromDate' => $formDate, 'toDate' =>  $toDate ,               
               
                ]);
            
            // Load HTML to Dompdf
            $dompdf->loadHtml($html);
    
            // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
            $dompdf->setPaper('A4', 'landscape');
    
            // Render the HTML as PDF
            $dompdf->render();
            
            // Output the generated PDF to Browser (force download)
            $dompdf->stream("financialOffer.pdf", [
                "Attachment" => false
            ]);
           

        }


        return $this->render('reports/index.html.twig', [
             'form' => $form->createView(), 'smallName' => 'Vehicle',
        ]);
    }


}
