<?php
/*
 * @package    ECLCDEK shipping plugin СДЭК to RadicalMart
 * @subpackage    ECLCDEK
 * @version    1.0.1
 * @author Econsult Lab.
 * @copyright  Copyright (c) 2025 Econsult Lab. All rights reserved.
 * @license    GNU/GPL license: https://www.gnu.org/copyleft/gpl.html
 * @link       https://econsultlab.ru
 */

use CdekSDK2\Dto\Tariff;
use Joomla\CMS\Language\Text;
use Joomla\Component\RadicalMart\Administrator\Helper\PriceHelper as RadicalMartPriceHelper;
use Joomla\Plugin\RadicalMartShipping\ECLCDEK\CDEK\ECLTariffList;
use Joomla\Plugin\RadicalMartShipping\ECLCDEK\CDEK\ECLTariffListItem;

extract($displayData);

/**
 * Layout variables
 * -----------------
 *
 * @var  Array  $address      Данные адреса.
 * @var  string $deliveryType Тип доставки.
 * @var  Tariff $tariff       Тариф доставки.
 *
 */
$pvs           = match (true)
{
    $address['type'] === "POSTAMAT" => Text::_("PLG_RADICALMART_SHIPPING_ECLCDEK_TYPE_POSTAMAT"),
    $address['type'] === "PVZ" => Text::_("PLG_RADICALMART_SHIPPING_ECLCDEK_TYPE_PVZ"),
    default => '',
};
$checked_price = "";
$checked_time  = "";
$currency_json = json_encode(RadicalMartPriceHelper::getCurrentCurrency(), JSON_UNESCAPED_UNICODE);
$is_enabled    = $tariff instanceof ECLTariffList && !empty($tariff->tariff_codes);
?>
<?php if ($address): ?>
    <div class="card" id="cdek-tariffs-block">
    <div class="card-body">
    <?php if ($is_enabled): ?>
        <div class="mb1">
            <div class="form-label"><?php echo Text::_("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_TARIFFS_TITLE"); ?></div>
            <div class="form-controls">
                <?php
                /** @var ECLTariffListItem $t */
                foreach ($tariff->tariff_codes as $t):
                    try
                    {
                        $price = RadicalMartPriceHelper::toString($t->delivery_sum);
                    }
                    catch (Exception $e)
                    {
                        $price = $t->delivery_sum . ' ' . $t->currency ?? '₽';
                    }
                    $checked = "";
                    if ($t->checked)
                    {
                        $checked       = 'checked = "checked"';
                        $checked_price = $price;
                        $checked_time  = $t->calendar_max;
                    }
                    ?>
                    <label>
                        <input
                                class="form-check-input"
                                type="radio" <?php echo $checked; ?>
                                name="tariff"
                                value="<?php echo $t->tariff_code; ?>"
                                data-price="<?php echo $price; ?>"
                                data-tariff='<?php echo json_encode($t, JSON_UNESCAPED_UNICODE + JSON_UNESCAPED_SLASHES); ?>'/>
                        <?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_ORDER_LAYOUT_TARIFF", $t->calendar_max, $price, $t->tariff_name) ?>
                    </label><br>
                <?php
                endforeach;
                ?>
            </div>
        </div>
    <?php endif; ?>

    <?php if (!$is_enabled): ?>
        <div class="card-title h4 mb2 text-danger"><?php echo Text::_("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_SHIPPING_DENIED"); ?></div>
        <div class="mb1">
            <div class="card-text text-danger">
                <div class="fs-6 text-danger">
                    <?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_DENIED_ADDRESS", $address['region'], $address['city'], $address['address']); ?>
                </div>
                <div class="fs-6 text-danger">
                    <?php echo Text::_("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_DENIED_ADDRESS_MSG"); ?>
                </div>
            </div>
        </div>
    <?php else: ?>
        <?php if ($deliveryType === "office"): ?>
            <div class="card-title h4 mb2"><?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_OFFICE_TITLE", $pvs, $address['code']); ?></div>
            <div class="mb1">
                <div class="card-text text-muted">
                    <div class="fs-6">
                        <?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_ADDRESS", $address['region'], $address['city'], $address['address']); ?>
                    </div>
                    <div class="fs-6">
                        <?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_WORK_TIME", $address['work_time']); ?>
                    </div>
                    <div class="fs-6" id="calendar_max">
                        <?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_DELIVERY_TIME", $checked_time); ?>
                    </div>
                    <div class="fs-6" id="delivery_sum">
                        <?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_DELIVERY_PRICE", $checked_price); ?>
                    </div>
                </div>
            </div>
        <?php else: ?>
            <div class="uk-h4"><?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_DOOR_TITLE", ""); ?></div>
            <div class="mb1">
                <div class="card-text text-muted">
                    <div class="fs-6">
                        <?php
                        $result   = [];
                        $result[] = $address["formatted"];
                        if (isset($address["entrance"]) && $address["entrance"])
                        {
                            $result[] = mb_strtolower(Text::_("PLG_RADICALMART_SHIPPING_ECLCDEK_CDEK_FIELD_ENTRANCE")) . " " . $address["entrance"];
                        }

                        if (isset($address["floor"]) && $address["floor"])
                        {
                            $result[] = mb_strtolower(Text::_("PLG_RADICALMART_SHIPPING_ECLCDEK_CDEK_FIELD_FLOOR")) . " " . $address["floor"];
                        }

                        if (isset($address["apartment"]) && $address["apartment"])
                        {
                            $result[] = mb_strtolower(Text::_("PLG_RADICALMART_SHIPPING_ECLCDEK_CDEK_FIELD_APARTMENT")) . " " . $address["apartment"];
                        }

                        $result = implode(", ", $result);
                        ?>
                        <?php echo $result; ?>
                    </div>
                    <div class="fs-6" id="calendar_max">
                        <?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_DELIVERY_TIME", $checked_time); ?>
                    </div>
                    <div class="fs-6" id="delivery_sum">
                        <?php echo Text::sprintf("PLG_RADICALMART_SHIPPING_ECLCDEK_LAYOUT_RESULT_DELIVERY_PRICE", $checked_price); ?>
                    </div>
                </div>
            </div>
        <?php endif; ?>
        </div>
        </div>
    <?php endif; ?>
<?php endif; ?>
