<?php
/**
 * Advanced settings for Featured Image Column.
 *
 * @package FeaturedImageColumn
 */

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;

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

$module_id = $module_id ?? 'featured-image-column';

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

$raw      = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
$settings = wp_parse_args( is_array( $raw ) ? $raw : [], $defaults );

$enabled_post_types = $settings['enabled_post_types'] ?? [ 'post', 'page' ];
if ( ! is_array( $enabled_post_types ) ) {
	$enabled_post_types = [ 'post', 'page' ];
}

$available_post_types = [];
foreach ( get_post_types( [ 'public' => true ], 'objects' ) as $post_type_name => $post_type_object ) {
	if ( $post_type_name === 'attachment' || ! post_type_supports( $post_type_name, 'thumbnail' ) ) {
		continue;
	}
	$available_post_types[ $post_type_name ] = $post_type_object;
}

$columns_customizer_enabled = OptionStoreModuleSettingsRepository::isModuleEnabled( 'admin-columns-customizer' );
?>

<div class="wp-powersuite-module-settings">
	<?php if ( $columns_customizer_enabled ) : ?>
		<div class="notice notice-warning inline" style="margin: 0 0 12px;">
			<p>
				<?php
				esc_html_e(
					'Admin Columns Customizer is enabled. It conflicts with this module—disable one of them to use featured image columns reliably.',
					'wp-powersuite'
				);
				?>
			</p>
		</div>
	<?php else : ?>
		<p class="description">
			<?php esc_html_e( 'Choose which list tables show a featured image column. Click a thumbnail in the list to set or change the featured image without opening the editor.', 'wp-powersuite' ); ?>
		</p>
	<?php endif; ?>

	<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( 'Post types', 'wp-powersuite' ); ?></label>
					</th>
					<td>
						<?php if ( $available_post_types === [] ) : ?>
							<p class="description"><?php esc_html_e( 'No public post types with featured image support were found.', 'wp-powersuite' ); ?></p>
						<?php else : ?>
							<p class="description" style="margin-bottom:8px;">
								<?php esc_html_e( 'Show the image column on these admin list tables.', 'wp-powersuite' ); ?>
							</p>
							<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_post_types as $post_type_name => $post_type_object ) : ?>
											<?php
											$label   = $post_type_object->labels->name ?? $post_type_name;
											$checked = in_array( $post_type_name, $enabled_post_types, true );
											?>
											<label class="wpps-ct-multiselect__option" data-slug="<?php echo esc_attr( $post_type_name ); ?>" 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( $post_type_name ); ?>"
													<?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( $post_type_name ); ?>)</span>
											</label>
										<?php endforeach; ?>
									</div>
								</div>
							</div>
						<?php endif; ?>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
</div>
