<?php
/**
 * Tab: Roles (Free)
 * Displays per-role menu visibility settings.
 * When Pro is active, shows Client Mode badge and status for each role.
 *
 * @package Panelra
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

$pnlrdb_vis    = get_option( 'pnlrdb_role_menu_visibility', array() );
$pnlrdb_is_pro = defined( 'PNLRDBPRO_VERSION' );

// Load Client Mode data only when Pro is active.
$pnlrdb_cm_enabled = $pnlrdb_is_pro && get_option( 'pnlrdbpro_client_mode_enabled', 0 );
$pnlrdb_cm_url     = $pnlrdb_is_pro ? admin_url( 'admin.php?page=pnlrdb-settings&tab=clientmode' ) : '';

global $menu;
$pnlrdb_items = array();
if ( is_array( $menu ) ) {
	foreach ( $menu as $pnlrdb_item ) {
		if ( empty( $pnlrdb_item[2] ) || false !== strpos( $pnlrdb_item[2], 'separator' ) ) { continue; }
		$pnlrdb_items[ $pnlrdb_item[2] ] = wp_strip_all_tags( $pnlrdb_item[0] );
	}
}
?>

<h2 class="ap773-section-title"><?php esc_html_e( 'Roles', 'panelra-dashboard-builder' ); ?></h2>
<p class="ap773-section-desc"><?php esc_html_e( 'Control which main menu items are visible for each user role. Admins always have full access.', 'panelra-dashboard-builder' ); ?></p>

<?php foreach ( get_editable_roles() as $pnlrdb_role_slug => $pnlrdb_role_info ) :
	if ( 'administrator' === $pnlrdb_role_slug ) { continue; }

	$pnlrdb_saved     = isset( $pnlrdb_vis[ $pnlrdb_role_slug ] ) ? (array) $pnlrdb_vis[ $pnlrdb_role_slug ] : array();
	$pnlrdb_in_cm     = $pnlrdb_is_pro && class_exists( 'PnlrdbPro_Client_Mode' )
	             && PnlrdbPro_Client_Mode::is_role_in_client_mode( $pnlrdb_role_slug );
?>

<div class="ap773-role-card" style="<?php echo $pnlrdb_in_cm ? 'border-color:#c7d2fe;' : ''; ?>">

	<!-- Role header -->
	<div class="ap773-role-card-title">
		<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
			<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/>
			<circle cx="12" cy="7" r="4"/>
		</svg>
		<?php echo esc_html( $pnlrdb_role_info['name'] ); ?>
		<span class="ap773-role-badge"><?php echo esc_html( $pnlrdb_role_slug ); ?></span>

		<?php if ( $pnlrdb_in_cm ) : ?>
			<span style="
				margin-left:auto;
				display:inline-flex;
				align-items:center;
				gap:5px;
				font-size:11px;
				font-weight:700;
				color:#6366f1;
				background:#eef2ff;
				border:1px solid #c7d2fe;
				padding:3px 9px;
				border-radius:20px;
				letter-spacing:.03em;
			">
				<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="9 11 12 14 22 4"/></svg>
				<?php esc_html_e( 'Client Mode active', 'panelra-dashboard-builder' ); ?>
			</span>
		<?php endif; ?>
	</div>

	<!-- Banner Client Mode sopra i checkbox -->
	<?php if ( $pnlrdb_in_cm ) : ?>
	<div style="
		padding:10px 14px;
		background:#eef2ff;
		border:1px solid #c7d2fe;
		border-radius:8px;
		margin-bottom:12px;
		font-size:12.5px;
		color:#3730a3;
		line-height:1.6;
		display:flex;
		align-items:flex-start;
		justify-content:space-between;
		gap:12px;
		flex-wrap:wrap;
	">
		<div style="display:flex;align-items:flex-start;gap:8px;">
			<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="#6366f1" stroke-width="2" style="flex-shrink:0;margin-top:1px;">
				<circle cx="12" cy="12" r="10"/>
				<line x1="12" y1="8" x2="12" y2="12"/>
				<line x1="12" y1="16" x2="12.01" y2="16"/>
			</svg>
			<span>
				<strong><?php esc_html_e( 'Pre-populated by Client Mode.', 'panelra-dashboard-builder' ); ?></strong>
				<?php esc_html_e( 'These checkboxes reflect the Client Mode preset. You can modify them freely — your changes will be saved and will override the preset.', 'panelra-dashboard-builder' ); ?>
				<a href="<?php echo esc_url( $pnlrdb_cm_url ); ?>" style="color:#6366f1;font-weight:600;text-decoration:none;margin-left:4px;">
					<?php esc_html_e( 'Manage Client Mode →', 'panelra-dashboard-builder' ); ?>
				</a>
			</span>
		</div>
		<button
			type="button"
			class="ap773pro-reset-to-cm"
			data-role="<?php echo esc_attr( $pnlrdb_role_slug ); ?>"
			data-label="<?php echo esc_attr( $pnlrdb_role_info['name'] ); ?>"
			style="
				display:inline-flex;
				align-items:center;
				gap:5px;
				padding:5px 12px;
				background:#fff;
				border:1px solid #c7d2fe;
				border-radius:6px;
				color:#6366f1;
				font-size:12px;
				font-weight:600;
				cursor:pointer;
				white-space:nowrap;
				flex-shrink:0;
				font-family:inherit;
			"
			title="<?php esc_attr_e( 'Reset checkboxes to Client Mode defaults', 'panelra-dashboard-builder' ); ?>"
		>
			<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
				<polyline points="1 4 1 10 7 10"/>
				<path d="M3.51 15a9 9 0 1 0 .49-3.5"/>
			</svg>
			<?php esc_html_e( 'Reset to preset', 'panelra-dashboard-builder' ); ?>
		</button>
	</div>
	<?php endif; ?>

	<!-- Checkbox menu -->
	<div class="ap773-menu-grid">
		<?php foreach ( $pnlrdb_items as $pnlrdb_slug => $pnlrdb_label ) :
			$pnlrdb_is_checked = in_array( $pnlrdb_slug, $pnlrdb_saved, true );

			// Visual style: in Client Mode, highlight items that belong to the preset
			$pnlrdb_is_preset = $pnlrdb_in_cm && in_array(
				$pnlrdb_slug,
				class_exists( 'PnlrdbPro_Client_Mode' )
					? PnlrdbPro_Client_Mode::get_visible_menu_preset()
					: array(),
				true
			);
		?>
			<label class="ap773-checkbox-item" style="<?php echo ( $pnlrdb_in_cm && $pnlrdb_is_preset && ! $pnlrdb_is_checked ) ? 'opacity:.5;' : ''; ?>">
				<input type="checkbox"
				       name="pnlrdb_role_menu_visibility[<?php echo esc_attr( $pnlrdb_role_slug ); ?>][]"
				       value="<?php echo esc_attr( $pnlrdb_slug ); ?>"
				       <?php checked( $pnlrdb_is_checked ); ?>>
				<span><?php echo esc_html( $pnlrdb_label ); ?></span>
				<?php if ( $pnlrdb_in_cm && $pnlrdb_is_preset ) : ?>
					<span style="font-size:10px;color:#6366f1;font-weight:600;margin-left:2px;" title="<?php esc_attr_e( 'Visible in Client Mode preset', 'panelra-dashboard-builder' ); ?>">●</span>
				<?php endif; ?>
			</label>
		<?php endforeach; ?>
	</div>

	<?php if ( $pnlrdb_in_cm ) : ?>
	<p style="margin:10px 0 0;font-size:11px;color:#94a3b8;">
		<span style="color:#6366f1;">●</span> <?php esc_html_e( 'Dot = included in Client Mode preset', 'panelra-dashboard-builder' ); ?>
	</p>
	<?php endif; ?>

</div>
<?php endforeach; ?>

<?php do_action( 'pnlrdb_after_roles_settings' );
do_action( 'pnlrdb_after_roles_settings' ); ?>

<?php if ( $pnlrdb_is_pro ) : ?>

<?php endif; ?>
