<?php
/**
 * BookiFlex Core
 *
 * @package    BookiFlex
 * @subpackage Core
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Core\Admin\Menu;

use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Core\Util\Request;

defined( 'ABSPATH' ) || exit;

/**
 * @important
 * In the name of fields use only underscore and lowercase letters.
 */
abstract class AbstractMenu
{
    protected PluginInterface $plugin;

    public function register(PluginInterface $plugin): void
    {
        $this->plugin = $plugin;
        $this->plugin->addAction('admin_menu', [$this, 'registerMenu']);
    }

    abstract public function registerMenu(): void;

	protected function displayListTable(\WP_List_Table $table, string $id, string $title): void
	{
		?>
		<div class="wrap">
			<h1 class="wp-heading-inline">
				<?php echo esc_html($title); ?>
			</h1>

			<turbo-frame
				id="bflex-custom-tables"
				data-turbo-action="advance"
				refresh="morph"
			>
				<?php
				$searchTerm = Request::getString('s');
				if ($searchTerm !== '') {
					printf(
						'<span class="subtitle">%s</span>',
						sprintf(
						    /* translators: %s: search term */
							esc_html__('Search results for: %s', 'bookiflex'),
							'<strong>' . esc_html($searchTerm) . '</strong>'
						)
					);
				}
				?>

				<hr class="wp-header-end" />

				<?php
				$table->prepare_items();
				$table->views();
				?>

				<form id="posts-filter" method="post">
					<?php
					$table->search_box(
						esc_html__('Search', 'bookiflex'),
						'search-field-' . esc_attr($id)
					);

					wp_nonce_field();
					$table->display();
					?>
				</form>
			</turbo-frame>

			<div
				id="<?php echo esc_attr('bflex-app--' . $id . '-app'); ?>"
				data-turbo-permanent
			></div>
		</div>
		<?php
	}
}
