<?php
/**
 * Role Manager panel — partial template
 *
 * Included by templates/admin-page.php inside the Role Manager tab panel,
 * only when a valid license is active.
 *
 * Variables available from the including scope:
 *   $role_manager_access  array  Output of Agency_Pulse_Admin_Menu_Role_Manager::get_plugin_access_list().
 *   $all_roles            array  All registered WP roles: slug => display name (from render_builder_page()).
 *
 * @package Agency_Pulse_Admin_Menu
 * @since   1.0.0
 */

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

// Build the list of roles not yet granted the cap (for the grant dropdown).
$_rm_granted_slugs   = array_column( $role_manager_access['roles'], 'slug' );
?>

<?php // ── Plugin Access card ─────────────────────────────────────────────────── ?>
<div class="aamo-access-card">

	<div class="aamo-access-card-header">
		<h2><?php esc_html_e( 'Plugin Access', 'agency-pulse-admin-menu' ); ?></h2>
		<p class="description">
			<?php esc_html_e(
				'Controls which roles and users can open the Menu Organizer. Grant or revoke access below.',
				'agency-pulse-admin-menu'
			); ?>
		</p>
	</div><!-- /.aamo-access-card-header -->

	<?php // ── Grant form ──────────────────────────────────────────────────────── ?>
	<div class="aamo-access-grant">

		<div class="aamo-grant-row">
			<label for="aamo-grant-role-select">
				<?php esc_html_e( 'Grant to role:', 'agency-pulse-admin-menu' ); ?>
			</label>
			<select id="aamo-grant-role-select">
				<option value=""><?php esc_html_e( '— select a role —', 'agency-pulse-admin-menu' ); ?></option>
				<?php foreach ( $all_roles as $_rm_slug => $_rm_name ) :
					$_rm_has = in_array( $_rm_slug, $_rm_granted_slugs, true );
				?>
				<option value="<?php echo esc_attr( $_rm_slug ); ?>"<?php disabled( $_rm_has, true ); ?>>
					<?php echo esc_html( translate_user_role( $_rm_name ) ); ?>
					<?php if ( $_rm_has ) : ?>
					<?php esc_html_e( '(already granted)', 'agency-pulse-admin-menu' ); ?>
					<?php endif; ?>
				</option>
				<?php endforeach; ?>
			</select>
			<button type="button" id="aamo-grant-role" class="button">
				<?php esc_html_e( 'Grant Access', 'agency-pulse-admin-menu' ); ?>
			</button>
		</div><!-- /.aamo-grant-row -->

		<div class="aamo-grant-row">
			<label for="aamo-grant-user-search">
				<?php esc_html_e( 'Grant to user:', 'agency-pulse-admin-menu' ); ?>
			</label>
			<div class="aamo-user-search-wrap">
				<input type="text" id="aamo-grant-user-search" autocomplete="off"
					placeholder="<?php esc_attr_e( 'Search by name or emailâ¦', 'agency-pulse-admin-menu' ); ?>">
				<input type="hidden" id="aamo-grant-user-id" value="">
				<div id="aamo-user-suggestions" class="aamo-user-suggestions"></div>
			</div>
			<button type="button" id="aamo-grant-user" class="button">
				<?php esc_html_e( 'Grant Access', 'agency-pulse-admin-menu' ); ?>
			</button>
		</div><!-- /.aamo-grant-row -->

		<span id="aamo-rm-status" class="aamo-status" aria-live="polite"></span>

	</div><!-- /.aamo-access-grant -->

	<?php // ── Current access list ─────────────────────────────────────────────── ?>
	<div class="aamo-access-current">

		<h3><?php esc_html_e( 'Current Access', 'agency-pulse-admin-menu' ); ?></h3>

		<?php if ( empty( $role_manager_access['roles'] ) && empty( $role_manager_access['users'] ) ) : ?>

			<p class="description aamo-no-access">
				<?php esc_html_e( 'No roles or users currently have Menu Organizer access.', 'agency-pulse-admin-menu' ); ?>
			</p>

		<?php else : ?>

		<ul class="aamo-access-list">

			<?php foreach ( $role_manager_access['roles'] as $entry ) :
				$_is_admin = ( 'administrator' === $entry['slug'] );
			?>
			<li class="aamo-access-entry<?php echo $entry['is_builtin'] ? ' aamo-access-builtin' : ''; ?>"
				data-role="<?php echo esc_attr( $entry['slug'] ); ?>">
				<span class="aamo-access-icon dashicons dashicons-groups"></span>
				<span class="aamo-access-name"><?php echo esc_html( $entry['name'] ); ?></span>
				<span class="aamo-access-badge">
					<?php echo $entry['is_builtin']
						? esc_html__( 'built-in role', 'agency-pulse-admin-menu' )
						: esc_html__( 'role', 'agency-pulse-admin-menu' ); ?>
				</span>
				<button type="button"
					class="aamo-revoke-role button button-small<?php echo $_is_admin ? ' aamo-revoke-warn' : ''; ?>"
					data-role="<?php echo esc_attr( $entry['slug'] ); ?>"
					<?php if ( $_is_admin ) : ?>
					title="<?php esc_attr_e( 'Revoking administrator access may lock you out of the Menu Organizer.', 'agency-pulse-admin-menu' ); ?>"
					<?php endif; ?>>
					<?php esc_html_e( 'Revoke', 'agency-pulse-admin-menu' ); ?>
				</button>
			</li>
			<?php endforeach; ?>

			<?php foreach ( $role_manager_access['users'] as $entry ) : ?>
			<li class="aamo-access-entry aamo-access-user"
				data-user-id="<?php echo esc_attr( $entry['id'] ); ?>">
				<span class="aamo-access-icon dashicons dashicons-admin-users"></span>
				<span class="aamo-access-name"><?php echo esc_html( $entry['display_name'] ); ?></span>
				<span class="aamo-access-email">&lt;<?php echo esc_html( $entry['email'] ); ?>&gt;</span>
				<span class="aamo-access-badge aamo-access-badge-user">
					<?php esc_html_e( 'user', 'agency-pulse-admin-menu' ); ?>
				</span>
				<button type="button"
					class="aamo-revoke-user button button-small"
					data-user-id="<?php echo esc_attr( $entry['id'] ); ?>">
					<?php esc_html_e( 'Revoke', 'agency-pulse-admin-menu' ); ?>
				</button>
			</li>
			<?php endforeach; ?>

		</ul>

		<?php endif; ?>

	</div><!-- /.aamo-access-current -->

