<?php
/**
 * Global section library admin UI.
 *
 * @package EasySite_Pro
 */

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

/**
 * Register section library menu.
 */
function easysite_section_library_menu() {
	if ( ! easysite_bundle_active() ) {
		return;
	}
	add_submenu_page(
		'easysite-setup',
		__( 'Section Library', 'easysite-pro' ),
		__( 'Section Library', 'easysite-pro' ),
		'edit_theme_options',
		'easysite-section-library',
		'easysite_render_section_library_page'
	);
}
add_action( 'admin_menu', 'easysite_section_library_menu', 18 );

/**
 * Handle library actions.
 */
function easysite_section_library_actions() {
	if ( ! isset( $_GET['page'] ) || 'easysite-section-library' !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return;
	}
	if ( ! current_user_can( 'edit_theme_options' ) ) {
		return;
	}

	if ( isset( $_POST['easysite_library_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['easysite_library_nonce'] ) ), 'easysite_section_library' ) ) {
		$sections = easysite_get_global_sections();
		if ( isset( $_POST['delete_index'] ) ) {
			$idx = absint( $_POST['delete_index'] );
			if ( isset( $sections[ $idx ] ) ) {
				array_splice( $sections, $idx, 1 );
				update_option( 'easysite_global_sections', $sections );
			}
		}
		if ( isset( $_POST['rename_index'], $_POST['rename_label'] ) ) {
			$idx = absint( $_POST['rename_index'] );
			if ( isset( $sections[ $idx ] ) ) {
				$sections[ $idx ]['label'] = sanitize_text_field( wp_unslash( $_POST['rename_label'] ) );
				update_option( 'easysite_global_sections', $sections );
			}
		}
	}

	if ( isset( $_GET['export_index'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$idx = absint( $_GET['export_index'] );
		$sections = easysite_get_global_sections();
		if ( isset( $sections[ $idx ] ) ) {
			header( 'Content-Type: application/json' );
			header( 'Content-Disposition: attachment; filename="easysite-section.json"' );
			echo wp_json_encode( $sections[ $idx ] );
			exit;
		}
	}
}
add_action( 'admin_init', 'easysite_section_library_actions' );

/**
 * Render section library page.
 */
function easysite_render_section_library_page() {
	$sections = easysite_get_global_sections();
	?>
	<div class="wrap easysite-section-library">
		<h1><?php esc_html_e( 'Section Library', 'easysite-pro' ); ?></h1>
		<p><?php esc_html_e( 'Saved sections from the visual editor. Insert them on any page via Add Section.', 'easysite-pro' ); ?></p>
		<?php if ( empty( $sections ) ) : ?>
			<p><?php esc_html_e( 'No saved sections yet. In the visual editor, click the star icon on any section to save it here.', 'easysite-pro' ); ?></p>
		<?php else : ?>
			<table class="widefat striped">
				<thead><tr><th><?php esc_html_e( 'Name', 'easysite-pro' ); ?></th><th><?php esc_html_e( 'Type', 'easysite-pro' ); ?></th><th><?php esc_html_e( 'Actions', 'easysite-pro' ); ?></th></tr></thead>
				<tbody>
				<?php foreach ( $sections as $i => $section ) : ?>
					<tr>
						<td>
							<form method="post" style="display:inline">
								<?php wp_nonce_field( 'easysite_section_library', 'easysite_library_nonce' ); ?>
								<input type="hidden" name="rename_index" value="<?php echo esc_attr( (string) $i ); ?>" />
								<input type="text" name="rename_label" value="<?php echo esc_attr( $section['label'] ?? '' ); ?>" />
								<button type="submit" class="button button-small"><?php esc_html_e( 'Rename', 'easysite-pro' ); ?></button>
							</form>
						</td>
						<td><?php echo esc_html( $section['block']['type'] ?? '' ); ?></td>
						<td>
							<a class="button button-small" href="<?php echo esc_url( admin_url( 'admin.php?page=easysite-section-library&export_index=' . $i ) ); ?>"><?php esc_html_e( 'Export', 'easysite-pro' ); ?></a>
							<form method="post" style="display:inline">
								<?php wp_nonce_field( 'easysite_section_library', 'easysite_library_nonce' ); ?>
								<input type="hidden" name="delete_index" value="<?php echo esc_attr( (string) $i ); ?>" />
								<button type="submit" class="button button-small" onclick="return confirm('<?php esc_attr_e( 'Delete this section?', 'easysite-pro' ); ?>');"><?php esc_html_e( 'Delete', 'easysite-pro' ); ?></button>
							</form>
						</td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>
	<?php
}
