<?php
/**
 * Passwords management page view.
 *
 * @package PrivexPasswordProtect
 */

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

$ppmstr_s             = PPMSTR_Settings::get_all();
$ppmstr_is_pro        = function_exists( 'ppp_fs' ) && ppp_fs()->can_use_premium_code__premium_only();
$ppmstr_all_passwords = PPMSTR_Password_Admin::get_all_passwords();
$ppmstr_total         = count( $ppmstr_all_passwords );
$ppmstr_active        = 0;
$ppmstr_expired       = 0;

foreach ( $ppmstr_all_passwords as $ppmstr_pw ) {
	if ( 'active' === ( $ppmstr_pw['status'] ?? 'active' ) ) {
		$ppmstr_active++;
	} else {
		$ppmstr_expired++;
	}
}

// Include global passwords in counts if configured.
if ( ! empty( $ppmstr_s['site_password'] ) ) {
	$ppmstr_total++;
	$ppmstr_active++;
}
if ( ! empty( $ppmstr_s['post_type_password'] ) ) {
	$ppmstr_total++;
	$ppmstr_active++;
}
if ( ! empty( $ppmstr_s['category_password'] ) ) {
	$ppmstr_total++;
	$ppmstr_active++;
}
if ( ! empty( $ppmstr_s['master_password'] ) ) {
	$ppmstr_total++;
	$ppmstr_active++;
}

// Get posts for the dropdown.
$ppmstr_posts = get_posts( array(
	'post_type'      => $ppmstr_s['enabled_post_types'],
	'posts_per_page' => 200,
	'post_status'    => 'publish',
	'orderby'        => 'title',
	'order'          => 'ASC',
) );

// Get data for global settings.
$ppmstr_all_post_types = get_post_types( array( 'public' => true ), 'objects' );
unset( $ppmstr_all_post_types['attachment'] );
$ppmstr_all_categories = get_categories( array( 'hide_empty' => false ) );
$ppmstr_saved          = isset( $_GET['ppmstr_saved'] ) && '1' === $_GET['ppmstr_saved']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
?>

<div class="ppmstr-page-header">
	<h2><?php esc_html_e( 'Password Manager', 'privex-password-protect' ); ?></h2>
	<p class="ppmstr-page-desc"><?php esc_html_e( 'Manage global protection settings and individual passwords in one place.', 'privex-password-protect' ); ?></p>
</div>

<?php if ( $ppmstr_saved ) : ?>
	<div class="ppmstr-notice ppmstr-notice--success">
		<?php esc_html_e( 'Settings saved successfully.', 'privex-password-protect' ); ?>
	</div>
<?php endif; ?>

<!-- Stats -->
<div class="ppmstr-stats-row" style="margin-bottom: 24px;">
	<div class="ppmstr-stat-card">
		<div class="ppmstr-stat-icon ppmstr-stat-icon--primary">
			<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 2l-2 2m-7.61 7.61a5.5 5.5 0 1 1-7.78 7.78 5.5 5.5 0 0 1 7.78-7.78zm0 0L15.5 7.5m0 0l3 3L22 7l-3-3m-3.5 3.5L19 4"/></svg>
		</div>
		<div class="ppmstr-stat-info">
			<span class="ppmstr-stat-number"><?php echo absint( $ppmstr_total ); ?></span>
			<span class="ppmstr-stat-label"><?php esc_html_e( 'Total Passwords', 'privex-password-protect' ); ?></span>
		</div>
	</div>
	<div class="ppmstr-stat-card">
		<div class="ppmstr-stat-icon ppmstr-stat-icon--success">
			<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg>
		</div>
		<div class="ppmstr-stat-info">
			<span class="ppmstr-stat-number"><?php echo absint( $ppmstr_active ); ?></span>
			<span class="ppmstr-stat-label"><?php esc_html_e( 'Active', 'privex-password-protect' ); ?></span>
		</div>
	</div>
	<div class="ppmstr-stat-card">
		<div class="ppmstr-stat-icon ppmstr-stat-icon--danger">
			<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="15" y1="9" x2="9" y2="15"/><line x1="9" y1="9" x2="15" y2="15"/></svg>
		</div>
		<div class="ppmstr-stat-info">
			<span class="ppmstr-stat-number"><?php echo absint( $ppmstr_expired ); ?></span>
			<span class="ppmstr-stat-label"><?php esc_html_e( 'Expired', 'privex-password-protect' ); ?></span>
		</div>
	</div>
