<?php
/**
 * Options Page Builder - List View
 *
 * @package OptionsPageBuilder
 * @since 1.0.0
 */

use WP_PowerSuite\Core\ModuleRegistry;

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

$pages = $pages ?? [];

$module_meta = ModuleRegistry::getInstance()->getModuleMetadata( 'options-page-builder' );
$is_pro      = ! empty( $module_meta['is_pro'] );

$add_url = admin_url( 'admin.php?page=powersuite-options-page-builder&action=add' );

$opb_meta = '';
if ( ! empty( $pages ) ) {
	$opb_meta = sprintf(
		'<button type="button" class="button button-secondary" id="wpps-opb-export-btn">%s</button>'
		. '<button type="button" class="button button-secondary" id="wpps-opb-import-btn">%s</button>',
		esc_html__( 'Export All', 'wp-powersuite' ),
		esc_html__( 'Import', 'wp-powersuite' )
	);
}
?>
<div class="wrap wpps-opb-page" data-opb-nonce="<?php echo esc_attr( wp_create_nonce( 'wpps_opb' ) ); ?>">
	<hr class="wp-header-end" style="margin:0;padding:0;border:none;height:0;overflow:hidden;">
	<?php
	\WP_PowerSuite\Admin\PageHero::render_for_module(
		[
			'icon'      => 'dashicons-admin-settings',
			'title'     => __( 'Options Page Builder', 'wp-powersuite' ),
			'tagline'   => __( 'Create custom admin pages with settings panels to store site options and configuration', 'wp-powersuite' ),
			'status'    => $is_pro ? __( 'Premium', 'wp-powersuite' ) : '',
			'actions'   => [
				[
					'label' => __( 'Add New Page', 'wp-powersuite' ),
					'url'   => $add_url,
					'icon'  => 'dashicons-plus-alt',
				],
			],
			'meta_html' => $opb_meta,
		]
	);
	?>

	<div class="wpps-opb-container">
		<div class="wpps-opb-main">
			<?php if ( empty( $pages ) ) : ?>
				<div class="wpps-opb-empty-state">
					<span class="dashicons dashicons-admin-settings wpps-opb-empty-icon" aria-hidden="true"></span>
					<h3><?php esc_html_e( 'No options pages yet', 'wp-powersuite' ); ?></h3>
					<p class="description"><?php esc_html_e( 'Create your first options page to add custom settings screens in the WordPress admin.', 'wp-powersuite' ); ?></p>
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=powersuite-options-page-builder&action=add' ) ); ?>" class="button button-primary button-hero">
						<?php esc_html_e( 'Create Options Page', 'wp-powersuite' ); ?>
					</a>
				</div>
			<?php else : ?>
				<div class="wpps-opb-list-header">
					<h2><?php esc_html_e( 'Your Options Pages', 'wp-powersuite' ); ?></h2>
					<p class="description"><?php esc_html_e( 'Click a page to edit its settings and fields. Export individual pages or use Import to merge JSON definitions.', 'wp-powersuite' ); ?></p>
				</div>
				<div class="wpps-opb-pages-list">
					<?php foreach ( $pages as $p ) : ?>
						<?php
						if ( ! is_array( $p ) || empty( $p['slug'] ) ) {
							continue;
						}
						$field_count = 0;
						foreach ( $p['fields'] ?? [] as $field ) {
							if ( is_array( $field ) && ( $field['type'] ?? '' ) !== 'group' ) {
								++$field_count;
							}
						}
						$icon_class   = ! empty( $p['icon'] ) ? (string) $p['icon'] : 'dashicons-admin-generic';
						$is_top_level = empty( $p['parent_slug'] );
						$edit_url     = admin_url( 'admin.php?page=powersuite-options-page-builder&action=edit&page_id=' . urlencode( $p['id'] ?? '' ) );
						$live_cap     = ! empty( $p['capability'] ) ? sanitize_key( (string) $p['capability'] ) : 'manage_options';
						$live_url     = admin_url( 'admin.php?page=' . rawurlencode( (string) $p['slug'] ) );
						$can_open     = current_user_can( $live_cap );
						?>
						<div class="wpps-opb-page-item" data-page-id="<?php echo esc_attr( $p['id'] ?? '' ); ?>">
							<span class="wpps-opb-page-item-icon dashicons <?php echo esc_attr( $icon_class ); ?>" aria-hidden="true"></span>
							<div class="wpps-opb-page-item-content">
								<div class="wpps-opb-page-item-info">
									<a href="<?php echo esc_url( $edit_url ); ?>" class="wpps-opb-page-item-title"><?php echo esc_html( $p['page_title'] ?? '' ); ?></a>
									<span class="wpps-opb-page-item-meta">
										<code><?php echo esc_html( $p['slug'] ); ?></code>
										<span class="wpps-opb-page-item-sep" aria-hidden="true">&middot;</span>
										<span><?php echo esc_html( $is_top_level ? __( 'Top level menu', 'wp-powersuite' ) : __( 'Submenu', 'wp-powersuite' ) ); ?></span>
										<span class="wpps-opb-page-item-sep" aria-hidden="true">&middot;</span>
										<span><?php
										printf(
											/* translators: %d: number of fields */
											esc_html( _n( '%d field', '%d fields', $field_count, 'wp-powersuite' ) ),
											(int) $field_count
										);
										?></span>
									</span>
								</div>
								<div class="wpps-opb-page-item-actions">
									<?php if ( $can_open ) : ?>
										<a href="<?php echo esc_url( $live_url ); ?>" class="button button-small"><?php esc_html_e( 'Open', 'wp-powersuite' ); ?></a>
									<?php endif; ?>
									<a href="<?php echo esc_url( $edit_url ); ?>" class="button button-small"><?php esc_html_e( 'Edit', 'wp-powersuite' ); ?></a>
									<button type="button" class="button button-small wpps-opb-page-export" data-page-id="<?php echo esc_attr( $p['id'] ?? '' ); ?>"><?php esc_html_e( 'Export', 'wp-powersuite' ); ?></button>
									<button type="button" class="button button-small button-link-delete wpps-opb-page-delete" data-page-id="<?php echo esc_attr( $p['id'] ?? '' ); ?>"><?php esc_html_e( 'Delete', 'wp-powersuite' ); ?></button>
								</div>
							</div>
						</div>
					<?php endforeach; ?>
				</div>
			<?php endif; ?>
		</div>
	</div>

	<div class="wpps-modal" id="wpps-opb-import-modal" role="dialog" aria-hidden="true" style="display: none;">
		<div class="wpps-modal-content">
			<div class="wpps-modal-header">
				<h3><?php esc_html_e( 'Import Options Pages', 'wp-powersuite' ); ?></h3>
				<button type="button" class="wpps-modal-close" aria-label="<?php esc_attr_e( 'Close', 'wp-powersuite' ); ?>">&times;</button>
			</div>
			<div class="wpps-modal-body">
				<p class="description"><?php esc_html_e( 'Paste exported JSON below. Page definitions are merged by slug; saved field values are restored when included in the export.', 'wp-powersuite' ); ?></p>
				<textarea id="wpps-opb-import-json" class="large-text code" rows="8" placeholder='{"pages":[...]}'></textarea>
				<p>
					<label>
						<input type="checkbox" id="wpps-opb-import-replace">
						<?php esc_html_e( 'Replace all (delete existing pages first)', 'wp-powersuite' ); ?>
					</label>
				</p>
			</div>
			<div class="wpps-modal-footer">
				<button type="button" class="button" id="wpps-opb-import-cancel"><?php esc_html_e( 'Cancel', 'wp-powersuite' ); ?></button>
				<button type="button" class="button button-primary" id="wpps-opb-import-submit"><?php esc_html_e( 'Import', 'wp-powersuite' ); ?></button>
			</div>
		</div>
	</div>
</div>
