<?php
/**
 * Created by PhpStorm.
 * User: danielknudsen
 * Date: 05.04.18
 * Time: 13:09
 */
namespace App\Controller;

use App\Entity\EntUser;
use App\Utils\Reply;
use Dompdf\Dompdf;
use Dompdf\Options;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ExportController extends AbstractController
{
    const CONTACT_PDF_TYPE_OPERATOR = 'operator';
    const CONTACT_PDF_TYPE_CUSTOMER = 'customer';
    const CONTACT_PDF_TYPE_PRODUCTION = 'production';
    const CONTACT_PDF_TYPE_SERVICE = 'service';

    /**
     * @Route("/export-contacts-pdf", name="export_contacts_pdf")
     * @IsGranted({"ROLE_ACCOUNTING", "ROLE_TECHNIQUE"})
     */
    public function exportContacts(Request $request)
    {
        ini_set('max_execution_time', '300');
        ini_set('memory_limit','1024');
        $type = $request->request->get('type');
        $em = $this->getDoctrine()->getManager();
        $title = "";
        /** @var EntUser $user */
        $user = $this->getUser();


        switch ($type) {
            case self::CONTACT_PDF_TYPE_CUSTOMER:
                if (!$user->isAdmin()) {
                    throw new Exception('invalid role');
                }
                $sql = "SELECT cc.*, c.name AS company 
                        FROM customer_contact cc, customer c 
                        WHERE cc.customer_id = c.id 
                        ORDER BY company ASC, cc.lastname ASC";
                $statement = $em->getConnection()->prepare($sql);
                $statement->execute();
                $entries = $statement->fetchAll();
                $title = "Kunden Kontakte";
                break;
            case self::CONTACT_PDF_TYPE_OPERATOR:
                $sql = "SELECT oc.*, o.name AS company 
                        FROM operator_contact oc, operator o 
                        WHERE oc.operator_id = o.id 
                        ORDER BY company ASC, oc.lastname ASC";
                $statement = $em->getConnection()->prepare($sql);
                $statement->execute();
                $entries = $statement->fetchAll();
                $title = "Betreiber Kontakte";
                break;
            case self::CONTACT_PDF_TYPE_PRODUCTION:
                if (!$user->isAdmin()) {
                    throw new Exception('invalid role');
                }
                $sql = "SELECT pc.*, p.name AS company 
                        FROM production_contact pc, production p 
                        WHERE pc.production_id = p.id 
                        ORDER BY company ASC, pc.lastname ASC";
                $statement = $em->getConnection()->prepare($sql);
                $statement->execute();
                $entries = $statement->fetchAll();
                $title = "Produktion Kontakte";
                break;
            case self::CONTACT_PDF_TYPE_SERVICE:
                if (!$user->isAdmin()) {
                    throw new Exception('invalid role');
                }
                $sql = "SELECT sc.*, s.name AS company 
                        FROM service_contact sc, service s 
                        WHERE sc.service_id = s.id 
                        ORDER BY company ASC, sc.lastname ASC";
                $statement = $em->getConnection()->prepare($sql);
                $statement->execute();
                $entries = $statement->fetchAll();
                $title = "Service Kontakte";
                break;
            default:
                throw new Exception('invalid type given');
        }

        // Configure Dompdf according to your needs
        $pdfOptions = new Options();
        $pdfOptions->set('defaultFont', 'Arial');

        // Instantiate Dompdf with our options
        $dompdf = new Dompdf($pdfOptions);

        // Retrieve the HTML generated in our twig file
        $html = $this->renderView('contacts_export.html.twig', [
            'data' => $entries,
            'title' => $title
        ]);

        // 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();

        return Reply::getSteamedFileResponse($dompdf->output());
    }

}