</div>

<div class="ppmstr-section-header" style="margin-bottom: 20px;">
	<h3><?php esc_html_e( 'Global Protection Settings', 'privex-password-protect' ); ?></h3>
</div>

<form method="post" action="" class="ppmstr-settings-form">
	<?php wp_nonce_field( 'ppmstr_save_settings', 'ppmstr_settings_nonce' ); ?>
	<input type="hidden" name="ppmstr_redirect_to" value="ppmstr-passwords" />

	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Site-Wide Lock', 'privex-password-protect' ); ?></h3></div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-site-toggle"><?php esc_html_e( 'Enable Site-Wide Protection', 'privex-password-protect' ); ?></label>
					<p class="ppmstr-field-hint"><?php esc_html_e( 'Require a password to access any page on the site.', 'privex-password-protect' ); ?></p>
				</div>
				<div class="ppmstr-field-input">
					<label class="ppmstr-toggle-wrap">
						<input type="checkbox" name="ppmstr_site_protection_enabled" id="ppmstr-site-toggle" value="1" <?php checked( $ppmstr_s['site_protection_enabled'] ); ?> />
						<span class="ppmstr-toggle-slider"></span>
					</label>
				</div>
			</div>
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-site-pass"><?php esc_html_e( 'Site Password', 'privex-password-protect' ); ?></label>
				</div>
				<div class="ppmstr-field-input">
					<div class="ppmstr-input-group">
						<input type="password" name="ppmstr_site_password" id="ppmstr-site-pass"
							placeholder="<?php echo ! empty( $ppmstr_s['site_password'] ) ? esc_attr__( '••••••••', 'privex-password-protect' ) : esc_attr__( 'Set site password', 'privex-password-protect' ); ?>"
							autocomplete="new-password" />
						<button type="button" class="ppmstr-toggle-visibility" data-target="ppmstr-site-pass">
							<span class="dashicons dashicons-visibility"></span>
						</button>
					</div>
					<?php if ( ! empty( $ppmstr_s['site_password'] ) ) : ?>
						<div class="ppmstr-saved-indicator" style="margin-top:8px;">
							<span class="ppmstr-badge ppmstr-badge--active"><span class="dashicons dashicons-yes-alt" style="font-size:14px; width:14px; height:14px;"></span><?php esc_html_e( 'Saved', 'privex-password-protect' ); ?></span>
							<input type="checkbox" name="ppmstr_clear_site" value="1" class="ppmstr-clear-pw-cb" style="display:none;" />
							<button type="button" class="button button-small ppmstr-remove-pw-btn" style="color:#d63638; border-color:#d63638; background:transparent; margin-left:8px;">
								<?php esc_html_e( 'Remove', 'privex-password-protect' ); ?>
							</button>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Protection Scope', 'privex-password-protect' ); ?> <span class="ppmstr-pro-badge">PRO</span></h3></div>
		<div class="ppmstr-card-body <?php echo ! $ppmstr_is_pro ? 'ppmstr-pro-locked-wrap' : ''; ?>">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label><?php esc_html_e( 'Global Post Type Protection', 'privex-password-protect' ); ?></label>
				</div>
				<div class="ppmstr-field-input">
					<div class="ppmstr-checkbox-group" style="margin-bottom:15px;">
						<?php foreach ( $ppmstr_all_post_types as $ppmstr_pt_obj ) : ?>
							<label class="ppmstr-checkbox-label">
								<input type="checkbox" name="ppmstr_protected_post_types[]"
									value="<?php echo esc_attr( $ppmstr_pt_obj->name ); ?>"
									<?php checked( in_array( $ppmstr_pt_obj->name, $ppmstr_s['protected_post_types'] ?? array(), true ) ); ?> />
								<?php echo esc_html( $ppmstr_pt_obj->labels->singular_name ); ?>
							</label>
						<?php endforeach; ?>
					</div>
					<div class="ppmstr-input-group">
						<input type="password" name="ppmstr_post_type_password" id="ppmstr-post-type-pass"
							placeholder="<?php echo ! empty( $ppmstr_s['post_type_password'] ) ? esc_attr__( '••••••••', 'privex-password-protect' ) : esc_attr__( 'Enter global password...', 'privex-password-protect' ); ?>"
							autocomplete="new-password" style="max-width:300px;" />
						<button type="button" class="ppmstr-toggle-visibility" data-target="ppmstr-post-type-pass">
							<span class="dashicons dashicons-visibility"></span>
						</button>
					</div>
					<?php if ( ! empty( $ppmstr_s['post_type_password'] ) ) : ?>
						<div class="ppmstr-saved-indicator" style="margin-top:8px;">
							<span class="ppmstr-badge ppmstr-badge--active"><span class="dashicons dashicons-yes-alt" style="font-size:14px; width:14px; height:14px;"></span><?php esc_html_e( 'Saved', 'privex-password-protect' ); ?></span>
							<input type="checkbox" name="ppmstr_clear_post_type" value="1" class="ppmstr-clear-pw-cb" style="display:none;" />
							<button type="button" class="button button-small ppmstr-remove-pw-btn" style="color:#d63638; border-color:#d63638; background:transparent; margin-left:8px;">
								<?php esc_html_e( 'Remove', 'privex-password-protect' ); ?>
							</button>
						</div>
					<?php endif; ?>
				</div>
			</div>
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-excluded"><?php esc_html_e( 'Excluded Post IDs', 'privex-password-protect' ); ?></label>
				</div>
				<div class="ppmstr-field-input">
					<input type="text" name="ppmstr_excluded_posts" id="ppmstr-excluded"
						value="<?php echo esc_attr( implode( ', ', $ppmstr_s['excluded_posts'] ) ); ?>" />
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Category Protection', 'privex-password-protect' ); ?> <span class="ppmstr-pro-badge">PRO</span></h3></div>
		<div class="ppmstr-card-body <?php echo ! $ppmstr_is_pro ? 'ppmstr-pro-locked-wrap' : ''; ?>">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label><?php esc_html_e( 'Protected Categories', 'privex-password-protect' ); ?></label>
				</div>
				<div class="ppmstr-field-input">
					<div class="ppmstr-checkbox-group ppmstr-checkbox-scroll">
						<?php foreach ( $ppmstr_all_categories as $cat ) : ?>
							<label class="ppmstr-checkbox-label">
								<input type="checkbox" name="ppmstr_protected_categories[]" value="<?php echo absint( $cat->term_id ); ?>" <?php checked( in_array( $cat->term_id, $ppmstr_s['protected_categories'], true ) ); ?> />
								<?php echo esc_html( $cat->name ); ?> (<?php echo absint( $cat->count ); ?>)
							</label>
						<?php endforeach; ?>
					</div>
					<div class="ppmstr-input-group" style="margin-top:15px;">
						<input type="password" name="ppmstr_category_password" id="ppmstr-category-pass"
							placeholder="<?php echo ! empty( $ppmstr_s['category_password'] ) ? esc_attr__( '••••••••', 'privex-password-protect' ) : esc_attr__( 'Enter category password...', 'privex-password-protect' ); ?>"
							autocomplete="new-password" style="max-width:300px;" />
						<button type="button" class="ppmstr-toggle-visibility" data-target="ppmstr-category-pass">
							<span class="dashicons dashicons-visibility"></span>
						</button>
					</div>
					<?php if ( ! empty( $ppmstr_s['category_password'] ) ) : ?>
						<div class="ppmstr-saved-indicator" style="margin-top:8px;">
							<span class="ppmstr-badge ppmstr-badge--active"><span class="dashicons dashicons-yes-alt" style="font-size:14px; width:14px; height:14px;"></span><?php esc_html_e( 'Saved', 'privex-password-protect' ); ?></span>
							<input type="checkbox" name="ppmstr_clear_category" value="1" class="ppmstr-clear-pw-cb" style="display:none;" />
							<button type="button" class="button button-small ppmstr-remove-pw-btn" style="color:#d63638; border-color:#d63638; background:transparent; margin-left:8px;">
								<?php esc_html_e( 'Remove', 'privex-password-protect' ); ?>
							</button>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>

	<div class="ppmstr-card">
		<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Master Password', 'privex-password-protect' ); ?></h3></div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-field-row">
				<div class="ppmstr-field-label">
					<label for="ppmstr-master-pass"><?php esc_html_e( 'Master Password', 'privex-password-protect' ); ?></label>
				</div>
				<div class="ppmstr-field-input">
					<div class="ppmstr-input-group">
						<input type="password" name="ppmstr_master_password" id="ppmstr-master-pass"
							placeholder="<?php echo ! empty( $ppmstr_s['master_password'] ) ? esc_attr__( '••••••••', 'privex-password-protect' ) : esc_attr__( 'Set master password', 'privex-password-protect' ); ?>"
							autocomplete="new-password" />
						<button type="button" class="ppmstr-toggle-visibility" data-target="ppmstr-master-pass">
							<span class="dashicons dashicons-visibility"></span>
						</button>
					</div>
					<?php if ( ! empty( $ppmstr_s['master_password'] ) ) : ?>
						<div class="ppmstr-saved-indicator" style="margin-top:8px;">
							<span class="ppmstr-badge ppmstr-badge--active"><span class="dashicons dashicons-yes-alt" style="font-size:14px; width:14px; height:14px;"></span><?php esc_html_e( 'Saved', 'privex-password-protect' ); ?></span>
							<input type="checkbox" name="ppmstr_clear_master" value="1" class="ppmstr-clear-pw-cb" style="display:none;" />
							<button type="button" class="button button-small ppmstr-remove-pw-btn" style="color:#d63638; border-color:#d63638; background:transparent; margin-left:8px;">
								<?php esc_html_e( 'Remove', 'privex-password-protect' ); ?>
							</button>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<div class="ppmstr-card-footer" style="padding: 15px 24px; border-top: 1px solid #e2e8f0; background: #fdfdfd; text-align: right; border-bottom-left-radius: 8px; border-bottom-right-radius: 8px;">
			<button type="submit" class="ppmstr-btn ppmstr-btn--primary">
				<?php esc_html_e( 'Save Settings', 'privex-password-protect' ); ?>
			</button>
		</div>
	</div>
