<?php

use WP_PowerSuite\Admin\AdminIcons;
use WP_PowerSuite\Core\ModuleRegistry;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'wp_powersuite_is_license_valid' ) || ! wp_powersuite_is_license_valid() ) {
	$license_page_url = admin_url( 'admin.php?page=powersuite-license' );
	?>
	<div class="wp-powersuite-module-settings wpps-settings-panel" data-module-id="<?php echo esc_attr( $module_id ?? 'admin-menu-organizer' ); ?>">
		<div class="notice notice-warning inline" style="margin:0;">
			<p>
				<?php esc_html_e( 'Admin Menu Organizer requires an active license. Activate your license to reorder, rename, and hide admin menu items.', 'wp-powersuite' ); ?>
				<a href="<?php echo esc_url( $license_page_url ); ?>"><?php esc_html_e( 'Activate license', 'wp-powersuite' ); ?></a>
			</p>
		</div>
	</div>
	<?php
	return;
}

$module_id   = 'admin-menu-organizer';
$module_meta = ModuleRegistry::getInstance()->getModuleMetadata( $module_id );
$is_pro      = ! empty( $module_meta['is_pro'] );

if ( ! isset( $menu_config ) || ! is_array( $menu_config ) ) {
	$menu_config = [];
}

if ( ! isset( $users_map ) ) {
	$users_map = (object) [];
}

?>
<script type="text/javascript">
var wppsMenuOrganizerInitialMenu = <?php echo wp_json_encode( $menu_config ); ?>;
var wppsMenuOrganizerUsersMap = <?php echo wp_json_encode( $users_map ); ?>;
var wppsMenuOrganizerRoles = <?php
	$roles = wp_roles()->get_names();
	$roles_data = [];
	foreach ( $roles as $role_key => $role_label ) {
		$role = get_role( $role_key );
		if ( $role && $role->has_cap( 'read' ) ) {
			$roles_data[ $role_key ] = $role_label;
		}
	}
	echo wp_json_encode( $roles_data );
