<?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;

/**
 * Restaurant reservation details metadata representation.
 * 
 * @since 1.10
 */
class RestaurantReservationDetails implements Metadata
{
    /** @var int */
    protected $reservationId;

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

    /**
     * Class constructor.
     * 
     * @param  int    $reservationId  The restaurant reservation ID.
     * @param  array  $options        An array of options.
     */
    public function __construct(int $reservationId, array $options = [])
    {
        $this->reservationId = $reservationId;
        $this->options = array_merge([
            'showtable' => false,
            'edit' => true,
        ], $options);
    }

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

        // prepare layout widget data
        $data = [
            'title' => \JText::sprintf('VRE_LOG_RES_NUM', $this->reservationId),
            'icon'  => 'fas fa-shopping-basket',
            'class' => 'restaurant-reservation',
        ];

        // render restaurant reservation widget body
        $data['summary'] = \JLayoutHelper::render('addons.components.reservation', [
            'reservation' => $reservation,
            'showtable' => $this->options['showtable'] ?? false,
        ]);

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

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