</form>

<hr style="margin: 40px 0; border: none; border-top: 1px solid #e2e8f0;" />

<div class="ppmstr-section-header" style="margin-bottom: 20px;">
	<h3><?php esc_html_e( 'Password Manager', 'privex-password-protect' ); ?></h3>
</div>

<!-- Add Single Page Dedicated Password Form, Bulk, Table... (rest of the file remains same) -->
<?php 
// For brevity, I'll just include the rest as it was, but ensure the JS is added at the end.
?>
<div class="ppmstr-card" id="ppmstr-add-password-card">
	<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Add Single Page Dedicated Password', 'privex-password-protect' ); ?></h3></div>
	<div class="ppmstr-card-body">
		<div class="ppmstr-form-grid" id="ppmstr-add-form">
			<div class="ppmstr-form-grid-row">
				<div class="ppmstr-form-group">
					<label for="ppmstr-pw-post"><?php esc_html_e( 'Content', 'privex-password-protect' ); ?></label>
					<select id="ppmstr-pw-post">
						<option value=""><?php esc_html_e( '— Select —', 'privex-password-protect' ); ?></option>
						<?php foreach ( $ppmstr_posts as $ppmstr_p_post ) : ?>
							<option value="<?php echo absint( $ppmstr_p_post->ID ); ?>"><?php echo esc_html( $ppmstr_p_post->post_title ); ?> (<?php echo esc_html( $ppmstr_p_post->post_type ); ?>)</option>
						<?php endforeach; ?>
					</select>
				</div>
				<div class="ppmstr-form-group">
					<label for="ppmstr-pw-password"><?php esc_html_e( 'Password', 'privex-password-protect' ); ?></label>
					<div class="ppmstr-input-group">
						<input type="text" id="ppmstr-pw-password" placeholder="<?php esc_attr_e( 'Enter or generate', 'privex-password-protect' ); ?>" />
						<button type="button" class="ppmstr-toggle-visibility" id="ppmstr-pw-generate" title="<?php esc_attr_e( 'Generate random password', 'privex-password-protect' ); ?>">
							<span class="dashicons dashicons-randomize"></span>
						</button>
					</div>
				</div>
			</div>
			<div class="ppmstr-form-grid-row">
				<div class="ppmstr-form-group">
					<label for="ppmstr-pw-label"><?php esc_html_e( 'Label (optional)', 'privex-password-protect' ); ?></label>
					<input type="text" id="ppmstr-pw-label" placeholder="<?php esc_attr_e( 'e.g. Team Access', 'privex-password-protect' ); ?>" />
				</div>
				<div class="ppmstr-form-group">
					<label for="ppmstr-pw-expires"><?php esc_html_e( 'Expires (optional)', 'privex-password-protect' ); ?></label>
					<input type="datetime-local" id="ppmstr-pw-expires" />
				</div>
				<div class="ppmstr-form-group">
					<label for="ppmstr-pw-maxuses"><?php esc_html_e( 'Max Uses', 'privex-password-protect' ); ?></label>
					<input type="number" id="ppmstr-pw-maxuses" min="0" value="0" placeholder="<?php esc_attr_e( '0 = unlimited', 'privex-password-protect' ); ?>" />
				</div>
			</div>
			<div class="ppmstr-form-grid-actions">
				<button type="button" class="ppmstr-btn ppmstr-btn--primary" id="ppmstr-add-pw-btn">
					<?php esc_html_e( 'Add Single Page Dedicated Password', 'privex-password-protect' ); ?>
				</button>
				<span class="ppmstr-ajax-status" id="ppmstr-add-status"></span>
			</div>
		</div>
	</div>
