<?php
namespace Modules\Consultation\Controller;

use Modules\Consultation\Service\ExportComparatifService;
use Skeleton\Controller\Controller;
use Helpers\Fichier\Fichier;

/**
 * This controller use an excel file as template
 * An example for this template can be found here :
 * src\Modules\Consultation\Views\ExportComparatif\AOTemplateExport.example.xlsx
 * This template is expected to be found at "{directory "etc"}\AOTemplateExport.xlsx"
 *
 * Excel Named Range and placeholders values are used for all the dynamic rendering.
 */
class ExportComparatifController extends Controller
{
    public function popupExportComparatifAction() :string
    {
        $aoId = $this->request->getAttribute('ao');
        return $this->renderer->render('@Consultation/ExportComparatif/popupExport', [
            'aoId' => $aoId,
        ]);
    }
    public function exportComparatifAction() :string
    {
        $aoId = $this->request->getAttribute('ao');
        $type = $this->request->getParsedBody()['format'] ?? '';


        switch ($type) {
            case 'pdf':
                $consultationWSDL = new \Modules\Consultation\WebService\Consultation();
                $xmlResult = $consultationWSDL->impressionComparatif($aoId);

                $reponse = new \SimpleXMLElement($xmlResult->responseData);
                $filePath = $reponse->ListeDoc->Doc->Fichier;
                $filePath = Fichier::URLDownload($filePath);
                break;
            case 'xlsx':
            default:
                $filePath = (new ExportComparatifService($this->pdo, $this->getTranslationService()))->exportComparatifExcel($aoId);
        }

        return Fichier::UploadClient($filePath, basename($filePath));
    }
}
