<?php

/**
 * @package         Convert Forms
 * @version         5.2.4 Pro
 * 
 * @author          Tassos Marinos <info@tassos.gr>
 * @link            https://www.tassos.gr
 * @copyright       Copyright © 2024 Tassos All Rights Reserved
 * @license         GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
*/

defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Form\Field\TextField;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Language\Text;

class JFormFieldPDFSubmission extends TextField
{
    /**
     * Cached PDF URL for this submission (null = not yet checked, '' = no PDF).
     *
     * @var string|null
     */
    private $pdfUrl = null;

    /**
     * Retrieve (and cache) the PDF URL for the current submission.
     *
     * @return string  Absolute PDF URL, or empty string when none exists.
     */
    private function getPdfUrl()
    {
        if ($this->pdfUrl !== null)
        {
            return $this->pdfUrl;
        }

        JLoader::register('PDFHelper', __DIR__ . '/helper/pdfhelper.php');

        $id    = $this->form->getData()->get('id');
        $model = BaseDatabaseModel::getInstance('Conversion', 'ConvertFormsModel', ['ignore_request' => true]);
        $submission = $model->getItem($id);

        $this->pdfUrl = PDFHelper::getSubmissionPDF($submission) ?: '';

        return $this->pdfUrl;
    }

    /**
     * Skip rendering the control group entirely when no PDF exists.
     *
     * @param   array  $options
     *
     * @return  string
     */
    public function renderField($options = [])
    {
        if (!$this->getPdfUrl())
        {
            return '';
        }

        return parent::renderField($options);
    }

    /**
     * Method to get a list of options for a list input.
     *
     * @return string
     */
    protected function getInput()
    {
        if (!$this->value = $this->getPdfUrl())
        {
            return '';
        }

        return $this->getFilePreview($this->value) . $this->getHelperButtons($this->value);
    }

    /**
     * Renders a file preview block (icon + filename + folder).
     *
     * @param   string  $pdf_url
     *
     * @return  string
     */
    private function getFilePreview($pdf_url)
    {
        $filename = htmlspecialchars(basename(parse_url($pdf_url, PHP_URL_PATH)), ENT_QUOTES, 'UTF-8');
        $folder   = htmlspecialchars(ltrim(dirname(parse_url($pdf_url, PHP_URL_PATH)), '/'), ENT_QUOTES, 'UTF-8');

        return '<div class="d-flex align-items-center gap-3 p-3 rounded border mb-3">'
            . '<div class="rounded d-flex align-items-center justify-content-center text-danger p-2" style="background:#fde8e8;width:40px;height:40px;flex-shrink:0">'
            . '<span class="icon-file-alt fs-5"></span>'
            . '</div>'
            . '<div>'
            . '<div>' . $filename . '</div>'
            . '<div class="text-muted small">' . $folder . '</div>'
            . '</div>'
            . '</div>';
    }

    /**
     * Renders View/Download buttons.
     *
     * @param   string  $pdf_url
     *
     * @return  string
     */
    private function getHelperButtons($pdf_url)
    {
        return '<div class="d-flex gap-2">'
            . '<a href="' . $pdf_url . '" target="_blank" class="btn btn-primary btn-sm">'
            . '<span class="icon-search me-1"></span>' . Text::_('PLG_CONVERTFORMSTOOLS_PDF_SUBMISSION_VIEW_BTN')
            . '</a>'
            . '<a href="' . $pdf_url . '" class="btn btn-outline-secondary btn-sm" download>'
            . '<span class="icon-download me-1"></span>' . Text::_('PLG_CONVERTFORMSTOOLS_PDF_SUBMISSION_DOWNLOAD_BTN')
            . '</a>'
            . '</div>';
    }
}