</div>

<div class="ppmstr-card">
	<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Bulk Generate', 'privex-password-protect' ); ?> <span class="ppmstr-pro-badge">PRO</span></h3></div>
	<div class="ppmstr-card-body <?php echo ! $ppmstr_is_pro ? 'ppmstr-pro-locked-wrap' : ''; ?>">
		<div class="ppmstr-form-grid" id="ppmstr-bulk-form">
			<div class="ppmstr-form-grid-row">
				<div class="ppmstr-form-group">
					<label for="ppmstr-bulk-post"><?php esc_html_e( 'Content', 'privex-password-protect' ); ?></label>
					<select id="ppmstr-bulk-post">
						<option value=""><?php esc_html_e( '— Select —', 'privex-password-protect' ); ?></option>
						<?php foreach ( $ppmstr_posts as $ppmstr_p_post ) : ?>
							<option value="<?php echo absint( $ppmstr_p_post->ID ); ?>"><?php echo esc_html( $ppmstr_p_post->post_title ); ?></option>
						<?php endforeach; ?>
					</select>
				</div>
				<div class="ppmstr-form-group">
					<label for="ppmstr-bulk-count"><?php esc_html_e( 'Count', 'privex-password-protect' ); ?></label>
					<input type="number" id="ppmstr-bulk-count" min="1" max="100" value="5" />
				</div>
				<div class="ppmstr-form-group">
					<label for="ppmstr-bulk-length"><?php esc_html_e( 'Length', 'privex-password-protect' ); ?></label>
					<input type="number" id="ppmstr-bulk-length" min="6" max="32" value="12" />
				</div>
			</div>
			<div class="ppmstr-form-grid-actions">
				<button type="button" class="ppmstr-btn ppmstr-btn--primary" id="ppmstr-bulk-btn">
					<?php esc_html_e( 'Generate Passwords', 'privex-password-protect' ); ?>
				</button>
				<button type="button" class="ppmstr-btn ppmstr-btn--secondary" id="ppmstr-export-btn">
					<?php esc_html_e( 'Export CSV', 'privex-password-protect' ); ?>
				</button>
				<span class="ppmstr-ajax-status" id="ppmstr-bulk-status"></span>
			</div>
		</div>
		<div id="ppmstr-generated-output" style="display:none; margin-top: 16px;">
			<h4 style="margin: 0 0 8px; font-size: 14px;"><?php esc_html_e( 'Generated Passwords (copy before leaving):', 'privex-password-protect' ); ?></h4>
			<textarea id="ppmstr-generated-list" readonly rows="6" style="width:100%; font-family:monospace; font-size:13px; padding:10px; border:1px solid var(--ppmstr-border); border-radius:8px; background:#FAFBFC; resize:vertical;"></textarea>
		</div>
	</div>
