<?php

namespace KupShop\ReclamationsSuppliersBundle\Attachment;

use KupShop\KupShopBundle\Util\Pdf\HTMLToPDF;
use KupShop\OrderingBundle\Attachment\BaseAttachment;
use KupShop\OrderingBundle\Util\Invoice\PdfGenerator;
use KupShop\ReclamationsBundle\Util\ReclamationsUtil;
use KupShop\ReclamationsSuppliersBundle\Util\ReclamationsSuppliersUtil;
use Query\Operator;

class ReclamationSuppliersPDF extends BaseAttachment
{
    protected static $name = 'Reklamace dodavateli';
    protected static $type = 'reclamationSupplier';

    protected $filename = 'ReklamaceDodavateli_{KOD}.pdf';
    protected $template = 'pdf/reclamationSupplierPDF.tpl';

    private $reclamationSupplier;

    private $pdfGenerator;
    private $reclamationsUtil;

    private ReclamationsSuppliersUtil $reclamationsSuppliersUtil;

    public function __construct(PdfGenerator $pdfGenerator, ReclamationsUtil $reclamationsUtil, ReclamationsSuppliersUtil $reclamationsSuppliersUtil)
    {
        $this->pdfGenerator = $pdfGenerator;
        $this->reclamationsUtil = $reclamationsUtil;
        $this->reclamationsSuppliersUtil = $reclamationsSuppliersUtil;
    }

    public static function isAllowed()
    {
        // aby se nezobrazovala v prilohach k emailum
        return false;
    }

    public function getFilename(): string
    {
        return $this->reclamationsUtil->replacePlaceholders($this->filename, ['KOD' => $this->reclamationSupplier['code']]);
    }

    public function setReclamationSupplier($reclamationSupplier)
    {
        $this->reclamationSupplier = $reclamationSupplier;
    }

    public function getContent()
    {
        $supplier = sqlQueryBuilder()->select('*')
            ->from('suppliers', 's')
            ->where(Operator::equals(['id' => $this->reclamationSupplier['id_supplier']]))
            ->execute()->fetchAssociative();

        $items = $this->reclamationsSuppliersUtil->getItems($this->reclamationSupplier['id']);

        $smarty = createSmarty(false, true);
        $smarty->assign([
            'reclamationSupplier' => $this->reclamationSupplier,
            'supplier' => $supplier,
            'items' => $items,
        ]);

        $content = $smarty->fetch($this->template);

        $html2pdf = new HTMLToPDF();

        return $html2pdf->generatePDF($content, null, true);
    }
}
