<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use BLR\Backline\Core\Media;
use BLR\Backline\Core\SettingsPage;

$dashboard_slug                 = (string) ( $settings['dashboard_slug'] ?? 'blr-backline' );
$dashboard_url                  = (string) ( $settings['dashboard_url'] ?? home_url( '/' . $dashboard_slug . '/' ) );
$allowed_users                  = is_array( $settings['allowed_users'] ?? null ) ? $settings['allowed_users'] : array();
$users                          = is_array( $settings['users'] ?? null ) ? $settings['users'] : array();
$enabled_modules                = is_array( $settings['enabled_modules'] ?? null ) ? $settings['enabled_modules'] : array();
$module_labels                  = is_array( $settings['module_labels'] ?? null ) ? $settings['module_labels'] : array();
$operator_caps                  = is_array( $settings['operator_caps'] ?? null ) ? $settings['operator_caps'] : array();
$user_module_access             = is_array( $settings['user_module_access'] ?? null ) ? $settings['user_module_access'] : array();
$user_extra_caps                = is_array( $settings['user_extra_caps'] ?? null ) ? $settings['user_extra_caps'] : array();
$capability_labels              = is_array( $settings['capability_labels'] ?? null ) ? $settings['capability_labels'] : array();
$available_extra_caps           = is_array( $settings['available_extra_caps'] ?? null ) ? $settings['available_extra_caps'] : array();
$default_from_name              = (string) ( $settings['default_from_name'] ?? '' );
$default_from_email             = (string) ( $settings['default_from_email'] ?? '' );
$default_reply_to               = (string) ( $settings['default_reply_to'] ?? '' );
$wp_default_from_name           = (string) ( $settings['wp_default_from_name'] ?? get_bloginfo( 'name' ) );
$wp_default_from_email          = (string) ( $settings['wp_default_from_email'] ?? get_option( 'admin_email' ) );
$theme_primary                  = (string) ( $settings['theme_primary'] ?? SettingsPage::theme_primary() );
$sidebar_logo_id                = (int) ( $settings['sidebar_logo_id'] ?? 0 );
$sidebar_logo_url               = (string) ( $settings['sidebar_logo_url'] ?? SettingsPage::sidebar_logo_url() );
$remove_data                    = ! empty( $settings['remove_data_on_uninstall'] );
$diagnostics                    = is_array( $settings['diagnostics'] ?? null ) ? $settings['diagnostics'] : array();
$sidebar_fallback_logo          = defined( 'BLR_BA_URL' ) ? BLR_BA_URL . 'assets/blr-ba/img/Backline.png' : '';
$has_custom_sidebar_logo        = $sidebar_logo_id > 0 && '' !== $sidebar_logo_url;
$sidebar_preview_logo           = $has_custom_sidebar_logo ? $sidebar_logo_url : $sidebar_fallback_logo;
?>
<div class="wrap blr-ba-settings">
	<div class="blr-ba-settings__header">
		<div class="blr-ba-settings__header-copy">
			<div class="blr-ba-settings__title">
					<img class="blr-ba-settings__logo" src="<?php echo esc_url( $sidebar_fallback_logo ); ?>" alt="<?php echo esc_attr__( 'BLR Backline logo', 'blr-backline' ); ?>">
				<h1><?php echo esc_html__( 'Backline Settings', 'blr-backline' ); ?></h1>
			</div>
		</div>

		<div class="blr-ba-settings__header-actions">
			<a class="button button-secondary text-center" href="<?php echo esc_url( $dashboard_url ); ?>" target="_blank" rel="noopener noreferrer">
				<?php echo esc_html__( 'Open dashboard', 'blr-backline' ); ?>
			</a>
		</div>
	</div>

	<?php if ( ! empty( $updated ) ) : ?>
		<div class="blr-ba-alert blr-ba-alert--success" role="status">
			<div class="blr-ba-alert__icon" aria-hidden="true">✓</div>
			<div class="blr-ba-alert__content">
				<div class="blr-ba-alert__title"><?php echo esc_html__( 'Settings saved', 'blr-backline' ); ?></div>
				<p><?php echo esc_html__( 'Your BLR Backline settings were updated successfully.', 'blr-backline' ); ?></p>
			</div>
		</div>
	<?php endif; ?>

	<?php if ( '' !== $err ) : ?>
		<div class="blr-ba-alert blr-ba-alert--error" role="alert">
			<div class="blr-ba-alert__icon" aria-hidden="true">!</div>
			<div class="blr-ba-alert__content">
				<div class="blr-ba-alert__title"><?php echo esc_html__( 'There was a problem', 'blr-backline' ); ?></div>
				<p><?php echo esc_html( $err ); ?></p>
			</div>
		</div>
	<?php endif; ?>

	<div class="blr-ba-settings__layout blr-fw">
		<div class="blr-ba-settings__main">
			<div class="blr-ba-settings__panel">
				<div class="blr-ba-settings__tabs" role="tablist" aria-label="<?php echo esc_attr__( 'Settings sections', 'blr-backline' ); ?>">
					<button type="button" class="blr-ba-settings__tab is-active" data-tab-target="general" role="tab" aria-selected="true"><?php echo esc_html__( 'General', 'blr-backline' ); ?></button>
					<button type="button" class="blr-ba-settings__tab" data-tab-target="access" role="tab" aria-selected="false"><?php echo esc_html__( 'Access', 'blr-backline' ); ?></button>
					<button type="button" class="blr-ba-settings__tab" data-tab-target="mail" role="tab" aria-selected="false"><?php echo esc_html__( 'Mail', 'blr-backline' ); ?></button>
					<button type="button" class="blr-ba-settings__tab" data-tab-target="branding" role="tab" aria-selected="false"><?php echo esc_html__( 'Branding', 'blr-backline' ); ?></button>
					<button type="button" class="blr-ba-settings__tab" data-tab-target="docs" role="tab" aria-selected="false"><?php echo esc_html__( 'Docs', 'blr-backline' ); ?></button>
				</div>

				<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
					<input type="hidden" name="action" value="blr_ba_save_admin_settings">
					<?php wp_nonce_field( 'blr_ba_save_admin_settings' ); ?>

					<section class="blr-ba-settings__tab-panel is-active" data-tab-panel="general" role="tabpanel">
						<div class="blr-ba-settings__section-head">
							<h2><?php echo esc_html__( 'General settings', 'blr-backline' ); ?></h2>
							<p><?php echo esc_html__( 'Core app structure and destructive uninstall behavior.', 'blr-backline' ); ?></p>
						</div>

						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<label for="<?php echo esc_attr( SettingsPage::OPT_DASHBOARD_SLUG ); ?>"><?php echo esc_html__( 'Dashboard slug', 'blr-backline' ); ?></label>
								<p><?php echo esc_html__( 'This controls the public-facing BLR Backline dashboard URL.', 'blr-backline' ); ?></p>
							</div>
							<div class="blr-ba-setting-row__control">
								<input class="regular-text" id="<?php echo esc_attr( SettingsPage::OPT_DASHBOARD_SLUG ); ?>" name="<?php echo esc_attr( SettingsPage::OPT_DASHBOARD_SLUG ); ?>" type="text" value="<?php echo esc_attr( $dashboard_slug ); ?>">
								<p class="description">
									<?php echo esc_html__( 'Current URL:', 'blr-backline' ); ?>
									<a href="<?php echo esc_url( $dashboard_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $dashboard_url ); ?></a>
								</p>
							</div>
						</div>


						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<span><?php echo esc_html__( 'Data cleanup', 'blr-backline' ); ?></span>
								<p><?php echo esc_html__( 'Use this only if you truly want a full uninstall purge.', 'blr-backline' ); ?></p>
							</div>
							<div class="blr-ba-setting-row__control">
								<label class="blr-ba-checkcard blr-ba-checkcard--danger">
									<input name="<?php echo esc_attr( SettingsPage::OPT_REMOVE_DATA_ON_UNINSTALL ); ?>" type="checkbox" value="1" <?php checked( $remove_data, true ); ?>>
									<span class="blr-ba-checkcard__box" aria-hidden="true"></span>
									<span class="blr-ba-checkcard__content">
										<strong><?php echo esc_html__( 'Remove all BLR Backline data on uninstall', 'blr-backline' ); ?></strong>
										<span><?php echo esc_html__( 'This removes plugin data when the plugin is uninstalled.', 'blr-backline' ); ?></span>
									</span>
								</label>
							</div>
						</div>
					</section>

					<section class="blr-ba-settings__tab-panel" data-tab-panel="access" role="tabpanel" hidden>
						<div class="blr-ba-settings__section-head">
							<h2><?php echo esc_html__( 'Access and modules', 'blr-backline' ); ?></h2>
							<p><?php echo esc_html__( 'Enable modules site-wide, then assign only the modules each allowed user actually needs.', 'blr-backline' ); ?></p>
						</div>

						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<span><?php echo esc_html__( 'Enabled modules', 'blr-backline' ); ?></span>
								<p><?php echo esc_html__( 'Turn modules on only when you actually need them. Per-user assignments only apply to modules enabled here.', 'blr-backline' ); ?></p>
							</div>
							<div class="blr-ba-setting-row__control">
								<div class="blr-ba-checkgrid">
									<?php foreach ( $module_labels as $module_key => $module_label ) : ?>
										<label class="blr-ba-checkcard">
											<input type="checkbox" name="<?php echo esc_attr( SettingsPage::OPT_ENABLED_MODULES ); ?>[]" value="<?php echo esc_attr( $module_key ); ?>" <?php checked( in_array( $module_key, $enabled_modules, true ), true ); ?>>
											<span class="blr-ba-checkcard__box" aria-hidden="true"></span>
											<span class="blr-ba-checkcard__content">
												<strong><?php echo esc_html( $module_label ); ?></strong>
											</span>
										</label>
									<?php endforeach; ?>
								</div>
							</div>
						</div>

						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<label for="<?php echo esc_attr( SettingsPage::OPT_ALLOWED_USERS ); ?>"><?php echo esc_html__( 'Allowed users', 'blr-backline' ); ?></label>
								<p><?php echo esc_html__( 'These users can access the BLR Backline dashboard.', 'blr-backline' ); ?></p>
							</div>
							<div class="blr-ba-setting-row__control">
								<select class="blr-ba-select2" id="<?php echo esc_attr( SettingsPage::OPT_ALLOWED_USERS ); ?>" name="<?php echo esc_attr( SettingsPage::OPT_ALLOWED_USERS ); ?>[]" multiple="multiple">
									<?php foreach ( $users as $user ) : ?>
										<?php
										$user_id = (int) $user->ID;
										/* translators: 1: display name, 2: email address. */
										$label = sprintf( __( '%1$s (%2$s)', 'blr-backline' ), (string) $user->display_name, (string) $user->user_email );
										?>
										<option value="<?php echo esc_attr( (string) $user_id ); ?>" <?php selected( in_array( $user_id, $allowed_users, true ), true ); ?>><?php echo esc_html( $label ); ?></option>
									<?php endforeach; ?>
								</select>
							</div>
						</div>

						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<span><?php echo esc_html__( 'Per-user module access', 'blr-backline' ); ?></span>
								<p><?php echo esc_html__( 'Give each allowed user only the modules they need.', 'blr-backline' ); ?></p>
							</div>
							<div class="blr-ba-setting-row__control">
								<?php if ( empty( $allowed_users ) ) : ?>
									<p class="description"><?php echo esc_html__( 'Select one or more allowed users above, then save settings to assign module access.', 'blr-backline' ); ?></p>
								<?php else : ?>
									<div class="blr-ba-user-access-list">
										<?php foreach ( $users as $user ) : ?>
											<?php
											$user_id = (int) $user->ID;
											if ( ! in_array( $user_id, $allowed_users, true ) ) {
												continue;
											}

											$assigned_modules    = is_array( $user_module_access[ $user_id ] ?? null ) ? $user_module_access[ $user_id ] : array();
											$assigned_extra_caps = is_array( $user_extra_caps[ $user_id ] ?? null ) ? $user_extra_caps[ $user_id ] : array();
											$module_count        = count( array_intersect( $assigned_modules, $enabled_modules ) );
											$settings_summary    = array();
											if ( in_array( 'blr_ba_edit_settings', $assigned_extra_caps, true ) ) {
												$settings_summary[] = __( 'can edit settings', 'blr-backline' );
											} elseif ( in_array( 'blr_ba_view_settings', $assigned_extra_caps, true ) ) {
												$settings_summary[] = __( 'can view settings', 'blr-backline' );
											}
											?>
											<div class="blr-ba-checkcard blr-ba-checkcard--stacked">
												<div class="blr-ba-checkcard__content blr-ba-w-100">
													<strong><?php echo esc_html( (string) $user->display_name ); ?></strong>
													<span><?php echo esc_html( (string) $user->user_email ); ?></span>
													<span>
														<?php
														printf(
															/* translators: %d: number of assigned modules. */
															esc_html__( '%d assigned modules', 'blr-backline' ),
															(int) $module_count
														);
														?>
														<?php if ( ! empty( $settings_summary ) ) : ?>
															&middot; <?php echo esc_html( implode( ', ', $settings_summary ) ); ?>
														<?php endif; ?>
													</span>

													<div class="blr-ba-checkgrid blr-ba-mt-12">
														<?php foreach ( $module_labels as $module_key => $module_label ) : ?>
															<?php if ( ! in_array( $module_key, $enabled_modules, true ) ) { continue; } ?>
															<label class="blr-ba-checkcard">
																<input type="checkbox" name="<?php echo esc_attr( SettingsPage::OPT_USER_MODULE_ACCESS ); ?>[<?php echo esc_attr( (string) $user_id ); ?>][]" value="<?php echo esc_attr( $module_key ); ?>" <?php checked( in_array( $module_key, $assigned_modules, true ), true ); ?>>
																<span class="blr-ba-checkcard__box" aria-hidden="true"></span>
																<span class="blr-ba-checkcard__content">
																	<strong><?php echo esc_html( $module_label ); ?></strong>
																	<span><?php echo esc_html( $module_key ); ?></span>
																</span>
															</label>
														<?php endforeach; ?>
													</div>

													<details class="blr-ba-mt-12">
														<summary><?php echo esc_html__( 'Advanced access', 'blr-backline' ); ?></summary>
														<div class="blr-ba-checkgrid blr-ba-mt-12">
															<?php foreach ( $available_extra_caps as $cap_key ) : ?>
																<?php $cap_label = $capability_labels[ $cap_key ] ?? $cap_key; ?>
																<label class="blr-ba-checkcard">
																	<input type="checkbox" name="<?php echo esc_attr( SettingsPage::OPT_USER_EXTRA_CAPS ); ?>[<?php echo esc_attr( (string) $user_id ); ?>][]" value="<?php echo esc_attr( $cap_key ); ?>" <?php checked( in_array( $cap_key, $assigned_extra_caps, true ), true ); ?>>
																	<span class="blr-ba-checkcard__box" aria-hidden="true"></span>
																	<span class="blr-ba-checkcard__content">
																		<strong><?php echo esc_html( $cap_label ); ?></strong>
																		<span><?php echo esc_html( $cap_key ); ?></span>
																	</span>
																</label>
															<?php endforeach; ?>
														</div>
													</details>
												</div>
											</div>
										<?php endforeach; ?>
									</div>
								<?php endif; ?>
							</div>
						</div>
					</section>

					<section class="blr-ba-settings__tab-panel" data-tab-panel="mail" role="tabpanel" hidden>
						<div class="blr-ba-settings__section-head">
							<h2><?php echo esc_html__( 'Mail defaults', 'blr-backline' ); ?></h2>
							<p><?php echo esc_html__( 'Set the default sender identity for outreach and mail actions.', 'blr-backline' ); ?></p>
						</div>

						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<label for="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_FROM_NAME ); ?>"><?php echo esc_html__( 'Default From name', 'blr-backline' ); ?></label>
							</div>
							<div class="blr-ba-setting-row__control">
								<input class="regular-text" id="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_FROM_NAME ); ?>" name="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_FROM_NAME ); ?>" type="text" value="<?php echo esc_attr( $default_from_name ); ?>" placeholder="<?php echo esc_attr( $wp_default_from_name ); ?>">
							</div>
						</div>

						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<label for="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_FROM_EMAIL ); ?>"><?php echo esc_html__( 'Default From email', 'blr-backline' ); ?></label>
							</div>
							<div class="blr-ba-setting-row__control">
								<input class="regular-text" id="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_FROM_EMAIL ); ?>" name="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_FROM_EMAIL ); ?>" type="email" value="<?php echo esc_attr( $default_from_email ); ?>" placeholder="<?php echo esc_attr( $wp_default_from_email ); ?>">
							</div>
						</div>

						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<label for="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_REPLY_TO ); ?>"><?php echo esc_html__( 'Default Reply-To', 'blr-backline' ); ?></label>
							</div>
							<div class="blr-ba-setting-row__control">
								<input class="regular-text" id="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_REPLY_TO ); ?>" name="<?php echo esc_attr( SettingsPage::OPT_DEFAULT_REPLY_TO ); ?>" type="email" value="<?php echo esc_attr( $default_reply_to ); ?>">
							</div>
						</div>
					</section>

					<section class="blr-ba-settings__tab-panel" data-tab-panel="branding" role="tabpanel" hidden>
						<div class="blr-ba-settings__section-head">
							<h2><?php echo esc_html__( 'Branding', 'blr-backline' ); ?></h2>
							<p><?php echo esc_html__( 'Set the sidebar logo and dial in the admin visual identity.', 'blr-backline' ); ?></p>
						</div>

						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<label for="<?php echo esc_attr( SettingsPage::OPT_SIDEBAR_LOGO_ID ); ?>"><?php echo esc_html__( 'Sidebar logo', 'blr-backline' ); ?></label>
								<p><?php echo esc_html__( 'Choose a clean mark for the sidebar brand area.', 'blr-backline' ); ?></p>
							</div>
							<div class="blr-ba-setting-row__control">
								<input type="hidden" id="<?php echo esc_attr( SettingsPage::OPT_SIDEBAR_LOGO_ID ); ?>" name="<?php echo esc_attr( SettingsPage::OPT_SIDEBAR_LOGO_ID ); ?>" value="<?php echo esc_attr( (string) $sidebar_logo_id ); ?>">
								<div class="blr-ba-sidebar-logo-admin-control">
									<div class="blr-ba-sidebar-logo-admin-preview">
										<img
											id="blr-ba-sidebar-logo-preview"
											src="<?php echo esc_url( $sidebar_preview_logo ); ?>"
											alt="<?php echo esc_attr( $has_custom_sidebar_logo ? __( 'Sidebar logo preview', 'blr-backline' ) : __( 'BLR Backline logo', 'blr-backline' ) ); ?>"
											data-fallback-src="<?php echo esc_url( $sidebar_fallback_logo ); ?>"
											data-fallback-alt="<?php echo esc_attr__( 'BLR Backline logo', 'blr-backline' ); ?>"
											data-custom-alt="<?php echo esc_attr__( 'Sidebar logo preview', 'blr-backline' ); ?>"
										>
									</div>

									<div class="blr-ba-sidebar-logo-admin-actions">
										<button type="button" class="button button-secondary" id="blr-ba-choose-sidebar-logo">
											<?php echo esc_html__( 'Choose from Media Library', 'blr-backline' ); ?>
										</button>
										<button type="button" class="button button-link-delete" id="blr-ba-remove-sidebar-logo" <?php disabled( $sidebar_logo_id <= 0 ); ?>>
											<?php echo esc_html__( 'Remove logo', 'blr-backline' ); ?>
										</button>
									</div>

									<p class="description">
										<?php echo esc_html__( 'Select an image from the WordPress media library for the sidebar brand mark.', 'blr-backline' ); ?>
									</p>
								</div>
							</div>
						</div>
						<div class="blr-ba-setting-row">
							<div class="blr-ba-setting-row__label">
								<label for="<?php echo esc_attr( SettingsPage::OPT_THEME_PRIMARY ); ?>"><?php echo esc_html__( 'Primary color', 'blr-backline' ); ?></label>
								<p><?php echo esc_html__( 'Used as the main theme accent across Backline UI elements.', 'blr-backline' ); ?></p>
							</div>
							<div class="blr-ba-setting-row__control">
								<div class="blr-ba-color-control">
									<input id="<?php echo esc_attr( SettingsPage::OPT_THEME_PRIMARY ); ?>" name="<?php echo esc_attr( SettingsPage::OPT_THEME_PRIMARY ); ?>" type="color" value="<?php echo esc_attr( $theme_primary ); ?>">
									<span class="blr-ba-color-control__value"><?php echo esc_html( $theme_primary ); ?></span>
								</div>
							</div>
						</div>
					</section>

					<section class="blr-ba-settings__tab-panel" data-tab-panel="docs" role="tabpanel" hidden>
						<div class="blr-ba-settings__section-head">
							<h2><?php echo esc_html__( 'Documentation', 'blr-backline' ); ?></h2>
							<p><?php echo esc_html__( 'Reference info for common setup and workflow questions.', 'blr-backline' ); ?></p>
						</div>

						<div class="blr-ba-docs-layout">
							<div class="blr-ba-docs-nav" role="tablist" aria-label="<?php echo esc_attr__( 'Documentation topics', 'blr-backline' ); ?>">
								<button type="button" class="blr-ba-docs-tab is-active" data-doc-target="fan-signup" role="tab" aria-selected="true">
									<?php echo esc_html__( 'Fan signup shortcode', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="contacts" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Contacts', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="outreach" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Outreach', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="tasks" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Tasks', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="finances" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Finances', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="events" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Events', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="theme-queries" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Theme queries', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="mail-smtp" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Mail and SMTP', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="dashboard-access" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Dashboard access', 'blr-backline' ); ?>
								</button>
								<button type="button" class="blr-ba-docs-tab" data-doc-target="releases-tracks" role="tab" aria-selected="false">
									<?php echo esc_html__( 'Releases and tracks', 'blr-backline' ); ?>
								</button>
							</div>

							<div class="blr-ba-docs-content">
								<section class="blr-ba-docs-panel is-active" data-doc-panel="fan-signup" role="tabpanel">
									<h3><?php echo esc_html__( 'Fan signup shortcode', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Newsletter and fan signup form tied directly into your Backline contacts.', 'blr-backline' ); ?></p>

									<div class="blr-ba-codebox"><code>[blr_fan_signup]</code></div>

									<div class="blr-ba-codebox"><code>[blr_fan_signup title="Join the mailing list" description="Get updates on releases, shows, and other good stuff." button_text="Sign Up" confirm_sent_message="Check your email to confirm your signup." success_message="Thanks — you're confirmed and on the list." source="homepage" tag="newsletter"]</code></div>

									<p><?php echo esc_html__( 'Use clear source or tag values so you can tell where signups came from later.', 'blr-backline' ); ?></p>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="contacts" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Contacts', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Contacts are the people and organizations your operation depends on: fans, venues, promoters, press, playlist curators, labels, sync leads, and other industry relationships.', 'blr-backline' ); ?></p>
									<ul>
										<li><?php echo esc_html__( 'Use Contact Type to group records by Venue, Promoter, Label, Blog, Playlist, Sync, Press, Fan, or Other.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Store role, company, email, phone, website, city, state, public notes, and internal notes so outreach has useful context.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Newsletter status controls fan list eligibility. Pending is for confirmation-based signups; subscribed contacts are eligible for newsletter sends.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Contacts can be linked to outreach, finance records, tasks, and activity history so relationship context stays in one place.', 'blr-backline' ); ?></li>
									</ul>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="outreach" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Outreach campaigns', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Outreach sends email from your Backline contacts through WordPress mail. Build the campaign, preview the exact recipient list, exclude anyone who should not receive it, then save the draft or send now.', 'blr-backline' ); ?></p>
									<ul>
										<li><?php echo esc_html__( 'Recipients can be selected manually or generated from filters for contact type, state, company, role, newsletter-only contacts, and valid-email contacts.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Excluded contacts override both manual picks and filters. Use exclusions when a filter is almost right but a few people need to stay out.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Campaign delivery statuses include Draft, Queued, Sending, Sent, Partial, and Failed. Workflow statuses include Replied, Follow-up Needed, and Closed.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Attachments are intentionally disabled for outreach. Put EPKs, streams, downloads, and press kits in the email body as links.', 'blr-backline' ); ?></li>
									</ul>
									<p><?php echo esc_html__( 'Available merge tags:', 'blr-backline' ); ?></p>
									<div class="blr-ba-codebox"><code>{{first_name}}, {{name}}, {{email}}, {{company}}, {{city}}, {{state}}, {{role}}, {{contact_type}}</code></div>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="tasks" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Tasks', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Tasks are for keeping band, release, booking, admin, and follow-up work from living in your head or getting buried in texts.', 'blr-backline' ); ?></p>
									<ul>
										<li><?php echo esc_html__( 'Task statuses are Backlog, To Do, In Progress, Blocked, and Done.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Task priorities are Low, Medium, High, and Urgent.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Use due dates for anything that must show up on the dashboard as upcoming, due soon, overdue, or completed.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Assign tasks to the person responsible and keep the notes specific enough that the next action is obvious.', 'blr-backline' ); ?></li>
									</ul>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="finances" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Finances', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Finance records track money movement around the project: income, expenses, invoices, receipts, vendor payments, merch costs, studio bills, and show settlements.', 'blr-backline' ); ?></p>
									<ul>
										<li><?php echo esc_html__( 'Finance types are Income, Expense, and Invoice.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Finance statuses are Draft, Pending, Paid, Partial, Overdue, and Void.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Use category, amount, currency, payment method, date, due date, and reference fields to keep records sortable and useful.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Link a record to a contact when the money is tied to a person, venue, vendor, client, or collaborator.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Attach one image or PDF receipt, invoice, contract page, or proof of payment. Uploading a new file replaces the selected attachment.', 'blr-backline' ); ?></li>
									</ul>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="events" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Events', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Events are for shows, releases, sessions, meetings, livestreams, and other dated work that needs to appear in the Backline calendar or dashboard.', 'blr-backline' ); ?></p>
									<ul>
										<li><?php echo esc_html__( 'Keep event dates, times, location, status, and front-end visibility current so the dashboard and public helpers stay accurate.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Use confirmed public events for shows you want your theme to display, and keep drafts or private planning items hidden from the front end.', 'blr-backline' ); ?></li>
										<li><?php echo esc_html__( 'Events can be pulled into themes with the event helper functions listed in Theme queries.', 'blr-backline' ); ?></li>
									</ul>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="theme-queries" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Theme query helpers', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Themes can query Backline records without hard-coding Backline post types and meta keys. The get helpers return normalized arrays. The query helpers return WP_Query objects.', 'blr-backline' ); ?></p>

									<br>
									
									<h3><?php echo esc_html__( 'Events', 'blr-backline' ); ?></h3>
									<div class="blr-ba-codebox"><code>$events = blr_backline_get_events(['limit' =&gt; 5, 'timeframe' =&gt; 'upcoming']);</code></div>
									<div class="blr-ba-codebox"><code>$event = blr_backline_get_event(123);</code></div>
									<div class="blr-ba-codebox"><code>$event_query = blr_backline_query_events(['status' =&gt; 'confirmed', 'show_frontend' =&gt; true]);</code></div>
									<p><?php echo esc_html__( 'Event helpers default to upcoming confirmed events marked Show on front end. Event args include limit, page, timeframe, status, show_frontend, search, orderby, and order.', 'blr-backline' ); ?></p>

									<br>

									<h3><?php echo esc_html__( 'Albums and releases', 'blr-backline' ); ?></h3>
									<div class="blr-ba-codebox"><code>$albums = blr_backline_get_albums(['limit' =&gt; -1, 'release_status' =&gt; 'any']);</code></div>
									<div class="blr-ba-codebox"><code>$released_albums = blr_backline_get_albums(['release_status' =&gt; 'released']);</code></div>
									<div class="blr-ba-codebox"><code>$unreleased_albums = blr_backline_get_albums(['release_status' =&gt; 'unreleased']);</code></div>
									<div class="blr-ba-codebox"><code>$album_query = blr_backline_query_albums(['release_type' =&gt; 'album']);</code></div>
									<p><?php echo esc_html__( 'Album helpers return published Backline albums/releases. Album args include limit, page, ids, exclude_ids, release_status, release_type, search, orderby, and order.', 'blr-backline' ); ?></p>

									<br>

									<h3><?php echo esc_html__( 'Tracks and playlists', 'blr-backline' ); ?></h3>
									<div class="blr-ba-codebox"><code>$tracks = blr_backline_get_tracks(['limit' =&gt; 20, 'has_audio' =&gt; true]);</code></div>
									<div class="blr-ba-codebox"><code>$track = blr_backline_get_track(456, ['has_audio' =&gt; true]);</code></div>
									<div class="blr-ba-codebox"><code>$release_tracks = blr_backline_get_release_tracks(123);</code></div>
									<div class="blr-ba-codebox"><code>$playlist = blr_backline_get_playlist(['release_id' =&gt; 123]);</code></div>
									<p><?php echo esc_html__( 'Use the track helpers to feed whatever front-end player your theme uses. Track helper args include limit, page, ids, exclude_ids, release_id, release_status, has_audio, search, orderby, and order.', 'blr-backline' ); ?></p>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="mail-smtp" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Mail sending and SMTP', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Backline sends email through WordPress, so delivery depends on your site’s mail setup and cron behavior.', 'blr-backline' ); ?></p>
									<p><?php echo esc_html__( 'For more reliable delivery, use a proper SMTP service. A third-party SMTP plugin usually handles authentication and deliverability better than default PHP mail on many hosts.', 'blr-backline' ); ?></p>
									<p><?php echo esc_html__( 'If you use a third-party SMTP plugin, keep Backline on its default mail flow so WordPress and the SMTP plugin handle sending. Do not stack multiple mail systems unless you intentionally want Backline using its own SMTP configuration.', 'blr-backline' ); ?></p>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="dashboard-access" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Dashboard access and allowed users', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'BLR Backline dashboard access is restricted to users you explicitly allow. A normal WordPress user account does not automatically get dashboard access.', 'blr-backline' ); ?></p>
									<p><?php echo esc_html__( 'If an allowed user logs in, they can access the dashboard at the configured dashboard slug. Users who are not allowed will be blocked from the BLR Backline dashboard.', 'blr-backline' ); ?></p>
									<p><?php echo esc_html__( 'If someone cannot get in, check that their user account exists, confirm they are in the allowed users list, and verify the dashboard slug is correct.', 'blr-backline' ); ?></p>
									<p><?php echo esc_html__( 'If you change the dashboard slug, the dashboard URL changes too. Old bookmarked links will stop being correct.', 'blr-backline' ); ?></p>
								</section>

								<section class="blr-ba-docs-panel" data-doc-panel="releases-tracks" role="tabpanel" hidden>
									<h3><?php echo esc_html__( 'Releases and tracks', 'blr-backline' ); ?></h3>
									<p><?php echo esc_html__( 'Tracks and releases are separate but connected in Backline: tracks are individual songs, while releases group one or more tracks together. A track can exist on its own before being attached to a release, which lets you build and manage your catalog first and organize releases afterward.', 'blr-backline' ); ?></p>
									<p><?php echo esc_html__( 'When editing a release, you can attach existing tracks and control their order inside that release, keeping your catalog cleaner and release planning easier to manage.', 'blr-backline' ); ?></p>
								</section>
							</div>
						</div>
					</section>

					<div class="blr-ba-settings__footer">
						<?php submit_button( __( 'Save settings', 'blr-backline' ), 'primary', 'submit', false ); ?>
					</div>
				</form>
			</div>
		</div>

		<aside class="blr-ba-settings__side">
			<div class="blr-ba-sidecard">
				<ul class="blr-ba-statlist">
					<li><span><?php echo esc_html__( 'Dashboard URL', 'blr-backline' ); ?></span><strong><a href="<?php echo esc_url( $dashboard_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $dashboard_url ); ?></a></strong></li>
					<li><span><?php echo esc_html__( 'Allowed operators', 'blr-backline' ); ?></span><strong><?php echo esc_html( number_format_i18n( (int) ( $diagnostics['allowed_user_count'] ?? 0 ) ) ); ?></strong></li>
					<li><span><?php echo esc_html__( 'Enabled modules', 'blr-backline' ); ?></span><strong><?php echo esc_html( number_format_i18n( (int) ( $diagnostics['module_count'] ?? 0 ) ) ); ?></strong></li>
					<li><span><?php echo esc_html__( 'Default sender', 'blr-backline' ); ?></span><strong><?php echo ! empty( $diagnostics['default_sender_ready'] ) ? esc_html__( 'Ready', 'blr-backline' ) : esc_html__( 'Using fallback', 'blr-backline' ); ?></strong></li>
					<li><span><?php echo esc_html__( 'Sidebar logo', 'blr-backline' ); ?></span><strong><?php echo ! empty( $diagnostics['sidebar_logo_set'] ) ? esc_html__( 'Set', 'blr-backline' ) : esc_html__( 'Not set', 'blr-backline' ); ?></strong></li>
					<li><span><?php echo esc_html__( 'Outreach cron', 'blr-backline' ); ?></span><strong><?php echo ! empty( $diagnostics['cron_ready'] ) ? esc_html__( 'Scheduled', 'blr-backline' ) : esc_html__( 'Not scheduled', 'blr-backline' ); ?></strong></li>
					<?php if ( ! empty( $diagnostics['disable_wp_cron'] ) ) : ?>
					<li><span><?php echo esc_html__( 'WP-Cron disabled', 'blr-backline' ); ?></span><strong><?php echo esc_html__( 'Requires real server cron for reliable outreach', 'blr-backline' ); ?></strong></li>
					<?php endif; ?>
				</ul>
			</div>
		</aside>
	</div>
</div>
