<?php
// pages/reviews.php
require_once BEEZUI_ROOT . '/components/index.php';
require_once BEEZUI_ROOT . '/pages/layout.php';

use Components\Router;

$pageKey = 'reviews';
$activeTab = (string) mh_get('reviews_tab', 'reviews', 'string');
$search = (string)mh_get('search', '', 'string');
$statusFilter = (string)mh_get('status', '', 'string');
$starsFilter = (string)mh_get('stars', '', 'string');

$tabItems = [
    [
        'value' => 'reviews',
        'label' => \mh_lng('UI_TAB_REVIEWS', 'Reviews'),
    ],
];

$listUrl = Router::url('reviews', ['partial' => 1]);
$trigs = ['refresh'];

ob_start();
?>

<?php if (!mh_get('partial')): ?>
    <div x-data="{ 
            reviews_tab: <?= \Components\Base::h(json_encode($activeTab), false) ?>,
            search: <?= \Components\Base::h(json_encode($search), false) ?>,
            status: <?= \Components\Base::h(json_encode($statusFilter), false) ?>,
            stars: <?= \Components\Base::h(json_encode($starsFilter), false) ?>
         }"
         @hashchange.window="if(location.hash) reviews_tab = location.hash.substring(1)">
    <?php
    $baseNewUrl = Router::url('review_modal', ['partial' => 1, 'mode' => 'new']);
    echo \Components\Ui\PageHeader::render((string) \mh_lng('MAILBEEZ_APP_LABEL_REVIEWS', 'Reviews'), [
        'subtitle' => \mh_lng('MAILBEEZ_APP_LABEL_REVIEWS_SUBTITLE', 'Manage customer product reviews.'),
        'right' => \Components\Button::render(\mh_lng('MAILBEEZ_NEW_REVIEW', 'New Review'), [
            'variant' => 'secondary',
            'size' => 'sm',
            'icon' => \Components\Base::SVG_PLUS,
            'iconPosition' => 'front',
            'attrs' => [
                'command' => 'show-modal',
                'commandfor' => 'config-modal',
                'data-title' => \mh_lng('MAILBEEZ_NEW_REVIEW', 'New Review'),
                'data-size' => '3xl',
                'hx-get' => $baseNewUrl,
                'hx-target' => '#config-modal-body',
                'hx-swap' => 'innerHTML',
                ':hx-vals' => 'JSON.stringify({ reviews_tab: reviews_tab, search: search, status: status, stars: stars })',
            ],
        ]),
    ]);
    ?>
<?php endif; ?>

<div id="reviews-page-container"
     x-cloak
     class="space-y-4"
     hx-get="<?= \Components\Base::h($listUrl) ?>"
     hx-trigger="<?= \Components\Base::h(implode(', ', $trigs)) ?>"
     hx-swap="outerHTML"
     :hx-vals="JSON.stringify({ reviews_tab: reviews_tab, search: search, status: status, stars: stars })">

    <?= \Components\Ui\Tabs::mobile('reviews_tab', $tabItems, ['ariaLabel' => \mh_lng('UI_REVIEWS_SELECT_TAB', 'Select reviews tab')]) ?>

    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4 border-b border-divider">
        <div class="hidden sm:block">
            <?= \Components\Ui\Tabs::desktopNavOnly('reviews_tab', $tabItems) ?>
        </div>
    </div>

    <div class="mt-4">
        <div x-show="reviews_tab === 'reviews'" x-cloak class="space-y-4">
            <?php
            echo \Components\Notification::render(\mh_lng('MAILBEEZ_APP_REVIEWS_EXPLAINER', 'Manage and moderate product reviews from your customers.'), [
                'id' => 'reviews_info',
                'dismissible' => true,
            ]);
            ?>
            <div id="reviews-list"
                 hx-get="<?= Router::url('reviews_list', ['partial' => 1]) ?>"
                 hx-trigger="load, refresh consume"
                 hx-swap="outerHTML">
                <div class="text-sm text-muted"><?= \mh_lng('UI_TEXT_LOADING_REVIEWS', 'Loading reviews') ?></div>
            </div>
        </div>
    </div>
</div>

<?php if (!mh_get('partial')): ?>
</div>
<?php endif; ?>

<?php
$content = ob_get_clean();

if (mh_get('partial')) {
    echo $content;
} else {
    echo layout($pageKey, $content);
}
