<?php
// pages/components/topics_list.php
// Lists all topics with actions opening the global modal
use Components\Router;
use Components\Models\Topic;
use Components\Resources\ResourceList;

$oob = (int) (mh_get('oob', 0, 'int'));
$page = (int)mh_get('page', 1, 'int');
$search = (string)mh_get('search', '', 'string');
$visibility = (string)mh_get('visibility', 'public', 'string');
$perPage = 10;

// Build items from model
$items = [];
$total = 0;
if (function_exists('mh_db_query')) {
    $isPublic = ($visibility === 'public') ? 'True' : 'False';
    $paginationResult = Topic::paginate($perPage, $page, $search, ['newsletter_categories_name'], ['is_public' => $isPublic]);
    $total = $paginationResult['total'];
    foreach ($paginationResult['items'] as $t) {
        $id = (int)($t->newsletter_categories_id ?? 0);
        if ($id <= 0) { continue; }
        $name = (string)($t->newsletter_categories_name ?? (\mh_lng('MAILBEEZ_APP_LABEL_TOPIC', 'Topic') . ' ' . $id));
        $isPublic = (string)($t->is_public ?? '');
        $status = ($isPublic === 'True') ? 'Y' : '';
        $items[] = [
            'id' => $id, 
            'label' => $name,
            'description' => (string)($t->newsletter_categories_description) ?? '123',
            'status' => $status,
            'icon' => ['svg' => ($isPublic === 'True') ? \Components\Base::SVG_TAG : \Components\Base::SVG_OUTLINE_TAG, 'color' => 'brand', 'size' => 'sm']
        ];
    }
}

// Update outer attrs
$refreshUrl = Router::url('topics_list', [
    'partial' => 1,
    'search' => $search,
    'page' => $page,
    'visibility' => $visibility,
]);
$containerId = 'topics-list-' . ($visibility === 'private' ? 'private' : 'public');
$outerAttrs = 'id="' . $containerId . '" hx-trigger="topic:created from:body, topic:updated from:body, topic:deleted from:body" hx-get="' . \Components\Base::h($refreshUrl) . '" hx-target="this" hx-swap="outerHTML"';
if ($oob) { $outerAttrs .= ' hx-swap-oob="outerHTML"'; }

ob_start();

$viewBase = Router::url('topic_modal', ['partial' => 1, 'mode' => 'view']);
$editBase = Router::url('topic_modal', ['partial' => 1, 'mode' => 'edit']);

echo ResourceList::render([
    'title' => \mh_lng('MAILBEEZ_APP_LABEL_TOPICS', 'Topics'),
    'items' => $items,
    'id_key' => 'id',
    'label_key' => 'label',
    'sublabel_key' => 'description',
    'show_header' => false,
    'hide_name_column' => true,
    'columns' => [
        [
            'type' => 'identity',
            'label' => \mh_lng('MAILBEEZ_APP_LABEL_TOPIC', 'Topic'),
        ]
    ],
    'outer_attrs' => $outerAttrs,
    'empty_text' => \mh_lng('MAILBEEZ_NO_TOPICS_FOUND', 'No topics found.'),
    'panel_type' => 'drawer',
    'search' => [
        'value' => $search,
        'target' => '#' . $containerId,
        'url' => Router::url('topics_list', ['partial' => 1, 'visibility' => $visibility]),
    ],
    'pagination' => [
        'total' => $total,
        'per_page' => $perPage,
        'current_page' => $page,
        'url' => Router::url('topics_list', ['partial' => 1, 'search' => $search, 'visibility' => $visibility]),
        'target' => '#' . $containerId,
    ],
//    'new_action' => [
//        'label' => 'New',
//        'url' => Router::url('topic_modal', ['partial' => 1, 'mode' => 'new']),
//        'attrs' => [
//            'data-title' => 'New Topic',
//            'data-size' => 'xl',
//        ],
//        'variant' => 'primary',
//    ],
    'row_actions_as_menu' => true,
    'menu_trigger' => 'ellipsis',
    'row_actions' => [
        [
            'label' => \mh_lng('UI_VIEW', 'View'),
            'icon' => \Components\Base::SVG_EYE,
            'url' => $viewBase . '&id={id}',
            'attrs' => [ 'data-title' => \mh_lng('MAILBEEZ_VIEW_TOPIC', 'View Topic'), 'data-size' => 'xl' ],
            'variant' => 'secondary',
        ],
        [
            'label' => \mh_lng('UI_EDIT', 'Edit'),
            'icon' => \Components\Base::SVG_EDIT,
            'url' => $editBase . '&id={id}',
            'attrs' => [ 'data-title' => \mh_lng('MAILBEEZ_EDIT_TOPIC', 'Edit Topic'), 'data-size' => 'xl' ],
            'variant' => 'primary',
        ],
    ],
]);

$__html = ob_get_clean();
echo $__html;