</div><!-- /.aamo-access-card -->

<?php
$_rm_roles = Agency_Pulse_Admin_Menu_Role_Manager::get_instance()->get_roles_for_display();
?>

<?php // ── Role Editor ─────────────────────────────────────────────────────────── ?>
<div class="aamo-role-editor-wrap">

	<div class="aamo-role-editor-header">
		<h2><?php esc_html_e( 'Role Editor', 'agency-pulse-admin-menu' ); ?></h2>
		<p class="description">
			<?php esc_html_e( 'Select a role to view its capabilities. Clone or delete custom roles.', 'agency-pulse-admin-menu' ); ?>
		</p>
	</div><!-- /.aamo-role-editor-header -->

	<div class="aamo-role-editor-cols">

		<?php // ── Left: role list ─────────────────────────────────────────────── ?>
		<div class="aamo-role-list-col">
			<ul id="aamo-role-list">
				<?php foreach ( $_rm_roles as $_re_role ) : ?>
				<li class="aamo-role-item<?php echo $_re_role['is_builtin'] ? ' aamo-role-builtin' : ''; ?>"
					data-slug="<?php echo esc_attr( $_re_role['slug'] ); ?>"
					data-user-count="<?php echo esc_attr( $_re_role['user_count'] ); ?>"
					tabindex="0">
					<span class="aamo-role-name"><?php echo esc_html( $_re_role['name'] ); ?></span>
					<span class="aamo-role-meta">
						<span class="aamo-role-cap-count"><?php echo esc_html( sprintf(
							/* translators: %d = number of capabilities */
							_n( '%d cap', '%d caps', $_re_role['cap_count'], 'agency-pulse-admin-menu' ),
							$_re_role['cap_count']
						) ); ?></span>
						<?php if ( $_re_role['is_builtin'] ) : ?>
						<span class="aamo-role-builtin-badge"><?php esc_html_e( 'built-in', 'agency-pulse-admin-menu' ); ?></span>
						<?php endif; ?>
					</span>
					<span class="aamo-role-actions">
						<button type="button"
							class="aamo-clone-role button button-small"
							data-slug="<?php echo esc_attr( $_re_role['slug'] ); ?>"
							title="<?php esc_attr_e( 'Clone this role', 'agency-pulse-admin-menu' ); ?>">
							<?php esc_html_e( 'Clone', 'agency-pulse-admin-menu' ); ?>
						</button>
						<?php if ( ! $_re_role['is_builtin'] ) : ?>
						<button type="button"
							class="aamo-delete-role button button-small button-link-delete"
							data-slug="<?php echo esc_attr( $_re_role['slug'] ); ?>"
							title="<?php esc_attr_e( 'Delete this role', 'agency-pulse-admin-menu' ); ?>">
							<?php esc_html_e( 'Delete', 'agency-pulse-admin-menu' ); ?>
						</button>
						<?php endif; ?>
					</span>
				</li>
				<?php endforeach; ?>
			</ul>
			<div class="aamo-role-list-footer">
				<button type="button" id="aamo-new-role" class="button button-small">
					<?php esc_html_e( '+ New Role', 'agency-pulse-admin-menu' ); ?>
				</button>
			</div>
		</div><!-- /.aamo-role-list-col -->

		<?php // ── Right: capability panel ──────────────────────────────────────── ?>
		<div class="aamo-role-caps-col">
			<div id="aamo-role-caps-panel" class="aamo-role-caps-panel aamo-caps-empty">
				<p class="description">
					<?php esc_html_e( 'Select a role on the left to view and edit its capabilities.', 'agency-pulse-admin-menu' ); ?>
				</p>
			</div>
		</div><!-- /.aamo-role-caps-col -->

	</div><!-- /.aamo-role-editor-cols -->

</div><!-- /.aamo-role-editor-wrap -->
