<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

namespace E4J\VikRestaurants\Metadata\Addon;

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

use E4J\VikRestaurants\Metadata\Metadata;

/**
 * Take-away order details metadata representation.
 * 
 * @since 1.10
 */
class TakeAwayOrderDetails implements Metadata
{
    /** @var int */
    protected $orderId;

    /** @var array */
    protected $options;

    /**
     * Class constructor.
     * 
     * @param  int    $orderId  The take-away order ID.
     * @param  array  $options  An array of options.
     */
    public function __construct(int $orderId, array $options = [])
    {
        $this->orderId = $orderId;
        $this->options = array_merge([
            'showfood' => false,
            'edit' => true,
        ], $options);
    }

    /**
     * @inheritDoc
     */
    public function describe()
    {
        try
        {
            // obtain order details
            $order = \VREOrderFactory::getOrder($this->orderId);
        }
        catch (\Exception $error)
        {
            // the order doesn't exist any longer
            return '<span style="color: #900;">' . $error->getMessage() . '</span>';
        }

        // prepare layout widget data
        $data = [
            'title' => \JText::sprintf('VRE_LOG_ORDER_NUM', $this->orderId),
            'icon'  => 'fas fa-shopping-basket',
            'class' => 'takeaway-order',
        ];

        // render take-away order widget body
        $data['summary'] = \JLayoutHelper::render('addons.components.order', [
            'order' => $order,
            'showfood' => $this->options['showfood'] ?? false,
        ]);

        if (!empty($this->options['edit']))
        {
            // display the button to access the order details
            $data['footer'] = '<a class="btn btn-success" href="index.php?option=com_vikrestaurants&amp;task=tkreservation.edit&amp;cid[]=' . $this->orderId . '" target="_blank">' . \JText::translate('VREDIT') . '</a>';
        }

        // render add-on widget
        return \JLayoutHelper::render('addons.widget', $data);
    }
}