?>;
</script>
<div class="wrap wpps-menu-organizer-page">
	<hr class="wp-header-end">
	<?php
	\WP_PowerSuite\Admin\PageHero::render_for_module(
		[
			'icon'    => 'dashicons-menu',
			'title'   => __( 'Admin Menu Organizer', 'wp-powersuite' ),
			'tagline' => __( 'Drag and drop menu items to reorder them. Click on an item to edit its title, icon, or visibility. Nested submenu levels from plugins are shown when you expand a parent item.', 'wp-powersuite' ),
			'status'  => $is_pro ? __( 'Premium', 'wp-powersuite' ) : '',
		]
	);
	?>

	<div class="wpps-menu-organizer-notice"></div>

	<div class="wpps-menu-organizer-container">
		<div class="wpps-menu-organizer-main">
			<div class="wpps-menu-organizer-actions-wrapper">
				<div class="wpps-menu-organizer-actions">
					<button type="button" class="button button-primary wp-powersuite-refresh-page wpps-refresh-menu" title="<?php esc_attr_e( 'Refresh page to see changes', 'wp-powersuite' ); ?>" style="display: none;">
						<?php echo AdminIcons::refresh_svg(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- SVG markup from trusted helper. ?>
						<?php esc_html_e( 'Refresh', 'wp-powersuite' ); ?>
					</button>
					<button type="button" class="button wpps-restore-menu">
						<?php esc_html_e( 'Restore Defaults', 'wp-powersuite' ); ?>
					</button>
					<button type="button" class="button button-primary wpps-save-menu">
						<?php esc_html_e( 'Save Changes', 'wp-powersuite' ); ?>
					</button>
				</div>
			</div>

			<div class="wpps-menu-items-list" id="wpps-menu-items-list">
				<div class="wpps-loading-message">
					<?php esc_html_e( 'Loading menu items...', 'wp-powersuite' ); ?>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="wpps-modal" id="wpps-edit-menu-item-modal" style="display: none;">
	<div class="wpps-modal-content">
		<div class="wpps-modal-header">
			<h3 id="wpps-modal-title"><?php esc_html_e( 'Edit Menu Icon', 'wp-powersuite' ); ?></h3>
			<button type="button" class="wpps-modal-close">&times;</button>
		</div>
		<div class="wpps-modal-body">
			<form id="wpps-edit-menu-item-form">
				<input type="hidden" id="wpps-edit-menu-key" />
				<input type="hidden" id="wpps-edit-menu-slug" />
				<input type="hidden" id="wpps-edit-is-submenu" value="0" />
				<input type="hidden" id="wpps-edit-sub-index" value="" />

				<table class="form-table">
					<tbody>
						<tr id="wpps-edit-icon-row">
							<th scope="row">
								<label for="wpps-edit-menu-icon"><?php esc_html_e( 'Icon', 'wp-powersuite' ); ?></label>
							</th>
							<td>
								<input type="text" id="wpps-edit-menu-icon" class="regular-text" placeholder="dashicons-admin-generic" />
								<p class="description">
									<?php esc_html_e( 'Dashicon class name (e.g., dashicons-admin-generic) or URL to an icon image.', 'wp-powersuite' ); ?>
									<?php
									printf(
										' <a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
										esc_url( 'https://developer.wordpress.org/resource/dashicons/' ),
										esc_html__( 'View all Dashicons', 'wp-powersuite' )
									);
									?>
								</p>
							</td>
						</tr>
					</tbody>
				</table>
			</form>
		</div>
		<div class="wpps-modal-footer">
			<button type="button" class="button wpps-modal-cancel"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
			<button type="button" class="button button-primary wpps-modal-save"><?php esc_html_e( 'Save Changes', 'wp-powersuite' ); ?></button>
		</div>
	</div>
</div>

<div class="wpps-modal" id="wpps-add-menu-item-modal" style="display: none;">
	<div class="wpps-modal-content" style="max-width: 600px;">
		<div class="wpps-modal-header">
			<h3 id="wpps-add-modal-title"><?php esc_html_e( 'Add Menu Item', 'wp-powersuite' ); ?></h3>
			<button type="button" class="wpps-modal-close">&times;</button>
		</div>
		<div class="wpps-modal-body">
			<form id="wpps-add-menu-item-form">
				<input type="hidden" id="wpps-add-is-submenu" value="0" />
				<input type="hidden" id="wpps-add-parent-index" value="" />
				<input type="hidden" id="wpps-add-sub-path" value="" />

				<table class="form-table">
					<tbody>
						<tr>
							<th scope="row">
								<label for="wpps-add-menu-title"><?php esc_html_e( 'Title', 'wp-powersuite' ); ?> <span class="description">(required)</span></label>
							</th>
							<td>
								<input type="text" id="wpps-add-menu-title" class="regular-text" placeholder="<?php esc_attr_e( 'Menu Item Title', 'wp-powersuite' ); ?>" required />
							</td>
						</tr>
						<tr>
							<th scope="row">
								<label for="wpps-add-menu-url"><?php esc_html_e( 'URL / Menu Slug', 'wp-powersuite' ); ?> <span class="description">(required)</span></label>
							</th>
							<td>
								<input type="text" id="wpps-add-menu-url" class="regular-text" placeholder="custom-page" required />
								<p class="description">
									<?php esc_html_e( 'The menu slug or URL for this menu item. Must be unique.', 'wp-powersuite' ); ?>
								</p>
							</td>
						</tr>
		<tr id="wpps-add-icon-row">
			<th scope="row">
				<label for="wpps-add-menu-icon"><?php esc_html_e( 'Icon', 'wp-powersuite' ); ?></label>
			</th>
			<td>
				<input type="text" id="wpps-add-menu-icon" class="regular-text" placeholder="dashicons-admin-generic" />
				<p class="description">
					<?php esc_html_e( 'Dashicon class name (e.g., dashicons-admin-generic) or URL to an icon image.', 'wp-powersuite' ); ?>
					<?php
					printf(
						' <a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
						esc_url( 'https://developer.wordpress.org/resource/dashicons/' ),
						esc_html__( 'View all Dashicons', 'wp-powersuite' )
					);
					?>
				</p>
			</td>
		</tr>
		<tr>
			<th scope="row">
				<label for="wpps-add-menu-roles"><?php esc_html_e( 'Roles', 'wp-powersuite' ); ?></label>
			</th>
			<td>
				<fieldset>
					<label for="wpps-add-menu-roles-all">
						<input type="radio" id="wpps-add-menu-roles-all" name="wpps-add-menu-roles" value="all" checked style="margin-right: 5px;" />
						<span><?php esc_html_e( 'All Roles', 'wp-powersuite' ); ?></span>
					</label>
					<br />
					<label for="wpps-add-menu-roles-specific">
						<input type="radio" id="wpps-add-menu-roles-specific" name="wpps-add-menu-roles" value="specific" style="margin-right: 5px;" />
						<span><?php esc_html_e( 'Specific Role(s)', 'wp-powersuite' ); ?></span>
					</label>
				</fieldset>
				<div id="wpps-add-menu-roles-list" style="margin-top: 10px; display: none;">
					<?php
					$roles = wp_roles()->get_names();
					foreach ( $roles as $role_key => $role_label ) {
						echo '<label style="display: block; margin-bottom: 5px; cursor: pointer;">';
						echo '<input type="checkbox" id="wpps-add-menu-role-' . esc_attr( $role_key ) . '" name="wpps-add-menu-roles-list[]" value="' . esc_attr( $role_key ) . '" style="margin-right: 5px;" />';
						echo '<span>' . esc_html( $role_label ) . '</span>';
						echo '</label>';
					}
					?>
				</div>
				<p class="description">
					<?php esc_html_e( 'Select which roles can access this menu item.', 'wp-powersuite' ); ?>
				</p>
			</td>
		</tr>
		<tr>
			<th scope="row">
				<label for="wpps-add-menu-open-new-tab"><?php esc_html_e( 'Open in New Tab', 'wp-powersuite' ); ?></label>
			</th>
			<td>
				<label>
					<input type="checkbox" id="wpps-add-menu-open-new-tab" name="wpps-add-menu-open-new-tab" value="1" style="margin-right: 5px;" />
					<span><?php esc_html_e( 'Open this menu item link in a new tab', 'wp-powersuite' ); ?></span>
				</label>
			</td>
		</tr>
					</tbody>
				</table>
			</form>
		</div>
		<div class="wpps-modal-footer">
			<button type="button" class="button wpps-modal-cancel"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
			<button type="button" class="button button-primary wpps-add-menu-save"><?php esc_html_e( 'Add Menu Item', 'wp-powersuite' ); ?></button>
		</div>
	</div>
</div>