</div>

<div class="ppmstr-card">
	<div class="ppmstr-card-header"><h3><?php esc_html_e( 'All Passwords', 'privex-password-protect' ); ?></h3></div>
	<div class="ppmstr-card-body" style="padding:0;">
		<?php if ( empty( $ppmstr_all_passwords ) ) : ?>
			<div style="padding: 40px 24px; text-align:center;">
				<p class="ppmstr-text-muted"><?php esc_html_e( 'No passwords created yet. Use the form above to add one.', 'privex-password-protect' ); ?></p>
			</div>
		<?php else : ?>
			<div class="ppmstr-table-wrap">
				<table class="ppmstr-table" id="ppmstr-passwords-table">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Label', 'privex-password-protect' ); ?></th>
							<th><?php esc_html_e( 'Content', 'privex-password-protect' ); ?></th>
							<th><?php esc_html_e( 'Status', 'privex-password-protect' ); ?></th>
							<th><?php esc_html_e( 'Uses', 'privex-password-protect' ); ?></th>
							<th><?php esc_html_e( 'Expires', 'privex-password-protect' ); ?></th>
							<th><?php esc_html_e( 'Created', 'privex-password-protect' ); ?></th>
							<th><?php esc_html_e( 'Actions', 'privex-password-protect' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<?php foreach ( $ppmstr_all_passwords as $ppmstr_pw ) : ?>
							<?php
							$ppmstr_pw_status  = $ppmstr_pw['status'] ?? 'active';
							$ppmstr_pw_uses    = ( $ppmstr_pw['use_count'] ?? 0 );
							$ppmstr_pw_max     = ( $ppmstr_pw['max_uses'] ?? 0 );
							$ppmstr_pw_expires = ! empty( $ppmstr_pw['expires_at'] ) ? gmdate( 'M j, Y g:ia', $ppmstr_pw['expires_at'] ) : '—';
							$ppmstr_pw_created = ! empty( $ppmstr_pw['created'] ) ? gmdate( 'M j, Y', $ppmstr_pw['created'] ) : '—';
							$ppmstr_pw_label   = ! empty( $ppmstr_pw['label'] ) ? $ppmstr_pw['label'] : '(' . __( 'no label', 'privex-password-protect' ) . ')';
							?>
							<tr data-id="<?php echo esc_attr( $ppmstr_pw['id'] ?? '' ); ?>" data-post="<?php echo absint( $ppmstr_pw['post_id'] ?? 0 ); ?>">
								<td class="ppmstr-td-label"><?php echo esc_html( $ppmstr_pw_label ); ?></td>
								<td>
									<a href="<?php echo esc_url( get_edit_post_link( $ppmstr_pw['post_id'] ?? 0 ) ); ?>">
										<?php echo esc_html( $ppmstr_pw['post_title'] ?? '' ); ?>
									</a>
								</td>
								<td>
									<span class="ppmstr-badge ppmstr-badge--<?php echo esc_attr( $ppmstr_pw_status ); ?>">
										<?php echo esc_html( ucfirst( $ppmstr_pw_status ) ); ?>
									</span>
								</td>
								<td>
									<?php echo absint( $ppmstr_pw_uses ) . ( $ppmstr_pw_max > 0 ? ' / ' . absint( $ppmstr_pw_max ) : '' ); ?>
								</td>
								<td><?php echo esc_html( $ppmstr_pw_expires ); ?></td>
								<td><?php echo esc_html( $ppmstr_pw_created ); ?></td>
								<td>
									<button type="button" class="ppmstr-btn-icon ppmstr-delete-pw" data-id="<?php echo esc_attr( $ppmstr_pw['id'] ?? '' ); ?>" data-post="<?php echo absint( $ppmstr_pw['post_id'] ?? 0 ); ?>">
										<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
									</button>
								</td>
							</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			</div>
		<?php endif; ?>
	</div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
	// Toggle Password Visibility
	const toggleBtns = document.querySelectorAll('.ppmstr-toggle-visibility');
	toggleBtns.forEach(btn => {
		btn.addEventListener('click', function() {
			const targetId = this.getAttribute('data-target');
			const input = document.getElementById(targetId);
			if (input) {
				if (input.type === 'password') {
					input.type = 'text';
					this.querySelector('.dashicons').classList.replace('dashicons-visibility', 'dashicons-hidden');
				} else {
					input.type = 'password';
					this.querySelector('.dashicons').classList.replace('dashicons-hidden', 'dashicons-visibility');
				}
			}
		});
	});

	// Remove Password Logic
	const removeBtns = document.querySelectorAll('.ppmstr-remove-pw-btn');
	removeBtns.forEach(btn => {
		btn.addEventListener('click', function() {
			if (confirm('<?php echo esc_js( __( 'Are you sure you want to remove this password?', 'privex-password-protect' ) ); ?>')) {
				const container = this.closest('.ppmstr-saved-indicator');
				const checkbox = container.querySelector('.ppmstr-clear-pw-cb');
				if (checkbox) {
					checkbox.checked = true;
					this.closest('form').submit();
				}
			}
		});
	});
});
</script>
