<?php
/**
 * White Label settings panel (shared by Settings tab and standalone page).
 *
 * @package WP_PowerSuite\Admin\Views\Partials
 * @since 1.0.0
 *
 * Expects: $white_label_settings, $has_agency_license, $brand_name, $default_brand_name,
 * $default_brand_description, $preview_* variables, $hero_logo_url, $white_label_logo_url,
 * $white_label_menu_icon_url, and optional $standalone (bool).
 */

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

use WP_PowerSuite\Core\WhiteLabel;

$standalone           = ! empty( $standalone );
$wl_direct_url        = WhiteLabel::whiteLabelDirectUrl();
$license_direct_url   = WhiteLabel::licenseDirectUrl();
$default_short_name   = WhiteLabel::defaultPluginShortName();
$default_company      = WhiteLabel::defaultCompanyName();
?>

<div class="wpps-white-label-layout">
	<div class="wpps-white-label-main">
		<?php if ( ! $has_agency_license ) : ?>
			<div class="notice notice-info inline wpps-white-label-notice">
				<p><?php esc_html_e( 'Save branding fields anytime. Turning on “Enable white label” requires an Agency license (Elite Licenser Extra Param: agency).', 'wp-powersuite' ); ?></p>
			</div>
		<?php endif; ?>

		<div class="wp-powersuite-settings__card card wpps-settings-card--section">
			<h2 class="wp-powersuite-settings__card-title wpps-settings-section-title"><?php esc_html_e( 'Branding', 'wp-powersuite' ); ?></h2>
			<p class="wpps-settings-section-desc"><?php echo esc_html( sprintf(
				/* translators: %s: current or default plugin name */
				__( 'Replace %s branding across the admin menu, page headers, Quick Launcher, and Plugins screen.', 'wp-powersuite' ),
				$brand_name
			) ); ?></p>

			<div class="wpps-settings-row wpps-white-label-toggle-row">
				<div class="wpps-settings-row__content">
					<strong class="wpps-settings-row__label"><?php esc_html_e( 'Enable white label', 'wp-powersuite' ); ?></strong>
					<p class="description"><?php esc_html_e( 'Applies your custom name, logo, and visibility options site-wide.', 'wp-powersuite' ); ?></p>
				</div>
				<div class="wpps-settings-row__control">
					<label class="wpps-settings-toggle">
						<input type="checkbox" id="wpps-white-label-enabled" value="1" <?php checked( ! empty( $white_label_settings['enabled'] ) ); ?> <?php disabled( ! $has_agency_license ); ?> />
						<span><?php esc_html_e( 'Enabled', 'wp-powersuite' ); ?></span>
					</label>
				</div>
			</div>

			<div class="wpps-white-label-fields">
				<div class="wpps-white-label-field-grid">
					<div class="wpps-white-label-field">
						<label class="wpps-white-label-field__label" for="wpps-white-label-plugin-name"><?php esc_html_e( 'Plugin name', 'wp-powersuite' ); ?></label>
						<p class="description"><?php esc_html_e( 'Full name shown in page headers, Quick Launcher, and the Plugins list.', 'wp-powersuite' ); ?></p>
						<input type="text" id="wpps-white-label-plugin-name" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_attr( (string) ( $white_label_settings['plugin_name'] ?? '' ) ); ?>" placeholder="<?php echo esc_attr( $default_brand_name ); ?>" autocomplete="organization" />
					</div>
					<div class="wpps-white-label-field">
						<label class="wpps-white-label-field__label" for="wpps-white-label-plugin-short-name"><?php esc_html_e( 'Short name', 'wp-powersuite' ); ?></label>
						<p class="description"><?php esc_html_e( 'Compact label for tight UI spaces and emails.', 'wp-powersuite' ); ?></p>
						<input type="text" id="wpps-white-label-plugin-short-name" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_attr( (string) ( $white_label_settings['plugin_short_name'] ?? '' ) ); ?>" placeholder="<?php echo esc_attr( $default_short_name ); ?>" autocomplete="off" />
					</div>
				</div>

				<div class="wpps-white-label-field-grid">
					<div class="wpps-white-label-field">
						<label class="wpps-white-label-field__label" for="wpps-white-label-menu-title"><?php esc_html_e( 'Menu title', 'wp-powersuite' ); ?></label>
						<p class="description"><?php esc_html_e( 'Left admin menu label. Defaults to the plugin name.', 'wp-powersuite' ); ?></p>
						<input type="text" id="wpps-white-label-menu-title" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_attr( (string) ( $white_label_settings['menu_title'] ?? '' ) ); ?>" placeholder="<?php echo esc_attr( $preview_plugin_label ); ?>" autocomplete="off" />
					</div>
					<div class="wpps-white-label-field">
						<label class="wpps-white-label-field__label" for="wpps-white-label-dashboard-title"><?php esc_html_e( 'Dashboard title', 'wp-powersuite' ); ?></label>
						<p class="description"><?php esc_html_e( 'Modules hub page title. Defaults to the plugin name.', 'wp-powersuite' ); ?></p>
						<input type="text" id="wpps-white-label-dashboard-title" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_attr( (string) ( $white_label_settings['dashboard_title'] ?? '' ) ); ?>" placeholder="<?php echo esc_attr( $preview_plugin_label ); ?>" autocomplete="off" />
					</div>
				</div>

				<div class="wpps-white-label-field">
					<label class="wpps-white-label-field__label" for="wpps-white-label-plugin-description"><?php esc_html_e( 'Plugin description', 'wp-powersuite' ); ?></label>
					<p class="description"><?php esc_html_e( 'Shown on the Plugins screen under your plugin name.', 'wp-powersuite' ); ?></p>
					<textarea id="wpps-white-label-plugin-description" class="wpps-white-label-input wpps-white-label-input--textarea wpps-white-label-field" rows="3" placeholder="<?php echo esc_attr( $default_brand_description ); ?>"><?php echo esc_textarea( (string) ( $white_label_settings['plugin_description'] ?? '' ) ); ?></textarea>
				</div>

				<div class="wpps-white-label-field-grid">
					<div class="wpps-white-label-field">
						<label class="wpps-white-label-field__label" for="wpps-white-label-company-name"><?php esc_html_e( 'Company / agency name', 'wp-powersuite' ); ?></label>
						<p class="description"><?php esc_html_e( 'Used in emails and footer branding.', 'wp-powersuite' ); ?></p>
						<input type="text" id="wpps-white-label-company-name" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_attr( (string) ( $white_label_settings['company_name'] ?? '' ) ); ?>" placeholder="<?php echo esc_attr( $default_company ); ?>" autocomplete="organization" />
					</div>
					<div class="wpps-white-label-field">
						<label class="wpps-white-label-field__label" for="wpps-white-label-plugin-author"><?php esc_html_e( 'Author name', 'wp-powersuite' ); ?></label>
						<p class="description"><?php esc_html_e( 'Plugins screen author field.', 'wp-powersuite' ); ?></p>
						<input type="text" id="wpps-white-label-plugin-author" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_attr( (string) ( $white_label_settings['plugin_author'] ?? '' ) ); ?>" autocomplete="organization" />
					</div>
				</div>

				<div class="wpps-white-label-field">
					<label class="wpps-white-label-field__label" for="wpps-white-label-plugin-author-uri"><?php esc_html_e( 'Author / company URL', 'wp-powersuite' ); ?></label>
					<input type="url" id="wpps-white-label-plugin-author-uri" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_url( (string) ( $white_label_settings['plugin_author_uri'] ?? '' ) ); ?>" placeholder="https://example.com" autocomplete="url" />
				</div>
			</div>
		</div>

		<div class="wp-powersuite-settings__card card wpps-settings-card--section">
			<h2 class="wp-powersuite-settings__card-title wpps-settings-section-title"><?php esc_html_e( 'Links', 'wp-powersuite' ); ?></h2>
			<p class="wpps-settings-section-desc"><?php esc_html_e( 'Custom URLs replace vendor links in plugin action links, license UI, and help references when white label is active.', 'wp-powersuite' ); ?></p>

			<div class="wpps-white-label-stacked-fields">
			<div class="wpps-white-label-field-grid">
				<div class="wpps-white-label-field">
					<label class="wpps-white-label-field__label" for="wpps-white-label-website-url"><?php esc_html_e( 'Website URL', 'wp-powersuite' ); ?></label>
					<input type="url" id="wpps-white-label-website-url" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_url( (string) ( $white_label_settings['website_url'] ?? '' ) ); ?>" placeholder="https://example.com" autocomplete="url" />
				</div>
				<div class="wpps-white-label-field">
					<label class="wpps-white-label-field__label" for="wpps-white-label-support-url"><?php esc_html_e( 'Support URL', 'wp-powersuite' ); ?></label>
					<input type="url" id="wpps-white-label-support-url" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_url( (string) ( $white_label_settings['support_url'] ?? '' ) ); ?>" placeholder="https://example.com/support" autocomplete="url" />
				</div>
			</div>
			<div class="wpps-white-label-field">
				<label class="wpps-white-label-field__label" for="wpps-white-label-documentation-url"><?php esc_html_e( 'Documentation URL', 'wp-powersuite' ); ?></label>
				<input type="url" id="wpps-white-label-documentation-url" class="wpps-white-label-input wpps-white-label-field" value="<?php echo esc_url( (string) ( $white_label_settings['documentation_url'] ?? '' ) ); ?>" placeholder="https://example.com/docs" autocomplete="url" />
			</div>
			</div>
		</div>

		<div class="wp-powersuite-settings__card card wpps-settings-card--section">
			<h2 class="wp-powersuite-settings__card-title wpps-settings-section-title"><?php esc_html_e( 'Visual identity', 'wp-powersuite' ); ?></h2>
			<p class="wpps-settings-section-desc"><?php esc_html_e( 'Square PNG or SVG images work best. The menu icon falls back to the logo when empty.', 'wp-powersuite' ); ?></p>

			<div class="wpps-white-label-media-grid">
				<div class="wpps-white-label-media">
					<span class="wpps-white-label-media__label"><?php esc_html_e( 'Logo', 'wp-powersuite' ); ?></span>
					<div class="wpps-white-label-media__box" id="wpps-white-label-logo-preview"<?php echo $white_label_logo_url === '' ? ' data-empty="1"' : ''; ?>>
						<?php if ( $white_label_logo_url !== '' ) : ?>
							<img src="<?php echo esc_url( $white_label_logo_url ); ?>" alt="" width="56" height="56" />
						<?php else : ?>
							<span class="wpps-white-label-media__placeholder dashicons dashicons-format-image" aria-hidden="true"></span>
						<?php endif; ?>
					</div>
					<input type="hidden" id="wpps-white-label-logo-id" class="wpps-white-label-field" value="<?php echo esc_attr( (string) (int) ( $white_label_settings['logo_attachment_id'] ?? 0 ) ); ?>" />
					<div class="wpps-white-label-media__actions">
						<button type="button" class="button button-secondary" id="wpps-white-label-logo-select"><?php esc_html_e( 'Choose logo', 'wp-powersuite' ); ?></button>
						<button type="button" class="button button-link-delete" id="wpps-white-label-logo-remove" <?php echo $white_label_logo_url === '' ? 'style="display:none;"' : ''; ?>><?php esc_html_e( 'Remove', 'wp-powersuite' ); ?></button>
					</div>
				</div>

				<div class="wpps-white-label-media">
					<span class="wpps-white-label-media__label"><?php esc_html_e( 'Menu icon', 'wp-powersuite' ); ?></span>
					<div class="wpps-white-label-media__box" id="wpps-white-label-menu-icon-preview"<?php echo $white_label_menu_icon_url === '' ? ' data-empty="1"' : ''; ?>>
						<?php if ( $white_label_menu_icon_url !== '' ) : ?>
							<img src="<?php echo esc_url( $white_label_menu_icon_url ); ?>" alt="" width="56" height="56" />
						<?php else : ?>
							<span class="wpps-white-label-media__placeholder dashicons dashicons-admin-plugins" aria-hidden="true"></span>
						<?php endif; ?>
					</div>
					<input type="hidden" id="wpps-white-label-menu-icon-id" class="wpps-white-label-field" value="<?php echo esc_attr( (string) (int) ( $white_label_settings['menu_icon_attachment_id'] ?? 0 ) ); ?>" />
					<div class="wpps-white-label-media__actions">
						<button type="button" class="button button-secondary" id="wpps-white-label-menu-icon-select"><?php esc_html_e( 'Choose icon', 'wp-powersuite' ); ?></button>
						<button type="button" class="button button-link-delete" id="wpps-white-label-menu-icon-remove" <?php echo $white_label_menu_icon_url === '' ? 'style="display:none;"' : ''; ?>><?php esc_html_e( 'Remove', 'wp-powersuite' ); ?></button>
					</div>
				</div>
			</div>
		</div>

		<div class="wp-powersuite-settings__card card wpps-settings-card--section">
			<h2 class="wp-powersuite-settings__card-title wpps-settings-section-title"><?php esc_html_e( 'Visibility', 'wp-powersuite' ); ?></h2>
			<p class="wpps-settings-section-desc"><?php esc_html_e( 'Hide vendor-facing items when delivering sites to clients.', 'wp-powersuite' ); ?></p>

			<div class="wpps-settings-row">
				<div class="wpps-settings-row__content">
					<strong class="wpps-settings-row__label"><?php esc_html_e( 'Hide vendor links', 'wp-powersuite' ); ?></strong>
					<p class="description"><?php esc_html_e( 'Hides purchase, renew, documentation, and support links that point to the original vendor.', 'wp-powersuite' ); ?></p>
				</div>
				<div class="wpps-settings-row__control">
					<label class="wpps-settings-toggle">
						<input type="checkbox" id="wpps-white-label-hide-external" class="wpps-white-label-field" value="1" <?php checked( ! empty( $white_label_settings['hide_external_links'] ) ); ?> />
						<span><?php esc_html_e( 'Hidden', 'wp-powersuite' ); ?></span>
					</label>
				</div>
			</div>

			<div class="wpps-settings-row">
				<div class="wpps-settings-row__content">
					<strong class="wpps-settings-row__label"><?php esc_html_e( 'Hide “powered by” references', 'wp-powersuite' ); ?></strong>
					<p class="description"><?php esc_html_e( 'Removes vendor attribution text where the plugin exposes it.', 'wp-powersuite' ); ?></p>
				</div>
				<div class="wpps-settings-row__control">
					<label class="wpps-settings-toggle">
						<input type="checkbox" id="wpps-white-label-hide-powered-by" class="wpps-white-label-field" value="1" <?php checked( ! empty( $white_label_settings['hide_powered_by'] ) ); ?> />
						<span><?php esc_html_e( 'Hidden', 'wp-powersuite' ); ?></span>
					</label>
				</div>
			</div>

			<div class="wpps-settings-row wpps-white-label-hide-row" id="wpps-white-label-hide-license-row">
				<div class="wpps-settings-row__content">
					<strong class="wpps-settings-row__label"><?php esc_html_e( 'Hide License page', 'wp-powersuite' ); ?></strong>
					<p class="description"><?php esc_html_e( 'Removes the License submenu and navigation shortcuts. License checks and updates continue in the background.', 'wp-powersuite' ); ?></p>
					<div class="wpps-white-label-direct-url" id="wpps-white-label-license-url-wrap">
						<label class="wpps-white-label-direct-url__label" for="wpps-white-label-license-direct-url"><?php esc_html_e( 'Direct access URL (bookmark before hiding)', 'wp-powersuite' ); ?></label>
						<div class="wpps-white-label-direct-url__field">
							<input type="text" class="regular-text wpps-white-label-direct-url__input" id="wpps-white-label-license-direct-url" value="<?php echo esc_url( $license_direct_url ); ?>" readonly />
							<button type="button" class="button button-secondary wpps-white-label-copy-url" data-copy-target="wpps-white-label-license-direct-url"><?php esc_html_e( 'Copy link', 'wp-powersuite' ); ?></button>
						</div>
						<p class="description wpps-white-label-recovery-note"><?php esc_html_e( 'Save this URL before hiding the License page. Authorized admins can still open it directly.', 'wp-powersuite' ); ?></p>
					</div>
				</div>
				<div class="wpps-settings-row__control">
					<label class="wpps-settings-toggle">
						<input type="checkbox" id="wpps-white-label-hide-license" class="wpps-white-label-field wpps-white-label-hide-toggle" data-direct-url-target="wpps-white-label-license-direct-url" data-confirm-message="<?php echo esc_attr__( 'Hide the License page from navigation? Copy and save the direct access URL first — you will need it to manage the license later.', 'wp-powersuite' ); ?>" value="1" <?php checked( ! empty( $white_label_settings['hide_license_menu'] ) ); ?> />
						<span><?php esc_html_e( 'Hidden', 'wp-powersuite' ); ?></span>
					</label>
				</div>
			</div>

			<?php if ( ! $standalone ) : ?>
			<div class="wpps-settings-row wpps-white-label-hide-row" id="wpps-white-label-hide-settings-row">
				<div class="wpps-settings-row__content">
					<strong class="wpps-settings-row__label"><?php esc_html_e( 'Hide White Label settings', 'wp-powersuite' ); ?></strong>
					<p class="description"><?php esc_html_e( 'Removes this tab from Settings, Quick Launcher, and search results.', 'wp-powersuite' ); ?></p>
					<div class="wpps-white-label-direct-url" id="wpps-white-label-settings-url-wrap">
						<label class="wpps-white-label-direct-url__label" for="wpps-white-label-settings-direct-url"><?php esc_html_e( 'Direct access URL (bookmark before hiding)', 'wp-powersuite' ); ?></label>
						<div class="wpps-white-label-direct-url__field">
							<input type="text" class="regular-text wpps-white-label-direct-url__input" id="wpps-white-label-settings-direct-url" value="<?php echo esc_url( $wl_direct_url ); ?>" readonly />
							<button type="button" class="button button-secondary wpps-white-label-copy-url" data-copy-target="wpps-white-label-settings-direct-url"><?php esc_html_e( 'Copy link', 'wp-powersuite' ); ?></button>
						</div>
						<p class="description wpps-white-label-recovery-note"><?php esc_html_e( 'After hiding, open this URL to change branding again. Only site/network administrators with plugin access can use it.', 'wp-powersuite' ); ?></p>
					</div>
				</div>
				<div class="wpps-settings-row__control">
					<label class="wpps-settings-toggle">
						<input type="checkbox" id="wpps-white-label-hide-settings" class="wpps-white-label-field wpps-white-label-hide-toggle" data-direct-url-target="wpps-white-label-settings-direct-url" data-confirm-message="<?php echo esc_attr__( 'Hide White Label settings from the admin UI? Copy and save the direct access URL first — you will need it to change branding later.', 'wp-powersuite' ); ?>" value="1" <?php checked( ! empty( $white_label_settings['hide_white_label_settings'] ) ); ?> />
						<span><?php esc_html_e( 'Hidden', 'wp-powersuite' ); ?></span>
					</label>
				</div>
			</div>
			<?php else : ?>
				<div class="notice notice-warning inline wpps-white-label-notice">
					<p><?php esc_html_e( 'You are viewing White Label via the direct access URL. This page stays available to authorized administrators even when hidden from Settings.', 'wp-powersuite' ); ?></p>
				</div>
			<?php endif; ?>
		</div>

		<div class="wpps-white-label-savebar">
			<button type="button" class="button button-primary" id="wpps-white-label-save"><?php esc_html_e( 'Save branding', 'wp-powersuite' ); ?></button>
			<button type="button" class="button button-secondary" id="wpps-white-label-reset"><?php esc_html_e( 'Reset to defaults', 'wp-powersuite' ); ?></button>
			<span class="wpps-white-label-savebar__hint"><?php esc_html_e( 'Changes apply immediately after saving. Refresh the admin if the menu icon does not update.', 'wp-powersuite' ); ?></span>
		</div>
	</div>

	<aside class="wpps-white-label-aside" aria-label="<?php esc_attr_e( 'Branding preview', 'wp-powersuite' ); ?>">
		<div class="wp-powersuite-settings__card card wpps-white-label-preview-card">
			<h2 class="wpps-white-label-preview-card__title"><?php esc_html_e( 'Live preview', 'wp-powersuite' ); ?></h2>
			<p class="description"><?php esc_html_e( 'Approximate appearance before you save.', 'wp-powersuite' ); ?></p>

			<div class="wpps-white-label-preview-block">
				<span class="wpps-white-label-preview-block__label"><?php esc_html_e( 'Admin menu', 'wp-powersuite' ); ?></span>
				<div class="wpps-white-label-preview-menu">
					<span class="wpps-white-label-preview-menu__icon" id="wpps-white-label-preview-menu-icon">
						<?php if ( $white_label_menu_icon_url !== '' ) : ?>
							<img src="<?php echo esc_url( $white_label_menu_icon_url ); ?>" alt="" width="20" height="20" />
						<?php elseif ( $white_label_logo_url !== '' ) : ?>
							<img src="<?php echo esc_url( $white_label_logo_url ); ?>" alt="" width="20" height="20" />
						<?php else : ?>
							<span class="dashicons dashicons-admin-plugins" aria-hidden="true"></span>
						<?php endif; ?>
					</span>
					<span class="wpps-white-label-preview-menu__title" id="wpps-white-label-preview-menu-title"><?php echo esc_html( $preview_menu_label ); ?></span>
				</div>
			</div>

			<div class="wpps-white-label-preview-block">
				<span class="wpps-white-label-preview-block__label"><?php esc_html_e( 'Page header', 'wp-powersuite' ); ?></span>
				<div class="wpps-white-label-preview-hero">
					<span class="wpps-white-label-preview-hero__icon" id="wpps-white-label-preview-hero-icon">
						<?php if ( $white_label_logo_url !== '' ) : ?>
							<img src="<?php echo esc_url( $white_label_logo_url ); ?>" alt="" width="28" height="28" />
						<?php else : ?>
							<span class="dashicons dashicons-admin-plugins" aria-hidden="true"></span>
						<?php endif; ?>
					</span>
					<div class="wpps-white-label-preview-hero__copy">
						<strong id="wpps-white-label-preview-hero-title"><?php echo esc_html( $preview_plugin_label ); ?></strong>
						<span id="wpps-white-label-preview-hero-tagline"><?php echo esc_html( $preview_description ); ?></span>
					</div>
				</div>
			</div>

			<div class="wpps-white-label-preview-block">
				<span class="wpps-white-label-preview-block__label"><?php esc_html_e( 'Plugins screen', 'wp-powersuite' ); ?></span>
				<div class="wpps-white-label-preview-plugins">
					<strong id="wpps-white-label-preview-plugin-name"><?php echo esc_html( $preview_plugin_label ); ?></strong>
					<p id="wpps-white-label-preview-plugin-description"><?php echo esc_html( $preview_description ); ?></p>
					<?php if ( $preview_author !== '' ) : ?>
						<cite id="wpps-white-label-preview-plugin-author"><?php echo esc_html( sprintf(
							/* translators: %s: author name */
							__( 'By %s', 'wp-powersuite' ),
							$preview_author
						) ); ?></cite>
					<?php else : ?>
						<cite id="wpps-white-label-preview-plugin-author" style="display:none;"></cite>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</aside>
</div>
