<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use BlockVideoUploads\Module;
use WP_PowerSuite\Core\NetworkScope;
use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

if ( ! NetworkScope::canManagePlugin() ) {
	return;
}

if ( ! isset( $module_id ) || $module_id === '' ) {
	$module_id = 'block-video-uploads';
}

$raw_settings = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $raw_settings ) ) {
	$raw_settings = [];
}

$settings       = class_exists( Module::class ) ? Module::normalizeSettings( $raw_settings ) : $raw_settings;
$selected_roles = $settings['roles'] ?? [];
$all_roles      = wp_roles()->get_names();
?>

<div class="wp-powersuite-module-settings" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<table class="form-table" role="presentation">
			<tbody>
				<tr>
					<th scope="row">
						<label><?php esc_html_e( 'Block for Roles:', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<?php if ( $all_roles === [] ) : ?>
							<p class="description"><?php esc_html_e( 'No roles found.', 'wp-powersuite' ); ?></p>
						<?php else : ?>
							<p class="description" style="margin-bottom: 8px;"><?php esc_html_e( 'Select user roles that are not allowed to upload video files. They will see a message to embed videos instead. Other roles can upload videos normally.', 'wp-powersuite' ); ?></p>
							<div class="wpps-ct-multiselect" id="<?php echo esc_attr( $module_id ); ?>_roles_ms"
								data-label-none="<?php echo esc_attr( __( 'Select roles...', 'wp-powersuite' ) ); ?>"
								data-label-single="<?php echo esc_attr( __( '1 selected', 'wp-powersuite' ) ); ?>"
								data-label-multi="<?php echo esc_attr( __( '%d selected', 'wp-powersuite' ) ); ?>">
								<button type="button" class="wpps-ct-multiselect__trigger" aria-expanded="false" aria-haspopup="listbox" aria-controls="<?php echo esc_attr( $module_id ); ?>_roles_list">
									<span class="wpps-ct-multiselect__trigger-text"><?php esc_html_e( 'Select roles...', 'wp-powersuite' ); ?></span>
									<span class="dashicons dashicons-arrow-down-alt2 wpps-ct-multiselect__chevron"></span>
								</button>
								<div class="wpps-ct-multiselect__dropdown" id="<?php echo esc_attr( $module_id ); ?>_roles_list" role="listbox" hidden>
									<input type="text" class="wpps-ct-multiselect__search" placeholder="<?php esc_attr_e( 'Search roles...', 'wp-powersuite' ); ?>" autocomplete="off" />
									<div class="wpps-ct-multiselect__actions">
										<button type="button" class="wpps-ct-multiselect__action" data-action="select-all"><?php esc_html_e( 'Select all', 'wp-powersuite' ); ?></button>
										<button type="button" class="wpps-ct-multiselect__action" data-action="clear"><?php esc_html_e( 'Clear', 'wp-powersuite' ); ?></button>
									</div>
									<div class="wpps-ct-multiselect__options">
										<?php foreach ( $all_roles as $role_key => $role_label ) : ?>
											<label class="wpps-ct-multiselect__option" data-slug="<?php echo esc_attr( $role_key ); ?>" data-label="<?php echo esc_attr( $role_label ); ?>">
												<input type="checkbox"
													name="roles[]"
													class="wp-powersuite-advanced-setting"
													data-setting-key="roles"
													value="<?php echo esc_attr( $role_key ); ?>"
													<?php checked( in_array( $role_key, $selected_roles, true ) ); ?>
												/>
												<span class="wpps-ct-multiselect__option-label"><?php echo esc_html( $role_label ); ?></span>
												<span class="wpps-ct-multiselect__option-slug">(<?php echo esc_html( $role_key ); ?>)</span>
											</label>
										<?php endforeach; ?>
									</div>
								</div>
							</div>
						<?php endif; ?>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
