<?php

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

if ( ! \WP_PowerSuite\Core\NetworkScope::canManagePlugin() ) {
	return;
}

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'duplicate-post-type';
}

$defaults = class_exists( \DuplicatePostType\Module::class )
	? \DuplicatePostType\Module::getDefaultSettings()
	: [
		'enabled_post_types'    => [ 'post', 'page' ],
		'enable_bulk_duplicate' => false,
	];

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

$settings = class_exists( \DuplicatePostType\Module::class )
	? \DuplicatePostType\Module::normalizeSettings( array_merge( $defaults, $settings ) )
	: array_merge( $defaults, $settings );

$enabled_post_types    = $settings['enabled_post_types'] ?? [ 'post', 'page' ];
$enable_bulk_duplicate = ! empty( $settings['enable_bulk_duplicate'] );

$excluded_types  = class_exists( \DuplicatePostType\Module::class )
	? \DuplicatePostType\Module::getExcludedPostTypes()
	: [ 'attachment', 'revision', 'nav_menu_item' ];
$available_types = get_post_types( [ 'public' => true, 'show_ui' => true ], 'objects' );
$available_types = array_diff_key( $available_types, array_flip( $excluded_types ) );
?>

<div class="wp-powersuite-module-settings" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<p class="description">
		<?php esc_html_e( 'Add duplicate functionality to posts, pages, and custom post types. When duplicating, you can specify how many copies to create (1–20).', 'wp-powersuite' ); ?>
	</p>

	<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( 'Enabled Post Types', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<p class="description" style="margin-bottom: 8px;">
							<?php esc_html_e( 'Choose which post types show the Duplicate row action.', 'wp-powersuite' ); ?>
						</p>
						<?php if ( ! empty( $available_types ) ) : ?>
							<div class="wpps-ct-multiselect" id="<?php echo esc_attr( $module_id ); ?>_post_types_ms"
								data-label-none="<?php echo esc_attr( __( 'Select post types…', '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 ); ?>_post_types_list">
									<span class="wpps-ct-multiselect__trigger-text"><?php esc_html_e( 'Select post types…', '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 ); ?>_post_types_list" role="listbox" hidden>
									<input type="text" class="wpps-ct-multiselect__search" placeholder="<?php esc_attr_e( 'Search post types…', '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 ( $available_types as $slug => $obj ) : ?>
											<?php
											$label   = $obj->labels->name ?? ( $obj->labels->singular_name ?? $slug );
											$checked = in_array( $slug, $enabled_post_types, true );
											?>
											<label class="wpps-ct-multiselect__option" data-slug="<?php echo esc_attr( $slug ); ?>" data-label="<?php echo esc_attr( $label ); ?>">
												<input type="checkbox"
													name="enabled_post_types[]"
													class="wp-powersuite-advanced-setting"
													data-setting-key="enabled_post_types"
													value="<?php echo esc_attr( $slug ); ?>"
													<?php checked( $checked ); ?>
												/>
												<span class="wpps-ct-multiselect__option-label"><?php echo esc_html( $label ); ?></span>
												<span class="wpps-ct-multiselect__option-slug">(<?php echo esc_html( $slug ); ?>)</span>
											</label>
										<?php endforeach; ?>
									</div>
								</div>
							</div>
						<?php else : ?>
							<p class="description"><?php esc_html_e( 'No public post types found.', 'wp-powersuite' ); ?></p>
						<?php endif; ?>
					</td>
				</tr>
				<tr>
					<th scope="row">
						<?php esc_html_e( 'Bulk Duplicate', 'wp-powersuite' ); ?>
					</th>
					<td>
						<label>
							<input
								type="checkbox"
								name="enable_bulk_duplicate"
								class="wp-powersuite-advanced-setting"
								data-setting-key="enable_bulk_duplicate"
								value="1"
								<?php checked( $enable_bulk_duplicate ); ?>
							/>
							<?php esc_html_e( 'Enable bulk duplicate action', 'wp-powersuite' ); ?>
						</label>
						<p class="description">
							<?php esc_html_e( 'Adds a Duplicate option to the bulk actions dropdown on enabled post list tables.', 'wp-powersuite' ); ?>
						</p>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
